source: tags/release-1_3_1beta/include/functions_category.inc.php @ 16883

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

didn't take correctly into account forbidden categories

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