source: trunk/include/functions_category.inc.php @ 16

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

* empty log message *

  • 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 *                         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 ***************************************************************************/
17function 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
40function 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)
57function 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
84function 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']."&amp;expand=$expand";
133      if ( $page['cat'] == 'search' )
134      {
135        $url.= "&amp;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', '&gt;' );
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', '&gt;' );
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'].'&amp;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.'&nbsp;&nbsp;&nbsp;&nbsp;',
199                     $restriction, $tab_expand );
200      }
201    }
202  }
203}
204       
205function 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       
222function 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']
261function 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
317function 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
359function 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'].'&amp;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.= '&amp;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_cats = get_all_restrictions( $user['id'],$user['status'] );
459        foreach ( $restricted_cats as $restricted_cat ) {
460          $page['where'].= ' AND cat_id != '.$restricted_cat;
461        }
462      }
463    }
464    if ( $calling_page == 'category' )
465    {
466      $page['navigation_bar'] =
467        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
468                               $user['nb_image_page'], 'back' );
469    }
470  }
471  else
472  {
473    $page['title'] = $lang['diapo_default_page_title'];
474  }
475}
476
477// get_non_empty_sub_cat_ids returns an array composing of the infos of the
478// direct sub-categories of the given uppercat id. Each of these infos is
479// associated to the first found non empty category id. eg :
480//
481// - catname [cat_id]
482// - cat1 [1] -> given uppercat
483//   - cat1.1 [2] (empty)
484//     - cat1.1.1 [5] (empty)
485//     - cat1.1.2 [6]
486//   - cat1.2 [3]
487//   - cat1.3 [4]
488//
489// get_non_empty_sub_cat_ids will return :
490//   $cats[0]['id']            = 2;
491//   $cats[0]['name']          = '';
492//   $cats[0]['dir']           = 'cat1';
493//   $cats[0]['date_dernier']  = '2003-05-17';
494//   $cats[0]['non_empty_cat'] = 6;
495//
496//   $cats[1]['id']            = 3;
497//   $cats[1]['non_empty_cat'] = 3;
498//
499//   $cats[1]['id']            = 4;
500//   $cats[1]['non_empty_cat'] = 4;
501function get_non_empty_sub_cat_ids( $id_uppercat )
502{
503  global $user;
504
505  $cats = array();
506
507  $query = 'SELECT id,name,dir,date_dernier,nb_images';
508  $query.= ' FROM '.PREFIX_TABLE.'categories';
509  $query.= ' WHERE id_uppercat = '.$id_uppercat;
510  // we must not show pictures of a forbidden category
511  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
512  foreach ( $restricted_cats as $restricted_cat ) {
513    $query.= ' AND id != '.$restricted_cat;
514  }
515  $query.= ' ORDER BY rank';
516  $query.= ';';
517
518  $result = mysql_query( $query );
519  while ( $row = mysql_fetch_array( $result ) )
520  {
521    if ( $row['nb_images'] == 0 )
522    {
523      $non_empty_cat = get_first_non_empty_cat_id( $row['id'] );
524    }
525    else
526    {
527      $non_empty_cat = $row['id'];
528    }
529    // only categories with findable picture in any of its subcats is
530    // represented.
531    if ( $non_empty_cat != false )
532    {
533      $temp_cat = array(
534        'id' => $row['id'],
535        'name' => $row['name'],
536        'dir' => $row['dir'],
537        'date_dernier' => $row['date_dernier'],
538        'non_empty_cat' => $non_empty_cat );
539      array_push( $cats, $temp_cat );
540    }
541  }
542  return $cats;
543}
544
545// get_first_non_empty_cat_id returns the id of the first non empty
546// sub-category to the given uppercat. If no picture is found in any
547// subcategory, false is returned.
548function get_first_non_empty_cat_id( $id_uppercat )
549{
550  global $user;
551
552  $query = 'SELECT id,nb_images';
553  $query.= ' FROM '.PREFIX_TABLE.'categories';
554  $query.= ' WHERE id_uppercat = '.$id_uppercat;
555  // we must not show pictures of a forbidden category
556  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
557  foreach ( $restricted_cats as $restricted_cat ) {
558    $query.= ' AND id != '.$restricted_cat;
559  }
560  $query.= ' ORDER BY RAND()';
561  $query.= ';';
562  $result = mysql_query( $query );
563  while ( $row = mysql_fetch_array( $result ) )
564  {
565    if ( $row['nb_images'] > 0 )
566    {
567      return $row['id'];
568    }
569  }
570  $result = mysql_query( $query );
571  while ( $row = mysql_fetch_array( $result ) )
572  {
573    // recursive call
574    if ( $subcat = get_first_non_empty_cat_id( $row['id'] ) )
575    {
576      return $subcat;
577    }
578  }
579  return false;
580}
581?>
Note: See TracBrowser for help on using the repository browser.