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

Last change on this file since 21 was 21, 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 ***************************************************************************/
17
18function get_subcats_id( $cat_id )
19{
20  $restricted_cats = array();
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    array_push( $restricted_cats, $row['id'] );
30    $sub_restricted_cats = get_subcats_id( $row['id'] );
31    foreach ( $sub_restricted_cats as $sub_restricted_cat ) {
32      array_push( $restricted_cats, $sub_restricted_cat );
33    }
34  }
35  return $restricted_cats;
36}
37
38function check_restrictions( $category_id )
39{
40  global $user,$lang;
41
42  if ( is_user_allowed( $category_id, $user['restrictions'] ) > 0 )
43  {
44    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
45    echo '<a href="'.add_session_id( './category.php' ).'">';
46    echo $lang['thumbnails'].'</a></div>';
47    exit();
48  }
49}
50       
51// the check_cat_id function check whether the $cat is a right parameter :
52//  - $cat is numeric and corresponds to a category in the database
53//  - $cat equals 'fav' (for favorites)
54//  - $cat equals 'search' (when the result of a search is displayed)
55function check_cat_id( $cat )
56{
57  global $page;
58
59  unset( $page['cat'] );
60  if ( isset( $cat ) )
61  {
62    if ( is_numeric( $cat ) )
63    {
64      $query = 'select id';
65      $query.= ' from '.PREFIX_TABLE.'categories';
66      $query.= ' where id = '.$cat;
67      $query. ';';
68      $result = mysql_query( $query );
69      if ( mysql_num_rows( $result ) != 0 )
70      {
71        $page['cat'] = $cat;
72      }
73    }
74    if ( $cat == 'fav' or $cat == 'search' or $cat == 'most_visited'
75         or $cat == 'best_rated' or $cat == 'recent' )
76    {
77      $page['cat'] = $cat;
78    }
79  }
80}
81
82function display_cat( $id_uppercat, $indent, $restriction, $tab_expand )
83{
84  global $user,$lang,$conf,$page,$vtp,$handle;
85 
86  $query = 'select name,id,date_dernier,nb_images,dir';
87  $query.= ' from '.PREFIX_TABLE.'categories';
88  $query.= ' where id_uppercat';
89  if ( $id_uppercat == "" )
90  {
91    $query.= ' is NULL';
92  }
93  else
94  {
95    $query.= ' = '.$id_uppercat;
96  }
97  $query.= ' order by rank asc;';
98  $result = mysql_query( $query );
99  while ( $row = mysql_fetch_array( $result ) )
100  {
101    if ( !in_array( $row['id'], $restriction ) )
102    {
103      $nb_subcats = get_nb_subcats( $row['id'] );
104                               
105      $expand = "";
106      // si la catégorie n'a pas de sous catégorie
107      // ou que l'on doit développer toutes les catégories par défaut
108      // alors on utilise l'expand par défaut
109      if ( $nb_subcats == 0 or $user['expand'] == "true" )
110      {
111        $expand = $page['expand'];
112      }
113      // si la catégorie n'est pas dans les catégories à développer
114      // alors on l'ajoute aux catégories à développer
115      else if ( !in_array( $row['id'], $tab_expand ) )
116      {
117        $expand = implode( ",", $tab_expand );
118        if ( strlen( $expand ) > 0 )
119        {
120          $expand.= ",";
121        }
122        $expand.= $row['id'];
123      }
124      // si la catégorie est déjà dans les catégories à développer
125      // alors on la retire des catégories à développer
126      else
127      {
128        $expand = array_remove( $tab_expand, $row['id'] );
129      }
130      $url = "./category.php?cat=".$page['cat']."&amp;expand=$expand";
131      if ( $page['cat'] == 'search' )
132      {
133        $url.= "&amp;search=".$_GET['search'].'&amp;mode='.$_GET['mode'];
134      }
135      $lien_cat = add_session_id( $url );
136      if ( $row['name'] == "" )
137      {
138        $name = str_replace( "_", " ", $row['dir'] );
139      }
140      else
141      {
142        $name = $row['name'];
143      }
144
145      $vtp->addSession( $handle, 'category' );
146      $vtp->setVar( $handle, 'category.indent', $indent );
147
148      if ( $user['expand'] == "true" or $nb_subcats == 0 )
149      {
150        $vtp->addSession( $handle, 'bullet_wo_link' );
151        $vtp->setVar( $handle, 'bullet_wo_link.bullet_url',
152                      $user['lien_collapsed'] );
153        $vtp->setVar( $handle, 'bullet_wo_link.bullet_alt', '&gt;' );
154        $vtp->closeSession( $handle, 'bullet_wo_link' );
155      }
156      else
157      {
158        $vtp->addSession( $handle, 'bullet_w_link' );
159        $vtp->setVar( $handle, 'bullet_w_link.bullet_link', $lien_cat );
160        $vtp->setVar( $handle, 'bullet_w_link.bullet_alt', '&gt;' );
161        if ( in_array( $row['id'], $tab_expand ) )
162        {
163          $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
164                        $user['lien_expanded'] );
165        }
166        else
167        {
168          $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
169                        $user['lien_collapsed'] );
170        }
171        $vtp->closeSession( $handle, 'bullet_w_link' );
172      }
173      $vtp->setVar( $handle, 'category.link_url',
174                    add_session_id( './category.php?cat='.
175                                    $row['id'].'&amp;expand='.$expand ) );
176      $vtp->setVar( $handle, 'category.link_name', $name );
177      if ( $id_uppercat == "" )
178      {
179        $vtp->setVar( $handle, 'category.name_style', 'font-weight:bold;' );
180      }
181      if ( $nb_subcats > 0 )
182      {
183        $vtp->addSession( $handle, 'subcat' );
184        $vtp->setVar( $handle, 'subcat.nb_subcats', $nb_subcats );
185        $vtp->closeSession( $handle, 'subcat' );
186      }
187      $vtp->setVar( $handle, 'category.total_cat', $row['nb_images'] );
188      $date_dispo = explode( "-", $row['date_dernier'] );
189      $date_cat = mktime( 0, 0, 0, $date_dispo[1], $date_dispo[2],
190                          $date_dispo[0] );
191      $vtp->setVar( $handle, 'category.cat_icon', get_icon( $date_cat ) );
192      $vtp->closeSession( $handle, 'category' );
193
194      if ( in_array( $row['id'], $tab_expand ) or $user['expand'] == "true" )
195      {
196        display_cat( $row['id'], $indent.'&nbsp;&nbsp;&nbsp;&nbsp;',
197                     $restriction, $tab_expand );
198      }
199    }
200  }
201}
202       
203function get_nb_subcats( $id )
204{
205  global $user;
206               
207  $query = 'select count(*) as count';
208  $query.= ' from '.PREFIX_TABLE.'categories';
209  $query.= ' where id_uppercat = '.$id;
210  for ( $i = 0; $i < sizeof( $user['restrictions'] ); $i++ )
211  {
212    $query.= " and id != ".$user['restrictions'][$i];
213  }
214  $query.= ';';
215  $result = mysql_query( $query );
216  $row = mysql_fetch_array( $result );
217  return $row['count'];
218}
219       
220function get_total_image( $id, $restriction )
221{
222  $total = 0;
223               
224  $query = 'select id,nb_images';
225  $query.= ' from '.PREFIX_TABLE.'categories';
226  $query.= ' where id_uppercat';
227  if ( !is_numeric( $id ) )
228  {
229    $query.= ' is NULL';
230  }
231  else
232  {
233    $query.= ' = '.$id;
234  }
235  $query.= ";";
236  $result = mysql_query( $query );
237  while ( $row = mysql_fetch_array( $result ) )
238  {
239    if ( !in_array( $row['id'], $restriction ) )
240    {
241      $total+= $row['nb_images'];
242      $total+= get_total_image( $row['id'], $restriction );
243    }
244  }
245  return $total;
246}
247
248// variables :
249// $cat['comment']
250// $cat['dir']
251// $cat['last_dir']
252// $cat['name'] is an array :
253//      - $cat['name'][0] is the lowest cat name
254//      and
255//      - $cat['name'][n] is the most uppercat name findable
256// $cat['nb_images']
257// $cat['id_uppercat']
258// $cat['site_id']
259function get_cat_info( $id )
260{
261  $cat = array();
262  $cat['name'] = array();
263               
264  $query = 'select nb_images,id_uppercat,comment,site_id,galleries_url,dir';
265  $query.= ' from '.PREFIX_TABLE.'categories as a';
266  $query.= ', '.PREFIX_TABLE.'sites as b';
267  $query.= ' where a.id = '.$id;
268  $query.= ' and a.site_id = b.id;';
269  $row = mysql_fetch_array( mysql_query( $query ) );
270  $cat['site_id']     = $row['site_id'];
271  $cat['id_uppercat'] = $row['id_uppercat'];
272  $cat['comment']     = nl2br( $row['comment'] );
273  $cat['nb_images']   = $row['nb_images'];
274  $cat['last_dir']    = $row['dir'];
275  $galleries_url = $row['galleries_url'];
276               
277  $cat['dir'] = "";
278  $i = 0;
279  $is_root = false;
280  $row['id_uppercat'] = $id;
281  while ( !$is_root )
282  {
283    $query = 'select name,dir,id_uppercat';
284    $query.= ' from '.PREFIX_TABLE.'categories';
285    $query.= ' where id = '.$row['id_uppercat'].';';
286    $row = mysql_fetch_array( mysql_query( $query ) );
287    $cat['dir'] = $row['dir']."/".$cat['dir'];
288    if ( $row['name'] == "" )
289    {
290      $cat['name'][$i] = str_replace( "_", " ", $row['dir'] );
291    }
292    else
293    {
294      $cat['name'][$i] = $row['name'];
295    }
296    if ( $row['id_uppercat'] == "" )
297    {
298      $is_root = true;
299    }
300    $i++;
301  }
302  $cat['local_dir'] = substr( $cat['dir'], 0 , strlen( $cat['dir'] ) - 1 );
303  $cat['dir'] = $galleries_url.$cat['dir'];
304               
305  return $cat;
306}
307       
308// The function get_cat_display_name returns a string containing the list
309// of upper categories to the root category from the lowest category shown
310// example : "anniversaires - fete mere 2002 - animaux - erika"
311// You can give two parameters :
312//   - $separation : the string between each category name " - " for example
313//   - $style : the style of the span tag for the lowest category,
314//     "font-style:italic;" for example
315function get_cat_display_name( $array_cat_names, $separation, $style )
316{
317  $output = "";
318  for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- )
319  {
320    if ( $i != sizeof( $array_cat_names ) - 1 )
321    {
322      $output.= $separation;
323    }
324    if ( $i != 0 )
325    {
326      $output.= $array_cat_names[$i];
327    }
328    else
329    {
330      if ( $style != "" )
331      {
332        $output.= '<span style="'.$style.'">';
333      }
334      $output.= $array_cat_names[$i];
335      if ( $style != "" )
336      {
337        $output.= "</span>";
338      }
339    }
340  }
341  return replace_space( $output );
342}
343
344// initialize_category initializes ;-) the variables in relation
345// with category :
346// 1. calculation of the number of pictures in the category
347// 2. determination of the SQL query part to ask to find the right category
348//    $page['where'] is not the same if we are in
349//       - simple category
350//       - search result
351//       - favorites displaying
352//       - most visited pictures
353//       - best rated pictures
354//       - recent pictures
355// 3. determination of the title of the page
356// 4. creation of the navigation bar
357function initialize_category( $calling_page = 'category' )
358{
359  global $page,$lang,$user,$conf;
360 
361  if ( isset( $page['cat'] ) )
362  {
363    // $page['nb_image_page'] is the number of picture to display on this page
364    // By default, it is the same as the $user['nb_image_page']
365    $page['nb_image_page'] = $user['nb_image_page'];
366    // $url is used to create the navigation bar
367    $url = './category.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
368    // simple category
369    if ( is_numeric( $page['cat'] ) )
370    {
371      $result = get_cat_info( $page['cat'] );
372      $page['comment']       = $result['comment'];
373      $page['cat_dir']       = $result['dir'];
374      $page['cat_name']      = $result['name'];
375      $page['cat_nb_images'] = $result['nb_images'];
376      $page['cat_site_id']   = $result['site_id'];
377      $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' );
378      $page['where'] = ' WHERE cat_id = '.$page['cat'];
379    }
380    else
381    {
382      if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited'
383           or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' )
384      {
385        // we must not show pictures of a forbidden category
386        $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
387        foreach ( $restricted_cats as $restricted_cat ) {
388          $where_append.= ' AND cat_id != '.$restricted_cat;
389        }
390      }
391      // search result
392      if ( $page['cat'] == 'search' )
393      {
394        $page['title'] = $lang['search_result'];
395        if ( $calling_page == 'picture' )
396        {
397          $page['title'].= ' : <span style="font-style:italic;">';
398          $page['title'].= $_GET['search']."</span>";
399        }
400
401        $page['where'] = ' WHERE (';
402        $fields = array( 'file', 'name', 'comment' );
403        $words = explode( ',', $_GET['search'] );
404        $sql_search = array();
405        foreach ( $words as $i => $word ) {
406          // if the user searchs any of the words, the where statement must
407          // be :
408          // field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' ...
409          // OR field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' ...
410          if ( $_GET['mode'] == 'OR' )
411          {
412            if ( $i != 0 ) $page['where'].= ' OR';
413            foreach ( $fields as $j => $field ) {
414              if ( $j != 0 ) $page['where'].= ' OR';
415              $page['where'].= ' '.$field." LIKE '%".$word."%'";
416            }
417          }
418          // if the user searchs all the words :
419          // ( field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' )
420          // AND ( field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' )
421          else if ( $_GET['mode'] == 'AND' )
422          {
423            if ( $i != 0 ) $page['where'].= ' AND';
424            $page['where'].= ' (';
425            foreach ( $fields as $j => $field ) {
426              if ( $j != 0 ) $page['where'].= ' OR';
427              $page['where'].= ' '.$field." LIKE '%".$word."%'";
428            }
429            $page['where'].= ' )';
430          }
431        }
432        $page['where'].= ' )';
433        $page['where'].= $where_append;
434
435        $query = 'SELECT COUNT(*) AS nb_total_images';
436        $query.= ' FROM '.PREFIX_TABLE.'images';
437        $query.= $page['where'];
438        $query.= ';';
439
440        $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
441      }
442      // favorites displaying
443      else if ( $page['cat'] == 'fav' )
444      {
445        $page['title'] = $lang['favorites'];
446
447        $page['where'] = ', '.PREFIX_TABLE.'favorites';
448        $page['where'].= ' WHERE user_id = '.$user['id'];
449        $page['where'].= ' AND image_id = id';
450     
451        $query = 'SELECT COUNT(*) AS nb_total_images';
452        $query.= ' FROM '.PREFIX_TABLE.'favorites';
453        $query.= ' WHERE user_id = '.$user['id'];
454        $query.= ';';
455      }
456      // pictures within the short period
457      else if ( $page['cat'] == 'recent' )
458      {
459        $page['title'] = $lang['recent_cat_title'];
460        // We must find the date corresponding to :
461        // today - $conf['periode_courte']
462        $date = time() - 60*60*24*$user['short_period'];
463        $page['where'] = " WHERE date_available > '";
464        $page['where'].= date( 'Y-m-d', $date )."'";
465        $page['where'].= $where_append;
466
467        $query = 'SELECT COUNT(*) AS nb_total_images';
468        $query.= ' FROM '.PREFIX_TABLE.'images';
469        $query.= $page['where'];
470        $query.= ';';
471      }
472      // most visited pictures
473      else if ( $page['cat'] == 'most_visited' )
474      {
475        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
476        $page['where'] = ' WHERE cat_id != -1'.$where_append;
477        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
478        $page['cat_nb_images'] = $conf['top_number'];
479        if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] )
480        {
481          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
482        }
483      }
484     
485      if ( $query != '' )
486      {
487        $result = mysql_query( $query );
488        $row = mysql_fetch_array( $result );
489        $page['cat_nb_images'] = $row['nb_total_images'];
490      }
491    }
492    if ( $calling_page == 'category' )
493    {
494      $page['navigation_bar'] =
495        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
496                               $user['nb_image_page'], 'back' );
497    }
498  }
499  else
500  {
501    $page['title'] = $lang['diapo_default_page_title'];
502  }
503}
504
505// get_non_empty_sub_cat_ids returns an array composing of the infos of the
506// direct sub-categories of the given uppercat id. Each of these infos is
507// associated to the first found non empty category id. eg :
508//
509// - catname [cat_id]
510// - cat1 [1] -> given uppercat
511//   - cat1.1 [2] (empty)
512//     - cat1.1.1 [5] (empty)
513//     - cat1.1.2 [6]
514//   - cat1.2 [3]
515//   - cat1.3 [4]
516//
517// get_non_empty_sub_cat_ids will return :
518//   $cats[0]['id']            = 2;
519//   $cats[0]['name']          = '';
520//   $cats[0]['dir']           = 'cat1';
521//   $cats[0]['date_dernier']  = '2003-05-17';
522//   $cats[0]['non_empty_cat'] = 6;
523//
524//   $cats[1]['id']            = 3;
525//   $cats[1]['non_empty_cat'] = 3;
526//
527//   $cats[1]['id']            = 4;
528//   $cats[1]['non_empty_cat'] = 4;
529function get_non_empty_sub_cat_ids( $id_uppercat )
530{
531  global $user;
532
533  $cats = array();
534
535  $query = 'SELECT id,name,dir,date_dernier,nb_images';
536  $query.= ' FROM '.PREFIX_TABLE.'categories';
537  $query.= ' WHERE id_uppercat = '.$id_uppercat;
538  // we must not show pictures of a forbidden category
539  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
540  foreach ( $restricted_cats as $restricted_cat ) {
541    $query.= ' AND id != '.$restricted_cat;
542  }
543  $query.= ' ORDER BY rank';
544  $query.= ';';
545
546  $result = mysql_query( $query );
547  while ( $row = mysql_fetch_array( $result ) )
548  {
549    // only categories with findable picture in any of its subcats is
550    // represented.
551    if ( ( $row['nb_images'] != 0 and $non_empty_cat = $row['id'] )
552         or $non_empty_cat = get_first_non_empty_cat_id( $row['id'] ) )
553    {
554      $temp_cat = array(
555        'id'            => $row['id'],
556        'name'          => $row['name'],
557        'dir'           => $row['dir'],
558        'date_dernier'  => $row['date_dernier'],
559        'non_empty_cat' => $non_empty_cat );
560      array_push( $cats, $temp_cat );
561    }
562  }
563  return $cats;
564}
565
566// get_first_non_empty_cat_id returns the id of the first non empty
567// sub-category to the given uppercat. If no picture is found in any
568// subcategory, false is returned.
569function get_first_non_empty_cat_id( $id_uppercat )
570{
571  global $user;
572
573  $query = 'SELECT id,nb_images';
574  $query.= ' FROM '.PREFIX_TABLE.'categories';
575  $query.= ' WHERE id_uppercat = '.$id_uppercat;
576  // we must not show pictures of a forbidden category
577  $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
578  foreach ( $restricted_cats as $restricted_cat ) {
579    $query.= ' AND id != '.$restricted_cat;
580  }
581  $query.= ' ORDER BY RAND()';
582  $query.= ';';
583  $result = mysql_query( $query );
584  while ( $row = mysql_fetch_array( $result ) )
585  {
586    if ( $row['nb_images'] > 0 )
587    {
588      return $row['id'];
589    }
590  }
591  $result = mysql_query( $query );
592  while ( $row = mysql_fetch_array( $result ) )
593  {
594    // recursive call
595    if ( $subcat = get_first_non_empty_cat_id( $row['id'] ) )
596    {
597      return $subcat;
598    }
599  }
600  return false;
601}
602?>
Note: See TracBrowser for help on using the repository browser.