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

Last change on this file since 1930 was 1900, checked in by rub, 18 years ago

Apply property svn:eol-style Value: LF

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