source: tags/build-A01/include/functions_tag.inc.php @ 12409

Last change on this file since 12409 was 1678, checked in by rvelices, 17 years ago

Very small corrections:

  • syntax error (action.php)
  • language (call to l10n_dec and English language)
  • replace some fetch_array with fetch_assoc (less memory used)
  • removed one unnecessary assign_block_vars
  • removed meta name="robots" (conflict with notification.php)
  • Property svn:eol-style set to native
File size: 6.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-2006 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2006-03-16 23:58:16 +0100 (jeu, 16 mar 2006) $
10// | last modifier : $Author: rub $
11// | revision      : $Revision: 1085 $
12// | revision      : $Revision: 1085 $
13// +-----------------------------------------------------------------------+
14// | This program is free software; you can redistribute it and/or modify  |
15// | it under the terms of the GNU General Public License as published by  |
16// | the Free Software Foundation                                          |
17// |                                                                       |
18// | This program is distributed in the hope that it will be useful, but   |
19// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
20// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
21// | General Public License for more details.                              |
22// |                                                                       |
23// | You should have received a copy of the GNU General Public License     |
24// | along with this program; if not, write to the Free Software           |
25// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
26// | USA.                                                                  |
27// +-----------------------------------------------------------------------+
28
29
30/**
31 * Tags available. Each return tag is represented as an array with its id,
32 * its name, its weight (count), its url name. Tags are not sorted.
33 *
34 * The returned list can be a subset of all existing tags due to
35 * permissions, only if a list of forbidden categories is provided
36 *
37 * @param array forbidden categories
38 * @return array
39 */
40function get_available_tags()
41{
42  // we can find top fatter tags among reachable images
43  $tags_query = '
44SELECT tag_id, name, url_name, count(*) counter
45  FROM '.IMAGE_TAG_TABLE.'
46    INNER JOIN '.TAGS_TABLE.' ON tag_id = id';
47
48  $where_tag_img =
49    get_sql_condition_FandF
50    (
51      array
52        (
53          'forbidden_categories' => 'category_id',
54          'visible_categories' => 'category_id',
55          'visible_images' => 'image_id'
56        ),
57      'WHERE'
58    );
59
60  if (!is_null($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 id AS tag_id,
105       name,
106       url_name
107  FROM '.TAGS_TABLE.'
108;';
109  $result = pwg_query($query);
110  $tags = array();
111  while ($row = mysql_fetch_assoc($result))
112  {
113    array_push($tags, $row);
114  }
115
116  usort($tags, 'name_compare');
117
118  return $tags;
119}
120
121/**
122 * Giving a set of tags with a counter for each one, calculate the display
123 * level of each tag.
124 *
125 * The level of each tag depends on the average count of tags. This
126 * calcylation method avoid having very different levels for tags having
127 * nearly the same count when set are small.
128 *
129 * @param array tags
130 * @return array
131 */
132function add_level_to_tags($tags)
133{
134  global $conf;
135
136  if (count($tags) == 0)
137  {
138    return $tags;
139  }
140
141  $total_count = 0;
142
143  foreach ($tags as $tag)
144  {
145    $total_count+= $tag['counter'];
146  }
147
148  // average count of available tags will determine the level of each tag
149  $tag_average_count = $total_count / count($tags);
150
151  // tag levels threshold calculation: a tag with an average rate must have
152  // the middle level.
153  for ($i = 1; $i < $conf['tags_levels']; $i++)
154  {
155    $threshold_of_level[$i] =
156      2 * $i * $tag_average_count / $conf['tags_levels'];
157  }
158
159  // display sorted tags
160  foreach (array_keys($tags) as $k)
161  {
162    $tags[$k]['level'] = 1;
163
164    // based on threshold, determine current tag level
165    for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
166    {
167      if ($tags[$k]['counter'] > $threshold_of_level[$i])
168      {
169        $tags[$k]['level'] = $i + 1;
170        break;
171      }
172    }
173  }
174
175  return $tags;
176}
177
178/**
179 * return the list of image ids corresponding to given tags. AND & OR mode
180 * supported.
181 *
182 * @param array tag ids
183 * @param string mode
184 * @return array
185 */
186function get_image_ids_for_tags($tag_ids, $mode = 'AND')
187{
188  switch ($mode)
189  {
190    case 'AND':
191    {
192      // strategy is to list images associated to each tag
193      $tag_images = array();
194
195      foreach ($tag_ids as $tag_id)
196      {
197        $query = '
198SELECT image_id
199  FROM '.IMAGE_TAG_TABLE.'
200  WHERE tag_id = '.$tag_id.'
201;';
202        $tag_images[$tag_id] = array_from_query($query, 'image_id');
203      }
204
205      // then we calculate the intersection, the images that are associated to
206      // every tags
207      $items = array_shift($tag_images);
208      foreach ($tag_images as $images)
209      {
210        $items = array_intersect($items, $images);
211      }
212
213      return array_unique($items);
214      break;
215    }
216    case 'OR':
217    {
218      $query = '
219SELECT DISTINCT image_id
220  FROM '.IMAGE_TAG_TABLE.'
221  WHERE tag_id IN ('.implode(',', $tag_ids).')
222;';
223      return array_from_query($query, 'image_id');
224      break;
225    }
226    default:
227    {
228      die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
229    }
230  }
231}
232
233/**
234 * return a list of tags corresponding to given items.
235 *
236 * @param array items
237 * @param array max_tags
238 * @param array excluded_tag_ids
239 * @return array
240 */
241function get_common_tags($items, $max_tags, $excluded_tag_ids=null)
242{
243  if (empty($items))
244  {
245    return array();
246  }
247  $query = '
248SELECT tag_id, name, url_name, count(*) counter
249  FROM '.IMAGE_TAG_TABLE.'
250    INNER JOIN '.TAGS_TABLE.' ON tag_id = id
251  WHERE image_id IN ('.implode(',', $items).')';
252  if (!empty($excluded_tag_ids))
253  {
254    $query.='
255    AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')';
256  }
257  $query .='
258  GROUP BY tag_id
259  ORDER BY counter DESC';
260  if ($max_tags>0)
261  {
262    $query .= '
263  LIMIT 0,'.$max_tags;
264  }
265
266  $result = pwg_query($query);
267  $tags = array();
268  while($row = mysql_fetch_assoc($result))
269  {
270    array_push($tags, $row);
271  }
272  usort($tags, 'name_compare');
273  return $tags;
274}
275?>
Note: See TracBrowser for help on using the repository browser.