1<?php 2// +-----------------------------------------------------------------------+ 3// | This file is part of Piwigo. | 4// | | 5// | For copyright and license information, please view the COPYING.txt | 6// | file that was distributed with this source code. | 7// +-----------------------------------------------------------------------+ 8 9/** 10 * This file is included by the main page to show subcategories of a category 11 * or to show recent categories or main page categories list 12 * 13 */ 14 15 16// $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE 17$query = ' 18SELECT 19 c.*, 20 user_representative_picture_id, 21 nb_images, 22 date_last, 23 max_date_last, 24 count_images, 25 nb_categories, 26 count_categories 27 FROM '.CATEGORIES_TABLE.' c 28 INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' ucc 29 ON id = cat_id 30 AND user_id = '.$user['id']; 31 32if ('recent_cats' == $page['section']) 33{ 34 $query.= ' 35 WHERE '.get_recent_photos_sql('date_last'); 36} 37else 38{ 39 $query.= ' 40 WHERE id_uppercat '.(!isset($page['category']) ? 'is NULL' : '= '.$page['category']['id']); 41} 42 43$query.= ' 44 '.get_sql_condition_FandF( 45 array('visible_categories' => 'id'), 46 'AND' 47 ); 48 49if ('recent_cats' != $page['section']) 50{ 51 $query.= ' 52 ORDER BY rank'; 53} 54 55$result = pwg_query($query); 56$categories = array(); 57$category_ids = array(); 58$image_ids = array(); 59$user_representative_updates_for = array(); 60 61while ($row = pwg_db_fetch_assoc($result)) 62{ 63 $row['is_child_date_last'] = @$row['max_date_last']>@$row['date_last']; 64 65 if (!empty($row['user_representative_picture_id'])) 66 { 67 $image_id = $row['user_representative_picture_id']; 68 } 69 elseif (!empty($row['representative_picture_id'])) 70 { // if a representative picture is set, it has priority 71 $image_id = $row['representative_picture_id']; 72 } 73 elseif ($conf['allow_random_representative']) 74 { // searching a random representant among elements in sub-categories 75 $image_id = get_random_image_in_category($row); 76 } 77 elseif ($row['count_categories']>0 and $row['count_images']>0) 78 { // searching a random representant among representant of sub-categories 79 $query = ' 80SELECT representative_picture_id 81 FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' 82 ON id = cat_id and user_id = '.$user['id'].' 83 WHERE uppercats LIKE \''.$row['uppercats'].',%\' 84 AND representative_picture_id IS NOT NULL' 85 .get_sql_condition_FandF 86 ( 87 array 88 ( 89 'visible_categories' => 'id', 90 ), 91 "\n AND" 92 ).' 93 ORDER BY '.DB_RANDOM_FUNCTION.'() 94 LIMIT 1 95;'; 96 $subresult = pwg_query($query); 97 if (pwg_db_num_rows($subresult) > 0) 98 { 99 list($image_id) = pwg_db_fetch_row($subresult); 100 } 101 } 102 103 104 if (isset($image_id)) 105 { 106 if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id) 107 { 108 $user_representative_updates_for[ $row['id'] ] = $image_id; 109 } 110 111 $row['representative_picture_id'] = $image_id; 112 $image_ids[] = $image_id; 113 $categories[] = $row; 114 $category_ids[] = $row['id']; 115 } 116 unset($image_id); 117} 118 119if ($conf['display_fromto']) 120{ 121 if (count($category_ids) > 0) 122 { 123 $query = ' 124SELECT 125 category_id, 126 MIN(date_creation) AS `from`, 127 MAX(date_creation) AS `to` 128 FROM '.IMAGE_CATEGORY_TABLE.' 129 INNER JOIN '.IMAGES_TABLE.' ON image_id = id 130 WHERE category_id IN ('.implode(',', $category_ids).') 131'.get_sql_condition_FandF 132 ( 133 array 134 ( 135 'visible_categories' => 'category_id', 136 'visible_images' => 'id' 137 ), 138 'AND' 139 ).' 140 GROUP BY category_id 141;'; 142 $dates_of_category = query2array($query, 'category_id'); 143 } 144} 145 146if ($page['section']=='recent_cats') 147{ 148 usort($categories, 'global_rank_compare'); 149} 150 151if (count($categories) > 0) 152{ 153 $infos_of_image = array(); 154 $new_image_ids = array(); 155 156 $query = ' 157SELECT * 158 FROM '.IMAGES_TABLE.' 159 WHERE id IN ('.implode(',', $image_ids).') 160;'; 161 $result = pwg_query($query); 162 while ($row = pwg_db_fetch_assoc($result)) 163 { 164 if ($row['level'] <= $user['level']) 165 { 166 $infos_of_image[$row['id']] = $row; 167 } 168 else 169 { 170 // problem: we must not display the thumbnail of a photo which has a 171 // higher privacy level than user privacy level 172 // 173 // * what is the represented category? 174 // * find a random photo matching user permissions 175 // * register it at user_representative_picture_id 176 // * set it as the representative_picture_id for the category 177 178 foreach ($categories as &$category) 179 { 180 if ($row['id'] == $category['representative_picture_id']) 181 { 182 // searching a random representant among elements in sub-categories 183 $image_id = get_random_image_in_category($category); 184 185 if (isset($image_id) and !in_array($image_id, $image_ids)) 186 { 187 $new_image_ids[] = $image_id; 188 } 189 190 if ($conf['representative_cache_on_level']) 191 { 192 $user_representative_updates_for[ $category['id'] ] = $image_id; 193 } 194 195 $category['representative_picture_id'] = $image_id; 196 } 197 } 198 unset($category); 199 } 200 } 201 202 if (count($new_image_ids) > 0) 203 { 204 $query = ' 205SELECT * 206 FROM '.IMAGES_TABLE.' 207 WHERE id IN ('.implode(',', $new_image_ids).') 208;'; 209 $result = pwg_query($query); 210 while ($row = pwg_db_fetch_assoc($result)) 211 { 212 $infos_of_image[$row['id']] = $row; 213 } 214 } 215 216 foreach ($infos_of_image as &$info) 217 { 218 $info['src_image'] = new SrcImage($info); 219 } 220 unset($info); 221} 222 223if (count($user_representative_updates_for)) 224{ 225 $updates = array(); 226 227 foreach ($user_representative_updates_for as $cat_id => $image_id) 228 { 229 $updates[] = 230 array( 231 'user_id' => $user['id'], 232 'cat_id' => $cat_id, 233 'user_representative_picture_id' => $image_id, 234 ); 235 } 236 237 mass_updates( 238 USER_CACHE_CATEGORIES_TABLE, 239 array( 240 'primary' => array('user_id', 'cat_id'), 241 'update' => array('user_representative_picture_id') 242 ), 243 $updates 244 ); 245} 246 247if (count($categories) > 0) 248{ 249 // Update filtered data 250 if (function_exists('update_cats_with_filtered_data')) 251 { 252 update_cats_with_filtered_data($categories); 253 } 254 255 $template->set_filename('index_category_thumbnails', 'mainpage_categories.tpl'); 256 257 trigger_notify('loc_begin_index_category_thumbnails', $categories); 258 259 $tpl_thumbnails_var = array(); 260 261 foreach ($categories as $category) 262 { 263 if (0 == $category['count_images']) 264 { 265 continue; 266 } 267 268 $category['name'] = trigger_change( 269 'render_category_name', 270 $category['name'], 271 'subcatify_category_name' 272 ); 273 274 if ($page['section']=='recent_cats') 275 { 276 $name = get_cat_display_name_cache($category['uppercats'], null); 277 } 278 else 279 { 280 $name = $category['name']; 281 } 282 283 $representative_infos = $infos_of_image[ $category['representative_picture_id'] ]; 284 285 $tpl_var = array_merge( $category, array( 286 'ID' => $category['id'] /*obsolete*/, 287 'representative' => $representative_infos, 288 'TN_ALT' => strip_tags($category['name']), 289 290 'URL' => make_index_url( 291 array( 292 'category' => $category 293 ) 294 ), 295 'CAPTION_NB_IMAGES' => get_display_images_count 296 ( 297 $category['nb_images'], 298 $category['count_images'], 299 $category['count_categories'], 300 true, 301 '<br>' 302 ), 303 'DESCRIPTION' => 304 trigger_change('render_category_literal_description', 305 trigger_change('render_category_description', 306 @$category['comment'], 307 'subcatify_category_description')), 308 'NAME' => $name, 309 ) ); 310 if ($conf['index_new_icon']) 311 { 312 $tpl_var['icon_ts'] = get_icon($category['max_date_last'], $category['is_child_date_last']); 313 } 314 315 if ($conf['display_fromto']) 316 { 317 if (isset($dates_of_category[ $category['id'] ])) 318 { 319 $from = $dates_of_category[ $category['id'] ]['from']; 320 $to = $dates_of_category[ $category['id'] ]['to']; 321 322 if (!empty($from)) 323 { 324 $tpl_var['INFO_DATES'] = format_fromto($from, $to); 325 } 326 } 327 } 328 329 $tpl_thumbnails_var[] = $tpl_var; 330 } 331 332 // pagination 333 $page['total_categories'] = count($tpl_thumbnails_var); 334 335 $tpl_thumbnails_var_selection = array_slice( 336 $tpl_thumbnails_var, 337 $page['startcat'], 338 $conf['nb_categories_page'] 339 ); 340 341 $derivative_params = trigger_change('get_index_album_derivative_params', ImageStdParams::get_by_type(IMG_THUMB) ); 342 $tpl_thumbnails_var_selection = trigger_change('loc_end_index_category_thumbnails', $tpl_thumbnails_var_selection); 343 $template->assign( array( 344 'maxRequests' =>$conf['max_requests'], 345 'category_thumbnails' => $tpl_thumbnails_var_selection, 346 'derivative_params' => $derivative_params, 347 ) ); 348 349 $template->assign_var_from_handle('CATEGORIES', 'index_category_thumbnails'); 350 351 // navigation bar 352 $page['cats_navigation_bar'] = array(); 353 if ($page['total_categories'] > $conf['nb_categories_page']) 354 { 355 $page['cats_navigation_bar'] = create_navigation_bar( 356 duplicate_index_url(array(), array('startcat')), 357 $page['total_categories'], 358 $page['startcat'], 359 $conf['nb_categories_page'], 360 true, 'startcat' 361 ); 362 } 363 364 $template->assign('cats_navbar', $page['cats_navigation_bar'] ); 365} 366 367pwg_debug('end include/category_cats.inc.php'); 368?>