1 | <?php |
---|
2 | /*************************************************************************** |
---|
3 | * functions_category.inc.php * |
---|
4 | * -------------------- * |
---|
5 | * application : PhpWebGallery 1.3 * |
---|
6 | * author : Pierrick LE GALL <pierrick@z0rglub.com> * |
---|
7 | * * |
---|
8 | *************************************************************************** |
---|
9 | |
---|
10 | *************************************************************************** |
---|
11 | * * |
---|
12 | * This program is free software; you can redistribute it and/or modify * |
---|
13 | * it under the terms of the GNU General Public License as published by * |
---|
14 | * the Free Software Foundation; * |
---|
15 | * * |
---|
16 | ***************************************************************************/ |
---|
17 | function get_subcats_id( $cat_id ) |
---|
18 | { |
---|
19 | $restricted_cat = array(); |
---|
20 | $i = 0; |
---|
21 | |
---|
22 | $query = 'select id'; |
---|
23 | $query.= ' from '.PREFIX_TABLE.'categories'; |
---|
24 | $query.= ' where id_uppercat = '.$cat_id; |
---|
25 | $query.= ';'; |
---|
26 | $result = mysql_query( $query ); |
---|
27 | while ( $row = mysql_fetch_array( $result ) ) |
---|
28 | { |
---|
29 | $restricted_cat[$i++] = $row['id']; |
---|
30 | $sub_restricted_cat = get_subcats_id( $row['id'] ); |
---|
31 | for ( $j = 0; $j < sizeof( $sub_restricted_cat ); $j++ ) |
---|
32 | { |
---|
33 | $restricted_cat[$i++] = $sub_restricted_cat[$j]; |
---|
34 | } |
---|
35 | } |
---|
36 | |
---|
37 | return $restricted_cat; |
---|
38 | } |
---|
39 | |
---|
40 | function check_restrictions( $category_id ) |
---|
41 | { |
---|
42 | global $user,$lang; |
---|
43 | |
---|
44 | if ( is_user_allowed( $category_id, $user['restrictions'] ) > 0 ) |
---|
45 | { |
---|
46 | echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />'; |
---|
47 | echo '<a href="'.add_session_id( './category.php' ).'">'; |
---|
48 | echo $lang['thumbnails'].'</a></div>'; |
---|
49 | exit(); |
---|
50 | } |
---|
51 | } |
---|
52 | |
---|
53 | // the check_cat_id function check whether the $cat is a right parameter : |
---|
54 | // - $cat is numeric and corresponds to a category in the database |
---|
55 | // - $cat equals 'fav' (for favorites) |
---|
56 | // - $cat equals 'search' (when the result of a search is displayed) |
---|
57 | function check_cat_id( $cat ) |
---|
58 | { |
---|
59 | global $page; |
---|
60 | |
---|
61 | unset( $page['cat'] ); |
---|
62 | if ( isset( $cat ) ) |
---|
63 | { |
---|
64 | if ( is_numeric( $cat ) ) |
---|
65 | { |
---|
66 | $query = 'select id'; |
---|
67 | $query.= ' from '.PREFIX_TABLE.'categories'; |
---|
68 | $query.= ' where id = '.$cat; |
---|
69 | $query. ';'; |
---|
70 | $result = mysql_query( $query ); |
---|
71 | if ( mysql_num_rows( $result ) != 0 ) |
---|
72 | { |
---|
73 | $page['cat'] = $cat; |
---|
74 | } |
---|
75 | } |
---|
76 | if ( $cat == 'fav' or $cat == 'search' or $cat == 'most_visited' |
---|
77 | or $cat == 'best_rated' or $cat == 'recent' ) |
---|
78 | { |
---|
79 | $page['cat'] = $cat; |
---|
80 | } |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | function display_cat( $id_uppercat, $indent, $restriction, $tab_expand ) |
---|
85 | { |
---|
86 | global $user,$lang,$conf,$page,$vtp,$handle; |
---|
87 | |
---|
88 | $query = 'select name,id,date_dernier,nb_images,dir'; |
---|
89 | $query.= ' from '.PREFIX_TABLE.'categories'; |
---|
90 | $query.= ' where id_uppercat'; |
---|
91 | if ( $id_uppercat == "" ) |
---|
92 | { |
---|
93 | $query.= ' is NULL'; |
---|
94 | } |
---|
95 | else |
---|
96 | { |
---|
97 | $query.= ' = '.$id_uppercat; |
---|
98 | } |
---|
99 | $query.= ' order by rank asc;'; |
---|
100 | $result = mysql_query( $query ); |
---|
101 | while ( $row = mysql_fetch_array( $result ) ) |
---|
102 | { |
---|
103 | if ( !in_array( $row['id'], $restriction ) ) |
---|
104 | { |
---|
105 | $nb_subcats = get_nb_subcats( $row['id'] ); |
---|
106 | |
---|
107 | $expand = ""; |
---|
108 | // si la catégorie n'a pas de sous catégorie |
---|
109 | // ou que l'on doit développer toutes les catégories par défaut |
---|
110 | // alors on utilise l'expand par défaut |
---|
111 | if ( $nb_subcats == 0 or $user['expand'] == "true" ) |
---|
112 | { |
---|
113 | $expand = $page['expand']; |
---|
114 | } |
---|
115 | // si la catégorie n'est pas dans les catégories à développer |
---|
116 | // alors on l'ajoute aux catégories à développer |
---|
117 | else if ( !in_array( $row['id'], $tab_expand ) ) |
---|
118 | { |
---|
119 | $expand = implode( ",", $tab_expand ); |
---|
120 | if ( strlen( $expand ) > 0 ) |
---|
121 | { |
---|
122 | $expand.= ","; |
---|
123 | } |
---|
124 | $expand.= $row['id']; |
---|
125 | } |
---|
126 | // si la catégorie est déjà dans les catégories à développer |
---|
127 | // alors on la retire des catégories à développer |
---|
128 | else |
---|
129 | { |
---|
130 | $expand = array_remove( $tab_expand, $row['id'] ); |
---|
131 | } |
---|
132 | $url = "./category.php?cat=".$page['cat']."&expand=$expand"; |
---|
133 | if ( $page['cat'] == 'search' ) |
---|
134 | { |
---|
135 | $url.= "&search=".$_GET['search']; |
---|
136 | } |
---|
137 | $lien_cat = add_session_id( $url ); |
---|
138 | if ( $row['name'] == "" ) |
---|
139 | { |
---|
140 | $name = str_replace( "_", " ", $row['dir'] ); |
---|
141 | } |
---|
142 | else |
---|
143 | { |
---|
144 | $name = $row['name']; |
---|
145 | } |
---|
146 | |
---|
147 | $vtp->addSession( $handle, 'category' ); |
---|
148 | $vtp->setVar( $handle, 'category.indent', $indent ); |
---|
149 | |
---|
150 | if ( $user['expand'] == "true" or $nb_subcats == 0 ) |
---|
151 | { |
---|
152 | $vtp->addSession( $handle, 'bullet_wo_link' ); |
---|
153 | $vtp->setVar( $handle, 'bullet_wo_link.bullet_url', |
---|
154 | $user['lien_collapsed'] ); |
---|
155 | $vtp->setVar( $handle, 'bullet_wo_link.bullet_alt', '>' ); |
---|
156 | $vtp->closeSession( $handle, 'bullet_wo_link' ); |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | $vtp->addSession( $handle, 'bullet_w_link' ); |
---|
161 | $vtp->setVar( $handle, 'bullet_w_link.bullet_link', $lien_cat ); |
---|
162 | $vtp->setVar( $handle, 'bullet_w_link.bullet_alt', '>' ); |
---|
163 | if ( in_array( $row['id'], $tab_expand ) ) |
---|
164 | { |
---|
165 | $vtp->setVar( $handle, 'bullet_w_link.bullet_url', |
---|
166 | $user['lien_expanded'] ); |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | $vtp->setVar( $handle, 'bullet_w_link.bullet_url', |
---|
171 | $user['lien_collapsed'] ); |
---|
172 | } |
---|
173 | $vtp->closeSession( $handle, 'bullet_w_link' ); |
---|
174 | } |
---|
175 | $vtp->setVar( $handle, 'category.link_url', |
---|
176 | add_session_id( './category.php?cat='. |
---|
177 | $row['id'].'&expand='.$expand ) ); |
---|
178 | $vtp->setVar( $handle, 'category.link_name', $name ); |
---|
179 | if ( $id_uppercat == "" ) |
---|
180 | { |
---|
181 | $vtp->setVar( $handle, 'category.name_style', 'font-weight:bold;' ); |
---|
182 | } |
---|
183 | if ( $nb_subcats > 0 ) |
---|
184 | { |
---|
185 | $vtp->addSession( $handle, 'subcat' ); |
---|
186 | $vtp->setVar( $handle, 'subcat.nb_subcats', $nb_subcats ); |
---|
187 | $vtp->closeSession( $handle, 'subcat' ); |
---|
188 | } |
---|
189 | $vtp->setVar( $handle, 'category.total_cat', $row['nb_images'] ); |
---|
190 | $date_dispo = explode( "-", $row['date_dernier'] ); |
---|
191 | $date_cat = mktime( 0, 0, 0, $date_dispo[1], $date_dispo[2], |
---|
192 | $date_dispo[0] ); |
---|
193 | $vtp->setVar( $handle, 'category.cat_icon', get_icon( $date_cat ) ); |
---|
194 | $vtp->closeSession( $handle, 'category' ); |
---|
195 | |
---|
196 | if ( in_array( $row['id'], $tab_expand ) or $user['expand'] == "true" ) |
---|
197 | { |
---|
198 | display_cat( $row['id'], $indent.' ', |
---|
199 | $restriction, $tab_expand ); |
---|
200 | } |
---|
201 | } |
---|
202 | } |
---|
203 | } |
---|
204 | |
---|
205 | function get_nb_subcats( $id ) |
---|
206 | { |
---|
207 | global $user; |
---|
208 | |
---|
209 | $query = 'select count(*) as count'; |
---|
210 | $query.= ' from '.PREFIX_TABLE.'categories'; |
---|
211 | $query.= ' where id_uppercat = '.$id; |
---|
212 | for ( $i = 0; $i < sizeof( $user['restrictions'] ); $i++ ) |
---|
213 | { |
---|
214 | $query.= " and id != ".$user['restrictions'][$i]; |
---|
215 | } |
---|
216 | $query.= ';'; |
---|
217 | $result = mysql_query( $query ); |
---|
218 | $row = mysql_fetch_array( $result ); |
---|
219 | return $row['count']; |
---|
220 | } |
---|
221 | |
---|
222 | function get_total_image( $id, $restriction ) |
---|
223 | { |
---|
224 | $total = 0; |
---|
225 | |
---|
226 | $query = 'select id,nb_images'; |
---|
227 | $query.= ' from '.PREFIX_TABLE.'categories'; |
---|
228 | $query.= ' where id_uppercat'; |
---|
229 | if ( !is_numeric( $id ) ) |
---|
230 | { |
---|
231 | $query.= ' is NULL'; |
---|
232 | } |
---|
233 | else |
---|
234 | { |
---|
235 | $query.= ' = '.$id; |
---|
236 | } |
---|
237 | $query.= ";"; |
---|
238 | $result = mysql_query( $query ); |
---|
239 | while ( $row = mysql_fetch_array( $result ) ) |
---|
240 | { |
---|
241 | if ( !in_array( $row['id'], $restriction ) ) |
---|
242 | { |
---|
243 | $total+= $row['nb_images']; |
---|
244 | $total+= get_total_image( $row['id'], $restriction ); |
---|
245 | } |
---|
246 | } |
---|
247 | return $total; |
---|
248 | } |
---|
249 | |
---|
250 | // variables : |
---|
251 | // $cat['comment'] |
---|
252 | // $cat['dir'] |
---|
253 | // $cat['last_dir'] |
---|
254 | // $cat['name'] is an array : |
---|
255 | // - $cat['name'][0] is the lowest cat name |
---|
256 | // and |
---|
257 | // - $cat['name'][n] is the most uppercat name findable |
---|
258 | // $cat['nb_images'] |
---|
259 | // $cat['id_uppercat'] |
---|
260 | // $cat['site_id'] |
---|
261 | function get_cat_info( $id ) |
---|
262 | { |
---|
263 | $cat = array(); |
---|
264 | $cat['name'] = array(); |
---|
265 | |
---|
266 | $query = 'select nb_images,id_uppercat,comment,site_id,galleries_url,dir'; |
---|
267 | $query.= ' from '.PREFIX_TABLE.'categories as a'; |
---|
268 | $query.= ', '.PREFIX_TABLE.'sites as b'; |
---|
269 | $query.= ' where a.id = '.$id; |
---|
270 | $query.= ' and a.site_id = b.id;'; |
---|
271 | $row = mysql_fetch_array( mysql_query( $query ) ); |
---|
272 | $cat['site_id'] = $row['site_id']; |
---|
273 | $cat['id_uppercat'] = $row['id_uppercat']; |
---|
274 | $cat['comment'] = nl2br( $row['comment'] ); |
---|
275 | $cat['nb_images'] = $row['nb_images']; |
---|
276 | $cat['last_dir'] = $row['dir']; |
---|
277 | $galleries_url = $row['galleries_url']; |
---|
278 | |
---|
279 | $cat['dir'] = ""; |
---|
280 | $i = 0; |
---|
281 | $is_root = false; |
---|
282 | $row['id_uppercat'] = $id; |
---|
283 | while ( !$is_root ) |
---|
284 | { |
---|
285 | $query = 'select name,dir,id_uppercat'; |
---|
286 | $query.= ' from '.PREFIX_TABLE.'categories'; |
---|
287 | $query.= ' where id = '.$row['id_uppercat'].';'; |
---|
288 | $row = mysql_fetch_array( mysql_query( $query ) ); |
---|
289 | $cat['dir'] = $row['dir']."/".$cat['dir']; |
---|
290 | if ( $row['name'] == "" ) |
---|
291 | { |
---|
292 | $cat['name'][$i] = str_replace( "_", " ", $row['dir'] ); |
---|
293 | } |
---|
294 | else |
---|
295 | { |
---|
296 | $cat['name'][$i] = $row['name']; |
---|
297 | } |
---|
298 | if ( $row['id_uppercat'] == "" ) |
---|
299 | { |
---|
300 | $is_root = true; |
---|
301 | } |
---|
302 | $i++; |
---|
303 | } |
---|
304 | $cat['local_dir'] = substr( $cat['dir'], 0 , strlen( $cat['dir'] ) - 1 ); |
---|
305 | $cat['dir'] = $galleries_url.$cat['dir']; |
---|
306 | |
---|
307 | return $cat; |
---|
308 | } |
---|
309 | |
---|
310 | // The function get_cat_display_name returns a string containing the list |
---|
311 | // of upper categories to the root category from the lowest category shown |
---|
312 | // example : "anniversaires - fete mere 2002 - animaux - erika" |
---|
313 | // You can give two parameters : |
---|
314 | // - $separation : the string between each category name " - " for example |
---|
315 | // - $style : the style of the span tag for the lowest category, |
---|
316 | // "font-style:italic;" for example |
---|
317 | function get_cat_display_name( $array_cat_names, $separation, $style ) |
---|
318 | { |
---|
319 | $output = ""; |
---|
320 | for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- ) |
---|
321 | { |
---|
322 | if ( $i != sizeof( $array_cat_names ) - 1 ) |
---|
323 | { |
---|
324 | $output.= $separation; |
---|
325 | } |
---|
326 | if ( $i != 0 ) |
---|
327 | { |
---|
328 | $output.= $array_cat_names[$i]; |
---|
329 | } |
---|
330 | else |
---|
331 | { |
---|
332 | if ( $style != "" ) |
---|
333 | { |
---|
334 | $output.= '<span style="'.$style.'">'; |
---|
335 | } |
---|
336 | $output.= $array_cat_names[$i]; |
---|
337 | if ( $style != "" ) |
---|
338 | { |
---|
339 | $output.= "</span>"; |
---|
340 | } |
---|
341 | } |
---|
342 | } |
---|
343 | return replace_space( $output ); |
---|
344 | } |
---|
345 | |
---|
346 | // initialize_category initializes ;-) the variables in relation |
---|
347 | // with category : |
---|
348 | // 1. calculation of the number of pictures in the category |
---|
349 | // 2. determination of the SQL query part to ask to find the right category |
---|
350 | // $page['where'] is not the same if we are in |
---|
351 | // - simple category |
---|
352 | // - search result |
---|
353 | // - favorites displaying |
---|
354 | // - most visited pictures |
---|
355 | // - best rated pictures |
---|
356 | // - recent pictures |
---|
357 | // 3. determination of the title of the page |
---|
358 | // 4. creation of the navigation bar |
---|
359 | function initialize_category( $calling_page = 'category' ) |
---|
360 | { |
---|
361 | global $page,$lang,$user,$conf; |
---|
362 | |
---|
363 | if ( isset( $page['cat'] ) ) |
---|
364 | { |
---|
365 | // $page['nb_image_page'] is the number of picture to display on this page |
---|
366 | // By default, it is the same as the $user['nb_image_page'] |
---|
367 | $page['nb_image_page'] = $user['nb_image_page']; |
---|
368 | // $url is used to create the navigation bar |
---|
369 | $url = './category.php?cat='.$page['cat'].'&expand='.$page['expand']; |
---|
370 | // simple category |
---|
371 | if ( is_numeric( $page['cat'] ) ) |
---|
372 | { |
---|
373 | $result = get_cat_info( $page['cat'] ); |
---|
374 | $page['comment'] = $result['comment']; |
---|
375 | $page['cat_dir'] = $result['dir']; |
---|
376 | $page['cat_name'] = $result['name']; |
---|
377 | $page['cat_nb_images'] = $result['nb_images']; |
---|
378 | $page['cat_site_id'] = $result['site_id']; |
---|
379 | $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' ); |
---|
380 | $page['where'] = ' where cat_id = '.$page['cat']; |
---|
381 | } |
---|
382 | else |
---|
383 | { |
---|
384 | $query = ''; |
---|
385 | // search result |
---|
386 | if ( $page['cat'] == 'search' ) |
---|
387 | { |
---|
388 | $page['title'] = $lang['search_result']; |
---|
389 | if ( $calling_page == 'picture' ) |
---|
390 | { |
---|
391 | $page['title'].= ' : <span style="font-style:italic;">'; |
---|
392 | $page['title'].= $_GET['search']."</span>"; |
---|
393 | } |
---|
394 | $page['where'] = " where ( file like '%".$_GET['search']."%'"; |
---|
395 | $page['where'].= " or name like '%".$_GET['search']."%'"; |
---|
396 | $page['where'].= " or comment like '%".$_GET['search']."%' )"; |
---|
397 | |
---|
398 | $query = 'select count(*) as nb_total_images'; |
---|
399 | $query.= ' from '.PREFIX_TABLE.'images'; |
---|
400 | $query.= $page['where']; |
---|
401 | $query.= ';'; |
---|
402 | |
---|
403 | $url.= '&search='.$_GET['search']; |
---|
404 | } |
---|
405 | // favorites displaying |
---|
406 | else if ( $page['cat'] == 'fav' ) |
---|
407 | { |
---|
408 | $page['title'] = $lang['favorites']; |
---|
409 | |
---|
410 | $page['where'] = ', '.PREFIX_TABLE.'favorites'; |
---|
411 | $page['where'].= ' where user_id = '.$user['id']; |
---|
412 | $page['where'].= ' and image_id = id'; |
---|
413 | |
---|
414 | $query = 'select count(*) as nb_total_images'; |
---|
415 | $query.= ' from '.PREFIX_TABLE.'favorites'; |
---|
416 | $query.= ' where user_id = '.$user['id']; |
---|
417 | $query.= ';'; |
---|
418 | } |
---|
419 | // pictures within the short period |
---|
420 | else if ( $page['cat'] == 'recent' ) |
---|
421 | { |
---|
422 | $page['title'] = $lang['recent_cat_title']; |
---|
423 | // We must find the date corresponding to : |
---|
424 | // today - $conf['periode_courte'] |
---|
425 | $date = time() - 60*60*24*$user['short_period']; |
---|
426 | $page['where'] = " where date_available > '"; |
---|
427 | $page['where'].= date( 'Y-m-d', $date )."'"; |
---|
428 | |
---|
429 | $query = 'select count(*) as nb_total_images'; |
---|
430 | $query.= ' from '.PREFIX_TABLE.'images'; |
---|
431 | $query.= $page['where']; |
---|
432 | $query.= ';'; |
---|
433 | } |
---|
434 | // most visited pictures |
---|
435 | else if ( $page['cat'] == 'most_visited' ) |
---|
436 | { |
---|
437 | $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat']; |
---|
438 | $page['where'] = ' where cat_id != -1'; |
---|
439 | $conf['order_by'] = ' order by hit desc, file asc'; |
---|
440 | $page['cat_nb_images'] = $conf['top_number']; |
---|
441 | if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] ) |
---|
442 | { |
---|
443 | $page['nb_image_page'] = $conf['top_number'] - $page['start']; |
---|
444 | } |
---|
445 | } |
---|
446 | |
---|
447 | if ( $query != '' ) |
---|
448 | { |
---|
449 | $result = mysql_query( $query ); |
---|
450 | $row = mysql_fetch_array( $result ); |
---|
451 | $page['cat_nb_images'] = $row['nb_total_images']; |
---|
452 | } |
---|
453 | |
---|
454 | if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited' |
---|
455 | or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' ) |
---|
456 | { |
---|
457 | // we must not show pictures of a forbidden category |
---|
458 | $restricted_cat = get_all_restrictions( $user['id'], $user['status'] ); |
---|
459 | if ( sizeof( $restricted_cat ) > 0 ) |
---|
460 | { |
---|
461 | for ( $i = 0; $i < sizeof( $restricted_cat ); $i++ ) |
---|
462 | { |
---|
463 | $page['where'].= ' and cat_id != '.$restricted_cat[$i]; |
---|
464 | } |
---|
465 | } |
---|
466 | } |
---|
467 | } |
---|
468 | if ( $calling_page == 'category' ) |
---|
469 | { |
---|
470 | $page['navigation_bar'] = |
---|
471 | create_navigation_bar( $url, $page['cat_nb_images'], $page['start'], |
---|
472 | $user['nb_image_page'], 'back' ); |
---|
473 | } |
---|
474 | } |
---|
475 | else |
---|
476 | { |
---|
477 | $page['title'] = $lang['diapo_default_page_title']; |
---|
478 | } |
---|
479 | } |
---|
480 | ?> |
---|