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

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

deletion empty line out of PHP bounds

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