Changeset 25728 for trunk/include/functions_category.inc.php
- Timestamp:
- Nov 26, 2013, 9:48:08 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/functions_category.inc.php
r25658 r25728 481 481 } 482 482 483 /** 484 * Get computed array of categories, that means cache data of all categories 485 * available for the current user (count_categories, count_images, etc.). 486 * 487 * @param array &$userdata 488 * @param int $filter_days number of recent days to filter on or null 489 * @return array 490 */ 491 function get_computed_categories(&$userdata, $filter_days=null) 492 { 493 $query = 'SELECT c.id AS cat_id, id_uppercat'; 494 // Count by date_available to avoid count null 495 $query .= ', 496 MAX(date_available) AS date_last, COUNT(date_available) AS nb_images 497 FROM '.CATEGORIES_TABLE.' as c 498 LEFT JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.category_id = c.id 499 LEFT JOIN '.IMAGES_TABLE.' AS i 500 ON ic.image_id = i.id 501 AND i.level<='.$userdata['level']; 502 503 if ( isset($filter_days) ) 504 { 505 $query .= ' AND i.date_available > '.pwg_db_get_recent_period_expression($filter_days); 506 } 507 508 if ( !empty($userdata['forbidden_categories']) ) 509 { 510 $query.= ' 511 WHERE c.id NOT IN ('.$userdata['forbidden_categories'].')'; 512 } 513 514 $query.= ' 515 GROUP BY c.id'; 516 517 $result = pwg_query($query); 518 519 $userdata['last_photo_date'] = null; 520 $cats = array(); 521 while ($row = pwg_db_fetch_assoc($result)) 522 { 523 $row['user_id'] = $userdata['id']; 524 $row['nb_categories'] = 0; 525 $row['count_categories'] = 0; 526 $row['count_images'] = (int)$row['nb_images']; 527 $row['max_date_last'] = $row['date_last']; 528 if ($row['date_last'] > $userdata['last_photo_date']) 529 { 530 $userdata['last_photo_date'] = $row['date_last']; 531 } 532 533 $cats[$row['cat_id']] = $row; 534 } 535 536 foreach ($cats as $cat) 537 { 538 if ( !isset( $cat['id_uppercat'] ) ) 539 continue; 540 541 $parent = & $cats[ $cat['id_uppercat'] ]; 542 $parent['nb_categories']++; 543 544 do 545 { 546 $parent['count_images'] += $cat['nb_images']; 547 $parent['count_categories']++; 548 549 if ((empty($parent['max_date_last'])) or ($parent['max_date_last'] < $cat['date_last'])) 550 { 551 $parent['max_date_last'] = $cat['date_last']; 552 } 553 554 if ( !isset( $parent['id_uppercat'] ) ) 555 break; 556 $parent = & $cats[$parent['id_uppercat']]; 557 } 558 while (true); 559 unset($parent); 560 } 561 562 if ( isset($filter_days) ) 563 { 564 foreach ($cats as $category) 565 { 566 if (empty($category['max_date_last'])) 567 { 568 remove_computed_category($cats, $category); 569 } 570 } 571 } 572 return $cats; 573 } 574 575 /** 576 * Removes a category from computed array of categories and updates counters. 577 * 578 * @param array &$cats 579 * @param array $cat category to remove 580 */ 581 function remove_computed_category(&$cats, $cat) 582 { 583 if ( isset($cats[$cat['id_uppercat']]) ) 584 { 585 $parent = &$cats[ $cat['id_uppercat'] ]; 586 $parent['nb_categories']--; 587 588 do 589 { 590 $parent['count_images'] -= $cat['nb_images']; 591 $parent['count_categories'] -= 1+$cat['count_categories']; 592 593 if ( !isset($cats[$parent['id_uppercat']]) ) 594 { 595 break; 596 } 597 $parent = &$cats[$parent['id_uppercat']]; 598 } 599 while (true); 600 } 601 602 unset($cats[$cat['cat_id']]); 603 } 604 483 605 ?>
Note: See TracChangeset
for help on using the changeset viewer.