source: branches/release-1_3/include/functions_category.inc.php @ 296

Last change on this file since 296 was 296, checked in by z0rglub, 20 years ago

Modify function get_cat_info for preventing Php Warning when NULL values
found in category description

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