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

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

Removing debug code

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