source: trunk/category.php @ 120

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

When we use thumbnails for exploring sub-categories, the categories in the
left menu open with a difference of 1 page

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