source: tags/release-1_7_3/include/functions_tag.inc.php @ 16878

Last change on this file since 16878 was 2410, checked in by rvelices, 16 years ago
  • tags improvement : pass to templates all fields in table #tags (handy for plugins such as type tags)
  • 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 2410 2008-07-01 02:10:13Z rvelices $
8// | last update   : $Date: 2008-07-01 02:10:13 +0000 (Tue, 01 Jul 2008) $
9// | last modifier : $Author: rvelices $
10// | revision      : $Revision: 2410 $
11// | revision      : $Revision: 2410 $
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 t.*, count(*) counter
44  FROM '.IMAGE_TAG_TABLE.'
45    INNER JOIN '.TAGS_TABLE.' t 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      '
57  WHERE'
58    );
59
60  if (!empty($where_tag_img))
61  {
62    // first we need all reachable image ids
63    $images_query = '
64SELECT DISTINCT image_id
65  FROM '.IMAGE_CATEGORY_TABLE.'
66  '.$where_tag_img.'
67;';
68    $image_ids = array_from_query($images_query, 'image_id');
69    if ( empty($image_ids) )
70    {
71      return array();
72    }
73    $tags_query.= '
74  WHERE image_id IN ('.
75      wordwrap(
76        implode(', ', $image_ids),
77        80,
78        "\n"
79        ).')';
80  }
81
82  $tags_query.= '
83  GROUP BY tag_id
84;';
85
86  $result = pwg_query($tags_query);
87  $tags = array();
88  while ($row = mysql_fetch_assoc($result))
89  {
90    array_push($tags, $row);
91  }
92
93  return $tags;
94}
95
96/**
97 * All tags, even tags associated to no image.
98 *
99 * @return array
100 */
101function get_all_tags()
102{
103  $query = '
104SELECT *
105  FROM '.TAGS_TABLE.'
106;';
107  $result = pwg_query($query);
108  $tags = array();
109  while ($row = mysql_fetch_assoc($result))
110  {
111    array_push($tags, $row);
112  }
113
114  usort($tags, 'name_compare');
115
116  return $tags;
117}
118
119/**
120 * Giving a set of tags with a counter for each one, calculate the display
121 * level of each tag.
122 *
123 * The level of each tag depends on the average count of tags. This
124 * calcylation method avoid having very different levels for tags having
125 * nearly the same count when set are small.
126 *
127 * @param array tags
128 * @return array
129 */
130function add_level_to_tags($tags)
131{
132  global $conf;
133
134  if (count($tags) == 0)
135  {
136    return $tags;
137  }
138
139  $total_count = 0;
140
141  foreach ($tags as $tag)
142  {
143    $total_count+= $tag['counter'];
144  }
145
146  // average count of available tags will determine the level of each tag
147  $tag_average_count = $total_count / count($tags);
148
149  // tag levels threshold calculation: a tag with an average rate must have
150  // the middle level.
151  for ($i = 1; $i < $conf['tags_levels']; $i++)
152  {
153    $threshold_of_level[$i] =
154      2 * $i * $tag_average_count / $conf['tags_levels'];
155  }
156
157  // display sorted tags
158  foreach (array_keys($tags) as $k)
159  {
160    $tags[$k]['level'] = 1;
161
162    // based on threshold, determine current tag level
163    for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
164    {
165      if ($tags[$k]['counter'] > $threshold_of_level[$i])
166      {
167        $tags[$k]['level'] = $i + 1;
168        break;
169      }
170    }
171  }
172
173  return $tags;
174}
175
176/**
177 * return the list of image ids corresponding to given tags. AND & OR mode
178 * supported.
179 *
180 * @param array tag ids
181 * @param string mode
182 * @return array
183 */
184function get_image_ids_for_tags($tag_ids, $mode = 'AND')
185{
186  switch ($mode)
187  {
188    case 'AND':
189    {
190      // strategy is to list images associated to each tag
191      $tag_images = array();
192
193      foreach ($tag_ids as $tag_id)
194      {
195        $query = '
196SELECT image_id
197  FROM '.IMAGE_TAG_TABLE.'
198  WHERE tag_id = '.$tag_id.'
199;';
200        $tag_images[$tag_id] = array_from_query($query, 'image_id');
201      }
202
203      // then we calculate the intersection, the images that are associated to
204      // every tags
205      $items = array_shift($tag_images);
206      foreach ($tag_images as $images)
207      {
208        $items = array_intersect($items, $images);
209      }
210
211      return array_unique($items);
212      break;
213    }
214    case 'OR':
215    {
216      $query = '
217SELECT DISTINCT image_id
218  FROM '.IMAGE_TAG_TABLE.'
219  WHERE tag_id IN ('.implode(',', $tag_ids).')
220;';
221      return array_from_query($query, 'image_id');
222      break;
223    }
224    default:
225    {
226      die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
227    }
228  }
229}
230
231/**
232 * return a list of tags corresponding to given items.
233 *
234 * @param array items
235 * @param array max_tags
236 * @param array excluded_tag_ids
237 * @return array
238 */
239function get_common_tags($items, $max_tags, $excluded_tag_ids=null)
240{
241  if (empty($items))
242  {
243    return array();
244  }
245  $query = '
246SELECT t.*, count(*) counter
247  FROM '.IMAGE_TAG_TABLE.'
248    INNER JOIN '.TAGS_TABLE.' t ON tag_id = id
249  WHERE image_id IN ('.implode(',', $items).')';
250  if (!empty($excluded_tag_ids))
251  {
252    $query.='
253    AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
254  }
255  $query .='
256  GROUP BY tag_id';
257  if ($max_tags>0)
258  {
259    $query .= '
260  ORDER BY counter DESC
261  LIMIT 0,'.$max_tags;
262  }
263  else
264  {
265    $query .= '
266  ORDER BY NULL';
267  }
268
269  $result = pwg_query($query);
270  $tags = array();
271  while($row = mysql_fetch_assoc($result))
272  {
273    array_push($tags, $row);
274  }
275  usort($tags, 'name_compare');
276  return $tags;
277}
278
279/**
280 * return a list of tags corresponding to any of ids, url_names, names
281 *
282 * @param array ids
283 * @param array url_names
284 * @param array names
285 * @return array
286 */
287function find_tags($ids, $url_names=array(), $names=array() )
288{
289  $where_clauses = array();
290  if ( !empty($ids) )
291  {
292    $where_clauses[] = 'id IN ('.implode(',', $ids).')';
293  }
294  if ( !empty($url_names) )
295  {
296    $where_clauses[] =
297      'url_name IN ('.
298      implode(
299        ',',
300        array_map(
301          create_function('$s', 'return "\'".$s."\'";'),
302          $url_names
303          )
304        )
305      .')';
306  }
307  if ( !empty($names) )
308  {
309    $where_clauses[] =
310      'name IN ('.
311      implode(
312        ',',
313        array_map(
314          create_function('$s', 'return "\'".$s."\'";'),
315          $names
316          )
317        )
318      .')';
319  }
320  if (empty($where_clauses))
321  {
322    return array();
323  }
324
325  $query = '
326SELECT id, url_name, name
327  FROM '.TAGS_TABLE.'
328  WHERE '. implode( '
329    OR ', $where_clauses);
330
331  $result = pwg_query($query);
332  $tags = array();
333  while ($row = mysql_fetch_assoc($result))
334  {
335    array_push($tags, $row);
336  }
337  return $tags;
338}
339?>
Note: See TracBrowser for help on using the repository browser.