aboutsummaryrefslogtreecommitdiffstats
path: root/include/functions_category.inc.php
diff options
context:
space:
mode:
Diffstat (limited to 'include/functions_category.inc.php')
-rw-r--r--include/functions_category.inc.php254
1 files changed, 198 insertions, 56 deletions
diff --git a/include/functions_category.inc.php b/include/functions_category.inc.php
index 7cde5e35c..43289ca01 100644
--- a/include/functions_category.inc.php
+++ b/include/functions_category.inc.php
@@ -2,7 +2,7 @@
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
-// | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org |
+// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
@@ -22,19 +22,31 @@
// +-----------------------------------------------------------------------+
/**
- * Provides functions to handle categories.
- *
- *
+ * @package functions\category
*/
+
+/**
+ * Callback used for sorting by global_rank
+ */
+function global_rank_compare($a, $b)
+{
+ return strnatcasecmp($a['global_rank'], $b['global_rank']);
+}
+
+/**
+ * Callback used for sorting by rank
+ */
+function rank_compare($a, $b)
+{
+ return $a['rank'] - $b['rank'];
+}
+
/**
* Is the category accessible to the connected user ?
+ * If the user is not authorized to see this category, script exits
*
- * Note : if the user is not authorized to see this category, page creation
- * ends (exit command in this function)
- *
- * @param int category id to verify
- * @return void
+ * @param int $category_id
*/
function check_restrictions($category_id)
{
@@ -48,6 +60,11 @@ function check_restrictions($category_id)
}
}
+/**
+ * Returns template vars for main categories menu.
+ *
+ * @return array[]
+ */
function get_categories_menu()
{
global $page, $user, $filter, $conf;
@@ -143,25 +160,13 @@ WHERE '.$where.'
return $cats;
}
-
/**
- * Retrieve informations about a category in the database
- *
- * Returns an array with following keys :
- *
- * - comment
- * - dir : directory, might be empty for virtual categories
- * - name : an array with indexes from 0 (lowest cat name) to n (most
- * uppercat name findable)
- * - nb_images
- * - id_uppercat
- * - site_id
- * -
+ * Retrieves informations about a category.
*
- * @param int category id
+ * @param int $id
* @return array
*/
-function get_cat_info( $id )
+function get_cat_info($id)
{
$query = '
SELECT *
@@ -212,9 +217,15 @@ SELECT *
return $cat;
}
-
-
-// returns an array of image orders available for users/visitors
+/**
+ * Returns an array of image orders available for users/visitors.
+ * Each entry is an array containing
+ * 0: name
+ * 1: SQL ORDER command
+ * 2: visiblity (true or false)
+ *
+ * @return array[]
+ */
function get_category_preferred_image_orders()
{
global $conf, $page;
@@ -235,6 +246,14 @@ function get_category_preferred_image_orders()
));
}
+/**
+ * Assign a template var useable with {html_options} from a list of categories
+ *
+ * @param array[] $categories (at least id,name,global_rank,uppercats for each)
+ * @param int[] $selected ids of selected items
+ * @param string $blockname variable name in template
+ * @param bool $fullname full breadcrumb or not
+ */
function display_select_categories($categories,
$selecteds,
$blockname,
@@ -250,8 +269,7 @@ function display_select_categories($categories,
$option = strip_tags(
get_cat_display_name_cache(
$category['uppercats'],
- null,
- false
+ null
)
);
}
@@ -275,7 +293,13 @@ function display_select_categories($categories,
$template->assign( $blockname.'_selected', $selecteds);
}
-function display_select_cat_wrapper($query, $selecteds, $blockname,
+/**
+ * Same as display_select_categories but categories are ordered by rank
+ * @see display_select_categories()
+ */
+function display_select_cat_wrapper($query,
+ $selecteds,
+ $blockname,
$fullname = true)
{
$categories = array_from_query($query);
@@ -284,10 +308,10 @@ function display_select_cat_wrapper($query, $selecteds, $blockname,
}
/**
- * returns all subcategory identifiers of given category ids
+ * Returns all subcategory identifiers of given category ids
*
- * @param array ids
- * @return array
+ * @param int[] $ids
+ * @return int[]
*/
function get_subcat_ids($ids)
{
@@ -314,12 +338,14 @@ SELECT DISTINCT(id)
return array_from_query($query, 'id');
}
-/** finds a matching category id from a potential list of permalinks
- * @param array permalinks example: holiday holiday/france holiday/france/paris
- * @param int idx - output of the index in $permalinks that matches
- * return category id or null if no match
+/**
+ * Finds a matching category id from a potential list of permalinks
+ *
+ * @param string[] $permalinks
+ * @param int &$idx filled with the index in $permalinks that matches
+ * @return int|null
*/
-function get_cat_id_from_permalinks( $permalinks, &$idx )
+function get_cat_id_from_permalinks($permalinks, &$idx)
{
$in = '';
foreach($permalinks as $permalink)
@@ -360,23 +386,17 @@ UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
return null;
}
-function global_rank_compare($a, $b)
-{
- return strnatcasecmp($a['global_rank'], $b['global_rank']);
-}
-
-function rank_compare($a, $b)
-{
- return $a['rank'] - $b['rank'];
-}
-
/**
- * returns display text for information images of category
+ * Returns display text for images counter of category
*
- * @param array categories
+ * @param int $cat_nb_images nb images directly in category
+ * @param int $cat_count_images nb images in category (including subcats)
+ * @param int $cat_count_categories nb subcats
+ * @param bool $short_message if true append " in this album"
+ * @param string $separator
* @return string
*/
-function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $Separator = '\n')
+function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $separator = '\n')
{
$display_text = '';
@@ -384,7 +404,7 @@ function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_
{
if ($cat_nb_images > 0 and $cat_nb_images < $cat_count_images)
{
- $display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $Separator).$Separator;
+ $display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $separator).$separator;
$cat_count_images-= $cat_nb_images;
$cat_nb_images = 0;
}
@@ -395,7 +415,7 @@ function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_
if ($cat_count_categories == 0 or $cat_nb_images == $cat_count_images)
{
//no descendant categories or descendants do not contain images
- if (! $short_message)
+ if (!$short_message)
{
$display_text.= ' '.l10n('in this album');
}
@@ -410,10 +430,11 @@ function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_
}
/**
- * Find a random photo among all photos below a given album in the tree (not
- * only photo directly associated to the album but also to sub-albums)
+ * Find a random photo among all photos inside an album (including sub-albums)
*
- * we need $category['uppercats'], $category['id'], $category['count_images']
+ * @param array $category (at least id,uppercats,count_images)
+ * @param bool $recursive
+ * @return int|null
*/
function get_random_image_in_category($category, $recursive=true)
{
@@ -459,5 +480,126 @@ SELECT image_id
return $image_id;
}
+/**
+ * Get computed array of categories, that means cache data of all categories
+ * available for the current user (count_categories, count_images, etc.).
+ *
+ * @param array &$userdata
+ * @param int $filter_days number of recent days to filter on or null
+ * @return array
+ */
+function get_computed_categories(&$userdata, $filter_days=null)
+{
+ $query = 'SELECT c.id AS cat_id, id_uppercat';
+ // Count by date_available to avoid count null
+ $query .= ',
+ MAX(date_available) AS date_last, COUNT(date_available) AS nb_images
+FROM '.CATEGORIES_TABLE.' as c
+ LEFT JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.category_id = c.id
+ LEFT JOIN '.IMAGES_TABLE.' AS i
+ ON ic.image_id = i.id
+ AND i.level<='.$userdata['level'];
+
+ if ( isset($filter_days) )
+ {
+ $query .= ' AND i.date_available > '.pwg_db_get_recent_period_expression($filter_days);
+ }
+
+ if ( !empty($userdata['forbidden_categories']) )
+ {
+ $query.= '
+ WHERE c.id NOT IN ('.$userdata['forbidden_categories'].')';
+ }
+
+ $query.= '
+ GROUP BY c.id';
+
+ $result = pwg_query($query);
+
+ $userdata['last_photo_date'] = null;
+ $cats = array();
+ while ($row = pwg_db_fetch_assoc($result))
+ {
+ $row['user_id'] = $userdata['id'];
+ $row['nb_categories'] = 0;
+ $row['count_categories'] = 0;
+ $row['count_images'] = (int)$row['nb_images'];
+ $row['max_date_last'] = $row['date_last'];
+ if ($row['date_last'] > $userdata['last_photo_date'])
+ {
+ $userdata['last_photo_date'] = $row['date_last'];
+ }
+
+ $cats[$row['cat_id']] = $row;
+ }
+
+ foreach ($cats as $cat)
+ {
+ if ( !isset( $cat['id_uppercat'] ) )
+ continue;
+
+ $parent = & $cats[ $cat['id_uppercat'] ];
+ $parent['nb_categories']++;
+
+ do
+ {
+ $parent['count_images'] += $cat['nb_images'];
+ $parent['count_categories']++;
+
+ if ((empty($parent['max_date_last'])) or ($parent['max_date_last'] < $cat['date_last']))
+ {
+ $parent['max_date_last'] = $cat['date_last'];
+ }
+
+ if ( !isset( $parent['id_uppercat'] ) )
+ break;
+ $parent = & $cats[$parent['id_uppercat']];
+ }
+ while (true);
+ unset($parent);
+ }
+
+ if ( isset($filter_days) )
+ {
+ foreach ($cats as $category)
+ {
+ if (empty($category['max_date_last']))
+ {
+ remove_computed_category($cats, $category);
+ }
+ }
+ }
+ return $cats;
+}
+
+/**
+ * Removes a category from computed array of categories and updates counters.
+ *
+ * @param array &$cats
+ * @param array $cat category to remove
+ */
+function remove_computed_category(&$cats, $cat)
+{
+ if ( isset($cats[$cat['id_uppercat']]) )
+ {
+ $parent = &$cats[ $cat['id_uppercat'] ];
+ $parent['nb_categories']--;
+
+ do
+ {
+ $parent['count_images'] -= $cat['nb_images'];
+ $parent['count_categories'] -= 1+$cat['count_categories'];
+
+ if ( !isset($cats[$parent['id_uppercat']]) )
+ {
+ break;
+ }
+ $parent = &$cats[$parent['id_uppercat']];
+ }
+ while (true);
+ }
+
+ unset($cats[$cat['cat_id']]);
+}
?> \ No newline at end of file