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

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

deletion of expand string in $_GET array

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