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-2005 PhpWebGallery Team - http://phpwebgallery.net | |
---|
6 | // +-----------------------------------------------------------------------+ |
---|
7 | // | branch : BSF (Best So Far) |
---|
8 | // | file : $RCSfile$ |
---|
9 | // | last update : $Date: 2005-08-15 13:05:29 +0000 (Mon, 15 Aug 2005) $ |
---|
10 | // | last modifier : $Author: plg $ |
---|
11 | // | revision : $Revision: 813 $ |
---|
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 | * Provides functions to handle categories. |
---|
30 | * |
---|
31 | * |
---|
32 | */ |
---|
33 | |
---|
34 | /** |
---|
35 | * Is the category accessible to the connected user ? |
---|
36 | * |
---|
37 | * Note : if the user is not authorized to see this category, page creation |
---|
38 | * ends (exit command in this function) |
---|
39 | * |
---|
40 | * @param int category id to verify |
---|
41 | * @return void |
---|
42 | */ |
---|
43 | function check_restrictions( $category_id ) |
---|
44 | { |
---|
45 | global $user,$lang; |
---|
46 | |
---|
47 | if ( in_array( $category_id, $user['restrictions'] ) ) |
---|
48 | { |
---|
49 | echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />'; |
---|
50 | echo '<a href="'.add_session_id( './category.php' ).'">'; |
---|
51 | echo $lang['thumbnails'].'</a></div>'; |
---|
52 | exit(); |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | /** |
---|
57 | * Checks whether the argument is a right parameter category id |
---|
58 | * |
---|
59 | * The argument is a right parameter if corresponds to one of these : |
---|
60 | * |
---|
61 | * - is numeric and corresponds to a category in the database |
---|
62 | * - equals 'fav' (for favorites) |
---|
63 | * - equals 'search' (when the result of a search is displayed) |
---|
64 | * - equals 'most_visited' |
---|
65 | * - equals 'best_rated' |
---|
66 | * - equals 'recent_pics' |
---|
67 | * - equals 'recent_cats' |
---|
68 | * - equals 'calendar' |
---|
69 | * - equals 'list' |
---|
70 | * |
---|
71 | * The function fills the global var $page['cat'] and returns nothing |
---|
72 | * |
---|
73 | * @param mixed category id or special category name |
---|
74 | * @return void |
---|
75 | */ |
---|
76 | function check_cat_id( $cat ) |
---|
77 | { |
---|
78 | global $page; |
---|
79 | |
---|
80 | unset( $page['cat'] ); |
---|
81 | if ( isset( $cat ) ) |
---|
82 | { |
---|
83 | if ( isset( $page['plain_structure'][$cat] ) ) |
---|
84 | { |
---|
85 | $page['cat'] = $cat; |
---|
86 | } |
---|
87 | else if ( is_numeric( $cat ) ) |
---|
88 | { |
---|
89 | $query = 'SELECT id'; |
---|
90 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat.';'; |
---|
91 | $result = pwg_query( $query ); |
---|
92 | if ( mysql_num_rows( $result ) != 0 ) |
---|
93 | { |
---|
94 | $page['cat'] = $cat; |
---|
95 | } |
---|
96 | } |
---|
97 | if ( $cat == 'fav' |
---|
98 | or $cat == 'most_visited' |
---|
99 | or $cat == 'best_rated' |
---|
100 | or $cat == 'recent_pics' |
---|
101 | or $cat == 'recent_cats' |
---|
102 | or $cat == 'calendar' ) |
---|
103 | { |
---|
104 | $page['cat'] = $cat; |
---|
105 | } |
---|
106 | if ($cat == 'search' and isset($_GET['search'])) |
---|
107 | { |
---|
108 | $page['cat'] = $cat; |
---|
109 | } |
---|
110 | if ($cat == 'list' |
---|
111 | and isset($_GET['list']) |
---|
112 | and preg_match('/^\d+(,\d+)*$/', $_GET['list'])) |
---|
113 | { |
---|
114 | $page['cat'] = 'list'; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | function get_categories_menu() |
---|
120 | { |
---|
121 | global $page,$user; |
---|
122 | |
---|
123 | $infos = array(''); |
---|
124 | |
---|
125 | $query = ' |
---|
126 | SELECT name,id,date_last,nb_images,global_rank |
---|
127 | FROM '.CATEGORIES_TABLE.' |
---|
128 | WHERE 1 = 1'; // stupid but permit using AND after it ! |
---|
129 | if (!$user['expand']) |
---|
130 | { |
---|
131 | $query.= ' |
---|
132 | AND (id_uppercat is NULL'; |
---|
133 | if (isset ($page['tab_expand']) and count($page['tab_expand']) > 0) |
---|
134 | { |
---|
135 | $query.= ' OR id_uppercat IN ('.implode(',',$page['tab_expand']).')'; |
---|
136 | } |
---|
137 | $query.= ')'; |
---|
138 | } |
---|
139 | if ($user['forbidden_categories'] != '') |
---|
140 | { |
---|
141 | $query.= ' |
---|
142 | AND id NOT IN ('.$user['forbidden_categories'].')'; |
---|
143 | } |
---|
144 | $query.= ' |
---|
145 | ;'; |
---|
146 | |
---|
147 | $result = pwg_query($query); |
---|
148 | $cats = array(); |
---|
149 | while ($row = mysql_fetch_array($result)) |
---|
150 | { |
---|
151 | array_push($cats, $row); |
---|
152 | } |
---|
153 | usort($cats, 'global_rank_compare'); |
---|
154 | |
---|
155 | return get_html_menu_category($cats); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * returns the total number of elements viewable in the gallery by the |
---|
160 | * connected user |
---|
161 | * |
---|
162 | * @return int |
---|
163 | */ |
---|
164 | function count_user_total_images() |
---|
165 | { |
---|
166 | global $user; |
---|
167 | |
---|
168 | $query = ' |
---|
169 | SELECT COUNT(DISTINCT(image_id)) as total |
---|
170 | FROM '.IMAGE_CATEGORY_TABLE; |
---|
171 | if (count($user['restrictions']) > 0) |
---|
172 | { |
---|
173 | $query.= ' |
---|
174 | WHERE category_id NOT IN ('.$user['forbidden_categories'].')'; |
---|
175 | } |
---|
176 | $query.= ' |
---|
177 | ;'; |
---|
178 | |
---|
179 | $row = mysql_fetch_array(pwg_query($query)); |
---|
180 | |
---|
181 | return isset($row['total']) ? $row['total'] : 0; |
---|
182 | } |
---|
183 | |
---|
184 | /** |
---|
185 | * Retrieve informations about a category in the database |
---|
186 | * |
---|
187 | * Returns an array with following keys : |
---|
188 | * |
---|
189 | * - comment |
---|
190 | * - dir : directory, might be empty for virtual categories |
---|
191 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
192 | * uppercat name findable) |
---|
193 | * - nb_images |
---|
194 | * - id_uppercat |
---|
195 | * - site_id |
---|
196 | * - |
---|
197 | * |
---|
198 | * @param int category id |
---|
199 | * @return array |
---|
200 | */ |
---|
201 | function get_cat_info( $id ) |
---|
202 | { |
---|
203 | $infos = array('nb_images','id_uppercat','comment','site_id' |
---|
204 | ,'dir','date_last','uploadable','status','visible' |
---|
205 | ,'representative_picture_id','uppercats','commentable'); |
---|
206 | |
---|
207 | $query = ' |
---|
208 | SELECT '.implode(',', $infos).' |
---|
209 | FROM '.CATEGORIES_TABLE.' |
---|
210 | WHERE id = '.$id.' |
---|
211 | ;'; |
---|
212 | $row = mysql_fetch_array(pwg_query($query)); |
---|
213 | |
---|
214 | $cat = array(); |
---|
215 | foreach ($infos as $info) |
---|
216 | { |
---|
217 | if (isset($row[$info])) |
---|
218 | { |
---|
219 | $cat[$info] = $row[$info]; |
---|
220 | } |
---|
221 | else |
---|
222 | { |
---|
223 | $cat[$info] = ''; |
---|
224 | } |
---|
225 | // If the field is true or false, the variable is transformed into a |
---|
226 | // boolean value. |
---|
227 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
228 | { |
---|
229 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
230 | } |
---|
231 | } |
---|
232 | $cat['comment'] = nl2br($cat['comment']); |
---|
233 | |
---|
234 | $names = array(); |
---|
235 | $query = ' |
---|
236 | SELECT name,id |
---|
237 | FROM '.CATEGORIES_TABLE.' |
---|
238 | WHERE id IN ('.$cat['uppercats'].') |
---|
239 | ;'; |
---|
240 | $result = pwg_query($query); |
---|
241 | while($row = mysql_fetch_array($result)) |
---|
242 | { |
---|
243 | $names[$row['id']] = $row['name']; |
---|
244 | } |
---|
245 | |
---|
246 | // category names must be in the same order than uppercats list |
---|
247 | $cat['name'] = array(); |
---|
248 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
249 | { |
---|
250 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
251 | } |
---|
252 | |
---|
253 | return $cat; |
---|
254 | } |
---|
255 | |
---|
256 | // get_complete_dir returns the concatenation of get_site_url and |
---|
257 | // get_local_dir |
---|
258 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
259 | // PhpWebGallery files and this category has 22 for identifier |
---|
260 | // get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
261 | function get_complete_dir( $category_id ) |
---|
262 | { |
---|
263 | return get_site_url($category_id).get_local_dir($category_id); |
---|
264 | } |
---|
265 | |
---|
266 | // get_local_dir returns an array with complete path without the site url |
---|
267 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
268 | // PhpWebGallery files and this category has 22 for identifier |
---|
269 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
270 | function get_local_dir( $category_id ) |
---|
271 | { |
---|
272 | global $page; |
---|
273 | |
---|
274 | $uppercats = ''; |
---|
275 | $local_dir = ''; |
---|
276 | |
---|
277 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
278 | { |
---|
279 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
280 | } |
---|
281 | else |
---|
282 | { |
---|
283 | $query = 'SELECT uppercats'; |
---|
284 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
285 | $query.= ';'; |
---|
286 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
287 | $uppercats = $row['uppercats']; |
---|
288 | } |
---|
289 | |
---|
290 | $upper_array = explode( ',', $uppercats ); |
---|
291 | |
---|
292 | $database_dirs = array(); |
---|
293 | $query = 'SELECT id,dir'; |
---|
294 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
295 | $query.= ';'; |
---|
296 | $result = pwg_query( $query ); |
---|
297 | while( $row = mysql_fetch_array( $result ) ) |
---|
298 | { |
---|
299 | $database_dirs[$row['id']] = $row['dir']; |
---|
300 | } |
---|
301 | foreach ($upper_array as $id) |
---|
302 | { |
---|
303 | $local_dir.= $database_dirs[$id].'/'; |
---|
304 | } |
---|
305 | |
---|
306 | return $local_dir; |
---|
307 | } |
---|
308 | |
---|
309 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
310 | // simply "./galleries/" |
---|
311 | function get_site_url($category_id) |
---|
312 | { |
---|
313 | global $page; |
---|
314 | |
---|
315 | $query = ' |
---|
316 | SELECT galleries_url |
---|
317 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
318 | WHERE s.id = c.site_id |
---|
319 | AND c.id = '.$category_id.' |
---|
320 | ;'; |
---|
321 | $row = mysql_fetch_array(pwg_query($query)); |
---|
322 | return $row['galleries_url']; |
---|
323 | } |
---|
324 | |
---|
325 | // initialize_category initializes ;-) the variables in relation |
---|
326 | // with category : |
---|
327 | // 1. calculation of the number of pictures in the category |
---|
328 | // 2. determination of the SQL query part to ask to find the right category |
---|
329 | // $page['where'] is not the same if we are in |
---|
330 | // - simple category |
---|
331 | // - search result |
---|
332 | // - favorites displaying |
---|
333 | // - most visited pictures |
---|
334 | // - best rated pictures |
---|
335 | // - recent pictures |
---|
336 | // - defined list (used for random) |
---|
337 | // 3. determination of the title of the page |
---|
338 | // 4. creation of the navigation bar |
---|
339 | function initialize_category( $calling_page = 'category' ) |
---|
340 | { |
---|
341 | pwg_debug( 'start initialize_category' ); |
---|
342 | global $page,$lang,$user,$conf; |
---|
343 | |
---|
344 | if ( isset( $page['cat'] ) ) |
---|
345 | { |
---|
346 | // $page['nb_image_page'] is the number of picture to display on this page |
---|
347 | // By default, it is the same as the $user['nb_image_page'] |
---|
348 | $page['nb_image_page'] = $user['nb_image_page']; |
---|
349 | // $url is used to create the navigation bar |
---|
350 | $url = PHPWG_ROOT_PATH.'category.php?cat='.$page['cat']; |
---|
351 | if ( isset($page['expand']) ) $url.= '&expand='.$page['expand']; |
---|
352 | // simple category |
---|
353 | if ( is_numeric( $page['cat'] ) ) |
---|
354 | { |
---|
355 | $result = get_cat_info( $page['cat'] ); |
---|
356 | $page['comment'] = $result['comment']; |
---|
357 | $page['cat_dir'] = $result['dir']; |
---|
358 | $page['cat_name'] = $result['name']; |
---|
359 | $page['cat_nb_images'] = $result['nb_images']; |
---|
360 | $page['cat_site_id'] = $result['site_id']; |
---|
361 | $page['cat_uploadable'] = $result['uploadable']; |
---|
362 | $page['cat_commentable'] = $result['commentable']; |
---|
363 | $page['uppercats'] = $result['uppercats']; |
---|
364 | $page['title'] = |
---|
365 | get_cat_display_name($page['cat_name'], |
---|
366 | '', |
---|
367 | false); |
---|
368 | $page['where'] = ' WHERE category_id = '.$page['cat']; |
---|
369 | } |
---|
370 | else |
---|
371 | { |
---|
372 | if ($page['cat'] == 'search' |
---|
373 | or $page['cat'] == 'most_visited' |
---|
374 | or $page['cat'] == 'recent_pics' |
---|
375 | or $page['cat'] == 'recent_cats' |
---|
376 | or $page['cat'] == 'best_rated' |
---|
377 | or $page['cat'] == 'calendar' |
---|
378 | or $page['cat'] == 'list') |
---|
379 | { |
---|
380 | // we must not show pictures of a forbidden category |
---|
381 | if ( $user['forbidden_categories'] != '' ) |
---|
382 | { |
---|
383 | $forbidden = ' category_id NOT IN '; |
---|
384 | $forbidden.= '('.$user['forbidden_categories'].')'; |
---|
385 | } |
---|
386 | } |
---|
387 | // search result |
---|
388 | if ( $page['cat'] == 'search' ) |
---|
389 | { |
---|
390 | // analyze search string given in URL (created in search.php) |
---|
391 | $tokens = explode('|', $_GET['search']); |
---|
392 | |
---|
393 | if (isset($tokens[1]) and $tokens[1] == 'AND') |
---|
394 | { |
---|
395 | $search['mode'] = 'AND'; |
---|
396 | } |
---|
397 | else |
---|
398 | { |
---|
399 | $search['mode'] = 'OR'; |
---|
400 | } |
---|
401 | |
---|
402 | $search_tokens = explode('--', $tokens[0]); |
---|
403 | foreach ($search_tokens as $search_token) |
---|
404 | { |
---|
405 | $tokens = explode(':', $search_token); |
---|
406 | $field_name = $tokens[0]; |
---|
407 | $field_content = $tokens[1]; |
---|
408 | |
---|
409 | $tokens = explode('~', $tokens[1]); |
---|
410 | if (isset($tokens[1])) |
---|
411 | { |
---|
412 | $search['fields'][$field_name]['mode'] = $tokens[1]; |
---|
413 | } |
---|
414 | else |
---|
415 | { |
---|
416 | $search['fields'][$field_name]['mode'] = ''; |
---|
417 | } |
---|
418 | |
---|
419 | $search['fields'][$field_name]['words'] = array(); |
---|
420 | $tokens = explode(',', $tokens[0]); |
---|
421 | foreach ($tokens as $token) |
---|
422 | { |
---|
423 | array_push($search['fields'][$field_name]['words'], |
---|
424 | htmlentities($token)); |
---|
425 | } |
---|
426 | } |
---|
427 | |
---|
428 | $page['title'] = $lang['search_result']; |
---|
429 | if ( $calling_page == 'picture' ) |
---|
430 | { |
---|
431 | $page['title'].= ' : <span style="font-style:italic;">'; |
---|
432 | $page['title'].= $_GET['search']."</span>"; |
---|
433 | } |
---|
434 | |
---|
435 | // SQL where clauses are stored in $clauses array during query |
---|
436 | // construction |
---|
437 | $clauses = array(); |
---|
438 | |
---|
439 | $textfields = array('file', 'name', 'comment', 'keywords', 'author'); |
---|
440 | foreach ($textfields as $textfield) |
---|
441 | { |
---|
442 | if (isset($search['fields'][$textfield])) |
---|
443 | { |
---|
444 | $local_clauses = array(); |
---|
445 | foreach ($search['fields'][$textfield]['words'] as $word) |
---|
446 | { |
---|
447 | array_push($local_clauses, $textfield." LIKE '%".$word."%'"); |
---|
448 | } |
---|
449 | // adds brackets around where clauses |
---|
450 | array_walk($local_clauses,create_function('&$s','$s="(".$s.")";')); |
---|
451 | array_push($clauses, |
---|
452 | implode(' '.$search['fields'][$textfield]['mode'].' ', |
---|
453 | $local_clauses)); |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | if (isset($search['fields']['allwords'])) |
---|
458 | { |
---|
459 | $fields = array('file', 'name', 'comment', 'keywords', 'author'); |
---|
460 | // in the OR mode, request bust be : |
---|
461 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
462 | // OR (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
463 | // |
---|
464 | // in the AND mode : |
---|
465 | // ((field1 LIKE '%word1%' OR field2 LIKE '%word1%') |
---|
466 | // AND (field1 LIKE '%word2%' OR field2 LIKE '%word2%')) |
---|
467 | $word_clauses = array(); |
---|
468 | foreach ($search['fields']['allwords']['words'] as $word) |
---|
469 | { |
---|
470 | $field_clauses = array(); |
---|
471 | foreach ($fields as $field) |
---|
472 | { |
---|
473 | array_push($field_clauses, $field." LIKE '%".$word."%'"); |
---|
474 | } |
---|
475 | // adds brackets around where clauses |
---|
476 | array_push($word_clauses, implode(' OR ', $field_clauses)); |
---|
477 | } |
---|
478 | array_walk($word_clauses, create_function('&$s','$s="(".$s.")";')); |
---|
479 | array_push($clauses, |
---|
480 | implode(' '.$search['fields']['allwords']['mode'].' ', |
---|
481 | $word_clauses)); |
---|
482 | } |
---|
483 | |
---|
484 | $datefields = array('date_available', 'date_creation'); |
---|
485 | foreach ($datefields as $datefield) |
---|
486 | { |
---|
487 | $key = $datefield; |
---|
488 | if (isset($search['fields'][$key])) |
---|
489 | { |
---|
490 | $local_clause = $datefield." = '"; |
---|
491 | $local_clause.= str_replace('.', '-', |
---|
492 | $search['fields'][$key]['words'][0]); |
---|
493 | $local_clause.= "'"; |
---|
494 | array_push($clauses, $local_clause); |
---|
495 | } |
---|
496 | |
---|
497 | foreach (array('after','before') as $suffix) |
---|
498 | { |
---|
499 | $key = $datefield.'-'.$suffix; |
---|
500 | if (isset($search['fields'][$key])) |
---|
501 | { |
---|
502 | $local_clause = $datefield; |
---|
503 | if ($suffix == 'after') |
---|
504 | { |
---|
505 | $local_clause.= ' >'; |
---|
506 | } |
---|
507 | else |
---|
508 | { |
---|
509 | $local_clause.= ' <'; |
---|
510 | } |
---|
511 | if (isset($search['fields'][$key]['mode']) |
---|
512 | and $search['fields'][$key]['mode'] == 'inc') |
---|
513 | { |
---|
514 | $local_clause.= '='; |
---|
515 | } |
---|
516 | $local_clause.= " '"; |
---|
517 | $local_clause.= str_replace('.', '-', |
---|
518 | $search['fields'][$key]['words'][0]); |
---|
519 | $local_clause.= "'"; |
---|
520 | array_push($clauses, $local_clause); |
---|
521 | } |
---|
522 | } |
---|
523 | } |
---|
524 | |
---|
525 | if (isset($search['fields']['cat'])) |
---|
526 | { |
---|
527 | if ($search['fields']['cat']['mode'] == 'sub_inc') |
---|
528 | { |
---|
529 | // searching all the categories id of sub-categories |
---|
530 | $cat_ids = get_subcat_ids($search['fields']['cat']['words']); |
---|
531 | } |
---|
532 | else |
---|
533 | { |
---|
534 | $cat_ids = $search['fields']['cat']['words']; |
---|
535 | } |
---|
536 | |
---|
537 | $local_clause = 'category_id IN ('.implode(',', $cat_ids).')'; |
---|
538 | array_push($clauses, $local_clause); |
---|
539 | } |
---|
540 | |
---|
541 | // adds brackets around where clauses |
---|
542 | array_walk($clauses, create_function('&$s', '$s = "(".$s.")";')); |
---|
543 | $page['where'] = 'WHERE '.implode(' '.$search['mode'].' ', $clauses); |
---|
544 | if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; |
---|
545 | |
---|
546 | $query = ' |
---|
547 | SELECT COUNT(DISTINCT(id)) AS nb_total_images |
---|
548 | FROM '.IMAGES_TABLE.' |
---|
549 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
550 | '.$page['where'].' |
---|
551 | ;'; |
---|
552 | $url.= '&search='.$_GET['search']; |
---|
553 | } |
---|
554 | // favorites displaying |
---|
555 | else if ( $page['cat'] == 'fav' ) |
---|
556 | { |
---|
557 | check_user_favorites(); |
---|
558 | |
---|
559 | $page['title'] = $lang['favorites']; |
---|
560 | |
---|
561 | $page['where'] = ', '.FAVORITES_TABLE.' AS fav'; |
---|
562 | $page['where'].= ' WHERE user_id = '.$user['id']; |
---|
563 | $page['where'].= ' AND fav.image_id = id'; |
---|
564 | |
---|
565 | $query = 'SELECT COUNT(*) AS nb_total_images'; |
---|
566 | $query.= ' FROM '.FAVORITES_TABLE; |
---|
567 | $query.= ' WHERE user_id = '.$user['id']; |
---|
568 | $query.= ';'; |
---|
569 | } |
---|
570 | // pictures within the short period |
---|
571 | else if ( $page['cat'] == 'recent_pics' ) |
---|
572 | { |
---|
573 | $page['title'] = $lang['recent_pics_cat']; |
---|
574 | // We must find the date corresponding to : |
---|
575 | // today - $conf['periode_courte'] |
---|
576 | $date = time() - 60*60*24*$user['recent_period']; |
---|
577 | $page['where'] = " WHERE date_available > '"; |
---|
578 | $page['where'].= date( 'Y-m-d', $date )."'"; |
---|
579 | if ( isset( $forbidden ) ) $page['where'].= ' AND '.$forbidden; |
---|
580 | |
---|
581 | $query = ' |
---|
582 | SELECT COUNT(DISTINCT(id)) AS nb_total_images |
---|
583 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic |
---|
584 | ON id = ic.image_id |
---|
585 | '.$page['where'].' |
---|
586 | ;'; |
---|
587 | } |
---|
588 | // categories containing recent pictures |
---|
589 | else if ( $page['cat'] == 'recent_cats' ) |
---|
590 | { |
---|
591 | $page['title'] = $lang['recent_cats_cat']; |
---|
592 | $page['cat_nb_images'] = 0; |
---|
593 | } |
---|
594 | // most visited pictures |
---|
595 | else if ( $page['cat'] == 'most_visited' ) |
---|
596 | { |
---|
597 | $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat']; |
---|
598 | |
---|
599 | $page['where'] = 'WHERE hit > 0'; |
---|
600 | if (isset($forbidden)) |
---|
601 | { |
---|
602 | $page['where'] = "\n".' AND '.$forbidden; |
---|
603 | } |
---|
604 | |
---|
605 | $conf['order_by'] = ' ORDER BY hit DESC, file ASC'; |
---|
606 | |
---|
607 | // $page['cat_nb_images'] equals $conf['top_number'] unless there |
---|
608 | // are less visited items |
---|
609 | $query =' |
---|
610 | SELECT COUNT(DISTINCT(id)) AS count |
---|
611 | FROM '.IMAGES_TABLE.' |
---|
612 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
613 | '.$page['where'].' |
---|
614 | ;'; |
---|
615 | $row = mysql_fetch_array(pwg_query($query)); |
---|
616 | if ($row['count'] < $conf['top_number']) |
---|
617 | { |
---|
618 | $page['cat_nb_images'] = $row['count']; |
---|
619 | } |
---|
620 | else |
---|
621 | { |
---|
622 | $page['cat_nb_images'] = $conf['top_number']; |
---|
623 | } |
---|
624 | unset($query); |
---|
625 | |
---|
626 | if ( isset( $page['start'] ) |
---|
627 | and ($page['start']+$user['nb_image_page']>=$conf['top_number'])) |
---|
628 | { |
---|
629 | $page['nb_image_page'] = $conf['top_number'] - $page['start']; |
---|
630 | } |
---|
631 | } |
---|
632 | else if ( $page['cat'] == 'calendar' ) |
---|
633 | { |
---|
634 | $page['cat_nb_images'] = 0; |
---|
635 | $page['title'] = $lang['calendar']; |
---|
636 | if (isset($_GET['year']) |
---|
637 | and preg_match('/^\d+$/', $_GET['year'])) |
---|
638 | { |
---|
639 | $page['calendar_year'] = (int)$_GET['year']; |
---|
640 | } |
---|
641 | if (isset($_GET['month']) |
---|
642 | and preg_match('/^(\d+)\.(\d{2})$/', $_GET['month'], $matches)) |
---|
643 | { |
---|
644 | $page['calendar_year'] = (int)$matches[1]; |
---|
645 | $page['calendar_month'] = (int)$matches[2]; |
---|
646 | } |
---|
647 | if (isset($_GET['day']) |
---|
648 | and preg_match('/^(\d+)\.(\d{2})\.(\d{2})$/', |
---|
649 | $_GET['day'], |
---|
650 | $matches)) |
---|
651 | { |
---|
652 | $page['calendar_year'] = (int)$matches[1]; |
---|
653 | $page['calendar_month'] = (int)$matches[2]; |
---|
654 | $page['calendar_day'] = (int)$matches[3]; |
---|
655 | } |
---|
656 | if (isset($page['calendar_year'])) |
---|
657 | { |
---|
658 | $page['title'] .= ' ('; |
---|
659 | if (isset($page['calendar_day'])) |
---|
660 | { |
---|
661 | if ($page['calendar_year'] >= 1970) |
---|
662 | { |
---|
663 | $unixdate = mktime(0,0,0, |
---|
664 | $page['calendar_month'], |
---|
665 | $page['calendar_day'], |
---|
666 | $page['calendar_year']); |
---|
667 | $page['title'].= $lang['day'][date("w", $unixdate)]; |
---|
668 | } |
---|
669 | $page['title'].= ' '.$page['calendar_day'].', '; |
---|
670 | } |
---|
671 | if (isset($page['calendar_month'])) |
---|
672 | { |
---|
673 | $page['title'] .= $lang['month'][$page['calendar_month']].' '; |
---|
674 | } |
---|
675 | $page['title'] .= $page['calendar_year']; |
---|
676 | $page['title'] .= ')'; |
---|
677 | } |
---|
678 | |
---|
679 | $page['where'] = 'WHERE '.$conf['calendar_datefield'].' IS NOT NULL'; |
---|
680 | if (isset($forbidden)) |
---|
681 | { |
---|
682 | $page['where'].= ' AND '.$forbidden; |
---|
683 | } |
---|
684 | } |
---|
685 | else if ($page['cat'] == 'best_rated') |
---|
686 | { |
---|
687 | $page['title'] = $conf['top_number'].' '.$lang['best_rated_cat']; |
---|
688 | |
---|
689 | $page['where'] = ' WHERE average_rate IS NOT NULL'; |
---|
690 | |
---|
691 | if (isset($forbidden)) |
---|
692 | { |
---|
693 | $page['where'].= ' AND '.$forbidden; |
---|
694 | } |
---|
695 | |
---|
696 | $conf['order_by'] = ' ORDER BY average_rate DESC, id ASC'; |
---|
697 | |
---|
698 | // $page['cat_nb_images'] equals $conf['top_number'] unless there |
---|
699 | // are less rated items |
---|
700 | $query =' |
---|
701 | SELECT COUNT(DISTINCT(id)) AS count |
---|
702 | FROM '.IMAGES_TABLE.' |
---|
703 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON id = ic.image_id |
---|
704 | '.$page['where'].' |
---|
705 | ;'; |
---|
706 | $row = mysql_fetch_array(pwg_query($query)); |
---|
707 | if ($row['count'] < $conf['top_number']) |
---|
708 | { |
---|
709 | $page['cat_nb_images'] = $row['count']; |
---|
710 | } |
---|
711 | else |
---|
712 | { |
---|
713 | $page['cat_nb_images'] = $conf['top_number']; |
---|
714 | } |
---|
715 | unset($query); |
---|
716 | |
---|
717 | |
---|
718 | if (isset($page['start']) |
---|
719 | and ($page['start']+$user['nb_image_page']>=$conf['top_number'])) |
---|
720 | { |
---|
721 | $page['nb_image_page'] = $conf['top_number'] - $page['start']; |
---|
722 | } |
---|
723 | } |
---|
724 | else if ($page['cat'] == 'list') |
---|
725 | { |
---|
726 | $page['title'] = $lang['random_cat']; |
---|
727 | |
---|
728 | $page['where'] = 'WHERE 1=1'; |
---|
729 | if (isset($forbidden)) |
---|
730 | { |
---|
731 | $page['where'].= ' AND '.$forbidden; |
---|
732 | } |
---|
733 | $page['where'].= ' AND image_id IN ('.$_GET['list'].')'; |
---|
734 | $page['cat_nb_images'] = count(explode(',', $_GET['list'])); |
---|
735 | |
---|
736 | $url.= '&list='.$_GET['list']; |
---|
737 | } |
---|
738 | |
---|
739 | if (isset($query)) |
---|
740 | { |
---|
741 | $result = pwg_query( $query ); |
---|
742 | $row = mysql_fetch_array( $result ); |
---|
743 | $page['cat_nb_images'] = $row['nb_total_images']; |
---|
744 | } |
---|
745 | } |
---|
746 | if ( $calling_page == 'category' ) |
---|
747 | { |
---|
748 | $page['navigation_bar'] = |
---|
749 | create_navigation_bar( $url, $page['cat_nb_images'], $page['start'], |
---|
750 | $user['nb_image_page'], 'back' ); |
---|
751 | } |
---|
752 | } |
---|
753 | else |
---|
754 | { |
---|
755 | $page['title'] = $lang['no_category']; |
---|
756 | } |
---|
757 | pwg_debug( 'end initialize_category' ); |
---|
758 | } |
---|
759 | |
---|
760 | function display_select_categories($categories, |
---|
761 | $selecteds, |
---|
762 | $blockname, |
---|
763 | $fullname = true) |
---|
764 | { |
---|
765 | global $template; |
---|
766 | |
---|
767 | foreach ($categories as $category) |
---|
768 | { |
---|
769 | $selected = ''; |
---|
770 | if (in_array($category['id'], $selecteds)) |
---|
771 | { |
---|
772 | $selected = ' selected="selected"'; |
---|
773 | } |
---|
774 | |
---|
775 | if ($fullname) |
---|
776 | { |
---|
777 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
778 | '', |
---|
779 | false); |
---|
780 | } |
---|
781 | else |
---|
782 | { |
---|
783 | $option = str_repeat(' ', |
---|
784 | (3 * substr_count($category['global_rank'], '.'))); |
---|
785 | $option.= '- '.$category['name']; |
---|
786 | } |
---|
787 | |
---|
788 | $template->assign_block_vars( |
---|
789 | $blockname, |
---|
790 | array('SELECTED'=>$selected, |
---|
791 | 'VALUE'=>$category['id'], |
---|
792 | 'OPTION'=>$option |
---|
793 | )); |
---|
794 | } |
---|
795 | } |
---|
796 | |
---|
797 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
798 | $fullname = true) |
---|
799 | { |
---|
800 | $result = pwg_query($query); |
---|
801 | $categories = array(); |
---|
802 | if (!empty($result)) |
---|
803 | { |
---|
804 | while ($row = mysql_fetch_array($result)) |
---|
805 | { |
---|
806 | array_push($categories, $row); |
---|
807 | } |
---|
808 | } |
---|
809 | usort($categories, 'global_rank_compare'); |
---|
810 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
811 | } |
---|
812 | |
---|
813 | /** |
---|
814 | * returns all subcategory identifiers of given category ids |
---|
815 | * |
---|
816 | * @param array ids |
---|
817 | * @return array |
---|
818 | */ |
---|
819 | function get_subcat_ids($ids) |
---|
820 | { |
---|
821 | $query = ' |
---|
822 | SELECT DISTINCT(id) |
---|
823 | FROM '.CATEGORIES_TABLE.' |
---|
824 | WHERE '; |
---|
825 | foreach ($ids as $num => $category_id) |
---|
826 | { |
---|
827 | if ($num > 0) |
---|
828 | { |
---|
829 | $query.= ' |
---|
830 | OR '; |
---|
831 | } |
---|
832 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
833 | } |
---|
834 | $query.= ' |
---|
835 | ;'; |
---|
836 | $result = pwg_query($query); |
---|
837 | |
---|
838 | $subcats = array(); |
---|
839 | while ($row = mysql_fetch_array($result)) |
---|
840 | { |
---|
841 | array_push($subcats, $row['id']); |
---|
842 | } |
---|
843 | return $subcats; |
---|
844 | } |
---|
845 | |
---|
846 | function global_rank_compare($a, $b) |
---|
847 | { |
---|
848 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
849 | } |
---|
850 | ?> |
---|