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 1816 2007-02-14 05:45:20Z rvelices $ |
---|
8 | // | last update : $Date: 2007-02-14 05:45:20 +0000 (Wed, 14 Feb 2007) $ |
---|
9 | // | last modifier : $Author: rvelices $ |
---|
10 | // | revision : $Revision: 1816 $ |
---|
11 | // | revision : $Revision: 1816 $ |
---|
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 | */ |
---|
39 | function get_available_tags() |
---|
40 | { |
---|
41 | // we can find top fatter tags among reachable images |
---|
42 | $tags_query = ' |
---|
43 | SELECT 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 = ' |
---|
63 | SELECT 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 | */ |
---|
100 | function get_all_tags() |
---|
101 | { |
---|
102 | $query = ' |
---|
103 | SELECT 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 | */ |
---|
131 | function 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 | */ |
---|
185 | function 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 = ' |
---|
197 | SELECT 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 = ' |
---|
218 | SELECT 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 | */ |
---|
240 | function get_common_tags($items, $max_tags, $excluded_tag_ids=null) |
---|
241 | { |
---|
242 | if (empty($items)) |
---|
243 | { |
---|
244 | return array(); |
---|
245 | } |
---|
246 | $query = ' |
---|
247 | SELECT 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 | ?> |
---|