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

Last change on this file since 19 was 19, 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: 19.2 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'].'&amp;mode='.$_GET['mode'];
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      if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited'
385           or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' )
386      {
387        // we must not show pictures of a forbidden category
388        $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
389        foreach ( $restricted_cats as $restricted_cat ) {
390          $where_append.= ' AND cat_id != '.$restricted_cat;
391        }
392      }
393      // search result
394      if ( $page['cat'] == 'search' )
395      {
396        $page['title'] = $lang['search_result'];
397        if ( $calling_page == 'picture' )
398        {
399          $page['title'].= ' : <span style="font-style:italic;">';
400          $page['title'].= $_GET['search']."</span>";
401        }
402
403        $page['where'] = ' WHERE (';
404        $fields = array( 'file', 'name', 'comment' );
405        $words = explode( ',', $_GET['search'] );
406        $sql_search = array();
407        foreach ( $words as $i => $word ) {
408          // if the user searchs any of the words, the where statement must
409          // be :
410          // field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' ...
411          // OR field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' ...
412          if ( $_GET['mode'] == 'OR' )
413          {
414            if ( $i != 0 ) $page['where'].= ' OR';
415            foreach ( $fields as $j => $field ) {
416              if ( $j != 0 ) $page['where'].= ' OR';
417              $page['where'].= ' '.$field." LIKE '%".$word."%'";
418            }
419          }
420          // if the user searchs all the words :
421          // ( field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' )
422          // AND ( field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' )
423          else if ( $_GET['mode'] == 'AND' )
424          {
425            if ( $i != 0 ) $page['where'].= ' AND';
426            $page['where'].= ' (';
427            foreach ( $fields as $j => $field ) {
428              if ( $j != 0 ) $page['where'].= ' OR';
429              $page['where'].= ' '.$field." LIKE '%".$word."%'";
430            }
431            $page['where'].= ' )';
432          }
433        }
434        $page['where'].= ' )';
435        $page['where'].= $where_append;
436
437        $query = 'SELECT COUNT(*) AS nb_total_images';
438        $query.= ' FROM '.PREFIX_TABLE.'images';
439        $query.= $page['where'];
440        $query.= ';';
441
442        $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
443      }
444      // favorites displaying
445      else if ( $page['cat'] == 'fav' )
446      {
447        $page['title'] = $lang['favorites'];
448
449        $page['where'] = ', '.PREFIX_TABLE.'favorites';
450        $page['where'].= ' WHERE user_id = '.$user['id'];
451        $page['where'].= ' AND image_id = id';
452     
453        $query = 'SELECT COUNT(*) AS nb_total_images';
454        $query.= ' FROM '.PREFIX_TABLE.'favorites';
455        $query.= ' WHERE user_id = '.$user['id'];
456        $query.= ';';
457      }
458      // pictures within the short period
459      else if ( $page['cat'] == 'recent' )
460      {
461        $page['title'] = $lang['recent_cat_title'];
462        // We must find the date corresponding to :
463        // today - $conf['periode_courte']
464        $date = time() - 60*60*24*$user['short_period'];
465        $page['where'] = " WHERE date_available > '";
466        $page['where'].= date( 'Y-m-d', $date )."'";
467        $page['where'].= $where_append;
468
469        $query = 'SELECT COUNT(*) AS nb_total_images';
470        $query.= ' FROM '.PREFIX_TABLE.'images';
471        $query.= $page['where'];
472        $query.= ';';
473      }
474      // most visited pictures
475      else if ( $page['cat'] == 'most_visited' )
476      {
477        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
478        $page['where'] = ' WHERE cat_id != -1'.$where_append;
479        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
480        $page['cat_nb_images'] = $conf['top_number'];
481        if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] )
482        {
483          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
484        }
485      }
486     
487      if ( $query != '' )
488      {
489        $result = mysql_query( $query );
490        $row = mysql_fetch_array( $result );
491        $page['cat_nb_images'] = $row['nb_total_images'];
492      }
493    }
494    if ( $calling_page == 'category' )
495    {
496      $page['navigation_bar'] =
497        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
498                               $user['nb_image_page'], 'back' );
499    }
500  }
501  else
502  {
503    $page['title'] = $lang['diapo_default_page_title'];
504  }
505}
506
507// get_non_empty_sub_cat_ids returns an array composing of the infos of the
508// direct sub-categories of the given uppercat id. Each of these infos is
509// associated to the first found non empty category id. eg :
510//
511// - catname [cat_id]
512// - cat1 [1] -> given uppercat
513//   - cat1.1 [2] (empty)
514//     - cat1.1.1 [5] (empty)
515//     - cat1.1.2 [6]
516//   - cat1.2 [3]
517//   - cat1.3 [4]
518//
519// get_non_empty_sub_cat_ids will return :
520//   $cats[0]['id']            = 2;
521//   $cats[0]['name']          = '';
522//   $cats[0]['dir']           = 'cat1';
523//   $cats[0]['date_dernier']  = '2003-05-17';
524//   $cats[0]['non_empty_cat'] = 6;
525//
526//   $cats[1]['id']            = 3;
527//   $cats[1]['non_empty_cat'] = 3;
528//
529//   $cats[1]['id']            = 4;
530//   $cats[1]['non_empty_cat'] = 4;
531function get_non_empty_sub_cat_ids( $id_uppercat )
532{
533  global $user;
534
535  $cats = array();
536
537  $query = 'SELECT id,name,dir,date_dernier,nb_images';
538  $query.= ' FROM '.PREFIX_TABLE.'categories';
539  $query.= ' WHERE id_uppercat = '.$id_uppercat;
540  // we must not show pictures of a forbidden category
541  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
542  foreach ( $restricted_cats as $restricted_cat ) {
543    $query.= ' AND id != '.$restricted_cat;
544  }
545  $query.= ' ORDER BY rank';
546  $query.= ';';
547
548  $result = mysql_query( $query );
549  while ( $row = mysql_fetch_array( $result ) )
550  {
551    // only categories with findable picture in any of its subcats is
552    // represented.
553    if ( ( $row['nb_images'] != 0 and $non_empty_cat = $row['id'] )
554         or $non_empty_cat = get_first_non_empty_cat_id( $row['id'] ) )
555    {
556      $temp_cat = array(
557        'id'            => $row['id'],
558        'name'          => $row['name'],
559        'dir'           => $row['dir'],
560        'date_dernier'  => $row['date_dernier'],
561        'non_empty_cat' => $non_empty_cat );
562      array_push( $cats, $temp_cat );
563    }
564  }
565  return $cats;
566}
567
568// get_first_non_empty_cat_id returns the id of the first non empty
569// sub-category to the given uppercat. If no picture is found in any
570// subcategory, false is returned.
571function get_first_non_empty_cat_id( $id_uppercat )
572{
573  global $user;
574
575  $query = 'SELECT id,nb_images';
576  $query.= ' FROM '.PREFIX_TABLE.'categories';
577  $query.= ' WHERE id_uppercat = '.$id_uppercat;
578  // we must not show pictures of a forbidden category
579  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
580  foreach ( $restricted_cats as $restricted_cat ) {
581    $query.= ' AND id != '.$restricted_cat;
582  }
583  $query.= ' ORDER BY RAND()';
584  $query.= ';';
585  $result = mysql_query( $query );
586  while ( $row = mysql_fetch_array( $result ) )
587  {
588    if ( $row['nb_images'] > 0 )
589    {
590      return $row['id'];
591    }
592  }
593  $result = mysql_query( $query );
594  while ( $row = mysql_fetch_array( $result ) )
595  {
596    // recursive call
597    if ( $subcat = get_first_non_empty_cat_id( $row['id'] ) )
598    {
599      return $subcat;
600    }
601  }
602  return false;
603}
604?>
Note: See TracBrowser for help on using the repository browser.