source: tags/release-1_3_0/category.php @ 16878

Last change on this file since 16878 was 193, checked in by z0rglub, 21 years ago

All parent categories of the displayed category are expanded in the menu

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 19.2 KB
Line 
1<?php
2/***************************************************************************
3 *                               category.php                              *
4 *                            -------------------                          *
5 *   application   : PhpWebGallery 1.3 <http://phpwebgallery.net>          *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: category.php 193 2003-10-11 13:36:43Z z0rglub $
9 *                                                                         *
10 ***************************************************************************/
11
12/***************************************************************************
13 *                                                                         *
14 *   This program is free software; you can redistribute it and/or modify  *
15 *   it under the terms of the GNU General Public License as published by  *
16 *   the Free Software Foundation;                                         *
17 *                                                                         *
18 ***************************************************************************/
19// determine the initial instant to indicate the generation time of this page
20$t1 = explode( ' ', microtime() );
21$t2 = explode( '.', $t1[0] );
22$t2 = $t1[1].'.'.$t2[1];
23//----------------------------------------------------------- personnal include
24include_once( './include/init.inc.php' );
25//---------------------------------------------------------------------- logout
26if ( $_GET['act'] == 'logout' and isset( $_COOKIE['id'] ) )
27{
28  // cookie deletion if exists
29  setcookie( 'id', '', 0, cookie_path() );
30  $url = 'category.php';
31  header( 'Request-URI: '.$url ); 
32  header( 'Content-Location: '.$url ); 
33  header( 'Location: '.$url );
34  exit();
35}
36//-------------------------------------------------- access authorization check
37// creating the plain structure : array of all the available categories and
38// their relative informations, see the definition of the function
39// get_plain_structure for further details.
40$page['plain_structure'] = get_plain_structure();
41
42check_cat_id( $_GET['cat'] );
43check_login_authorization();
44if ( isset( $page['cat'] ) and is_numeric( $page['cat'] ) )
45{
46  check_restrictions( $page['cat'] );
47}
48//-------------------------------------------------------------- initialization
49// creation of the array containing the cat ids to expand in the menu
50// $page['tab_expand'] contains an array with the category ids
51// $page['expand'] contains the string to display in URL with comma
52$page['tab_expand'] = array();
53if ( isset ( $_GET['expand'] ) and $_GET['expand'] != 'all' )
54{
55  $tab_expand = explode( ',', $_GET['expand'] );
56  foreach ( $tab_expand as $id ) {
57    if ( is_numeric( $id ) ) array_push( $page['tab_expand'], $id );
58  }
59  if ( is_numeric( $page['cat'] ) )
60  {
61    // the category displayed (in the URL cat=23) must be seen in the menu ->
62    // parent categories must be expanded
63    $parent = $page['plain_structure'][$page['cat']]['id_uppercat'];
64    while ( $parent != '' )
65    {
66      array_push( $page['tab_expand'], $parent );
67      $parent = $page['plain_structure'][$parent]['id_uppercat'];
68    }
69  }
70  $page['expand'] = implode( ',', $page['tab_expand'] );
71}
72// in case of expanding all authorized cats
73// The $page['expand'] equals 'all' and
74// $page['tab_expand'] contains all the authorized cat ids
75if ( $user['expand'] or $_GET['expand'] == 'all' )
76{
77  $page['tab_expand'] = array();
78  $page['expand'] = 'all';
79}
80// detection of the start picture to display
81if ( !isset( $_GET['start'] )
82     or !is_numeric( $_GET['start'] )
83     or ( is_numeric( $_GET['start'] ) and $_GET['start'] < 0 ) )
84{
85  $page['start'] = 0;
86}
87else
88{
89  $page['start'] = $_GET['start'];
90}
91// Sometimes, a "num" is provided in the URL. It is the number
92// of the picture to show. This picture must be in the thumbnails page.
93// We have to find the right $page['start'] that show the num picture
94// in this category
95if ( is_numeric( $_GET['num'] ) and $_GET['num'] >= 0 )
96{
97  $page['start'] = floor( $_GET['num'] / $user['nb_image_page'] );
98  $page['start']*= $user['nb_image_page'];
99}
100// creating the structure of the categories (useful for displaying the menu)
101$page['structure'] = create_structure( '', $user['restrictions'] );
102$page['structure'] = update_structure( $page['structure'] );
103initialize_category();
104//----------------------------------------------------- template initialization
105$vtp = new VTemplate;
106$handle = $vtp->Open( './template/'.$user['template'].'/category.vtp' );
107initialize_template();
108$tpl = array(
109  'categories','hint_category','sub-cat','images_available','total',
110  'title_menu','nb_image_category','send_mail','title_send_mail',
111  'generation_time','connected_user','recent_image','days','generation_time',
112  'favorite_cat_hint','favorite_cat','stats','most_visited_cat_hint',
113  'most_visited_cat','recent_cat','recent_cat_hint','upload_picture',
114  'comments' );
115templatize_array( $tpl, 'lang', $handle );
116
117$tpl = array( 'mail_webmaster','webmaster','top_number','version','site_url' );
118templatize_array( $tpl, 'conf', $handle );
119
120$tpl = array( 'short_period','long_period','lien_collapsed', 'username' );
121templatize_array( $tpl, 'user', $handle );
122
123$tpl = array( 'title','navigation_bar','cat_comment','cat_nb_images' );
124templatize_array( $tpl, 'page', $handle );
125
126// special global template vars
127$vtp->setGlobalVar( $handle, 'icon_short', get_icon( time() ) );
128$icon_long = get_icon( time() - ( $user['short_period'] * 24 * 60 * 60 + 1 ) );
129$vtp->setGlobalVar( $handle, 'icon_long', $icon_long );
130$nb_total_pictures = count_images( $page['structure'] );
131$vtp->setGlobalVar( $handle, 'nb_total_pictures',$nb_total_pictures );
132//------------------------------------------------------------- categories menu
133// normal categories
134foreach ( $page['structure'] as $category ) {
135  // display category is a function relative to the template
136  display_category( $category, '&nbsp;', $handle );
137}
138// favorites cat
139if ( !$user['is_the_guest'] )
140{
141  $vtp->addSession( $handle, 'favorites' );
142  $url = './category.php?cat=fav&amp;expand='.$page['expand'];
143  $vtp->setVar( $handle, 'favorites.url', add_session_id( $url ) );
144  // searching the number of favorite picture
145  $query = 'SELECT COUNT(*) AS count';
146  $query.= ' FROM '.PREFIX_TABLE.'favorites';
147  $query.= ' WHERE user_id = '.$user['id'].';';
148  $result = mysql_query( $query );
149  $row = mysql_fetch_array( $result );
150  $vtp->setVar( $handle, 'favorites.nb_favorites', $row['count'] );
151  $vtp->closeSession( $handle, 'favorites' );
152}
153// most visited pictures category
154$url = './category.php?cat=most_visited&amp;expand='.$page['expand'];
155$vtp->setGlobalVar( $handle, 'most_visited_url', add_session_id( $url ) );
156// recent pictures
157$url = './category.php?cat=recent&amp;expand='.$page['expand'];
158$vtp->setGlobalVar( $handle, 'recent_url', add_session_id( $url ) );
159//--------------------------------------------------------------------- summary
160$vtp->addSession( $handle, 'summary' );
161$vtp->setVar( $handle, 'summary.url', './identification.php' );
162if ( !$user['is_the_guest'] )
163{
164  $vtp->setVar( $handle, 'summary.title', '' );
165  $vtp->setVar( $handle, 'summary.name',replace_space($lang['change_login']));
166}
167else
168{
169  $vtp->setVar( $handle, 'summary.title', $lang['hint_login'] );
170  $vtp->setVar( $handle, 'summary.name',  replace_space( $lang['menu_login']));
171}
172$vtp->closeSession( $handle, 'summary' );
173// links for registered users
174if ( !$user['is_the_guest'] )
175{
176  // logout link
177  $vtp->addSession( $handle, 'summary' );
178  $vtp->setVar( $handle, 'summary.url', './category.php?act=logout' );
179  $vtp->setVar( $handle, 'summary.title', '' );
180  $vtp->setVar( $handle, 'summary.name', replace_space( $lang['logout'] ) );
181  $vtp->closeSession( $handle, 'summary' );
182  // customization link
183  $vtp->addSession( $handle, 'summary' );
184  $url = './profile.php?cat='.$page['cat'];
185  $url.= '&amp;expand='.$page['expand'];
186  if ( $page['cat'] == 'search' )
187  {
188    $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
189  }
190  $vtp->setVar( $handle, 'summary.url', add_session_id( $url ) );
191  $vtp->setVar( $handle, 'summary.title', $lang['hint_customize'] );
192  $vtp->setVar( $handle, 'summary.name', replace_space( $lang['customize'] ) );
193  $vtp->closeSession( $handle, 'summary' );
194}
195// search link
196$vtp->addSession( $handle, 'summary' );
197$vtp->setVar( $handle, 'summary.url', add_session_id( './search.php' ) );
198$vtp->setVar( $handle, 'summary.title', $lang['hint_search'] );
199$vtp->setVar( $handle, 'summary.name', replace_space( $lang['search'] ) );
200$vtp->closeSession( $handle, 'summary' );
201// comments link
202$vtp->addSession( $handle, 'summary' );
203$vtp->setVar( $handle, 'summary.url', add_session_id( './comments.php' ) );
204$vtp->setVar( $handle, 'summary.title', $lang['hint_comments'] );
205$vtp->setVar( $handle, 'summary.name', replace_space( $lang['comments'] ) );
206$vtp->closeSession( $handle, 'summary' );
207// about link
208$vtp->addSession( $handle, 'summary' );
209$vtp->setVar( $handle, 'summary.url', './about.php?'.
210              str_replace( '&', '&amp;', $_SERVER['QUERY_STRING'] ) );
211$vtp->setVar( $handle, 'summary.title', $lang['hint_about'] );
212$vtp->setVar( $handle, 'summary.name', replace_space( $lang['about'] ) );
213$vtp->closeSession( $handle, 'summary' );
214// administration link
215if ( $user['status'] == 'admin' )
216{
217  $vtp->addSession( $handle, 'summary' );
218  $vtp->setVar( $handle, 'summary.url',
219                add_session_id( './admin/admin.php' ) );
220  $vtp->setVar( $handle, 'summary.title', $lang['hint_admin'] );
221  $vtp->setVar( $handle, 'summary.name', replace_space( $lang['admin'] ) );
222  $vtp->closeSession( $handle, 'summary' );
223}
224//-------------------------------------------------------------- category title
225if ( isset ( $page['cat'] ) )
226{
227  if ( is_numeric( $page['cat'] ) )
228  {
229    $cat_title = get_cat_display_name( $page['cat_name'], '<br />',
230                                    'font-style:italic;' );
231    $vtp->setGlobalVar( $handle, "cat_title", $cat_title );
232  }
233  else
234  {
235    if ( $page['cat'] == 'search' )
236    {
237      $page['title'].= ' : <span style="font-style:italic;">';
238      $page['title'].= $_GET['search']."</span>";
239    }
240    $page['title'] = replace_space( $page['title'] );
241    $vtp->setGlobalVar( $handle, "cat_title", $page['title'] );
242  }
243}
244else
245{
246  $vtp->setGlobalVar( $handle, "cat_title",
247                      replace_space( $lang['no_category'] ) );
248}
249//------------------------------------------------------------------ thumbnails
250if ( isset( $page['cat'] ) and $page['cat_nb_images'] != 0 )
251{
252  $array_cat_directories = array();
253 
254  $query = 'SELECT distinct(id),file,date_available,tn_ext,name,filesize';
255  $query.= ',storage_category_id';
256  $query.= ' FROM '.PREFIX_TABLE.'images AS i';
257  $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category AS ic ON id=ic.image_id';
258  $query.= $page['where'];
259  $query.= $conf['order_by'];
260  $query.= ' LIMIT '.$page['start'].','.$page['nb_image_page'];
261  $query.= ';';
262  $result = mysql_query( $query );
263
264  $vtp->addSession( $handle, 'thumbnails' );
265  $vtp->addSession( $handle, 'line' );
266  // iteration counter to use a new <tr> every "$nb_image_line" pictures
267  $cell_number = 1;
268  // iteration counter to be sure not to create too much lines in the table
269  $line_number = 1;
270  while ( $row = mysql_fetch_array( $result ) )
271  {
272    // retrieving the storage dir of the picture
273    if ( $array_cat_directories[$row['storage_category_id']] == '' )
274    {
275      $array_cat_directories[$row['storage_category_id']] =
276        get_complete_dir( $row['storage_category_id'] );
277    }
278    $cat_directory = $array_cat_directories[$row['storage_category_id']];
279
280    $file = get_filename_wo_extension( $row['file'] );
281    // name of the picture
282    if ( $row['name'] != '' ) $name = $row['name'];
283    else                      $name = str_replace( '_', ' ', $file );
284
285    if ( $page['cat'] == 'search' )
286    {
287      $name = replace_search( $name, $_GET['search'] );
288    }
289    // thumbnail url
290    $thumbnail_url = $cat_directory;
291    $thumbnail_url.= 'thumbnail/'.$conf['prefix_thumbnail'];
292    $thumbnail_url.= $file.'.'.$row['tn_ext'];
293    // message in title for the thumbnail
294    $thumbnail_title = $row['file'];
295    if ( $row['filesize'] == '' )
296      $poids = floor( filesize( $cat_directory.$row['file'] ) / 1024 );
297    else
298      $poids = $row['filesize'];
299    $thumbnail_title .= ' : '.$poids.' KB';
300    // url link on picture.php page
301    $url_link = './picture.php?cat='.$page['cat'];
302    $url_link.= '&amp;image_id='.$row['id'].'&amp;expand='.$page['expand'];
303    if ( $page['cat'] == 'search' )
304    {
305      $url_link.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
306    }
307    // date of availability for creation icon
308    list( $year,$month,$day ) = explode( '-', $row['date_available'] );
309    $date = mktime( 0, 0, 0, $month, $day, $year );
310    // sending vars to display
311    $vtp->addSession( $handle, 'thumbnail' );
312    $vtp->setVar( $handle, 'thumbnail.url', add_session_id( $url_link ) );
313    $vtp->setVar( $handle, 'thumbnail.src', $thumbnail_url );
314    $vtp->setVar( $handle, 'thumbnail.alt', $row['file'] );
315    $vtp->setVar( $handle, 'thumbnail.title', $thumbnail_title );
316    $vtp->setVar( $handle, 'thumbnail.name', $name );
317    $vtp->setVar( $handle, 'thumbnail.icon', get_icon( $date ) );
318
319    if ( $conf['show_comments'] and $user['show_nb_comments'] )
320    {
321      $vtp->addSession( $handle, 'nb_comments' );
322      $query = 'SELECT COUNT(*) AS nb_comments';
323      $query.= ' FROM '.PREFIX_TABLE.'comments';
324      $query.= ' WHERE image_id = '.$row['id'];
325      $query.= " AND validated = 'true'";
326      $query.= ';';
327      $row = mysql_fetch_array( mysql_query( $query ) );
328      $vtp->setVar( $handle, 'nb_comments.nb', $row['nb_comments'] );
329      $vtp->closeSession( $handle, 'nb_comments' );
330    }
331   
332    $vtp->closeSession( $handle, 'thumbnail' );
333   
334    if ( $cell_number++ == $user['nb_image_line'] )
335    {
336      // creating a new line
337      $vtp->closeSession( $handle, 'line' );
338      // the number of the next cell is 1
339      $cell_number = 1;
340      // we only create a new line if it does not exceed the maximum line
341      // per page for the logged user
342      if ( $line_number++ < $user['nb_line_page'] )
343      {
344        $vtp->addSession( $handle, 'line' );
345      }
346    }
347  }
348  $vtp->closeSession( $handle, 'thumbnails' );
349}
350//-------------------------------------------------------------- empty category
351elseif ( ( isset( $page['cat'] )
352           and is_numeric( $page['cat'] )
353           and $page['cat_nb_images'] == 0 )
354         or $_GET['cat'] == '' )
355{
356  $vtp->addSession( $handle, 'thumbnails' );
357  $vtp->addSession( $handle, 'line' );
358
359  $subcats = get_non_empty_subcat_ids( $page['cat'] );
360  $cell_number = 1;
361  $i = 0;
362  foreach ( $subcats as $subcat_id => $non_empty_id ) {
363    $name = '<img src="'.$user['lien_collapsed'].'" style="border:none;"';
364    $name.= ' alt="&gt;"/> ';
365    $name.= '[ <span style="font-weight:bold;">';
366    $name.= $page['plain_structure'][$subcat_id]['name'];
367    $name.= '</span> ]';
368
369    // searching the representative picture of the category
370    $query = 'SELECT representative_picture_id';
371    $query.= ' FROM '.PREFIX_TABLE.'categories';
372    $query.= ' WHERE id = '.$non_empty_id;
373    $row = mysql_fetch_array( mysql_query( $query ) );
374   
375    $query = 'SELECT file,tn_ext,storage_category_id';
376    $query.= ' FROM '.PREFIX_TABLE.'images';
377    $query.= ' LEFT JOIN '.PREFIX_TABLE.'image_category ON id = image_id';
378    $query.= ' WHERE category_id = '.$non_empty_id;
379    // if the category has a representative picture, this is its thumbnail
380    // tha will be displayed !
381    if ( $row['representative_picture_id'] != '' )
382      $query.= ' AND id = '.$row['representative_picture_id'];
383    else
384      $query.= ' ORDER BY RAND()';
385    $query.= ' LIMIT 0,1';
386    $query.= ';';
387    $image_result = mysql_query( $query );
388    $image_row    = mysql_fetch_array( $image_result );
389
390    $file = get_filename_wo_extension( $image_row['file'] );
391
392    // creating links for thumbnail and associated category
393    $thumbnail_link = get_complete_dir( $image_row['storage_category_id'] );
394    $thumbnail_link.= 'thumbnail/'.$conf['prefix_thumbnail'];
395    $thumbnail_link.= $file.'.'.$image_row['tn_ext'];
396
397    $thumbnail_title = $lang['hint_category'];
398
399    $url_link = './category.php?cat='.$subcat_id;
400    if ( !in_array( $page['cat'], $page['tab_expand'] ) )
401    {
402      array_push( $page['tab_expand'], $page['cat'] );
403      $page['expand'] = implode( ',', $page['tab_expand'] );
404    }
405    $url_link.= '&amp;expand='.$page['expand'];
406    // we add the category to explore in the expand list
407    if ( $page['expand'] != '' ) $url_link.= ',';
408    $url_link.= $subcat_id;
409
410    $date = $page['plain_structure'][$subcat_id]['date_last'];
411
412    // sending vars to display
413    $vtp->addSession( $handle, 'thumbnail' );
414    $vtp->setVar( $handle, 'thumbnail.url', add_session_id( $url_link ) );
415    $vtp->setVar( $handle, 'thumbnail.src', $thumbnail_link );
416    $vtp->setVar( $handle, 'thumbnail.alt', $image_row['file'] );
417    $vtp->setVar( $handle, 'thumbnail.title', $thumbnail_title );
418    $vtp->setVar( $handle, 'thumbnail.name', $name );
419    $vtp->setVar( $handle, 'thumbnail.icon', get_icon( $date ) );
420    $vtp->closeSession( $handle, 'thumbnail' );
421
422    if ( $cell_number++ == $user['nb_image_line'] )
423    {
424      $vtp->closeSession( $handle, 'line' );
425      $cell_number = 1;
426      // we open a new line if the subcat was not the last one
427      if ( $i++ < count( $subcats ) - 1 )
428      {
429        $vtp->addSession( $handle, 'line' );
430      }
431    }
432  }
433  if ( $i < count( $subcats ) - 1 )
434  {
435    $vtp->closeSession( $handle, 'line' );
436  }
437  $vtp->closeSession( $handle, 'thumbnails' );
438}
439//------------------------------------------------------- category informations
440if ( isset ( $page['cat'] ) )
441{
442  $vtp->addSession( $handle, 'cat_infos' );
443  // navigation bar
444  if ( $page['navigation_bar'] != '' )
445  {
446    $vtp->addSession( $handle, 'navigation' );
447    $vtp->closeSession( $handle, 'navigation' );
448  }
449  // category comment
450  if ( isset( $page['comment'] ) and $page['comment'] != '' )
451  {
452    $vtp->addSession( $handle, 'comment' );
453    $vtp->setVar( $handle, 'comment.cat_comment', $page['comment'] );
454    $vtp->closeSession( $handle, 'comment' );
455  }
456  // total number of pictures in the category
457  if ( is_numeric( $page['cat'] ) )
458  {
459    $vtp->setVar( $handle, 'cat_infos.cat_name',
460                  get_cat_display_name( $page['cat_name'], ' - ',
461                                        'font-style:italic;' ) );
462  }
463  else
464  {
465    $vtp->setVar( $handle, 'cat_infos.cat_name', $page['title'] );
466  }
467  // upload a picture in the category
468  if ( $page['cat_site_id'] == 1
469       and $conf['upload_available']
470       and $page['cat_uploadable'] )
471  {
472    $vtp->addSession( $handle, 'upload' );
473    $url = './upload.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
474    $vtp->setVar( $handle, 'upload.url', add_session_id( $url ) );
475    $vtp->closeSession( $handle, 'upload' );
476  }
477  $vtp->closeSession( $handle, 'cat_infos' );
478}
479//------------------------------------------------------------ log informations
480pwg_log( 'category', $page['title'] );
481mysql_close();
482//------------------------------------------------------------- generation time
483$time = get_elapsed_time( $t2, get_moment() );
484$vtp->setGlobalVar( $handle, 'time', $time );
485//----------------------------------------------------------- html code display
486$code = $vtp->Display( $handle, 0 );
487echo $code;
488?>
Note: See TracBrowser for help on using the repository browser.