[1597] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[1597] | 23 | |
---|
| 24 | /** |
---|
[18165] | 25 | * This file is included by the main page to show subcategories of a category |
---|
| 26 | * or to show recent categories or main page categories list |
---|
[1597] | 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
[18165] | 30 | |
---|
[8802] | 31 | // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE |
---|
| 32 | $query = ' |
---|
| 33 | SELECT |
---|
| 34 | c.*, |
---|
| 35 | user_representative_picture_id, |
---|
| 36 | nb_images, |
---|
| 37 | date_last, |
---|
| 38 | max_date_last, |
---|
| 39 | count_images, |
---|
[22879] | 40 | nb_categories, |
---|
[8802] | 41 | count_categories |
---|
| 42 | FROM '.CATEGORIES_TABLE.' c |
---|
[18924] | 43 | INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc |
---|
| 44 | ON id = cat_id |
---|
[18462] | 45 | AND user_id = '.$user['id']; |
---|
[18924] | 46 | |
---|
[18462] | 47 | if ('recent_cats' == $page['section']) |
---|
| 48 | { |
---|
| 49 | $query.= ' |
---|
[21802] | 50 | WHERE '.get_recent_photos_sql('date_last'); |
---|
[18462] | 51 | } |
---|
| 52 | else |
---|
| 53 | { |
---|
| 54 | $query.= ' |
---|
| 55 | WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']); |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | $query.= ' |
---|
| 59 | '.get_sql_condition_FandF( |
---|
| 60 | array('visible_categories' => 'id'), |
---|
| 61 | 'AND' |
---|
| 62 | ); |
---|
[18924] | 63 | |
---|
[8802] | 64 | if ('recent_cats' != $page['section']) |
---|
| 65 | { |
---|
| 66 | $query.= ' |
---|
| 67 | ORDER BY rank'; |
---|
| 68 | } |
---|
| 69 | |
---|
[18892] | 70 | $result = pwg_query($query); |
---|
[1597] | 71 | $categories = array(); |
---|
[1970] | 72 | $category_ids = array(); |
---|
[1597] | 73 | $image_ids = array(); |
---|
[8802] | 74 | $user_representative_updates_for = array(); |
---|
[1597] | 75 | |
---|
[18892] | 76 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1597] | 77 | { |
---|
[1643] | 78 | $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last']; |
---|
[1624] | 79 | |
---|
[8802] | 80 | if (!empty($row['user_representative_picture_id'])) |
---|
| 81 | { |
---|
| 82 | $image_id = $row['user_representative_picture_id']; |
---|
| 83 | } |
---|
[21802] | 84 | elseif (!empty($row['representative_picture_id'])) |
---|
[1597] | 85 | { // if a representative picture is set, it has priority |
---|
| 86 | $image_id = $row['representative_picture_id']; |
---|
| 87 | } |
---|
[21802] | 88 | elseif ($conf['allow_random_representative']) |
---|
| 89 | { // searching a random representant among elements in sub-categories |
---|
[8802] | 90 | $image_id = get_random_image_in_category($row); |
---|
[1597] | 91 | } |
---|
[21802] | 92 | elseif ($row['count_categories']>0 and $row['count_images']>0) |
---|
[1597] | 93 | { // searching a random representant among representant of sub-categories |
---|
[21802] | 94 | $query = ' |
---|
| 95 | SELECT representative_picture_id |
---|
| 96 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
---|
| 97 | ON id = cat_id and user_id = '.$user['id'].' |
---|
| 98 | WHERE uppercats LIKE \''.$row['uppercats'].',%\' |
---|
| 99 | AND representative_picture_id IS NOT NULL' |
---|
| 100 | .get_sql_condition_FandF |
---|
| 101 | ( |
---|
| 102 | array |
---|
| 103 | ( |
---|
| 104 | 'visible_categories' => 'id', |
---|
| 105 | ), |
---|
| 106 | "\n AND" |
---|
| 107 | ).' |
---|
| 108 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
| 109 | LIMIT 1 |
---|
| 110 | ;'; |
---|
| 111 | $subresult = pwg_query($query); |
---|
| 112 | if (pwg_db_num_rows($subresult) > 0) |
---|
[1597] | 113 | { |
---|
[21802] | 114 | list($image_id) = pwg_db_fetch_row($subresult); |
---|
[1597] | 115 | } |
---|
| 116 | } |
---|
| 117 | |
---|
[21802] | 118 | |
---|
[1597] | 119 | if (isset($image_id)) |
---|
| 120 | { |
---|
[11739] | 121 | if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id) |
---|
[8802] | 122 | { |
---|
[22879] | 123 | $user_representative_updates_for[ $row['id'] ] = $image_id; |
---|
[8802] | 124 | } |
---|
[18924] | 125 | |
---|
[1597] | 126 | $row['representative_picture_id'] = $image_id; |
---|
[21802] | 127 | $image_ids[] = $image_id; |
---|
| 128 | $categories[] = $row; |
---|
| 129 | $category_ids[] = $row['id']; |
---|
[1597] | 130 | } |
---|
| 131 | unset($image_id); |
---|
| 132 | } |
---|
| 133 | |
---|
[1970] | 134 | if ($conf['display_fromto']) |
---|
| 135 | { |
---|
| 136 | $dates_of_category = array(); |
---|
| 137 | if (count($category_ids) > 0) |
---|
| 138 | { |
---|
| 139 | $query = ' |
---|
| 140 | SELECT |
---|
| 141 | category_id, |
---|
| 142 | MIN(date_creation) AS date_creation_min, |
---|
| 143 | MAX(date_creation) AS date_creation_max |
---|
| 144 | FROM '.IMAGE_CATEGORY_TABLE.' |
---|
| 145 | INNER JOIN '.IMAGES_TABLE.' ON image_id = id |
---|
[2091] | 146 | WHERE category_id IN ('.implode(',', $category_ids).') |
---|
[1971] | 147 | '.get_sql_condition_FandF |
---|
| 148 | ( |
---|
| 149 | array |
---|
| 150 | ( |
---|
| 151 | 'visible_categories' => 'category_id', |
---|
[2091] | 152 | 'visible_images' => 'id' |
---|
[1971] | 153 | ), |
---|
| 154 | 'AND' |
---|
| 155 | ).' |
---|
[1970] | 156 | GROUP BY category_id |
---|
| 157 | ;'; |
---|
| 158 | $result = pwg_query($query); |
---|
[4325] | 159 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1970] | 160 | { |
---|
| 161 | $dates_of_category[ $row['category_id'] ] = array( |
---|
| 162 | 'from' => $row['date_creation_min'], |
---|
| 163 | 'to' => $row['date_creation_max'], |
---|
| 164 | ); |
---|
| 165 | } |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
[18892] | 169 | if ($page['section']=='recent_cats') |
---|
| 170 | { |
---|
| 171 | usort($categories, 'global_rank_compare'); |
---|
| 172 | } |
---|
| 173 | |
---|
[1597] | 174 | if (count($categories) > 0) |
---|
| 175 | { |
---|
[12546] | 176 | $infos_of_image = array(); |
---|
[8802] | 177 | $new_image_ids = array(); |
---|
[1597] | 178 | |
---|
| 179 | $query = ' |
---|
[12546] | 180 | SELECT * |
---|
[1597] | 181 | FROM '.IMAGES_TABLE.' |
---|
| 182 | WHERE id IN ('.implode(',', $image_ids).') |
---|
| 183 | ;'; |
---|
| 184 | $result = pwg_query($query); |
---|
[4325] | 185 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[1597] | 186 | { |
---|
[8802] | 187 | if ($row['level'] <= $user['level']) |
---|
| 188 | { |
---|
[12546] | 189 | $infos_of_image[$row['id']] = $row; |
---|
[8802] | 190 | } |
---|
| 191 | else |
---|
| 192 | { |
---|
| 193 | // problem: we must not display the thumbnail of a photo which has a |
---|
| 194 | // higher privacy level than user privacy level |
---|
| 195 | // |
---|
| 196 | // * what is the represented category? |
---|
| 197 | // * find a random photo matching user permissions |
---|
| 198 | // * register it at user_representative_picture_id |
---|
| 199 | // * set it as the representative_picture_id for the category |
---|
| 200 | |
---|
| 201 | foreach ($categories as &$category) |
---|
| 202 | { |
---|
| 203 | if ($row['id'] == $category['representative_picture_id']) |
---|
| 204 | { |
---|
[9365] | 205 | // searching a random representant among elements in sub-categories |
---|
| 206 | $image_id = get_random_image_in_category($category); |
---|
| 207 | |
---|
| 208 | if (isset($image_id) and !in_array($image_id, $image_ids)) |
---|
[8802] | 209 | { |
---|
[25018] | 210 | $new_image_ids[] = $image_id; |
---|
[8802] | 211 | } |
---|
[11739] | 212 | |
---|
| 213 | if ($conf['representative_cache_on_level']) |
---|
| 214 | { |
---|
[22879] | 215 | $user_representative_updates_for[ $category['id'] ] = $image_id; |
---|
[11739] | 216 | } |
---|
[18924] | 217 | |
---|
[9365] | 218 | $category['representative_picture_id'] = $image_id; |
---|
[8802] | 219 | } |
---|
| 220 | } |
---|
| 221 | unset($category); |
---|
| 222 | } |
---|
[1597] | 223 | } |
---|
[8802] | 224 | |
---|
| 225 | if (count($new_image_ids) > 0) |
---|
| 226 | { |
---|
| 227 | $query = ' |
---|
[12546] | 228 | SELECT * |
---|
[8802] | 229 | FROM '.IMAGES_TABLE.' |
---|
| 230 | WHERE id IN ('.implode(',', $new_image_ids).') |
---|
| 231 | ;'; |
---|
| 232 | $result = pwg_query($query); |
---|
| 233 | while ($row = pwg_db_fetch_assoc($result)) |
---|
| 234 | { |
---|
[12546] | 235 | $infos_of_image[$row['id']] = $row; |
---|
[8802] | 236 | } |
---|
| 237 | } |
---|
[18924] | 238 | |
---|
[12920] | 239 | foreach ($infos_of_image as &$info) |
---|
| 240 | { |
---|
| 241 | $info['src_image'] = new SrcImage($info); |
---|
| 242 | } |
---|
| 243 | unset($info); |
---|
[1597] | 244 | } |
---|
| 245 | |
---|
[8802] | 246 | if (count($user_representative_updates_for)) |
---|
| 247 | { |
---|
| 248 | $updates = array(); |
---|
[18924] | 249 | |
---|
[22879] | 250 | foreach ($user_representative_updates_for as $cat_id => $image_id) |
---|
[8802] | 251 | { |
---|
[22879] | 252 | $updates[] = |
---|
[8802] | 253 | array( |
---|
[22879] | 254 | 'user_id' => $user['id'], |
---|
[8802] | 255 | 'cat_id' => $cat_id, |
---|
| 256 | 'user_representative_picture_id' => $image_id, |
---|
[22879] | 257 | ); |
---|
[8802] | 258 | } |
---|
| 259 | |
---|
| 260 | mass_updates( |
---|
| 261 | USER_CACHE_CATEGORIES_TABLE, |
---|
| 262 | array( |
---|
| 263 | 'primary' => array('user_id', 'cat_id'), |
---|
| 264 | 'update' => array('user_representative_picture_id') |
---|
| 265 | ), |
---|
| 266 | $updates |
---|
| 267 | ); |
---|
| 268 | } |
---|
| 269 | |
---|
[1597] | 270 | if (count($categories) > 0) |
---|
| 271 | { |
---|
[1677] | 272 | // Update filtered data |
---|
[1722] | 273 | if (function_exists('update_cats_with_filtered_data')) |
---|
| 274 | { |
---|
| 275 | update_cats_with_filtered_data($categories); |
---|
| 276 | } |
---|
[2079] | 277 | |
---|
[2274] | 278 | $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl'); |
---|
| 279 | |
---|
[1880] | 280 | trigger_action('loc_begin_index_category_thumbnails', $categories); |
---|
[2274] | 281 | |
---|
[3124] | 282 | $tpl_thumbnails_var = array(); |
---|
| 283 | |
---|
[2274] | 284 | foreach ($categories as $category) |
---|
[1597] | 285 | { |
---|
[9365] | 286 | if (0 == $category['count_images']) |
---|
| 287 | { |
---|
| 288 | continue; |
---|
| 289 | } |
---|
[18924] | 290 | |
---|
[2433] | 291 | $category['name'] = trigger_event( |
---|
| 292 | 'render_category_name', |
---|
| 293 | $category['name'], |
---|
| 294 | 'subcatify_category_name' |
---|
| 295 | ); |
---|
| 296 | |
---|
[2274] | 297 | if ($page['section']=='recent_cats') |
---|
[1597] | 298 | { |
---|
[25425] | 299 | $name = get_cat_display_name_cache($category['uppercats'], null); |
---|
[2274] | 300 | } |
---|
| 301 | else |
---|
| 302 | { |
---|
| 303 | $name = $category['name']; |
---|
| 304 | } |
---|
[1597] | 305 | |
---|
[12546] | 306 | $representative_infos = $infos_of_image[ $category['representative_picture_id'] ]; |
---|
| 307 | |
---|
[18924] | 308 | $tpl_var = array_merge( $category, array( |
---|
| 309 | 'ID' => $category['id'] /*obsolete*/, |
---|
[12920] | 310 | 'representative' => $representative_infos, |
---|
[2515] | 311 | 'TN_ALT' => strip_tags($category['name']), |
---|
[1597] | 312 | |
---|
| 313 | 'URL' => make_index_url( |
---|
| 314 | array( |
---|
[1861] | 315 | 'category' => $category |
---|
[1597] | 316 | ) |
---|
| 317 | ), |
---|
[1624] | 318 | 'CAPTION_NB_IMAGES' => get_display_images_count |
---|
| 319 | ( |
---|
| 320 | $category['nb_images'], |
---|
| 321 | $category['count_images'], |
---|
[1851] | 322 | $category['count_categories'], |
---|
| 323 | true, |
---|
[3185] | 324 | '<br>' |
---|
[1624] | 325 | ), |
---|
[2079] | 326 | 'DESCRIPTION' => |
---|
[2091] | 327 | trigger_event('render_category_literal_description', |
---|
| 328 | trigger_event('render_category_description', |
---|
[2175] | 329 | @$category['comment'], |
---|
| 330 | 'subcatify_category_description')), |
---|
[1597] | 331 | 'NAME' => $name, |
---|
[18924] | 332 | ) ); |
---|
[11285] | 333 | if ($conf['index_new_icon']) |
---|
| 334 | { |
---|
[11591] | 335 | $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']); |
---|
[11285] | 336 | } |
---|
[1970] | 337 | |
---|
[2274] | 338 | if ($conf['display_fromto']) |
---|
| 339 | { |
---|
| 340 | if (isset($dates_of_category[ $category['id'] ])) |
---|
[1970] | 341 | { |
---|
[2274] | 342 | $from = $dates_of_category[ $category['id'] ]['from']; |
---|
| 343 | $to = $dates_of_category[ $category['id'] ]['to']; |
---|
| 344 | |
---|
| 345 | if (!empty($from)) |
---|
[1970] | 346 | { |
---|
[2274] | 347 | $info = ''; |
---|
[2091] | 348 | |
---|
[19420] | 349 | if (date('Y-m-d', strtotime($from)) == date('Y-m-d', strtotime($to))) |
---|
[1970] | 350 | { |
---|
[2274] | 351 | $info = format_date($from); |
---|
[1970] | 352 | } |
---|
[2274] | 353 | else |
---|
| 354 | { |
---|
[25005] | 355 | $info = l10n( |
---|
| 356 | 'from %s to %s', |
---|
[2274] | 357 | format_date($from), |
---|
| 358 | format_date($to) |
---|
| 359 | ); |
---|
| 360 | } |
---|
| 361 | $tpl_var['INFO_DATES'] = $info; |
---|
[1970] | 362 | } |
---|
[2274] | 363 | } |
---|
| 364 | }//fromto |
---|
[2234] | 365 | |
---|
[3124] | 366 | $tpl_thumbnails_var[] = $tpl_var; |
---|
[1597] | 367 | } |
---|
[18924] | 368 | |
---|
[18892] | 369 | // pagination |
---|
| 370 | $page['total_categories'] = count($tpl_thumbnails_var); |
---|
[1597] | 371 | |
---|
[19002] | 372 | $tpl_thumbnails_var_selection = array_slice( |
---|
[18924] | 373 | $tpl_thumbnails_var, |
---|
[18892] | 374 | $page['startcat'], |
---|
| 375 | $conf['nb_categories_page'] |
---|
| 376 | ); |
---|
| 377 | |
---|
[12930] | 378 | $derivative_params = trigger_event('get_index_album_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) ); |
---|
[19002] | 379 | $tpl_thumbnails_var_selection = trigger_event('loc_end_index_category_thumbnails', $tpl_thumbnails_var_selection); |
---|
[12920] | 380 | $template->assign( array( |
---|
[20177] | 381 | 'maxRequests' =>$conf['max_requests'], |
---|
[19002] | 382 | 'category_thumbnails' => $tpl_thumbnails_var_selection, |
---|
[12920] | 383 | 'derivative_params' => $derivative_params, |
---|
| 384 | ) ); |
---|
[3124] | 385 | |
---|
[2274] | 386 | $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails'); |
---|
[18924] | 387 | |
---|
[18462] | 388 | // navigation bar |
---|
| 389 | $page['cats_navigation_bar'] = array(); |
---|
| 390 | if ($page['total_categories'] > $conf['nb_categories_page']) |
---|
| 391 | { |
---|
| 392 | $page['cats_navigation_bar'] = create_navigation_bar( |
---|
| 393 | duplicate_index_url(array(), array('startcat')), |
---|
| 394 | $page['total_categories'], |
---|
| 395 | $page['startcat'], |
---|
| 396 | $conf['nb_categories_page'], |
---|
| 397 | true, 'startcat' |
---|
| 398 | ); |
---|
| 399 | } |
---|
| 400 | |
---|
| 401 | $template->assign('cats_navbar', $page['cats_navigation_bar'] ); |
---|
[1597] | 402 | } |
---|
[18462] | 403 | |
---|
[3124] | 404 | pwg_debug('end include/category_cats.inc.php'); |
---|
| 405 | ?> |
---|