source: branches/1.6/include/functions_tag.inc.php @ 27601

Last change on this file since 27601 was 1309, checked in by plg, 18 years ago

bug 367 fixed: tags are sorted in logic order (case insensitive)

File size: 5.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($forbidden_categories = null)
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  if (!is_null($forbidden_categories))
49  {
50    // first we need all reachable image ids
51    $images_query = '
52SELECT DISTINCT image_id
53  FROM '.IMAGE_CATEGORY_TABLE.'
54  WHERE category_id NOT IN ('.implode(',', $forbidden_categories).')
55;';
56    $image_ids = array_from_query($images_query, 'image_id');
57    if ( empty($image_ids) )
58    {
59      return array();
60    }
61    $tags_query.= '
62  WHERE image_id IN ('.
63      wordwrap(
64        implode(', ', $image_ids),
65        80,
66        "\n"
67        ).')';
68  }
69
70  $tags_query.= '
71  GROUP BY tag_id
72;';
73
74  $result = pwg_query($tags_query);
75
76  $tags = array();
77
78  while ($row = mysql_fetch_array($result))
79  {
80    array_push($tags, $row);
81  }
82
83  return $tags;
84}
85
86/**
87 * All tags, even tags associated to no image.
88 *
89 * @return array
90 */
91function get_all_tags()
92{
93  $query = '
94SELECT id AS tag_id,
95       name,
96       url_name
97  FROM '.TAGS_TABLE.'
98;';
99  $result = pwg_query($query);
100
101  $tags = array();
102
103  while ($row = mysql_fetch_array($result))
104  {
105    array_push($tags, $row);
106  }
107
108  usort($tags, 'name_compare');
109
110  return $tags;
111}
112
113/**
114 * Giving a set of tags with a counter for each one, calculate the display
115 * level of each tag.
116 *
117 * The level of each tag depends on the average count of tags. This
118 * calcylation method avoid having very different levels for tags having
119 * nearly the same count when set are small.
120 *
121 * @param array tags
122 * @return array
123 */
124function add_level_to_tags($tags)
125{
126  global $conf;
127
128  if (count($tags) == 0)
129  {
130    return $tags;
131  }
132
133  $total_count = 0;
134
135  foreach ($tags as $tag)
136  {
137    $total_count+= $tag['counter'];
138  }
139
140  // average count of available tags will determine the level of each tag
141  $tag_average_count = $total_count / count($tags);
142
143  // tag levels threshold calculation: a tag with an average rate must have
144  // the middle level.
145  for ($i = 1; $i < $conf['tags_levels']; $i++)
146  {
147    $threshold_of_level[$i] =
148      2 * $i * $tag_average_count / $conf['tags_levels'];
149  }
150
151  // display sorted tags
152  foreach (array_keys($tags) as $k)
153  {
154    $tags[$k]['level'] = 1;
155
156    // based on threshold, determine current tag level
157    for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
158    {
159      if ($tags[$k]['counter'] > $threshold_of_level[$i])
160      {
161        $tags[$k]['level'] = $i + 1;
162        break;
163      }
164    }
165  }
166
167  return $tags;
168}
169
170/**
171 * return the list of image ids corresponding to given tags. AND & OR mode
172 * supported.
173 *
174 * @param array tag ids
175 * @param string mode
176 * @return array
177 */
178function get_image_ids_for_tags($tag_ids, $mode = 'AND')
179{
180  switch ($mode)
181  {
182    case 'AND':
183    {
184      // strategy is to list images associated to each tag
185      $tag_images = array();
186
187      foreach ($tag_ids as $tag_id)
188      {
189        $query = '
190SELECT image_id
191  FROM '.IMAGE_TAG_TABLE.'
192  WHERE tag_id = '.$tag_id.'
193;';
194        $tag_images[$tag_id] = array_from_query($query, 'image_id');
195      }
196
197      // then we calculate the intersection, the images that are associated to
198      // every tags
199      $items = array_shift($tag_images);
200      foreach ($tag_images as $images)
201      {
202        $items = array_intersect($items, $images);
203      }
204
205      return array_unique($items);
206      break;
207    }
208    case 'OR':
209    {
210      $query = '
211SELECT DISTINCT image_id
212  FROM '.IMAGE_TAG_TABLE.'
213  WHERE tag_id IN ('.implode(',', $tag_ids).')
214;';
215      return array_from_query($query, 'image_id');
216      break;
217    }
218    default:
219    {
220      die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
221    }
222  }
223}
224?>
Note: See TracBrowser for help on using the repository browser.