Changeset 345 for trunk/include/functions_category.inc.php
- Timestamp:
- Feb 2, 2004, 1:55:18 AM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions_category.inc.php
r133 r345 18 18 ***************************************************************************/ 19 19 20 function get_subcats_id( $cat_id )21 {22 $restricted_cats = array();23 24 $query = 'SELECT id';25 $query.= ' FROM '.PREFIX_TABLE.'categories';26 $query.= ' WHERE id_uppercat = '.$cat_id;27 $query.= ';';28 $result = mysql_query( $query );29 while ( $row = mysql_fetch_array( $result ) )30 {31 array_push( $restricted_cats, $row['id'] );32 $sub_restricted_cats = get_subcats_id( $row['id'] );33 foreach ( $sub_restricted_cats as $sub_restricted_cat ) {34 array_push( $restricted_cats, $sub_restricted_cat );35 }36 }37 return $restricted_cats;38 }39 40 20 function check_restrictions( $category_id ) 41 21 { 42 22 global $user,$lang; 43 23 44 if ( i s_user_allowed( $category_id, $user['restrictions'] ) > 0)24 if ( in_array( $category_id, $user['restrictions'] ) ) 45 25 { 46 26 echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />'; … … 50 30 } 51 31 } 52 32 53 33 // the check_cat_id function check whether the $cat is a right parameter : 54 34 // - $cat is numeric and corresponds to a category in the database … … 62 42 if ( isset( $cat ) ) 63 43 { 64 if ( isset( $page['plain_structure'] ) ) 65 { 66 if ( isset( $page['plain_structure'][$cat] ) ) 67 { 68 $page['cat'] = $cat; 69 } 44 if ( isset( $page['plain_structure'][$cat] ) ) 45 { 46 $page['cat'] = $cat; 70 47 } 71 48 else if ( is_numeric( $cat ) ) 72 49 { 73 50 $query = 'SELECT id'; 74 $query.= ' FROM '.PREFIX_TABLE.'categories'; 75 $query.= ' WHERE id = '.$cat; 76 $query. ';'; 51 $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';'; 77 52 $result = mysql_query( $query ); 78 53 if ( mysql_num_rows( $result ) != 0 ) … … 92 67 } 93 68 94 function get_plain_structure() 95 { 96 $infos = array( 'name','id','date_last','nb_images','dir','id_uppercat', 97 'rank','site_id'); 69 function get_user_plain_structure() 70 { 71 global $page,$user; 98 72 99 $query = 'SELECT '; 100 foreach ( $infos as $i => $info ) { 101 if ( $i > 0 ) $query.= ','; 102 $query.= $info; 103 } 104 $query.= ' FROM '.PREFIX_TABLE.'categories'; 73 $infos = array( 'name','id','uc.date_last','nb_images','dir','id_uppercat', 74 'rank','site_id','nb_sub_categories','uppercats'); 75 76 $query = 'SELECT '.implode( ',', $infos ); 77 $query.= ' FROM '.CATEGORIES_TABLE.' AS c'; 78 // $query.= ' ,'.PREFIX_TABLE.'user_category AS uc'; 79 $query.= ' INNER JOIN '.USER_CATEGORY_TABLE.' AS uc'; 80 $query.= ' ON c.id = uc.category_id'; 81 $query.= ' WHERE user_id = '.$user['id']; 82 if ( $page['expand'] != 'all' ) 83 { 84 $query.= ' AND (id_uppercat is NULL'; 85 if ( count( $page['tab_expand'] ) > 0 ) 86 { 87 $query.= ' OR id_uppercat IN ('.$page['expand'].')'; 88 } 89 $query.= ')'; 90 } 91 if ( $user['forbidden_categories'] != '' ) 92 { 93 $query.= ' AND id NOT IN '; 94 $query.= '('.$user['forbidden_categories'].')'; 95 } 96 // $query.= ' AND c.id = uc.category_id'; 105 97 $query.= ' ORDER BY id_uppercat ASC, rank ASC'; 106 98 $query.= ';'; … … 112 104 $category = array(); 113 105 foreach ( $infos as $info ) { 114 $category[$info] = $row[$info]; 115 if ( $info == 'date_last' ) 116 { 117 list($year,$month,$day) = explode( '-', $row[$info] ); 118 $category[$info] = mktime(0,0,0,$month,$day,$year); 119 } 106 if ( $info == 'uc.date_last' ) 107 { 108 list($year,$month,$day) = explode( '-', $row['date_last'] ); 109 $category['date_last'] = mktime(0,0,0,$month,$day,$year); 110 } 111 else if ( isset( $row[$info] ) ) $category[$info] = $row[$info]; 112 else $category[$info] = ''; 120 113 } 121 114 $plain_structure[$row['id']] = $category; … … 125 118 } 126 119 127 function create_ structure( $id_uppercat, $restrictions)120 function create_user_structure( $id_uppercat ) 128 121 { 129 122 global $page; 130 123 131 124 if ( !isset( $page['plain_structure'] ) ) 132 $page['plain_structure'] = get_ plain_structure();125 $page['plain_structure'] = get_user_plain_structure(); 133 126 134 127 $structure = array(); 135 $ids = get_ subcat_ids( $id_uppercat );128 $ids = get_user_subcat_ids( $id_uppercat ); 136 129 foreach ( $ids as $id ) { 137 if ( !in_array( $id, $restrictions ) ) 138 { 139 $category = $page['plain_structure'][$id]; 140 $category['subcats'] = create_structure( $id, $restrictions ); 141 array_push( $structure, $category ); 142 } 130 $category = $page['plain_structure'][$id]; 131 $category['subcats'] = create_user_structure( $id ); 132 array_push( $structure, $category ); 143 133 } 144 134 return $structure; 145 135 } 146 136 147 function get_ subcat_ids( $id_uppercat )137 function get_user_subcat_ids( $id_uppercat ) 148 138 { 149 139 global $page; … … 158 148 159 149 // update_structure updates or add informations about each node of the 160 // structure : the last date, should the category be expanded in the menu ?, 161 // the associated expand string "48,14,54" 150 // structure : 162 151 // 163 // 1. last date 164 // for each category of the structure, we have to find the most recent 165 // subcat so that the parent cat has the same last_date info. 166 // For example : we have : 167 // > pets (2003.02.15) 168 // > dogs (2003.06.14) 169 // > rex (2003.06.18) 170 // > toby (2003.06.13) 171 // > kitten (2003.07.05) 172 // We finally want to have : 173 // > pets (2003.07.05) <- changed to pets > kitten last date 174 // > dogs (2003.06.18) <- changed to pets > dogs > rex last date 175 // > rex (2003.06.18) 176 // > toby (2003.06.13) 177 // > kitten (2003.07.05) 178 // 179 // 2. should the category be expanded in the menu ? 152 // 1. should the category be expanded in the menu ? 180 153 // If the category has to be expanded (ie its id is in the 181 154 // $page['tab_expand'] or all the categories must be expanded by default), 182 155 // $category['expanded'] is set to true. 183 156 // 184 // 3. associated expand string157 // 2. associated expand string 185 158 // in the menu, there is a expand string (used in the URL) to tell which 186 159 // categories must be expanded in the menu if this category is chosen … … 192 165 193 166 foreach ( $categories as $category ) { 194 // update the last date of the category195 $last_date = search_last_date( $category );196 $category['date_last'] = $last_date;197 167 // update the "expanded" key 198 168 if ( $user['expand'] … … 206 176 $category['expanded'] = false; 207 177 } 208 // update the 178 // update the "expand_string" key 209 179 if ( $page['expand'] == 'all' ) 210 180 { … … 220 190 $tab_expand = array_diff( $page['tab_expand'],array($category['id']) ); 221 191 } 222 else if ( count( $category['subcats'] )> 0 )192 else if ( $category['nb_sub_categories'] > 0 ) 223 193 { 224 194 // we have this time to add the $category['id']... … … 234 204 235 205 return $updated_categories; 236 }237 238 // search_last_date searchs the last date for a given category. If we take239 // back the example given for update_last_dates, we should have :240 // search_last_date( pets ) --> 2003.07.05241 // search_last_date( pets > dogs ) --> 2003.06.18242 // and so on243 function search_last_date( $category )244 {245 $date_last = $category['date_last'];246 foreach ( $category['subcats'] as $subcat ) {247 $subcat_date_last = search_last_date( $subcat );248 if ( $subcat_date_last > $date_last )249 {250 $date_last = $subcat_date_last;251 }252 }253 return $date_last;254 206 } 255 207 … … 263 215 function count_images( $categories ) 264 216 { 217 return count_user_total_images(); 265 218 $total = 0; 266 219 foreach ( $categories as $category ) { … … 269 222 } 270 223 return $total; 224 } 225 226 function count_user_total_images() 227 { 228 global $user; 229 230 $query = 'SELECT SUM(nb_images) AS total'; 231 $query.= ' FROM '.CATEGORIES_TABLE; 232 if ( count( $user['restrictions'] ) > 0 ) 233 $query.= ' WHERE id NOT IN ('.$user['forbidden_categories'].')'; 234 $query.= ';'; 235 236 $row = mysql_fetch_array( mysql_query( $query ) ); 237 238 if ( !isset( $row['total'] ) ) $row['total'] = 0; 239 240 return $row['total']; 271 241 } 272 242 … … 286 256 global $page; 287 257 258 $infos = array( 'nb_images','id_uppercat','comment','site_id','galleries_url' 259 ,'dir','date_last','uploadable','status','visible' 260 ,'representative_picture_id','uppercats' ); 261 262 $query = 'SELECT '.implode( ',', $infos ); 263 $query.= ' FROM '.CATEGORIES_TABLE.' AS a'; 264 $query.= ', '.SITES_TABLE.' AS b'; 265 $query.= ' WHERE a.id = '.$id; 266 $query.= ' AND a.site_id = b.id'; 267 $query.= ';'; 268 $row = mysql_fetch_array( mysql_query( $query ) ); 269 288 270 $cat = array(); 289 290 $query = 'SELECT nb_images,id_uppercat,comment,site_id,galleries_url,dir'; 291 $query.= ',date_last,uploadable,status,visible,representative_picture_id'; 292 $query.= ' FROM '.PREFIX_TABLE.'categories AS a'; 293 $query.= ', '.PREFIX_TABLE.'sites AS b'; 294 $query.= ' WHERE a.id = '.$id; 295 $query.= ' AND a.site_id = b.id;'; 296 $row = mysql_fetch_array( mysql_query( $query ) ); 297 $cat['site_id'] = $row['site_id']; 298 $cat['id_uppercat'] = $row['id_uppercat']; 299 $cat['comment'] = nl2br( $row['comment'] ); 300 $cat['nb_images'] = $row['nb_images']; 301 $cat['dir'] = $row['dir']; 302 $cat['date_last'] = $row['date_last']; 303 $cat['uploadable'] = get_boolean( $row['uploadable'] ); 304 $cat['status'] = $row['status']; 305 $cat['visible'] = get_boolean( $row['visible'] ); 306 $cat['representative_picture_id'] = $row['representative_picture_id']; 271 // affectation of each field of the table "config" to an information of the 272 // array $cat. 273 foreach ( $infos as $info ) { 274 if ( isset( $row[$info] ) ) $cat[$info] = $row[$info]; 275 else $cat[$info] = ''; 276 // If the field is true or false, the variable is transformed into a 277 // boolean value. 278 if ( $cat[$info] == 'true' or $cat[$info] == 'false' ) 279 { 280 $cat[$info] = get_boolean( $cat[$info] ); 281 } 282 } 283 $cat['comment'] = nl2br( $cat['comment'] ); 307 284 308 285 $cat['name'] = array(); 309 286 310 if ( !isset( $page['plain_structure'] ) ) 311 $page['plain_structure'] = get_plain_structure(); 312 313 array_push( $cat['name'], $page['plain_structure'][$id]['name'] ); 314 while ( $page['plain_structure'][$id]['id_uppercat'] != '' ) 315 { 316 $id = $page['plain_structure'][$id]['id_uppercat']; 317 array_push( $cat['name'], $page['plain_structure'][$id]['name'] ); 318 } 287 $query = 'SELECT name FROM '.CATEGORIES_TABLE; 288 $query.= ' WHERE id IN ('.$cat['uppercats'].')'; 289 $query.= ' ORDER BY id ASC'; 290 $query.= ';'; 291 $result = mysql_query( $query ); 292 while( $row = mysql_fetch_array( $result ) ) 293 { 294 array_push( $cat['name'], $row['name'] ); 295 } 296 319 297 return $cat; 320 298 } … … 338 316 global $page; 339 317 340 if ( !isset( $page['plain_structure'] ) ) 341 $page['plain_structure'] = get_plain_structure(); 342 343 // creating the local path : "root_cat/sub_cat/sub_sub_cat/" 344 $dir = $page['plain_structure'][$category_id]['dir'].'/'; 345 while ( $page['plain_structure'][$category_id]['id_uppercat'] != '' ) 346 { 347 $category_id = $page['plain_structure'][$category_id]['id_uppercat']; 348 $dir = $page['plain_structure'][$category_id]['dir'].'/'.$dir; 349 } 350 return $dir; 318 $uppercats = ''; 319 $local_dir = ''; 320 321 if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) 322 { 323 $uppercats = $page['plain_structure'][$category_id]['uppercats']; 324 } 325 else 326 { 327 $query = 'SELECT uppercats'; 328 $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; 329 $query.= ';'; 330 $row = mysql_fetch_array( mysql_query( $query ) ); 331 $uppercats = $row['uppercats']; 332 } 333 334 $upper_array = explode( ',', $uppercats ); 335 336 $database_dirs = array(); 337 $query = 'SELECT id,dir'; 338 $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; 339 $query.= ';'; 340 $result = mysql_query( $query ); 341 while( $row = mysql_fetch_array( $result ) ) 342 { 343 $database_dirs[$row['id']] = $row['dir']; 344 } 345 foreach ( $upper_array as $id ) { 346 $local_dir.= $database_dirs[$id].'/'; 347 } 348 349 return $local_dir; 351 350 } 352 351 … … 357 356 global $page; 358 357 359 if ( !isset( $page['plain_structure'] ) )360 $page['plain_structure'] = get_plain_structure();361 362 358 $query = 'SELECT galleries_url'; 363 $query.= ' FROM '.PREFIX_TABLE.'sites'; 364 $query.= ' WHERE id = '.$page['plain_structure'][$category_id]['site_id']; 359 $query.= ' FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c'; 360 $query.= ' WHERE s.id = c.site_id'; 361 $query.= ' AND c.id = '.$category_id; 365 362 $query.= ';'; 366 363 $row = mysql_fetch_array( mysql_query( $query ) ); … … 375 372 // - $style : the style of the span tag for the lowest category, 376 373 // "font-style:italic;" for example 377 function get_cat_display_name( $array_cat_names, $separation, $style ) 378 { 379 $output = ""; 380 for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- ) 381 { 382 if ( $i != sizeof( $array_cat_names ) - 1 ) 383 { 384 $output.= $separation; 385 } 386 if ( $i != 0 ) 387 { 388 $output.= $array_cat_names[$i]; 389 } 374 function get_cat_display_name( $array_cat_names, $separation, 375 $style, $replace_space = true ) 376 { 377 $output = ''; 378 foreach ( $array_cat_names as $i => $name ) { 379 if ( $i > 0 ) $output.= $separation; 380 if ( $i < count( $array_cat_names ) - 1 or $style == '') 381 $output.= $name; 390 382 else 391 { 392 if ( $style != "" ) 393 { 394 $output.= '<span style="'.$style.'">'; 395 } 396 $output.= $array_cat_names[$i]; 397 if ( $style != "" ) 398 { 399 $output.= "</span>"; 400 } 401 } 402 } 403 return replace_space( $output ); 383 $output.= '<span style="'.$style.'">'.$name.'</span>'; 384 } 385 if ( $replace_space ) return replace_space( $output ); 386 else return $output; 404 387 } 405 388 … … 419 402 function initialize_category( $calling_page = 'category' ) 420 403 { 404 pwg_debug( 'start initialize_category' ); 421 405 global $page,$lang,$user,$conf; 422 406 … … 427 411 $page['nb_image_page'] = $user['nb_image_page']; 428 412 // $url is used to create the navigation bar 429 $url = './category.php?cat='.$page['cat'].'&expand='.$page['expand']; 413 $url = './category.php?cat='.$page['cat']; 414 if ( isset($page['expand']) ) $url.= '&expand='.$page['expand']; 430 415 // simple category 431 416 if ( is_numeric( $page['cat'] ) ) … … 438 423 $page['cat_site_id'] = $result['site_id']; 439 424 $page['cat_uploadable'] = $result['uploadable']; 440 $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' ); 425 $page['uppercats'] = $result['uppercats']; 426 $page['title'] = get_cat_display_name( $page['cat_name'],' - ','',false); 441 427 $page['where'] = ' WHERE category_id = '.$page['cat']; 442 428 } … … 447 433 { 448 434 // we must not show pictures of a forbidden category 449 $restricted_cats = get_all_restrictions( $user['id'],$user['status'] ); 450 if ( count( $restricted_cats ) > 0 ) 435 if ( $user['forbidden_categories'] != '' ) 451 436 { 452 $where_append.= ' AND category_id NOT IN ('; 453 foreach ( $restricted_cats as $i => $restricted_cat ) { 454 if ( $i > 0 ) $where_append.= ','; 455 $where_append.= $restricted_cat; 456 } 457 $where_append.= ')'; 437 $forbidden = ' category_id NOT IN '; 438 $forbidden.= '('.$user['forbidden_categories'].')'; 458 439 } 459 440 } … … 500 481 } 501 482 $page['where'].= ' )'; 502 $page['where'].= $where_append;483 if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; 503 484 504 485 $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images'; 505 $query.= ' FROM '. PREFIX_TABLE.'images';506 $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_categoryAS ic';486 $query.= ' FROM '.IMAGES_TABLE; 487 $query.= ' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic'; 507 488 $query.= ' ON id = ic.image_id'; 508 489 $query.= $page['where']; … … 516 497 $page['title'] = $lang['favorites']; 517 498 518 $page['where'] = ', '. PREFIX_TABLE.'favoritesAS fav';499 $page['where'] = ', '.FAVORITES_TABLE.' AS fav'; 519 500 $page['where'].= ' WHERE user_id = '.$user['id']; 520 501 $page['where'].= ' AND fav.image_id = id'; 521 502 522 503 $query = 'SELECT COUNT(*) AS nb_total_images'; 523 $query.= ' FROM '. PREFIX_TABLE.'favorites';504 $query.= ' FROM '.FAVORITES_TABLE; 524 505 $query.= ' WHERE user_id = '.$user['id']; 525 506 $query.= ';'; … … 534 515 $page['where'] = " WHERE date_available > '"; 535 516 $page['where'].= date( 'Y-m-d', $date )."'"; 536 $page['where'].= $where_append;517 if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; 537 518 538 519 $query = 'SELECT COUNT(DISTINCT(id)) AS nb_total_images'; 539 $query.= ' FROM '. PREFIX_TABLE.'images';540 $query.= ' LEFTJOIN '.PREFIX_TABLE.'image_category AS ic';520 $query.= ' FROM '.IMAGES_TABLE; 521 $query.= ' INNER JOIN '.PREFIX_TABLE.'image_category AS ic'; 541 522 $query.= ' ON id = ic.image_id'; 542 523 $query.= $page['where']; … … 547 528 { 548 529 $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat']; 549 $page['where'] = ' WHERE category_id != -1'.$where_append; 530 531 if ( isset( $forbidden ) ) $page['where'] = ' WHERE '.$forbidden; 532 else $page['where'] = ''; 550 533 $conf['order_by'] = ' ORDER BY hit DESC, file ASC'; 551 534 $page['cat_nb_images'] = $conf['top_number']; 552 if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] ) 535 if ( isset( $page['start'] ) 536 and ($page['start']+$user['nb_image_page']>=$conf['top_number'])) 553 537 { 554 538 $page['nb_image_page'] = $conf['top_number'] - $page['start']; … … 556 540 } 557 541 558 if ( $query != '')542 if ( isset($query)) 559 543 { 560 544 $result = mysql_query( $query ); … … 574 558 $page['title'] = $lang['diapo_default_page_title']; 575 559 } 560 pwg_debug( 'end initialize_category' ); 576 561 } 577 562 … … 600 585 601 586 $query = 'SELECT id,nb_images'; 602 $query.= ' FROM '. PREFIX_TABLE.'categories';587 $query.= ' FROM '.CATEGORIES_TABLE; 603 588 $query.= ' WHERE id_uppercat '; 604 589 if ( !is_numeric( $id_uppercat ) ) $query.= 'is NULL'; 605 590 else $query.= '= '.$id_uppercat; 606 591 // we must not show pictures of a forbidden category 607 foreach ( $user['restrictions'] as $restricted_cat ) { 608 $query.= ' AND id != '.$restricted_cat; 592 if ( $user['forbidden_categories'] != '' ) 593 { 594 $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')'; 609 595 } 610 596 $query.= ' ORDER BY rank'; … … 633 619 634 620 $query = 'SELECT id,nb_images'; 635 $query.= ' FROM '. PREFIX_TABLE.'categories';621 $query.= ' FROM '.CATEGORIES_TABLE; 636 622 $query.= ' WHERE id_uppercat = '.$id_uppercat; 637 623 // we must not show pictures of a forbidden category 638 foreach ( $user['restrictions'] as $restricted_cat ) { 639 $query.= ' AND id != '.$restricted_cat; 624 if ( $user['forbidden_categories'] != '' ) 625 { 626 $query.= ' AND id NOT IN ('.$user['forbidden_categories'].')'; 640 627 } 641 628 $query.= ' ORDER BY RAND()';
Note: See TracChangeset
for help on using the changeset viewer.