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

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