source: tags/release-1_3_3/include/functions_category.inc.php @ 20321

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

bug 26 : When categories tree has category ids not in ascending order, the order of display still uses ids order. Corrected using categories.uppercats order

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