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

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

Bug fixed: as rvelices notified me by email, my header replacement script was
bugged (r2297 was repeating new and old header).

By the way, I've also removed the replacement keywords. We were using them
because it was a common usage with CVS but it is advised not to use them with
Subversion. Personnaly, it is a problem when I search differences between 2
Piwigo installations outside Subversion.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 7.6 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
25/**
26 * Tags available. Each return tag is represented as an array with its id,
27 * its name, its weight (count), its url name. Tags are not sorted.
28 *
29 * The returned list can be a subset of all existing tags due to
30 * permissions, only if a list of forbidden categories is provided
31 *
32 * @param array forbidden categories
33 * @return array
34 */
35function get_available_tags()
36{
37  // we can find top fatter tags among reachable images
38  $query = '
39SELECT tag_id, COUNT(DISTINCT(it.image_id)) counter
40  FROM '.IMAGE_CATEGORY_TABLE.' ic
41    INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF
42    (
43      array
44        (
45          'forbidden_categories' => 'category_id',
46          'visible_categories' => 'category_id',
47          'visible_images' => 'ic.image_id'
48        ),
49      '
50  WHERE'
51    ).'
52  GROUP BY tag_id';
53  $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter');
54
55  if ( empty($tag_counters) )
56  {
57    return array();
58  }
59
60  $query = '
61SELECT id, name, url_name
62  FROM '.TAGS_TABLE;
63  $result = pwg_query($query);
64  $tags = array();
65  while ($row = mysql_fetch_assoc($result))
66  {
67    $counter = @$tag_counters[ $row['id'] ];
68    if ( $counter )
69    {
70      $row['counter'] = $counter;
71      array_push($tags, $row);
72    }
73  }
74  return $tags;
75}
76
77/**
78 * All tags, even tags associated to no image.
79 *
80 * @return array
81 */
82function get_all_tags()
83{
84  $query = '
85SELECT id,
86       name,
87       url_name
88  FROM '.TAGS_TABLE.'
89;';
90  $result = pwg_query($query);
91  $tags = array();
92  while ($row = mysql_fetch_assoc($result))
93  {
94    array_push($tags, $row);
95  }
96
97  usort($tags, 'name_compare');
98
99  return $tags;
100}
101
102/**
103 * Giving a set of tags with a counter for each one, calculate the display
104 * level of each tag.
105 *
106 * The level of each tag depends on the average count of tags. This
107 * calcylation method avoid having very different levels for tags having
108 * nearly the same count when set are small.
109 *
110 * @param array tags
111 * @return array
112 */
113function add_level_to_tags($tags)
114{
115  global $conf;
116
117  if (count($tags) == 0)
118  {
119    return $tags;
120  }
121
122  $total_count = 0;
123
124  foreach ($tags as $tag)
125  {
126    $total_count+= $tag['counter'];
127  }
128
129  // average count of available tags will determine the level of each tag
130  $tag_average_count = $total_count / count($tags);
131
132  // tag levels threshold calculation: a tag with an average rate must have
133  // the middle level.
134  for ($i = 1; $i < $conf['tags_levels']; $i++)
135  {
136    $threshold_of_level[$i] =
137      2 * $i * $tag_average_count / $conf['tags_levels'];
138  }
139
140  // display sorted tags
141  foreach (array_keys($tags) as $k)
142  {
143    $tags[$k]['level'] = 1;
144
145    // based on threshold, determine current tag level
146    for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
147    {
148      if ($tags[$k]['counter'] > $threshold_of_level[$i])
149      {
150        $tags[$k]['level'] = $i + 1;
151        break;
152      }
153    }
154  }
155
156  return $tags;
157}
158
159/**
160 * return the list of image ids corresponding to given tags. AND & OR mode
161 * supported.
162 *
163 * @param array tag ids
164 * @param string mode
165 * @return array
166 */
167function get_image_ids_for_tags($tag_ids, $mode = 'AND')
168{
169  switch ($mode)
170  {
171    case 'AND':
172    {
173      // strategy is to list images associated to each tag
174      $tag_images = array();
175
176      foreach ($tag_ids as $tag_id)
177      {
178        $query = '
179SELECT image_id
180  FROM '.IMAGE_TAG_TABLE.'
181  WHERE tag_id = '.$tag_id.'
182;';
183        $tag_images[$tag_id] = array_from_query($query, 'image_id');
184      }
185
186      // then we calculate the intersection, the images that are associated to
187      // every tags
188      $items = array_shift($tag_images);
189      foreach ($tag_images as $images)
190      {
191        $items = array_intersect($items, $images);
192      }
193
194      return array_unique($items);
195      break;
196    }
197    case 'OR':
198    {
199      $query = '
200SELECT DISTINCT image_id
201  FROM '.IMAGE_TAG_TABLE.'
202  WHERE tag_id IN ('.implode(',', $tag_ids).')
203;';
204      return array_from_query($query, 'image_id');
205      break;
206    }
207    default:
208    {
209      die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
210    }
211  }
212}
213
214/**
215 * return a list of tags corresponding to given items.
216 *
217 * @param array items
218 * @param array max_tags
219 * @param array excluded_tag_ids
220 * @return array
221 */
222function get_common_tags($items, $max_tags, $excluded_tag_ids=null)
223{
224  if (empty($items))
225  {
226    return array();
227  }
228  $query = '
229SELECT id, name, url_name, count(*) counter
230  FROM '.IMAGE_TAG_TABLE.'
231    INNER JOIN '.TAGS_TABLE.' ON tag_id = id
232  WHERE image_id IN ('.implode(',', $items).')';
233  if (!empty($excluded_tag_ids))
234  {
235    $query.='
236    AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
237  }
238  $query .='
239  GROUP BY tag_id';
240  if ($max_tags>0)
241  {
242    $query .= '
243  ORDER BY counter DESC
244  LIMIT 0,'.$max_tags;
245  }
246
247  $result = pwg_query($query);
248  $tags = array();
249  while($row = mysql_fetch_assoc($result))
250  {
251    array_push($tags, $row);
252  }
253  usort($tags, 'name_compare');
254  return $tags;
255}
256
257/**
258 * return a list of tags corresponding to any of ids, url_names, names
259 *
260 * @param array ids
261 * @param array url_names
262 * @param array names
263 * @return array
264 */
265function find_tags($ids, $url_names=array(), $names=array() )
266{
267  $where_clauses = array();
268  if ( !empty($ids) )
269  {
270    $where_clauses[] = 'id IN ('.implode(',', $ids).')';
271  }
272  if ( !empty($url_names) )
273  {
274    $where_clauses[] =
275      'url_name IN ('.
276      implode(
277        ',',
278        array_map(
279          create_function('$s', 'return "\'".$s."\'";'),
280          $url_names
281          )
282        )
283      .')';
284  }
285  if ( !empty($names) )
286  {
287    $where_clauses[] =
288      'name IN ('.
289      implode(
290        ',',
291        array_map(
292          create_function('$s', 'return "\'".$s."\'";'),
293          $names
294          )
295        )
296      .')';
297  }
298  if (empty($where_clauses))
299  {
300    return array();
301  }
302
303  $query = '
304SELECT id, url_name, name
305  FROM '.TAGS_TABLE.'
306  WHERE '. implode( '
307    OR ', $where_clauses);
308
309  $result = pwg_query($query);
310  $tags = array();
311  while ($row = mysql_fetch_assoc($result))
312  {
313    array_push($tags, $row);
314  }
315  return $tags;
316}
317?>
Note: See TracBrowser for help on using the repository browser.