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 | * Provides functions to handle categories. |
---|
26 | * |
---|
27 | * |
---|
28 | */ |
---|
29 | |
---|
30 | /** |
---|
31 | * Is the category accessible to the connected user ? |
---|
32 | * |
---|
33 | * Note : if the user is not authorized to see this category, page creation |
---|
34 | * ends (exit command in this function) |
---|
35 | * |
---|
36 | * @param int category id to verify |
---|
37 | * @return void |
---|
38 | */ |
---|
39 | function check_restrictions($category_id) |
---|
40 | { |
---|
41 | global $user; |
---|
42 | |
---|
43 | // $filter['visible_categories'] and $filter['visible_images'] |
---|
44 | // are not used because it's not necessary (filter <> restriction) |
---|
45 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
46 | { |
---|
47 | access_denied(); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | function get_categories_menu() |
---|
52 | { |
---|
53 | global $page, $user, $filter, $conf; |
---|
54 | |
---|
55 | $query = ' |
---|
56 | SELECT '; |
---|
57 | // From CATEGORIES_TABLE |
---|
58 | $query.= ' |
---|
59 | id, name, permalink, nb_images, global_rank,'; |
---|
60 | // From USER_CACHE_CATEGORIES_TABLE |
---|
61 | $query.= ' |
---|
62 | date_last, max_date_last, count_images, count_categories'; |
---|
63 | |
---|
64 | // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE |
---|
65 | $query.= ' |
---|
66 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
---|
67 | ON id = cat_id and user_id = '.$user['id']; |
---|
68 | |
---|
69 | // Always expand when filter is activated |
---|
70 | if (!$user['expand'] and !$filter['enabled']) |
---|
71 | { |
---|
72 | $where = ' |
---|
73 | (id_uppercat is NULL'; |
---|
74 | if (isset($page['category'])) |
---|
75 | { |
---|
76 | $where .= ' OR id_uppercat IN ('.$page['category']['uppercats'].')'; |
---|
77 | } |
---|
78 | $where .= ')'; |
---|
79 | } |
---|
80 | else |
---|
81 | { |
---|
82 | $where = ' |
---|
83 | '.get_sql_condition_FandF |
---|
84 | ( |
---|
85 | array |
---|
86 | ( |
---|
87 | 'visible_categories' => 'id', |
---|
88 | ), |
---|
89 | null, |
---|
90 | true |
---|
91 | ); |
---|
92 | } |
---|
93 | |
---|
94 | $where = trigger_event('get_categories_menu_sql_where', |
---|
95 | $where, $user['expand'], $filter['enabled'] ); |
---|
96 | |
---|
97 | $query.= ' |
---|
98 | WHERE '.$where.' |
---|
99 | ;'; |
---|
100 | |
---|
101 | $result = pwg_query($query); |
---|
102 | $cats = array(); |
---|
103 | $selected_category = isset($page['category']) ? $page['category'] : null; |
---|
104 | while ($row = pwg_db_fetch_assoc($result)) |
---|
105 | { |
---|
106 | $child_date_last = @$row['max_date_last']> @$row['date_last']; |
---|
107 | $row = array_merge($row, |
---|
108 | array( |
---|
109 | 'NAME' => trigger_event( |
---|
110 | 'render_category_name', |
---|
111 | $row['name'], |
---|
112 | 'get_categories_menu' |
---|
113 | ), |
---|
114 | 'TITLE' => get_display_images_count( |
---|
115 | $row['nb_images'], |
---|
116 | $row['count_images'], |
---|
117 | $row['count_categories'], |
---|
118 | false, |
---|
119 | ' / ' |
---|
120 | ), |
---|
121 | 'URL' => make_index_url(array('category' => $row)), |
---|
122 | 'LEVEL' => substr_count($row['global_rank'], '.') + 1, |
---|
123 | 'SELECTED' => $selected_category['id'] == $row['id'] ? true : false, |
---|
124 | 'IS_UPPERCAT' => $selected_category['id_uppercat'] == $row['id'] ? true : false, |
---|
125 | ) |
---|
126 | ); |
---|
127 | if ($conf['index_new_icon']) |
---|
128 | { |
---|
129 | $row['icon_ts'] = get_icon($row['max_date_last'], $child_date_last); |
---|
130 | } |
---|
131 | array_push($cats, $row); |
---|
132 | if ($row['id']==@$page['category']['id']) //save the number of subcats for later optim |
---|
133 | $page['category']['count_categories'] = $row['count_categories']; |
---|
134 | } |
---|
135 | usort($cats, 'global_rank_compare'); |
---|
136 | |
---|
137 | // Update filtered data |
---|
138 | if (function_exists('update_cats_with_filtered_data')) |
---|
139 | { |
---|
140 | update_cats_with_filtered_data($cats); |
---|
141 | } |
---|
142 | |
---|
143 | return $cats; |
---|
144 | } |
---|
145 | |
---|
146 | |
---|
147 | /** |
---|
148 | * Retrieve informations about a category in the database |
---|
149 | * |
---|
150 | * Returns an array with following keys : |
---|
151 | * |
---|
152 | * - comment |
---|
153 | * - dir : directory, might be empty for virtual categories |
---|
154 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
155 | * uppercat name findable) |
---|
156 | * - nb_images |
---|
157 | * - id_uppercat |
---|
158 | * - site_id |
---|
159 | * - |
---|
160 | * |
---|
161 | * @param int category id |
---|
162 | * @return array |
---|
163 | */ |
---|
164 | function get_cat_info( $id ) |
---|
165 | { |
---|
166 | $query = ' |
---|
167 | SELECT * |
---|
168 | FROM '.CATEGORIES_TABLE.' |
---|
169 | WHERE id = '.$id.' |
---|
170 | ;'; |
---|
171 | $cat = pwg_db_fetch_assoc(pwg_query($query)); |
---|
172 | if (empty($cat)) |
---|
173 | return null; |
---|
174 | |
---|
175 | foreach ($cat as $k => $v) |
---|
176 | { |
---|
177 | // If the field is true or false, the variable is transformed into a |
---|
178 | // boolean value. |
---|
179 | if ($cat[$k] == 'true' or $cat[$k] == 'false') |
---|
180 | { |
---|
181 | $cat[$k] = get_boolean( $cat[$k] ); |
---|
182 | } |
---|
183 | } |
---|
184 | |
---|
185 | $upper_ids = explode(',', $cat['uppercats']); |
---|
186 | if ( count($upper_ids)==1 ) |
---|
187 | {// no need to make a query for level 1 |
---|
188 | $cat['upper_names'] = array( |
---|
189 | array( |
---|
190 | 'id' => $cat['id'], |
---|
191 | 'name' => $cat['name'], |
---|
192 | 'permalink' => $cat['permalink'], |
---|
193 | ) |
---|
194 | ); |
---|
195 | } |
---|
196 | else |
---|
197 | { |
---|
198 | $names = array(); |
---|
199 | $query = ' |
---|
200 | SELECT id, name, permalink |
---|
201 | FROM '.CATEGORIES_TABLE.' |
---|
202 | WHERE id IN ('.$cat['uppercats'].') |
---|
203 | ;'; |
---|
204 | $names = hash_from_query($query, 'id'); |
---|
205 | |
---|
206 | // category names must be in the same order than uppercats list |
---|
207 | $cat['upper_names'] = array(); |
---|
208 | foreach ($upper_ids as $cat_id) |
---|
209 | { |
---|
210 | array_push( $cat['upper_names'], $names[$cat_id]); |
---|
211 | } |
---|
212 | } |
---|
213 | return $cat; |
---|
214 | } |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | // returns an array of image orders available for users/visitors |
---|
219 | function get_category_preferred_image_orders() |
---|
220 | { |
---|
221 | global $conf, $page; |
---|
222 | |
---|
223 | return trigger_event('get_category_preferred_image_orders', array( |
---|
224 | array(l10n('Default'), '', true), |
---|
225 | array(l10n('Photo title, A → Z'), 'name ASC', true), |
---|
226 | array(l10n('Photo title, Z → A'), 'name DESC', true), |
---|
227 | array(l10n('Date created, new → old'), 'date_creation DESC', true), |
---|
228 | array(l10n('Date created, old → new'), 'date_creation ASC', true), |
---|
229 | array(l10n('Date posted, new → old'), 'date_available DESC', true), |
---|
230 | array(l10n('Date posted, old → new'), 'date_available ASC', true), |
---|
231 | array(l10n('Rating score, high → low'), 'rating_score DESC', $conf['rate']), |
---|
232 | array(l10n('Rating score, low → high'), 'rating_score ASC', $conf['rate']), |
---|
233 | array(l10n('Visits, high → low'), 'hit DESC', true), |
---|
234 | array(l10n('Visits, low → high'), 'hit ASC', true), |
---|
235 | array(l10n('Permissions'), 'level DESC', is_admin()), |
---|
236 | )); |
---|
237 | } |
---|
238 | |
---|
239 | function display_select_categories($categories, |
---|
240 | $selecteds, |
---|
241 | $blockname, |
---|
242 | $fullname = true) |
---|
243 | { |
---|
244 | global $template; |
---|
245 | |
---|
246 | $tpl_cats = array(); |
---|
247 | foreach ($categories as $category) |
---|
248 | { |
---|
249 | if (!empty($category['permalink'])) |
---|
250 | { |
---|
251 | $category['name'] .= ' √'; |
---|
252 | } |
---|
253 | if ($fullname) |
---|
254 | { |
---|
255 | $option = strip_tags( |
---|
256 | get_cat_display_name_cache( |
---|
257 | $category['uppercats'], |
---|
258 | null, |
---|
259 | false |
---|
260 | ) |
---|
261 | ); |
---|
262 | } |
---|
263 | else |
---|
264 | { |
---|
265 | $option = str_repeat(' ', |
---|
266 | (3 * substr_count($category['global_rank'], '.'))); |
---|
267 | $option.= '- '; |
---|
268 | $option.= strip_tags( |
---|
269 | trigger_event( |
---|
270 | 'render_category_name', |
---|
271 | $category['name'], |
---|
272 | 'display_select_categories' |
---|
273 | ) |
---|
274 | ); |
---|
275 | } |
---|
276 | $tpl_cats[ $category['id'] ] = $option; |
---|
277 | } |
---|
278 | |
---|
279 | $template->assign( $blockname, $tpl_cats); |
---|
280 | $template->assign( $blockname.'_selected', $selecteds); |
---|
281 | } |
---|
282 | |
---|
283 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
284 | $fullname = true) |
---|
285 | { |
---|
286 | $result = pwg_query($query); |
---|
287 | $categories = array(); |
---|
288 | if (!empty($result)) |
---|
289 | { |
---|
290 | while ($row = pwg_db_fetch_assoc($result)) |
---|
291 | { |
---|
292 | array_push($categories, $row); |
---|
293 | } |
---|
294 | } |
---|
295 | usort($categories, 'global_rank_compare'); |
---|
296 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
297 | } |
---|
298 | |
---|
299 | /** |
---|
300 | * returns all subcategory identifiers of given category ids |
---|
301 | * |
---|
302 | * @param array ids |
---|
303 | * @return array |
---|
304 | */ |
---|
305 | function get_subcat_ids($ids) |
---|
306 | { |
---|
307 | $query = ' |
---|
308 | SELECT DISTINCT(id) |
---|
309 | FROM '.CATEGORIES_TABLE.' |
---|
310 | WHERE '; |
---|
311 | foreach ($ids as $num => $category_id) |
---|
312 | { |
---|
313 | is_numeric($category_id) |
---|
314 | or trigger_error( |
---|
315 | 'get_subcat_ids expecting numeric, not '.gettype($category_id), |
---|
316 | E_USER_WARNING |
---|
317 | ); |
---|
318 | if ($num > 0) |
---|
319 | { |
---|
320 | $query.= ' |
---|
321 | OR '; |
---|
322 | } |
---|
323 | $query.= 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\''; |
---|
324 | } |
---|
325 | $query.= ' |
---|
326 | ;'; |
---|
327 | $result = pwg_query($query); |
---|
328 | |
---|
329 | $subcats = array(); |
---|
330 | while ($row = pwg_db_fetch_assoc($result)) |
---|
331 | { |
---|
332 | array_push($subcats, $row['id']); |
---|
333 | } |
---|
334 | return $subcats; |
---|
335 | } |
---|
336 | |
---|
337 | /** finds a matching category id from a potential list of permalinks |
---|
338 | * @param array permalinks example: holiday holiday/france holiday/france/paris |
---|
339 | * @param int idx - output of the index in $permalinks that matches |
---|
340 | * return category id or null if no match |
---|
341 | */ |
---|
342 | function get_cat_id_from_permalinks( $permalinks, &$idx ) |
---|
343 | { |
---|
344 | $in = ''; |
---|
345 | foreach($permalinks as $permalink) |
---|
346 | { |
---|
347 | if ( !empty($in) ) $in.=', '; |
---|
348 | $in .= '\''.$permalink.'\''; |
---|
349 | } |
---|
350 | $query =' |
---|
351 | SELECT cat_id AS id, permalink, 1 AS is_old |
---|
352 | FROM '.OLD_PERMALINKS_TABLE.' |
---|
353 | WHERE permalink IN ('.$in.') |
---|
354 | UNION |
---|
355 | SELECT id, permalink, 0 AS is_old |
---|
356 | FROM '.CATEGORIES_TABLE.' |
---|
357 | WHERE permalink IN ('.$in.') |
---|
358 | ;'; |
---|
359 | $perma_hash = hash_from_query($query, 'permalink'); |
---|
360 | |
---|
361 | if ( empty($perma_hash) ) |
---|
362 | return null; |
---|
363 | for ($i=count($permalinks)-1; $i>=0; $i--) |
---|
364 | { |
---|
365 | if ( isset( $perma_hash[ $permalinks[$i] ] ) ) |
---|
366 | { |
---|
367 | $idx = $i; |
---|
368 | $cat_id = $perma_hash[ $permalinks[$i] ]['id']; |
---|
369 | if ($perma_hash[ $permalinks[$i] ]['is_old']) |
---|
370 | { |
---|
371 | $query=' |
---|
372 | UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1 |
---|
373 | WHERE permalink=\''.$permalinks[$i].'\' AND cat_id='.$cat_id.' |
---|
374 | LIMIT 1'; |
---|
375 | pwg_query($query); |
---|
376 | } |
---|
377 | return $cat_id; |
---|
378 | } |
---|
379 | } |
---|
380 | return null; |
---|
381 | } |
---|
382 | |
---|
383 | function global_rank_compare($a, $b) |
---|
384 | { |
---|
385 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
386 | } |
---|
387 | |
---|
388 | function rank_compare($a, $b) |
---|
389 | { |
---|
390 | return $a['rank'] - $b['rank']; |
---|
391 | } |
---|
392 | |
---|
393 | /** |
---|
394 | * returns display text for information images of category |
---|
395 | * |
---|
396 | * @param array categories |
---|
397 | * @return string |
---|
398 | */ |
---|
399 | function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $Separator = '\n') |
---|
400 | { |
---|
401 | $display_text = ''; |
---|
402 | |
---|
403 | if ($cat_count_images > 0) |
---|
404 | { |
---|
405 | if ($cat_nb_images > 0 and $cat_nb_images < $cat_count_images) |
---|
406 | { |
---|
407 | $display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $Separator).$Separator; |
---|
408 | $cat_count_images-= $cat_nb_images; |
---|
409 | $cat_nb_images = 0; |
---|
410 | } |
---|
411 | |
---|
412 | //at least one image direct or indirect |
---|
413 | $display_text.= l10n_dec('%d photo', '%d photos', $cat_count_images); |
---|
414 | |
---|
415 | if ($cat_count_categories == 0 or $cat_nb_images == $cat_count_images) |
---|
416 | { |
---|
417 | //no descendant categories or descendants do not contain images |
---|
418 | if (! $short_message) |
---|
419 | { |
---|
420 | $display_text.= ' '.l10n('in this album'); |
---|
421 | } |
---|
422 | } |
---|
423 | else |
---|
424 | { |
---|
425 | $display_text.= ' '.l10n_dec('in %d sub-album', 'in %d sub-albums', $cat_count_categories); |
---|
426 | } |
---|
427 | } |
---|
428 | |
---|
429 | return $display_text; |
---|
430 | } |
---|
431 | |
---|
432 | /** |
---|
433 | * Find a random photo among all photos below a given album in the tree (not |
---|
434 | * only photo directly associated to the album but also to sub-albums) |
---|
435 | * |
---|
436 | * we need $category['uppercats'], $category['id'], $category['count_images'] |
---|
437 | */ |
---|
438 | function get_random_image_in_category($category, $recursive=true) |
---|
439 | { |
---|
440 | $image_id = null; |
---|
441 | if ($category['count_images']>0) |
---|
442 | { |
---|
443 | $query = ' |
---|
444 | SELECT image_id |
---|
445 | FROM '.CATEGORIES_TABLE.' AS c |
---|
446 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.category_id = c.id |
---|
447 | WHERE '; |
---|
448 | if ($recursive) |
---|
449 | { |
---|
450 | $query.= ' |
---|
451 | (c.id='.$category['id'].' OR uppercats LIKE \''.$category['uppercats'].',%\')'; |
---|
452 | } |
---|
453 | else |
---|
454 | { |
---|
455 | $query.= ' |
---|
456 | c.id='.$category['id']; |
---|
457 | } |
---|
458 | $query.= ' |
---|
459 | '.get_sql_condition_FandF |
---|
460 | ( |
---|
461 | array |
---|
462 | ( |
---|
463 | 'forbidden_categories' => 'c.id', |
---|
464 | 'visible_categories' => 'c.id', |
---|
465 | 'visible_images' => 'image_id', |
---|
466 | ), |
---|
467 | "\n AND" |
---|
468 | ).' |
---|
469 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
470 | LIMIT 1 |
---|
471 | ;'; |
---|
472 | $result = pwg_query($query); |
---|
473 | if (pwg_db_num_rows($result) > 0) |
---|
474 | { |
---|
475 | list($image_id) = pwg_db_fetch_row($result); |
---|
476 | } |
---|
477 | } |
---|
478 | |
---|
479 | return $image_id; |
---|
480 | } |
---|
481 | |
---|
482 | |
---|
483 | ?> |
---|