1 | <?php |
---|
2 | // +-----------------------------------------------------------------------+ |
---|
3 | // | Piwigo - a PHP based photo gallery | |
---|
4 | // +-----------------------------------------------------------------------+ |
---|
5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
---|
6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
8 | // +-----------------------------------------------------------------------+ |
---|
9 | // | This program is free software; you can redistribute it and/or modify | |
---|
10 | // | it under the terms of the GNU General Public License as published by | |
---|
11 | // | the Free Software Foundation | |
---|
12 | // | | |
---|
13 | // | This program is distributed in the hope that it will be useful, but | |
---|
14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
16 | // | General Public License for more details. | |
---|
17 | // | | |
---|
18 | // | You should have received a copy of the GNU General Public License | |
---|
19 | // | along with this program; if not, write to the Free Software | |
---|
20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
21 | // | USA. | |
---|
22 | // +-----------------------------------------------------------------------+ |
---|
23 | |
---|
24 | |
---|
25 | /** |
---|
26 | * Tags available. Each return tag is represented as an array with its id, |
---|
27 | * its name, its weight (count), its url name. Tags are not sorted. |
---|
28 | * |
---|
29 | * The returned list can be a subset of all existing tags due to |
---|
30 | * permissions, only if a list of forbidden categories is provided |
---|
31 | * |
---|
32 | * @param array forbidden categories |
---|
33 | * @return array |
---|
34 | */ |
---|
35 | function get_available_tags() |
---|
36 | { |
---|
37 | // we can find top fatter tags among reachable images |
---|
38 | $query = ' |
---|
39 | SELECT tag_id, COUNT(DISTINCT(it.image_id)) AS counter |
---|
40 | FROM '.IMAGE_CATEGORY_TABLE.' ic |
---|
41 | INNER JOIN '.IMAGE_TAG_TABLE.' it ON ic.image_id=it.image_id'.get_sql_condition_FandF |
---|
42 | ( |
---|
43 | array |
---|
44 | ( |
---|
45 | 'forbidden_categories' => 'category_id', |
---|
46 | 'visible_categories' => 'category_id', |
---|
47 | 'visible_images' => 'ic.image_id' |
---|
48 | ), |
---|
49 | ' |
---|
50 | WHERE' |
---|
51 | ).' |
---|
52 | GROUP BY tag_id'; |
---|
53 | $tag_counters = simple_hash_from_query($query, 'tag_id', 'counter'); |
---|
54 | |
---|
55 | if ( empty($tag_counters) ) |
---|
56 | { |
---|
57 | return array(); |
---|
58 | } |
---|
59 | |
---|
60 | $query = ' |
---|
61 | SELECT * |
---|
62 | FROM '.TAGS_TABLE; |
---|
63 | $result = pwg_query($query); |
---|
64 | $tags = array(); |
---|
65 | while ($row = pwg_db_fetch_assoc($result)) |
---|
66 | { |
---|
67 | $counter = @$tag_counters[ $row['id'] ]; |
---|
68 | if ( $counter ) |
---|
69 | { |
---|
70 | $row['counter'] = $counter; |
---|
71 | $row['name'] = trigger_event('render_tag_name', $row['name']); |
---|
72 | array_push($tags, $row); |
---|
73 | } |
---|
74 | } |
---|
75 | return $tags; |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * All tags, even tags associated to no image. |
---|
80 | * |
---|
81 | * @return array |
---|
82 | */ |
---|
83 | function get_all_tags() |
---|
84 | { |
---|
85 | $query = ' |
---|
86 | SELECT * |
---|
87 | FROM '.TAGS_TABLE.' |
---|
88 | ;'; |
---|
89 | $result = pwg_query($query); |
---|
90 | $tags = array(); |
---|
91 | while ($row = pwg_db_fetch_assoc($result)) |
---|
92 | { |
---|
93 | $row['name'] = trigger_event('render_tag_name', $row['name']); |
---|
94 | array_push($tags, $row); |
---|
95 | } |
---|
96 | |
---|
97 | usort($tags, 'tag_alpha_compare'); |
---|
98 | |
---|
99 | return $tags; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * Giving a set of tags with a counter for each one, calculate the display |
---|
104 | * level of each tag. |
---|
105 | * |
---|
106 | * The level of each tag depends on the average count of tags. This |
---|
107 | * calcylation method avoid having very different levels for tags having |
---|
108 | * nearly the same count when set are small. |
---|
109 | * |
---|
110 | * @param array tags |
---|
111 | * @return array |
---|
112 | */ |
---|
113 | function add_level_to_tags($tags) |
---|
114 | { |
---|
115 | global $conf; |
---|
116 | |
---|
117 | if (count($tags) == 0) |
---|
118 | { |
---|
119 | return $tags; |
---|
120 | } |
---|
121 | |
---|
122 | $total_count = 0; |
---|
123 | |
---|
124 | foreach ($tags as $tag) |
---|
125 | { |
---|
126 | $total_count+= $tag['counter']; |
---|
127 | } |
---|
128 | |
---|
129 | // average count of available tags will determine the level of each tag |
---|
130 | $tag_average_count = $total_count / count($tags); |
---|
131 | |
---|
132 | // tag levels threshold calculation: a tag with an average rate must have |
---|
133 | // the middle level. |
---|
134 | for ($i = 1; $i < $conf['tags_levels']; $i++) |
---|
135 | { |
---|
136 | $threshold_of_level[$i] = |
---|
137 | 2 * $i * $tag_average_count / $conf['tags_levels']; |
---|
138 | } |
---|
139 | |
---|
140 | // display sorted tags |
---|
141 | foreach (array_keys($tags) as $k) |
---|
142 | { |
---|
143 | $tags[$k]['level'] = 1; |
---|
144 | |
---|
145 | // based on threshold, determine current tag level |
---|
146 | for ($i = $conf['tags_levels'] - 1; $i >= 1; $i--) |
---|
147 | { |
---|
148 | if ($tags[$k]['counter'] > $threshold_of_level[$i]) |
---|
149 | { |
---|
150 | $tags[$k]['level'] = $i + 1; |
---|
151 | break; |
---|
152 | } |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | return $tags; |
---|
157 | } |
---|
158 | |
---|
159 | /** |
---|
160 | * return the list of image ids corresponding to given tags. AND & OR mode |
---|
161 | * supported. |
---|
162 | * |
---|
163 | * @param array tag ids |
---|
164 | * @param string mode |
---|
165 | * @param string extra_images_where_sql - optionally apply a sql where filter to retrieved images |
---|
166 | * @param string order_by - optionally overwrite default photo order |
---|
167 | * @return array |
---|
168 | */ |
---|
169 | function get_image_ids_for_tags($tag_ids, $mode='AND', $extra_images_where_sql='', $order_by='', $use_permissions=true) |
---|
170 | { |
---|
171 | global $conf; |
---|
172 | if (empty($tag_ids)) |
---|
173 | { |
---|
174 | return array(); |
---|
175 | } |
---|
176 | |
---|
177 | $query = 'SELECT id |
---|
178 | FROM '.IMAGES_TABLE.' i '; |
---|
179 | |
---|
180 | if ($use_permissions) |
---|
181 | { |
---|
182 | $query.= ' |
---|
183 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' ic ON id=ic.image_id'; |
---|
184 | } |
---|
185 | |
---|
186 | $query.= ' |
---|
187 | INNER JOIN '.IMAGE_TAG_TABLE.' it ON id=it.image_id |
---|
188 | WHERE tag_id IN ('.implode(',', $tag_ids).')'; |
---|
189 | |
---|
190 | if ($use_permissions) |
---|
191 | { |
---|
192 | $query.= get_sql_condition_FandF( |
---|
193 | array( |
---|
194 | 'forbidden_categories' => 'category_id', |
---|
195 | 'visible_categories' => 'category_id', |
---|
196 | 'visible_images' => 'id' |
---|
197 | ), |
---|
198 | "\n AND" |
---|
199 | ); |
---|
200 | } |
---|
201 | |
---|
202 | $query.= (empty($extra_images_where_sql) ? '' : " \nAND (".$extra_images_where_sql.')').' |
---|
203 | GROUP BY id'; |
---|
204 | |
---|
205 | if ($mode=='AND' and count($tag_ids)>1) |
---|
206 | { |
---|
207 | $query .= ' |
---|
208 | HAVING COUNT(DISTINCT tag_id)='.count($tag_ids); |
---|
209 | } |
---|
210 | $query .= "\n".(empty($order_by) ? $conf['order_by'] : $order_by); |
---|
211 | |
---|
212 | return array_from_query($query, 'id'); |
---|
213 | } |
---|
214 | |
---|
215 | /** |
---|
216 | * return a list of tags corresponding to given items. |
---|
217 | * |
---|
218 | * @param array items |
---|
219 | * @param array max_tags |
---|
220 | * @param array excluded_tag_ids |
---|
221 | * @return array |
---|
222 | */ |
---|
223 | function get_common_tags($items, $max_tags, $excluded_tag_ids=null) |
---|
224 | { |
---|
225 | if (empty($items)) |
---|
226 | { |
---|
227 | return array(); |
---|
228 | } |
---|
229 | $query = ' |
---|
230 | SELECT t.*, count(*) AS counter |
---|
231 | FROM '.IMAGE_TAG_TABLE.' |
---|
232 | INNER JOIN '.TAGS_TABLE.' t ON tag_id = id |
---|
233 | WHERE image_id IN ('.implode(',', $items).')'; |
---|
234 | if (!empty($excluded_tag_ids)) |
---|
235 | { |
---|
236 | $query.=' |
---|
237 | AND tag_id NOT IN ('.implode(',', $excluded_tag_ids).')'; |
---|
238 | } |
---|
239 | $query .=' |
---|
240 | GROUP BY t.id |
---|
241 | ORDER BY '; |
---|
242 | if ($max_tags>0) |
---|
243 | { |
---|
244 | $query .= 'counter DESC |
---|
245 | LIMIT '.$max_tags; |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | $query .= 'NULL'; |
---|
250 | } |
---|
251 | |
---|
252 | $result = pwg_query($query); |
---|
253 | $tags = array(); |
---|
254 | while($row = pwg_db_fetch_assoc($result)) |
---|
255 | { |
---|
256 | $row['name'] = trigger_event('render_tag_name', $row['name']); |
---|
257 | array_push($tags, $row); |
---|
258 | } |
---|
259 | usort($tags, 'tag_alpha_compare'); |
---|
260 | return $tags; |
---|
261 | } |
---|
262 | |
---|
263 | /** |
---|
264 | * return a list of tags corresponding to any of ids, url_names, names |
---|
265 | * |
---|
266 | * @param array ids |
---|
267 | * @param array url_names |
---|
268 | * @param array names |
---|
269 | * @return array |
---|
270 | */ |
---|
271 | function find_tags($ids, $url_names=array(), $names=array() ) |
---|
272 | { |
---|
273 | $where_clauses = array(); |
---|
274 | if ( !empty($ids) ) |
---|
275 | { |
---|
276 | $where_clauses[] = 'id IN ('.implode(',', $ids).')'; |
---|
277 | } |
---|
278 | if ( !empty($url_names) ) |
---|
279 | { |
---|
280 | $where_clauses[] = |
---|
281 | 'url_name IN ('. |
---|
282 | implode( |
---|
283 | ',', |
---|
284 | array_map( |
---|
285 | create_function('$s', 'return "\'".$s."\'";'), |
---|
286 | $url_names |
---|
287 | ) |
---|
288 | ) |
---|
289 | .')'; |
---|
290 | } |
---|
291 | if ( !empty($names) ) |
---|
292 | { |
---|
293 | $where_clauses[] = |
---|
294 | 'name IN ('. |
---|
295 | implode( |
---|
296 | ',', |
---|
297 | array_map( |
---|
298 | create_function('$s', 'return "\'".$s."\'";'), |
---|
299 | $names |
---|
300 | ) |
---|
301 | ) |
---|
302 | .')'; |
---|
303 | } |
---|
304 | if (empty($where_clauses)) |
---|
305 | { |
---|
306 | return array(); |
---|
307 | } |
---|
308 | |
---|
309 | $query = ' |
---|
310 | SELECT * |
---|
311 | FROM '.TAGS_TABLE.' |
---|
312 | WHERE '. implode( ' |
---|
313 | OR ', $where_clauses); |
---|
314 | |
---|
315 | $result = pwg_query($query); |
---|
316 | $tags = array(); |
---|
317 | while ($row = pwg_db_fetch_assoc($result)) |
---|
318 | { |
---|
319 | array_push($tags, $row); |
---|
320 | } |
---|
321 | return $tags; |
---|
322 | } |
---|
323 | ?> |
---|