diff options
Diffstat (limited to 'include/functions_tag.inc.php')
-rw-r--r-- | include/functions_tag.inc.php | 142 |
1 files changed, 75 insertions, 67 deletions
diff --git a/include/functions_tag.inc.php b/include/functions_tag.inc.php index 5766b9509..6e047c502 100644 --- a/include/functions_tag.inc.php +++ b/include/functions_tag.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 | // +-----------------------------------------------------------------------+ @@ -21,16 +21,36 @@ // | USA. | // +-----------------------------------------------------------------------+ +/** + * @package functions\tag + */ + /** - * Tags available. Each return tag is represented as an array with its id, - * its name, its weight (count), its url name. Tags are not sorted. + * Returns the number of available tags for the connected user. * - * The returned list can be a subset of all existing tags due to - * permissions, only if a list of forbidden categories is provided + * @return int + */ +function get_nb_available_tags() +{ + global $user; + if (!isset($user['nb_available_tags'])) + { + $user['nb_available_tags'] = count(get_available_tags()); + single_update(USER_CACHE_TABLE, + array('nb_available_tags'=>$user['nb_available_tags']), + array('user_id'=>$user['id']) + ); + } + return $user['nb_available_tags']; +} + +/** + * Returns all available tags for the connected user (not sorted). + * The returned list can be a subset of all existing tags due to permissions, + * also tags with no images are not returned. * - * @param array forbidden categories - * @return array + * @return array [id, name, counter, url_name] */ function get_available_tags() { @@ -38,18 +58,18 @@ function get_available_tags() $query = ' SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter FROM '.IMAGE_CATEGORY_TABLE.' ic - INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF - ( - array - ( - 'forbidden_categories' => 'category_id', - 'visible_categories' => 'category_id', - 'visible_images' => 'ic.image_id' - ), - ' - WHERE' + INNER JOIN '.IMAGE_TAG_TABLE.' it + ON ic.image_id=it.image_id + '.get_sql_condition_FandF( + array( + 'forbidden_categories' => 'category_id', + 'visible_categories' => 'category_id', + 'visible_images' => 'ic.image_id' + ), + ' WHERE ' ).' - GROUP BY tag_id'; + GROUP BY tag_id +;'; $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter'); if ( empty($tag_counters) ) @@ -61,6 +81,7 @@ SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter SELECT * FROM '.TAGS_TABLE; $result = pwg_query($query); + $tags = array(); while ($row = pwg_db_fetch_assoc($result)) { @@ -68,7 +89,7 @@ SELECT * if ( $counter ) { $row['counter'] = $counter; - $row['name'] = trigger_event('render_tag_name', $row['name']); + $row['name'] = trigger_event('render_tag_name', $row['name'], $row); $tags[] = $row; } } @@ -76,9 +97,9 @@ SELECT * } /** - * All tags, even tags associated to no image. + * Returns all tags even associated to no image. * - * @return array + * @return array [id, name, url_name] */ function get_all_tags() { @@ -90,7 +111,7 @@ SELECT * $tags = array(); while ($row = pwg_db_fetch_assoc($result)) { - $row['name'] = trigger_event('render_tag_name', $row['name']); + $row['name'] = trigger_event('render_tag_name', $row['name'], $row); $tags[] = $row; } @@ -104,11 +125,11 @@ SELECT * * level of each tag. * * The level of each tag depends on the average count of tags. This - * calcylation method avoid having very different levels for tags having + * calculation method avoid having very different levels for tags having * nearly the same count when set are small. * - * @param array tags - * @return array + * @param array $tags at least [id, counter] + * @return array [..., level] */ function add_level_to_tags($tags) { @@ -158,13 +179,14 @@ function add_level_to_tags($tags) } /** - * return the list of image ids corresponding to given tags. AND & OR mode - * supported. + * Return the list of image ids corresponding to given tags. + * AND & OR mode supported. * - * @param array tag ids + * @param int[] $tag_ids * @param string mode - * @param string extra_images_where_sql - optionally apply a sql where filter to retrieved images - * @param string order_by - optionally overwrite default photo order + * @param string $extra_images_where_sql - optionally apply a sql where filter to retrieved images + * @param string $order_by - optionally overwrite default photo order + * @param bool $user_permissions * @return array */ function get_image_ids_for_tags($tag_ids, $mode='AND', $extra_images_where_sql='', $order_by='', $use_permissions=true) @@ -175,7 +197,8 @@ function get_image_ids_for_tags($tag_ids, $mode='AND', $extra_images_where_sql=' return array(); } - $query = 'SELECT id + $query = ' +SELECT id FROM '.IMAGES_TABLE.' i '; if ($use_permissions) @@ -214,14 +237,14 @@ function get_image_ids_for_tags($tag_ids, $mode='AND', $extra_images_where_sql=' } /** - * return a list of tags corresponding to given items. + * Return a list of tags corresponding to given items. * - * @param array items - * @param array max_tags - * @param array excluded_tag_ids - * @return array + * @param int[] $items + * @param int $max_tags + * @param int[] $excluded_tag_ids + * @return array [id, name, counter, url_name] */ -function get_common_tags($items, $max_tags, $excluded_tag_ids=null) +function get_common_tags($items, $max_tags, $excluded_tag_ids=array()) { if (empty($items)) { @@ -241,7 +264,7 @@ SELECT t.*, count(*) AS counter GROUP BY t.id ORDER BY '; if ($max_tags>0) - { + { // TODO : why ORDER field is in the if ? $query .= 'counter DESC LIMIT '.$max_tags; } @@ -254,7 +277,7 @@ SELECT t.*, count(*) AS counter $tags = array(); while($row = pwg_db_fetch_assoc($result)) { - $row['name'] = trigger_event('render_tag_name', $row['name']); + $row['name'] = trigger_event('render_tag_name', $row['name'], $row); $tags[] = $row; } usort($tags, 'tag_alpha_compare'); @@ -262,45 +285,29 @@ SELECT t.*, count(*) AS counter } /** - * return a list of tags corresponding to any of ids, url_names, names + * Return a list of tags corresponding to any of ids, url_names or names. * - * @param array ids - * @param array url_names - * @param array names - * @return array + * @param int[] $ids + * @param string[] $url_names + * @param string[] $names + * @return array [id, name, url_name] */ -function find_tags($ids, $url_names=array(), $names=array() ) +function find_tags($ids=array(), $url_names=array(), $names=array() ) { $where_clauses = array(); - if ( !empty($ids) ) + if (!empty($ids)) { $where_clauses[] = 'id IN ('.implode(',', $ids).')'; } - if ( !empty($url_names) ) + if (!empty($url_names)) { $where_clauses[] = - 'url_name IN ('. - implode( - ',', - array_map( - create_function('$s', 'return "\'".$s."\'";'), - $url_names - ) - ) - .')'; + 'url_name IN (\''. implode('\', \'', $url_names) .'\')'; } - if ( !empty($names) ) + if (!empty($names)) { $where_clauses[] = - 'name IN ('. - implode( - ',', - array_map( - create_function('$s', 'return "\'".$s."\'";'), - $names - ) - ) - .')'; + 'name IN (\''. implode('\', \'', $names) .'\')'; } if (empty($where_clauses)) { @@ -317,8 +324,9 @@ SELECT * $tags = array(); while ($row = pwg_db_fetch_assoc($result)) { - array_push($tags, $row); + $tags[] = $row; } return $tags; } + ?>
\ No newline at end of file |