source: trunk/include/functions_tag.inc.php @ 2297

Last change on this file since 2297 was 2297, checked in by plg, 16 years ago

Modification: new header on PHP files, PhpWebGallery renamed Piwigo.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008      Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23// +-----------------------------------------------------------------------+
24// | PhpWebGallery - a PHP based picture gallery                           |
25// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
26// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
27// +-----------------------------------------------------------------------+
28// | file          : $Id: functions_tag.inc.php 2297 2008-04-04 22:57:23Z plg $
29// | last update   : $Date: 2008-04-04 22:57:23 +0000 (Fri, 04 Apr 2008) $
30// | last modifier : $Author: plg $
31// | revision      : $Revision: 2297 $
32// +-----------------------------------------------------------------------+
33// | This program is free software; you can redistribute it and/or modify  |
34// | it under the terms of the GNU General Public License as published by  |
35// | the Free Software Foundation                                          |
36// |                                                                       |
37// | This program is distributed in the hope that it will be useful, but   |
38// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
39// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
40// | General Public License for more details.                              |
41// |                                                                       |
42// | You should have received a copy of the GNU General Public License     |
43// | along with this program; if not, write to the Free Software           |
44// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
45// | USA.                                                                  |
46// +-----------------------------------------------------------------------+
47
48
49/**
50 * Tags available. Each return tag is represented as an array with its id,
51 * its name, its weight (count), its url name. Tags are not sorted.
52 *
53 * The returned list can be a subset of all existing tags due to
54 * permissions, only if a list of forbidden categories is provided
55 *
56 * @param array forbidden categories
57 * @return array
58 */
59function get_available_tags()
60{
61  // we can find top fatter tags among reachable images
62  $query = '
63SELECT tag_id, COUNT(DISTINCT(it.image_id)) counter
64  FROM '.IMAGE_CATEGORY_TABLE.' ic
65    INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF
66    (
67      array
68        (
69          'forbidden_categories' => 'category_id',
70          'visible_categories' => 'category_id',
71          'visible_images' => 'ic.image_id'
72        ),
73      '
74  WHERE'
75    ).'
76  GROUP BY tag_id';
77  $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
78
79  if ( empty($tag_counters) )
80  {
81    return array();
82  }
83
84  $query = '
85SELECT id, name, url_name
86  FROM '.TAGS_TABLE;
87  $result = pwg_query($query);
88  $tags = array();
89  while ($row = mysql_fetch_assoc($result))
90  {
91    $counter = @$tag_counters[ $row['id'] ];
92    if ( $counter )
93    {
94      $row['counter'] = $counter;
95      array_push($tags, $row);
96    }
97  }
98  return $tags;
99}
100
101/**
102 * All tags, even tags associated to no image.
103 *
104 * @return array
105 */
106function get_all_tags()
107{
108  $query = '
109SELECT id,
110       name,
111       url_name
112  FROM '.TAGS_TABLE.'
113;';
114  $result = pwg_query($query);
115  $tags = array();
116  while ($row = mysql_fetch_assoc($result))
117  {
118    array_push($tags, $row);
119  }
120
121  usort($tags, 'name_compare');
122
123  return $tags;
124}
125
126/**
127 * Giving a set of tags with a counter for each one, calculate the display
128 * level of each tag.
129 *
130 * The level of each tag depends on the average count of tags. This
131 * calcylation method avoid having very different levels for tags having
132 * nearly the same count when set are small.
133 *
134 * @param array tags
135 * @return array
136 */
137function add_level_to_tags($tags)
138{
139  global $conf;
140
141  if (count($tags) == 0)
142  {
143    return $tags;
144  }
145
146  $total_count = 0;
147
148  foreach ($tags as $tag)
149  {
150    $total_count+= $tag['counter'];
151  }
152
153  // average count of available tags will determine the level of each tag
154  $tag_average_count = $total_count / count($tags);
155
156  // tag levels threshold calculation: a tag with an average rate must have
157  // the middle level.
158  for ($i = 1; $i < $conf['tags_levels']; $i++)
159  {
160    $threshold_of_level[$i] =
161      2 * $i * $tag_average_count / $conf['tags_levels'];
162  }
163
164  // display sorted tags
165  foreach (array_keys($tags) as $k)
166  {
167    $tags[$k]['level'] = 1;
168
169    // based on threshold, determine current tag level
170    for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
171    {
172      if ($tags[$k]['counter'] > $threshold_of_level[$i])
173      {
174        $tags[$k]['level'] = $i + 1;
175        break;
176      }
177    }
178  }
179
180  return $tags;
181}
182
183/**
184 * return the list of image ids corresponding to given tags. AND & OR mode
185 * supported.
186 *
187 * @param array tag ids
188 * @param string mode
189 * @return array
190 */
191function get_image_ids_for_tags($tag_ids, $mode = 'AND')
192{
193  switch ($mode)
194  {
195    case 'AND':
196    {
197      // strategy is to list images associated to each tag
198      $tag_images = array();
199
200      foreach ($tag_ids as $tag_id)
201      {
202        $query = '
203SELECT image_id
204  FROM '.IMAGE_TAG_TABLE.'
205  WHERE tag_id = '.$tag_id.'
206;';
207        $tag_images[$tag_id] = array_from_query($query, 'image_id');
208      }
209
210      // then we calculate the intersection, the images that are associated to
211      // every tags
212      $items = array_shift($tag_images);
213      foreach ($tag_images as $images)
214      {
215        $items = array_intersect($items, $images);
216      }
217
218      return array_unique($items);
219      break;
220    }
221    case 'OR':
222    {
223      $query = '
224SELECT DISTINCT image_id
225  FROM '.IMAGE_TAG_TABLE.'
226  WHERE tag_id IN ('.implode(',', $tag_ids).')
227;';
228      return array_from_query($query, 'image_id');
229      break;
230    }
231    default:
232    {
233      die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
234    }
235  }
236}
237
238/**
239 * return a list of tags corresponding to given items.
240 *
241 * @param array items
242 * @param array max_tags
243 * @param array excluded_tag_ids
244 * @return array
245 */
246function get_common_tags($items, $max_tags, $excluded_tag_ids=null)
247{
248  if (empty($items))
249  {
250    return array();
251  }
252  $query = '
253SELECT id, name, url_name, count(*) counter
254  FROM '.IMAGE_TAG_TABLE.'
255    INNER JOIN '.TAGS_TABLE.' ON tag_id = id
256  WHERE image_id IN ('.implode(',', $items).')';
257  if (!empty($excluded_tag_ids))
258  {
259    $query.='
260    AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
261  }
262  $query .='
263  GROUP BY tag_id';
264  if ($max_tags>0)
265  {
266    $query .= '
267  ORDER BY counter DESC
268  LIMIT 0,'.$max_tags;
269  }
270
271  $result = pwg_query($query);
272  $tags = array();
273  while($row = mysql_fetch_assoc($result))
274  {
275    array_push($tags, $row);
276  }
277  usort($tags, 'name_compare');
278  return $tags;
279}
280
281/**
282 * return a list of tags corresponding to any of ids, url_names, names
283 *
284 * @param array ids
285 * @param array url_names
286 * @param array names
287 * @return array
288 */
289function find_tags($ids, $url_names=array(), $names=array() )
290{
291  $where_clauses = array();
292  if ( !empty($ids) )
293  {
294    $where_clauses[] = 'id IN ('.implode(',', $ids).')';
295  }
296  if ( !empty($url_names) )
297  {
298    $where_clauses[] =
299      'url_name IN ('.
300      implode(
301        ',',
302        array_map(
303          create_function('$s', 'return "\'".$s."\'";'),
304          $url_names
305          )
306        )
307      .')';
308  }
309  if ( !empty($names) )
310  {
311    $where_clauses[] =
312      'name IN ('.
313      implode(
314        ',',
315        array_map(
316          create_function('$s', 'return "\'".$s."\'";'),
317          $names
318          )
319        )
320      .')';
321  }
322  if (empty($where_clauses))
323  {
324    return array();
325  }
326
327  $query = '
328SELECT id, url_name, name
329  FROM '.TAGS_TABLE.'
330  WHERE '. implode( '
331    OR ', $where_clauses);
332
333  $result = pwg_query($query);
334  $tags = array();
335  while ($row = mysql_fetch_assoc($result))
336  {
337    array_push($tags, $row);
338  }
339  return $tags;
340}
341?>
Note: See TracBrowser for help on using the repository browser.