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

Last change on this file since 1156 was 1135, checked in by rvelices, 18 years ago

merge -r1134 from branches/branch-1_6 into trunk

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, name, url_name
95  FROM '.TAGS_TABLE.'
96  ORDER BY name
97;';
98  $result = pwg_query($query);
99
100  $tags = array();
101
102  while ($row = mysql_fetch_array($result))
103  {
104    array_push($tags, $row);
105  }
106
107  return $tags;
108}
109
110/**
111 * Giving a set of tags with a counter for each one, calculate the display
112 * level of each tag.
113 *
114 * The level of each tag depends on the average count of tags. This
115 * calcylation method avoid having very different levels for tags having
116 * nearly the same count when set are small.
117 *
118 * @param array tags
119 * @return array
120 */
121function add_level_to_tags($tags)
122{
123  global $conf;
124
125  if (count($tags) == 0)
126  {
127    return $tags;
128  }
129
130  $total_count = 0;
131
132  foreach ($tags as $tag)
133  {
134    $total_count+= $tag['counter'];
135  }
136
137  // average count of available tags will determine the level of each tag
138  $tag_average_count = $total_count / count($tags);
139
140  // tag levels threshold calculation: a tag with an average rate must have
141  // the middle level.
142  for ($i = 1; $i < $conf['tags_levels']; $i++)
143  {
144    $threshold_of_level[$i] =
145      2 * $i * $tag_average_count / $conf['tags_levels'];
146  }
147
148  // display sorted tags
149  foreach (array_keys($tags) as $k)
150  {
151    $tags[$k]['level'] = 1;
152
153    // based on threshold, determine current tag level
154    for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--)
155    {
156      if ($tags[$k]['counter'] > $threshold_of_level[$i])
157      {
158        $tags[$k]['level'] = $i + 1;
159        break;
160      }
161    }
162  }
163
164  return $tags;
165}
166
167/**
168 * return the list of image ids corresponding to given tags. AND & OR mode
169 * supported.
170 *
171 * @param array tag ids
172 * @param string mode
173 * @return array
174 */
175function get_image_ids_for_tags($tag_ids, $mode = 'AND')
176{
177  switch ($mode)
178  {
179    case 'AND':
180    {
181      // strategy is to list images associated to each tag
182      $tag_images = array();
183
184      foreach ($tag_ids as $tag_id)
185      {
186        $query = '
187SELECT image_id
188  FROM '.IMAGE_TAG_TABLE.'
189  WHERE tag_id = '.$tag_id.'
190;';
191        $tag_images[$tag_id] = array_from_query($query, 'image_id');
192      }
193
194      // then we calculate the intersection, the images that are associated to
195      // every tags
196      $items = array_shift($tag_images);
197      foreach ($tag_images as $images)
198      {
199        $items = array_intersect($items, $images);
200      }
201
202      return array_unique($items);
203      break;
204    }
205    case 'OR':
206    {
207      $query = '
208SELECT DISTINCT image_id
209  FROM '.IMAGE_TAG_TABLE.'
210  WHERE tag_id IN ('.implode(',', $tag_ids).')
211;';
212      return array_from_query($query, 'image_id');
213      break;
214    }
215    default:
216    {
217      die('get_image_ids_for_tags: unknown mode, only AND & OR are supported');
218    }
219  }
220}
221?>
Note: See TracBrowser for help on using the repository browser.