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

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

improve the header of each file

  • 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 <http://phpwebgallery.net>   *
6 *   author        : Pierrick LE GALL <pierrick@z0rglub.com>               *
7 *                                                                         *
8 *   $Id: functions_category.inc.php 57 2003-08-24 07:40:56Z 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
20function get_subcats_id( $cat_id )
21{
22  $restricted_cats = array();
23               
24  $query = 'SELECT id';
25  $query.= ' FROM '.PREFIX_TABLE.'categories';
26  $query.= ' WHERE id_uppercat = '.$cat_id;
27  $query.= ';';
28  $result = mysql_query( $query );
29  while ( $row = mysql_fetch_array( $result ) )
30  {
31    array_push( $restricted_cats, $row['id'] );
32    $sub_restricted_cats = get_subcats_id( $row['id'] );
33    foreach ( $sub_restricted_cats as $sub_restricted_cat ) {
34      array_push( $restricted_cats, $sub_restricted_cat );
35    }
36  }
37  return $restricted_cats;
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 ( isset( $page['plain_structure'] ) )
65    {
66      if ( isset( $page['plain_structure'][$cat] ) )
67      {
68        $page['cat'] = $cat;
69      }
70    }
71    else if ( is_numeric( $cat ) )
72    {
73      $query = 'SELECT id';
74      $query.= ' FROM '.PREFIX_TABLE.'categories';
75      $query.= ' WHERE id = '.$cat;
76      $query. ';';
77      $result = mysql_query( $query );
78      if ( mysql_num_rows( $result ) != 0 )
79      {
80        $page['cat'] = $cat;
81      }
82    }
83    if ( $cat == 'fav'
84         or $cat == 'search'
85         or $cat == 'most_visited'
86         or $cat == 'best_rated'
87         or $cat == 'recent' )
88    {
89      $page['cat'] = $cat;
90    }
91  }
92}
93
94function get_plain_structure()
95{
96  $infos = array( 'name','id','date_last','nb_images','dir','id_uppercat',
97                  'rank');
98 
99  $query = 'SELECT ';
100  foreach ( $infos as $i => $info ) {
101    if ( $i > 0 ) $query.= ',';
102    $query.= $info;
103  }
104  $query.= ' FROM '.PREFIX_TABLE.'categories';
105  $query.= ' ORDER BY id_uppercat ASC, rank ASC';
106  $query.= ';';
107
108  $plain_structure = array();
109  $result = mysql_query( $query );
110  while ( $row = mysql_fetch_array( $result ) )
111  {
112    $category = array();
113    foreach ( $infos as $info ) {
114      $category[$info] = $row[$info];
115      if ( $info == 'date_last' )
116      {
117        list($year,$month,$day) = explode( '-', $row[$info] );
118        $category[$info] = mktime(0,0,0,$month,$day,$year);
119      }
120    }
121    $plain_structure[$row['id']] = $category;
122  }
123
124  return $plain_structure;
125}
126
127function create_structure( $id_uppercat, $restrictions )
128{
129  global $page;
130
131  $structure = array();
132  $ids = get_subcat_ids( $id_uppercat );
133  foreach ( $ids as $id ) {
134    if ( !in_array( $id, $restrictions ) )
135    {
136      $category = $page['plain_structure'][$id];
137      $category['subcats'] = create_structure( $id, $restrictions );
138      array_push( $structure, $category );
139    }
140  }
141  return $structure;
142}
143
144function get_subcat_ids( $id_uppercat )
145{
146  global $page;
147
148  $ids = array();
149  foreach ( $page['plain_structure'] as $id => $category ) {
150    if ( $category['id_uppercat'] == $id_uppercat ) array_push( $ids, $id );
151    else if ( count( $ids ) > 0 )                   return $ids;
152  }
153  return $ids;
154}
155
156// update_structure updates or add informations about each node of the
157// structure : the last date, should the category be expanded in the menu ?,
158// the associated expand string "48,14,54"
159//
160// 1. last date
161// for each category of the structure, we have to find the most recent
162// subcat so that the parent cat has the same last_date info.
163// For example : we have :
164// > pets       (2003.02.15)
165//    > dogs    (2003.06.14)
166//       > rex  (2003.06.18)
167//       > toby (2003.06.13)
168//    > kitten  (2003.07.05)
169// We finally want to have :
170// > pets       (2003.07.05) <- changed to pets > kitten last date
171//    > dogs    (2003.06.18) <- changed to pets > dogs > rex last date
172//       > rex  (2003.06.18)
173//       > toby (2003.06.13)
174//    > kitten  (2003.07.05)
175//
176// 2. should the category be expanded in the menu ?
177// If the category has to be expanded (ie its id is in the
178// $page['tab_expand'] or all the categories must be expanded by default),
179// $category['expanded'] is set to true.
180//
181// 3. associated expand string
182// in the menu, there is a expand string (used in the URL) to tell which
183// categories must be expanded in the menu if this category is chosen
184function update_structure( $categories )
185{
186  global $page, $user;
187
188  $updated_categories = array();
189
190  foreach ( $categories as $category ) {
191    // update the last date of the category
192    $last_date = search_last_date( $category );
193    $category['date_last'] = $last_date;
194    // update the "expanded" key
195    if ( $user['expand']
196         or $page['expand'] == 'all'
197         or in_array( $category['id'], $page['tab_expand'] ) )
198    {
199      $category['expanded'] = true;
200    }
201    else
202    {
203      $category['expanded'] = false;
204    }
205    // update the  "expand_string" key
206    if ( $page['expand'] == 'all' )
207    {
208      $category['expand_string'] = 'all';
209    }
210    else
211    {
212      $tab_expand = $page['tab_expand'];
213      if ( in_array( $category['id'], $page['tab_expand'] ) )
214      {
215        // the expand string corresponds to the $page['tab_expand'] without
216        // the $category['id']
217        $tab_expand = array_diff( $page['tab_expand'],array($category['id']) );
218      }
219      else if ( count( $category['subcats'] ) > 0 )
220      {
221        // we have this time to add the $category['id']...
222        $tab_expand = array_merge($page['tab_expand'],array($category['id']));
223      }
224      $category['expand_string'] = implode( ',', $tab_expand );
225    }
226    // recursive call
227    $category['subcats'] = update_structure( $category['subcats'] );
228    // adding the updated category
229    array_push( $updated_categories, $category );
230  }
231
232  return $updated_categories;
233}
234
235// search_last_date searchs the last date for a given category. If we take
236// back the example given for update_last_dates, we should have :
237// search_last_date( pets )        --> 2003.07.05
238// search_last_date( pets > dogs ) --> 2003.06.18
239// and so on
240function search_last_date( $category )
241{
242  $date_last = $category['date_last'];
243  foreach ( $category['subcats'] as $subcat ) {
244    $subcat_date_last = search_last_date( $subcat );
245    if ( $subcat_date_last > $date_last )
246    {
247      $date_last = $subcat_date_last;
248    }
249  }
250  return $date_last;
251}
252
253// count_images returns the number of pictures contained in the given
254// category represented by an array, in this array, we have (among other
255// things) :
256// $category['nb_images'] -> number of pictures in this category
257// $category['subcats'] -> array of sub-categories
258// count_images goes to the deepest sub-category to find the total number of
259// pictures contained in the given given category
260function count_images( $categories )
261{
262  $total = 0;
263  foreach ( $categories as $category ) {
264    $total+= $category['nb_images'];
265    $total+= count_images( $category['subcats'] );
266  }
267  return $total;
268}
269
270// variables :
271// $cat['comment']
272// $cat['dir']
273// $cat['last_dir']
274// $cat['name'] is an array :
275//      - $cat['name'][0] is the lowest cat name
276//      and
277//      - $cat['name'][n] is the most uppercat name findable
278// $cat['nb_images']
279// $cat['id_uppercat']
280// $cat['site_id']
281function get_cat_info( $id )
282{
283  $cat = array();
284  $cat['name'] = array();
285               
286  $query = 'SELECT nb_images,id_uppercat,comment,site_id,galleries_url,dir';
287  $query.= ',date_last,uploadable';
288  $query.= ' FROM '.PREFIX_TABLE.'categories AS a';
289  $query.= ', '.PREFIX_TABLE.'sites AS b';
290  $query.= ' WHERE a.id = '.$id;
291  $query.= ' AND a.site_id = b.id;';
292  $row = mysql_fetch_array( mysql_query( $query ) );
293  $cat['site_id']     = $row['site_id'];
294  $cat['id_uppercat'] = $row['id_uppercat'];
295  $cat['comment']     = nl2br( $row['comment'] );
296  $cat['nb_images']   = $row['nb_images'];
297  $cat['last_dir']    = $row['dir'];
298  $cat['date_last']   = $row['date_last'];
299  $cat['uploadable']  = get_boolean( $row['uploadable'] );
300  $galleries_url = $row['galleries_url'];
301
302  $cat['dir'] = "";
303  $i = 0;
304  $is_root = false;
305  $row['id_uppercat'] = $id;
306  while ( !$is_root )
307  {
308    $query = 'SELECT name,dir,id_uppercat';
309    $query.= ' FROM '.PREFIX_TABLE.'categories';
310    $query.= ' WHERE id = '.$row['id_uppercat'].';';
311    $row = mysql_fetch_array( mysql_query( $query ) );
312    $cat['dir'] = $row['dir'].'/'.$cat['dir'];
313    if ( $row['name'] == "" )
314    {
315      $cat['name'][$i] = str_replace( "_", " ", $row['dir'] );
316    }
317    else
318    {
319      $cat['name'][$i] = $row['name'];
320    }
321    if ( $row['id_uppercat'] == "" )
322    {
323      $is_root = true;
324    }
325    $i++;
326  }
327  $cat['local_dir'] = substr( $cat['dir'], 0 , strlen( $cat['dir'] ) - 1 );
328  $cat['dir'] = $galleries_url.$cat['dir'];
329               
330  return $cat;
331}
332       
333// The function get_cat_display_name returns a string containing the list
334// of upper categories to the root category from the lowest category shown
335// example : "anniversaires - fete mere 2002 - animaux - erika"
336// You can give two parameters :
337//   - $separation : the string between each category name " - " for example
338//   - $style : the style of the span tag for the lowest category,
339//     "font-style:italic;" for example
340function get_cat_display_name( $array_cat_names, $separation, $style )
341{
342  $output = "";
343  for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- )
344  {
345    if ( $i != sizeof( $array_cat_names ) - 1 )
346    {
347      $output.= $separation;
348    }
349    if ( $i != 0 )
350    {
351      $output.= $array_cat_names[$i];
352    }
353    else
354    {
355      if ( $style != "" )
356      {
357        $output.= '<span style="'.$style.'">';
358      }
359      $output.= $array_cat_names[$i];
360      if ( $style != "" )
361      {
362        $output.= "</span>";
363      }
364    }
365  }
366  return replace_space( $output );
367}
368
369// initialize_category initializes ;-) the variables in relation
370// with category :
371// 1. calculation of the number of pictures in the category
372// 2. determination of the SQL query part to ask to find the right category
373//    $page['where'] is not the same if we are in
374//       - simple category
375//       - search result
376//       - favorites displaying
377//       - most visited pictures
378//       - best rated pictures
379//       - recent pictures
380// 3. determination of the title of the page
381// 4. creation of the navigation bar
382function initialize_category( $calling_page = 'category' )
383{
384  global $page,$lang,$user,$conf;
385
386  if ( isset( $page['cat'] ) )
387  {
388    // $page['nb_image_page'] is the number of picture to display on this page
389    // By default, it is the same as the $user['nb_image_page']
390    $page['nb_image_page'] = $user['nb_image_page'];
391    // $url is used to create the navigation bar
392    $url = './category.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
393    // simple category
394    if ( is_numeric( $page['cat'] ) )
395    {
396      $result = get_cat_info( $page['cat'] );
397      $page['comment']        = $result['comment'];
398      $page['cat_dir']        = $result['dir'];
399      $page['cat_name']       = $result['name'];
400      $page['cat_nb_images']  = $result['nb_images'];
401      $page['cat_site_id']    = $result['site_id'];
402      $page['cat_uploadable'] = $result['uploadable'];
403      $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' );
404      $page['where'] = ' WHERE cat_id = '.$page['cat'];
405    }
406    else
407    {
408      if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited'
409           or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' )
410      {
411        // we must not show pictures of a forbidden category
412        $restricted_cats = get_all_restrictions( $user['id'],$user['status'] );
413        foreach ( $restricted_cats as $restricted_cat ) {
414          $where_append.= ' AND cat_id != '.$restricted_cat;
415        }
416      }
417      // search result
418      if ( $page['cat'] == 'search' )
419      {
420        $page['title'] = $lang['search_result'];
421        if ( $calling_page == 'picture' )
422        {
423          $page['title'].= ' : <span style="font-style:italic;">';
424          $page['title'].= $_GET['search']."</span>";
425        }
426
427        $page['where'] = ' WHERE (';
428        $fields = array( 'file', 'name', 'comment', 'keywords' );
429        $words = explode( ',', $_GET['search'] );
430        $sql_search = array();
431        foreach ( $words as $i => $word ) {
432          // if the user searchs any of the words, the where statement must
433          // be :
434          // field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' ...
435          // OR field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' ...
436          if ( $_GET['mode'] == 'OR' )
437          {
438            if ( $i != 0 ) $page['where'].= ' OR';
439            foreach ( $fields as $j => $field ) {
440              if ( $j != 0 ) $page['where'].= ' OR';
441              $page['where'].= ' '.$field." LIKE '%".$word."%'";
442            }
443          }
444          // if the user searchs all the words :
445          // ( field1 LIKE '%$word1%' OR field2 LIKE '%$word1%' )
446          // AND ( field1 LIKE '%$word2%' OR field2 LIKE '%$word2%' )
447          else if ( $_GET['mode'] == 'AND' )
448          {
449            if ( $i != 0 ) $page['where'].= ' AND';
450            $page['where'].= ' (';
451            foreach ( $fields as $j => $field ) {
452              if ( $j != 0 ) $page['where'].= ' OR';
453              $page['where'].= ' '.$field." LIKE '%".$word."%'";
454            }
455            $page['where'].= ' )';
456          }
457        }
458        $page['where'].= ' )';
459        $page['where'].= $where_append;
460
461        $query = 'SELECT COUNT(*) AS nb_total_images';
462        $query.= ' FROM '.PREFIX_TABLE.'images';
463        $query.= $page['where'];
464        $query.= ';';
465
466        $url.= '&amp;search='.$_GET['search'].'&amp;mode='.$_GET['mode'];
467      }
468      // favorites displaying
469      else if ( $page['cat'] == 'fav' )
470      {
471        $page['title'] = $lang['favorites'];
472
473        $page['where'] = ', '.PREFIX_TABLE.'favorites';
474        $page['where'].= ' WHERE user_id = '.$user['id'];
475        $page['where'].= ' AND image_id = id';
476     
477        $query = 'SELECT COUNT(*) AS nb_total_images';
478        $query.= ' FROM '.PREFIX_TABLE.'favorites';
479        $query.= ' WHERE user_id = '.$user['id'];
480        $query.= ';';
481      }
482      // pictures within the short period
483      else if ( $page['cat'] == 'recent' )
484      {
485        $page['title'] = $lang['recent_cat_title'];
486        // We must find the date corresponding to :
487        // today - $conf['periode_courte']
488        $date = time() - 60*60*24*$user['short_period'];
489        $page['where'] = " WHERE date_available > '";
490        $page['where'].= date( 'Y-m-d', $date )."'";
491        $page['where'].= $where_append;
492
493        $query = 'SELECT COUNT(*) AS nb_total_images';
494        $query.= ' FROM '.PREFIX_TABLE.'images';
495        $query.= $page['where'];
496        $query.= ';';
497      }
498      // most visited pictures
499      else if ( $page['cat'] == 'most_visited' )
500      {
501        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
502        $page['where'] = ' WHERE cat_id != -1'.$where_append;
503        $conf['order_by'] = ' ORDER BY hit DESC, file ASC';
504        $page['cat_nb_images'] = $conf['top_number'];
505        if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] )
506        {
507          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
508        }
509      }
510     
511      if ( $query != '' )
512      {
513        $result = mysql_query( $query );
514        $row = mysql_fetch_array( $result );
515        $page['cat_nb_images'] = $row['nb_total_images'];
516      }
517    }
518    if ( $calling_page == 'category' )
519    {
520      $page['navigation_bar'] =
521        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
522                               $user['nb_image_page'], 'back' );
523    }
524  }
525  else
526  {
527    $page['title'] = $lang['diapo_default_page_title'];
528  }
529}
530
531// get_non_empty_subcat_ids returns an array with sub-categories id
532// associated with their first non empty category id.
533//
534//                          example :
535//
536// - catname [cat_id]
537// - cat1 [1] -> given uppercat
538//   - cat1.1 [12] (empty)
539//     - cat1.1.1 [5] (empty)
540//     - cat1.1.2 [6]
541//   - cat1.2 [3]
542//   - cat1.3 [4]
543//
544// get_non_empty_sub_cat_ids will return :
545//   $ids[12] = 6;
546//   $ids[3]  = 3;
547//   $ids[4]  = 4;
548function get_non_empty_subcat_ids( $id_uppercat )
549{
550  global $user;
551
552  $ids = array();
553
554  $query = 'SELECT id,nb_images';
555  $query.= ' FROM '.PREFIX_TABLE.'categories';
556  $query.= ' WHERE id_uppercat ';
557  if ( !is_numeric( $id_uppercat ) ) $query.= 'is NULL';
558  else                               $query.= '= '.$id_uppercat;
559  // we must not show pictures of a forbidden category
560  foreach ( $user['restrictions'] as $restricted_cat ) {
561    $query.= ' AND id != '.$restricted_cat;
562  }
563  $query.= ' ORDER BY rank';
564  $query.= ';';
565
566  $result = mysql_query( $query );
567  while ( $row = mysql_fetch_array( $result ) )
568  {
569    // only categories with findable picture in any of its subcats is
570    // represented.
571    if ( ( $row['nb_images'] != 0 and $non_empty_cat = $row['id'] )
572         or $non_empty_cat = get_first_non_empty_cat_id( $row['id'] ) )
573    {
574      $ids[$row['id']] = $non_empty_cat;
575    }
576  }
577  return $ids;
578}
579
580// get_first_non_empty_cat_id returns the id of the first non empty
581// sub-category to the given uppercat. If no picture is found in any
582// subcategory, false is returned.
583function get_first_non_empty_cat_id( $id_uppercat )
584{
585  global $user;
586
587  $query = 'SELECT id,nb_images';
588  $query.= ' FROM '.PREFIX_TABLE.'categories';
589  $query.= ' WHERE id_uppercat = '.$id_uppercat;
590  // we must not show pictures of a forbidden category
591  foreach ( $user['restrictions'] as $restricted_cat ) {
592    $query.= ' AND id != '.$restricted_cat;
593  }
594  $query.= ' ORDER BY RAND()';
595  $query.= ';';
596  $result = mysql_query( $query );
597  while ( $row = mysql_fetch_array( $result ) )
598  {
599    if ( $row['nb_images'] > 0 )
600    {
601      return $row['id'];
602    }
603  }
604  $result = mysql_query( $query );
605  while ( $row = mysql_fetch_array( $result ) )
606  {
607    // recursive call
608    if ( $subcat = get_first_non_empty_cat_id( $row['id'] ) )
609    {
610      return $subcat;
611    }
612  }
613  return false;
614}
615?>
Note: See TracBrowser for help on using the repository browser.