2006-04-03 00:26:19 +02:00
|
|
|
<?php
|
|
|
|
// +-----------------------------------------------------------------------+
|
2011-01-18 01:02:52 +01:00
|
|
|
// | Piwigo - a PHP based photo gallery |
|
2008-04-05 00:57:23 +02:00
|
|
|
// +-----------------------------------------------------------------------+
|
2014-01-05 01:19:25 +01:00
|
|
|
// | Copyright(C) 2008-2014 Piwigo Team http://piwigo.org |
|
2008-04-05 00:57:23 +02:00
|
|
|
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
|
|
|
|
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
|
|
|
|
// +-----------------------------------------------------------------------+
|
|
|
|
// | This program is free software; you can redistribute it and/or modify |
|
2006-04-03 00:26:19 +02:00
|
|
|
// | it under the terms of the GNU General Public License as published by |
|
|
|
|
// | the Free Software Foundation |
|
|
|
|
// | |
|
|
|
|
// | This program is distributed in the hope that it will be useful, but |
|
|
|
|
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
|
|
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
|
|
|
// | General Public License for more details. |
|
|
|
|
// | |
|
|
|
|
// | You should have received a copy of the GNU General Public License |
|
|
|
|
// | along with this program; if not, write to the Free Software |
|
|
|
|
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
|
|
|
|
// | USA. |
|
|
|
|
// +-----------------------------------------------------------------------+
|
|
|
|
|
2013-11-23 23:57:15 +01:00
|
|
|
/**
|
|
|
|
* @package functions\tag
|
|
|
|
*/
|
|
|
|
|
2006-04-03 00:26:19 +02:00
|
|
|
|
2013-11-23 23:57:15 +01:00
|
|
|
/**
|
|
|
|
* Returns the number of available tags for the connected user.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2013-03-24 07:46:35 +01:00
|
|
|
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'];
|
|
|
|
}
|
|
|
|
|
2006-04-03 00:26:19 +02:00
|
|
|
/**
|
2013-11-23 23:57:15 +01:00
|
|
|
* 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.
|
2006-04-03 00:26:19 +02:00
|
|
|
*
|
2013-11-23 23:57:15 +01:00
|
|
|
* @return array [id, name, counter, url_name]
|
2006-04-03 00:26:19 +02:00
|
|
|
*/
|
2006-12-21 22:38:20 +01:00
|
|
|
function get_available_tags()
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
|
|
|
// we can find top fatter tags among reachable images
|
2007-09-12 05:37:01 +02:00
|
|
|
$query = '
|
2010-07-03 14:12:25 +02:00
|
|
|
SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter
|
2007-09-12 05:37:01 +02:00
|
|
|
FROM '.IMAGE_CATEGORY_TABLE.' ic
|
2013-11-23 23:57:15 +01:00
|
|
|
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 '
|
2007-09-12 05:37:01 +02:00
|
|
|
).'
|
2013-11-23 23:57:15 +01:00
|
|
|
GROUP BY tag_id
|
|
|
|
;';
|
2014-02-11 22:47:44 +01:00
|
|
|
$tag_counters = query2array($query, 'tag_id', 'counter');
|
2006-12-21 22:38:20 +01:00
|
|
|
|
2007-09-12 05:37:01 +02:00
|
|
|
if ( empty($tag_counters) )
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
2007-09-12 05:37:01 +02:00
|
|
|
return array();
|
2006-04-03 00:26:19 +02:00
|
|
|
}
|
2006-04-08 21:42:11 +02:00
|
|
|
|
2007-09-12 05:37:01 +02:00
|
|
|
$query = '
|
2008-07-01 04:09:21 +02:00
|
|
|
SELECT *
|
2007-09-12 05:37:01 +02:00
|
|
|
FROM '.TAGS_TABLE;
|
|
|
|
$result = pwg_query($query);
|
2013-11-23 23:57:15 +01:00
|
|
|
|
2006-04-03 00:26:19 +02:00
|
|
|
$tags = array();
|
2009-11-20 15:17:04 +01:00
|
|
|
while ($row = pwg_db_fetch_assoc($result))
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
2012-10-04 06:15:28 +02:00
|
|
|
$counter = intval(@$tag_counters[ $row['id'] ]);
|
2007-09-12 05:37:01 +02:00
|
|
|
if ( $counter )
|
|
|
|
{
|
|
|
|
$row['counter'] = $counter;
|
2014-06-02 09:55:46 +02:00
|
|
|
$row['name'] = trigger_change('render_tag_name', $row['name'], $row);
|
2012-10-04 06:15:28 +02:00
|
|
|
$tags[] = $row;
|
2007-09-12 05:37:01 +02:00
|
|
|
}
|
2006-04-03 00:26:19 +02:00
|
|
|
}
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-23 23:57:15 +01:00
|
|
|
* Returns all tags even associated to no image.
|
2006-04-03 00:26:19 +02:00
|
|
|
*
|
2013-11-23 23:57:15 +01:00
|
|
|
* @return array [id, name, url_name]
|
2006-04-03 00:26:19 +02:00
|
|
|
*/
|
|
|
|
function get_all_tags()
|
|
|
|
{
|
|
|
|
$query = '
|
2008-07-01 04:09:21 +02:00
|
|
|
SELECT *
|
2006-04-03 00:26:19 +02:00
|
|
|
FROM '.TAGS_TABLE.'
|
|
|
|
;';
|
|
|
|
$result = pwg_query($query);
|
|
|
|
$tags = array();
|
2009-11-20 15:17:04 +01:00
|
|
|
while ($row = pwg_db_fetch_assoc($result))
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
2014-06-02 09:55:46 +02:00
|
|
|
$row['name'] = trigger_change('render_tag_name', $row['name'], $row);
|
2012-10-04 06:15:28 +02:00
|
|
|
$tags[] = $row;
|
2006-04-03 00:26:19 +02:00
|
|
|
}
|
|
|
|
|
2008-07-01 04:09:21 +02:00
|
|
|
usort($tags, 'tag_alpha_compare');
|
2006-05-15 23:22:43 +02:00
|
|
|
|
2006-04-03 00:26:19 +02:00
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Giving a set of tags with a counter for each one, calculate the display
|
|
|
|
* level of each tag.
|
|
|
|
*
|
|
|
|
* The level of each tag depends on the average count of tags. This
|
2013-11-23 23:57:15 +01:00
|
|
|
* calculation method avoid having very different levels for tags having
|
2006-04-03 00:26:19 +02:00
|
|
|
* nearly the same count when set are small.
|
|
|
|
*
|
2013-11-23 23:57:15 +01:00
|
|
|
* @param array $tags at least [id, counter]
|
|
|
|
* @return array [..., level]
|
2006-04-03 00:26:19 +02:00
|
|
|
*/
|
|
|
|
function add_level_to_tags($tags)
|
|
|
|
{
|
|
|
|
global $conf;
|
|
|
|
|
|
|
|
if (count($tags) == 0)
|
|
|
|
{
|
|
|
|
return $tags;
|
|
|
|
}
|
2006-04-08 21:42:11 +02:00
|
|
|
|
2006-04-03 00:26:19 +02:00
|
|
|
$total_count = 0;
|
|
|
|
|
|
|
|
foreach ($tags as $tag)
|
|
|
|
{
|
|
|
|
$total_count+= $tag['counter'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// average count of available tags will determine the level of each tag
|
|
|
|
$tag_average_count = $total_count / count($tags);
|
|
|
|
|
|
|
|
// tag levels threshold calculation: a tag with an average rate must have
|
|
|
|
// the middle level.
|
|
|
|
for ($i = 1; $i < $conf['tags_levels']; $i++)
|
|
|
|
{
|
|
|
|
$threshold_of_level[$i] =
|
|
|
|
2 * $i * $tag_average_count / $conf['tags_levels'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// display sorted tags
|
2012-10-04 06:15:28 +02:00
|
|
|
foreach ($tags as &$tag)
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
2012-10-04 06:15:28 +02:00
|
|
|
$tag['level'] = 1;
|
2006-04-08 21:42:11 +02:00
|
|
|
|
2006-04-03 00:26:19 +02:00
|
|
|
// based on threshold, determine current tag level
|
|
|
|
for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
|
|
|
|
{
|
2012-10-04 06:15:28 +02:00
|
|
|
if ($tag['counter'] > $threshold_of_level[$i])
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
2012-10-04 06:15:28 +02:00
|
|
|
$tag['level'] = $i + 1;
|
2006-04-03 00:26:19 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-04 06:15:28 +02:00
|
|
|
unset($tag);
|
2006-04-03 00:26:19 +02:00
|
|
|
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-11-23 23:57:15 +01:00
|
|
|
* Return the list of image ids corresponding to given tags.
|
|
|
|
* AND & OR mode supported.
|
2006-04-03 00:26:19 +02:00
|
|
|
*
|
2013-11-23 23:57:15 +01:00
|
|
|
* @param int[] $tag_ids
|
2006-04-03 00:26:19 +02:00
|
|
|
* @param string mode
|
2013-11-23 23:57:15 +01:00
|
|
|
* @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
|
2006-04-03 00:26:19 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
2011-11-15 12:58:10 +01:00
|
|
|
function get_image_ids_for_tags($tag_ids, $mode='AND', $extra_images_where_sql='', $order_by='', $use_permissions=true)
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
2011-01-17 22:16:42 +01:00
|
|
|
global $conf;
|
|
|
|
if (empty($tag_ids))
|
2006-04-03 00:26:19 +02:00
|
|
|
{
|
2011-01-17 22:16:42 +01:00
|
|
|
return array();
|
|
|
|
}
|
2006-04-03 00:26:19 +02:00
|
|
|
|
2013-11-23 23:57:15 +01:00
|
|
|
$query = '
|
|
|
|
SELECT id
|
2011-11-15 12:58:10 +01:00
|
|
|
FROM '.IMAGES_TABLE.' i ';
|
|
|
|
|
|
|
|
if ($use_permissions)
|
|
|
|
{
|
|
|
|
$query.= '
|
|
|
|
INNER JOIN '.IMAGE_CATEGORY_TABLE.' ic ON id=ic.image_id';
|
|
|
|
}
|
|
|
|
|
|
|
|
$query.= '
|
2011-01-17 22:16:42 +01:00
|
|
|
INNER JOIN '.IMAGE_TAG_TABLE.' it ON id=it.image_id
|
2011-11-15 12:58:10 +01:00
|
|
|
WHERE tag_id IN ('.implode(',', $tag_ids).')';
|
|
|
|
|
|
|
|
if ($use_permissions)
|
|
|
|
{
|
|
|
|
$query.= get_sql_condition_FandF(
|
|
|
|
array(
|
|
|
|
'forbidden_categories' => 'category_id',
|
|
|
|
'visible_categories' => 'category_id',
|
|
|
|
'visible_images' => 'id'
|
2011-01-17 22:16:42 +01:00
|
|
|
),
|
|
|
|
"\n AND"
|
2011-11-15 12:58:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$query.= (empty($extra_images_where_sql) ? '' : " \nAND (".$extra_images_where_sql.')').'
|
2011-01-17 22:16:42 +01:00
|
|
|
GROUP BY id';
|
|
|
|
|
|
|
|
if ($mode=='AND' and count($tag_ids)>1)
|
|
|
|
{
|
|
|
|
$query .= '
|
|
|
|
HAVING COUNT(DISTINCT tag_id)='.count($tag_ids);
|
2006-04-03 00:26:19 +02:00
|
|
|
}
|
2011-01-17 22:16:42 +01:00
|
|
|
$query .= "\n".(empty($order_by) ? $conf['order_by'] : $order_by);
|
|
|
|
|
2014-02-11 22:47:44 +01:00
|
|
|
return query2array($query, null, 'id');
|
2006-04-03 00:26:19 +02:00
|
|
|
}
|
2006-08-15 04:06:06 +02:00
|
|
|
|
|
|
|
/**
|
2013-11-23 23:57:15 +01:00
|
|
|
* Return a list of tags corresponding to given items.
|
2006-08-15 04:06:06 +02:00
|
|
|
*
|
2013-11-23 23:57:15 +01:00
|
|
|
* @param int[] $items
|
|
|
|
* @param int $max_tags
|
|
|
|
* @param int[] $excluded_tag_ids
|
|
|
|
* @return array [id, name, counter, url_name]
|
2006-08-15 04:06:06 +02:00
|
|
|
*/
|
2013-11-23 23:57:15 +01:00
|
|
|
function get_common_tags($items, $max_tags, $excluded_tag_ids=array())
|
2006-08-15 04:06:06 +02:00
|
|
|
{
|
|
|
|
if (empty($items))
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
$query = '
|
2010-07-03 14:12:25 +02:00
|
|
|
SELECT t.*, count(*) AS counter
|
2006-08-15 04:06:06 +02:00
|
|
|
FROM '.IMAGE_TAG_TABLE.'
|
2008-07-01 04:09:21 +02:00
|
|
|
INNER JOIN '.TAGS_TABLE.' t ON tag_id = id
|
2006-08-15 04:06:06 +02:00
|
|
|
WHERE image_id IN ('.implode(',', $items).')';
|
|
|
|
if (!empty($excluded_tag_ids))
|
|
|
|
{
|
|
|
|
$query.='
|
|
|
|
AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
|
|
|
|
}
|
|
|
|
$query .='
|
2011-09-08 20:22:27 +02:00
|
|
|
GROUP BY t.id
|
|
|
|
ORDER BY ';
|
2006-08-15 04:06:06 +02:00
|
|
|
if ($max_tags>0)
|
2013-11-23 23:57:15 +01:00
|
|
|
{ // TODO : why ORDER field is in the if ?
|
2011-09-08 20:22:27 +02:00
|
|
|
$query .= 'counter DESC
|
2009-11-21 20:52:50 +01:00
|
|
|
LIMIT '.$max_tags;
|
2006-08-15 04:06:06 +02:00
|
|
|
}
|
2011-09-08 20:22:27 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$query .= 'NULL';
|
|
|
|
}
|
2006-08-15 04:06:06 +02:00
|
|
|
|
|
|
|
$result = pwg_query($query);
|
|
|
|
$tags = array();
|
2009-11-20 15:17:04 +01:00
|
|
|
while($row = pwg_db_fetch_assoc($result))
|
2006-08-15 04:06:06 +02:00
|
|
|
{
|
2014-06-02 09:55:46 +02:00
|
|
|
$row['name'] = trigger_change('render_tag_name', $row['name'], $row);
|
2012-10-04 06:15:28 +02:00
|
|
|
$tags[] = $row;
|
2006-08-15 04:06:06 +02:00
|
|
|
}
|
2008-07-01 04:09:21 +02:00
|
|
|
usort($tags, 'tag_alpha_compare');
|
2006-08-15 04:06:06 +02:00
|
|
|
return $tags;
|
|
|
|
}
|
2007-02-23 14:18:34 +01:00
|
|
|
|
|
|
|
/**
|
2013-11-23 23:57:15 +01:00
|
|
|
* Return a list of tags corresponding to any of ids, url_names or names.
|
2007-02-23 14:18:34 +01:00
|
|
|
*
|
2013-11-23 23:57:15 +01:00
|
|
|
* @param int[] $ids
|
|
|
|
* @param string[] $url_names
|
|
|
|
* @param string[] $names
|
|
|
|
* @return array [id, name, url_name]
|
2007-02-23 14:18:34 +01:00
|
|
|
*/
|
2013-11-23 23:57:15 +01:00
|
|
|
function find_tags($ids=array(), $url_names=array(), $names=array() )
|
2007-02-23 14:18:34 +01:00
|
|
|
{
|
|
|
|
$where_clauses = array();
|
2013-11-23 23:57:15 +01:00
|
|
|
if (!empty($ids))
|
2007-02-23 14:18:34 +01:00
|
|
|
{
|
|
|
|
$where_clauses[] = 'id IN ('.implode(',', $ids).')';
|
|
|
|
}
|
2013-11-23 23:57:15 +01:00
|
|
|
if (!empty($url_names))
|
2007-02-23 14:18:34 +01:00
|
|
|
{
|
|
|
|
$where_clauses[] =
|
2013-11-23 23:57:15 +01:00
|
|
|
'url_name IN (\''. implode('\', \'', $url_names) .'\')';
|
2007-02-23 14:18:34 +01:00
|
|
|
}
|
2013-11-23 23:57:15 +01:00
|
|
|
if (!empty($names))
|
2007-02-23 14:18:34 +01:00
|
|
|
{
|
|
|
|
$where_clauses[] =
|
2013-11-23 23:57:15 +01:00
|
|
|
'name IN (\''. implode('\', \'', $names) .'\')';
|
2007-02-23 14:18:34 +01:00
|
|
|
}
|
|
|
|
if (empty($where_clauses))
|
|
|
|
{
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$query = '
|
2008-07-01 04:09:21 +02:00
|
|
|
SELECT *
|
2007-02-23 14:18:34 +01:00
|
|
|
FROM '.TAGS_TABLE.'
|
|
|
|
WHERE '. implode( '
|
|
|
|
OR ', $where_clauses);
|
|
|
|
|
2014-02-11 22:47:44 +01:00
|
|
|
return query2array($query);
|
2007-02-23 14:18:34 +01:00
|
|
|
}
|
2013-11-23 23:57:15 +01:00
|
|
|
|
2006-04-03 00:26:19 +02:00
|
|
|
?>
|