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

Last change on this file since 9 was 9, checked in by z0rglub, 21 years ago

2003.05.13 user_add and user_modify added

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.3 KB
Line 
1<?php
2/***************************************************************************
3 *                         functions_category.inc.php                      *
4 *                            --------------------                         *
5 *   application          : PhpWebGallery 1.3                              *
6 *   author               : Pierrick LE GALL <pierrick@z0rglub.com>        *
7 *                                                                         *
8 ***************************************************************************
9
10 ***************************************************************************
11 *                                                                         *
12 *   This program is free software; you can redistribute it and/or modify  *
13 *   it under the terms of the GNU General Public License as published by  *
14 *   the Free Software Foundation;                                         *
15 *                                                                         *
16 ***************************************************************************/
17function get_subcats_id( $cat_id )
18{
19  global $prefixeTable;
20               
21  $restricted_cat = array();
22  $i = 0;
23               
24  $query = "select id";
25  $query.= " from $prefixeTable"."categories";
26  $query.= " where id_uppercat = $cat_id;";
27  $result = mysql_query( $query );
28  while ( $row = mysql_fetch_array( $result ) )
29  {
30    $restricted_cat[$i++] = $row['id'];
31    $sub_restricted_cat = get_subcats_id( $row['id'] );
32    for ( $j = 0; $j < sizeof( $sub_restricted_cat ); $j++ )
33    {
34      $restricted_cat[$i++] = $sub_restricted_cat[$j];
35    }
36  }
37               
38  return $restricted_cat;
39}
40
41function check_restrictions( $category_id )
42{
43  global $user,$lang,$prefixeTable;
44
45  if ( is_user_allowed( $category_id, $user['restrictions'] ) > 0 )
46  {
47    echo '<div style="text-align:center;">'.$lang['access_forbiden'].'<br />';
48    echo '<a href="'.add_session_id( './category.php' ).'">';
49    echo $lang['thumbnails'].'</a></div>';
50    exit();
51  }
52}
53       
54// the check_cat_id function check whether the $cat is a right parameter :
55//  - $cat is numeric and corresponds to a category in the database
56//  - $cat equals 'fav' (for favorites)
57//  - $cat equals 'search' (when the result of a search is displayed)
58function check_cat_id( $cat )
59{
60  global $page,$prefixeTable;
61  unset( $page['cat'] );
62  if ( isset( $cat ) )
63  {
64    if ( is_numeric( $cat ) )
65    {
66      $query = "select id from $prefixeTable"."categories where id = $cat;";
67      $result = mysql_query( $query );
68      if ( mysql_num_rows( $result ) != 0 )
69      {
70        $page['cat'] = $cat;
71      }
72    }
73    if ( $cat == 'fav' or $cat == 'search' or $cat == 'most_visited'
74         or $cat == 'best_rated' or $cat == 'recent' )
75    {
76      $page['cat'] = $cat;
77    }
78  }
79}
80
81function display_cat( $id_uppercat, $indent, $restriction, $tab_expand )
82{
83  global $prefixeTable,$user,$lang,$conf,$page,$vtp,$handle;
84 
85  $query = 'select name,id,date_dernier,nb_images,dir';
86  $query.= ' from '.$prefixeTable.'categories';
87  $query.= ' where id_uppercat';
88  if ( $id_uppercat == "" )
89  {
90    $query.= ' is NULL';
91  }
92  else
93  {
94    $query.= ' = '.$id_uppercat;
95  }
96  $query.= ' order by rank asc;';
97  $result = mysql_query( $query );
98  while ( $row = mysql_fetch_array( $result ) )
99  {
100    if ( !in_array( $row['id'], $restriction ) )
101    {
102      $nb_subcats = get_nb_subcats( $row['id'] );
103                               
104      $expand = "";
105      // si la catégorie n'a pas de sous catégorie
106      // ou que l'on doit développer toutes les catégories par défaut
107      // alors on utilise l'expand par défaut
108      if ( $nb_subcats == 0 or $user['expand'] == "true" )
109      {
110        $expand = $page['expand'];
111      }
112      // si la catégorie n'est pas dans les catégories à développer
113      // alors on l'ajoute aux catégories à développer
114      else if ( !in_array( $row['id'], $tab_expand ) )
115      {
116        $expand = implode( ",", $tab_expand );
117        if ( strlen( $expand ) > 0 )
118        {
119          $expand.= ",";
120        }
121        $expand.= $row['id'];
122      }
123      // si la catégorie est déjà dans les catégories à développer
124      // alors on la retire des catégories à développer
125      else
126      {
127        $expand = array_remove( $tab_expand, $row['id'] );
128      }
129      $url = "./category.php?cat=".$page['cat']."&amp;expand=$expand";
130      if ( $page['cat'] == 'search' )
131      {
132        $url.= "&amp;search=".$_GET['search'];
133      }
134      $lien_cat = add_session_id( $url );
135      if ( $row['name'] == "" )
136      {
137        $name = str_replace( "_", " ", $row['dir'] );
138      }
139      else
140      {
141        $name = $row['name'];
142      }
143
144      $vtp->addSession( $handle, 'category' );
145      $vtp->setVar( $handle, 'category.indent', $indent );
146
147      if ( $user['expand'] == "true" or $nb_subcats == 0 )
148      {
149        $vtp->addSession( $handle, 'bullet_wo_link' );
150        $vtp->setVar( $handle, 'bullet_wo_link.bullet_url',
151                      $user['lien_collapsed'] );
152        $vtp->setVar( $handle, 'bullet_wo_link.bullet_alt', '&gt;' );
153        $vtp->closeSession( $handle, 'bullet_wo_link' );
154      }
155      else
156      {
157        $vtp->addSession( $handle, 'bullet_w_link' );
158        $vtp->setVar( $handle, 'bullet_w_link.bullet_link', $lien_cat );
159        $vtp->setVar( $handle, 'bullet_w_link.bullet_alt', '&gt;' );
160        if ( in_array( $row['id'], $tab_expand ) )
161        {
162          $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
163                        $user['lien_expanded'] );
164        }
165        else
166        {
167          $vtp->setVar( $handle, 'bullet_w_link.bullet_url',
168                        $user['lien_collapsed'] );
169        }
170        $vtp->closeSession( $handle, 'bullet_w_link' );
171      }
172      $vtp->setVar( $handle, 'category.link_url',
173                    add_session_id( './category.php?cat='.
174                                    $row['id'].'&amp;expand='.$expand ) );
175      $vtp->setVar( $handle, 'category.link_name', $name );
176      if ( $id_uppercat == "" )
177      {
178        $vtp->setVar( $handle, 'category.name_style', 'font-weight:bold;' );
179      }
180      if ( $nb_subcats > 0 )
181      {
182        $vtp->addSession( $handle, 'subcat' );
183        $vtp->setVar( $handle, 'subcat.nb_subcats', $nb_subcats );
184        $vtp->closeSession( $handle, 'subcat' );
185      }
186      $vtp->setVar( $handle, 'category.total_cat', $row['nb_images'] );
187      $date_dispo = explode( "-", $row['date_dernier'] );
188      $date_cat = mktime( 0, 0, 0, $date_dispo[1], $date_dispo[2],
189                          $date_dispo[0] );
190      $vtp->setVar( $handle, 'category.cat_icon', get_icon( $date_cat ) );
191      $vtp->closeSession( $handle, 'category' );
192
193      if ( in_array( $row['id'], $tab_expand ) or $user['expand'] == "true" )
194      {
195        display_cat( $row['id'], $indent.'&nbsp;&nbsp;&nbsp;&nbsp;',
196                     $restriction, $tab_expand );
197      }
198    }
199  }
200}
201       
202function get_nb_subcats( $id )
203{
204  global $prefixeTable,$user;
205               
206  $query = 'select count(*) as count';
207  $query.= ' from '.$prefixeTable.'categories';
208  $query.= ' where id_uppercat = '.$id;
209  for ( $i = 0; $i < sizeof( $user['restrictions'] ); $i++ )
210  {
211    $query.= " and id != ".$user['restrictions'][$i];
212  }
213  $query.= ';';
214  $result = mysql_query( $query );
215  $row = mysql_fetch_array( $result );
216  return $row['count'];
217}
218       
219function get_total_image( $id, $restriction )
220{
221  global $prefixeTable;
222               
223  $total = 0;
224               
225  $query = 'select id,nb_images';
226  $query.= ' from '.$prefixeTable.'categories';
227  $query.= ' where id_uppercat';
228  if ( !is_numeric( $id ) )
229  {
230    $query.= ' is NULL';
231  }
232  else
233  {
234    $query.= ' = '.$id;
235  }
236  $query.= ";";
237  $result = mysql_query( $query );
238  while ( $row = mysql_fetch_array( $result ) )
239  {
240    if ( !in_array( $row['id'], $restriction ) )
241    {
242      $total+= $row['nb_images'];
243      $total+= get_total_image( $row['id'], $restriction );
244    }
245  }
246  return $total;
247}
248
249// variables :
250// $cat['comment']
251// $cat['dir']
252// $cat['last_dir']
253// $cat['name'] is an array :
254//      - $cat['name'][0] is the lowest cat name
255//      and
256//      - $cat['name'][n] is the most uppercat name findable
257// $cat['nb_images']
258// $cat['id_uppercat']
259// $cat['site_id']
260function get_cat_info( $id )
261{
262  global $prefixeTable;
263               
264  $cat = array();
265  $cat['name'] = array();
266               
267  $query = 'select nb_images,id_uppercat,comment,site_id,galleries_url,dir';
268  $query.= ' from '.$prefixeTable.'categories as a';
269  $query.= ', '.$prefixeTable.'sites as b';
270  $query.= ' where a.id = '.$id;
271  $query.= ' and a.site_id = b.id;';
272  $row = mysql_fetch_array( mysql_query( $query ) );
273  $cat['site_id']     = $row['site_id'];
274  $cat['id_uppercat'] = $row['id_uppercat'];
275  $cat['comment']     = nl2br( $row['comment'] );
276  $cat['nb_images']   = $row['nb_images'];
277  $cat['last_dir']    = $row['dir'];
278  $galleries_url = $row['galleries_url'];
279               
280  $cat['dir'] = "";
281  $i = 0;
282  $is_root = false;
283  $row['id_uppercat'] = $id;
284  while ( !$is_root )
285  {
286    $query = 'select name,dir,id_uppercat';
287    $query.= ' from '.$prefixeTable.'categories';
288    $query.= ' where id = '.$row['id_uppercat'].';';
289    $row = mysql_fetch_array( mysql_query( $query ) );
290    $cat['dir'] = $row['dir']."/".$cat['dir'];
291    if ( $row['name'] == "" )
292    {
293      $cat['name'][$i] = str_replace( "_", " ", $row['dir'] );
294    }
295    else
296    {
297      $cat['name'][$i] = $row['name'];
298    }
299    if ( $row['id_uppercat'] == "" )
300    {
301      $is_root = true;
302    }
303    $i++;
304  }
305  $cat['local_dir'] = substr( $cat['dir'], 0 , strlen( $cat['dir'] ) - 1 );
306  $cat['dir'] = $galleries_url.$cat['dir'];
307               
308  return $cat;
309}
310       
311// The function get_cat_display_name returns a string containing the list
312// of upper categories to the root category from the lowest category shown
313// example : "anniversaires - fete mere 2002 - animaux - erika"
314// You can give two parameters :
315//   - $separation : the string between each category name " - " for example
316//   - $style : the style of the span tag for the lowest category,
317//     "font-style:italic;" for example
318function get_cat_display_name( $array_cat_names, $separation, $style )
319{
320  $output = "";
321  for ( $i = sizeof( $array_cat_names ) - 1; $i >= 0; $i-- )
322  {
323    if ( $i != sizeof( $array_cat_names ) - 1 )
324    {
325      $output.= $separation;
326    }
327    if ( $i != 0 )
328    {
329      $output.= $array_cat_names[$i];
330    }
331    else
332    {
333      if ( $style != "" )
334      {
335        $output.= '<span style="'.$style.'">';
336      }
337      $output.= $array_cat_names[$i];
338      if ( $style != "" )
339      {
340        $output.= "</span>";
341      }
342    }
343  }
344  return replace_space( $output );
345}
346
347// initialize_category initializes ;-) the variables in relation
348// with category :
349// 1. calculation of the number of pictures in the category
350// 2. determination of the SQL query part to ask to find the right category
351//    $page['where'] is not the same if we are in
352//       - simple category
353//       - search result
354//       - favorites displaying
355//       - most visited pictures
356//       - best rated pictures
357//       - recent pictures
358// 3. determination of the title of the page
359// 4. creation of the navigation bar
360function initialize_category( $calling_page = 'category' )
361{
362  global $prefixeTable,$page,$lang,$user,$conf;
363 
364  if ( isset( $page['cat'] ) )
365  {
366    // $page['nb_image_page'] is the number of picture to display on this page
367    // By default, it is the same as the $user['nb_image_page']
368    $page['nb_image_page'] = $user['nb_image_page'];
369    // $url is used to create the navigation bar
370    $url = './category.php?cat='.$page['cat'].'&amp;expand='.$page['expand'];
371    // simple category
372    if ( is_numeric( $page['cat'] ) )
373    {
374      $result = get_cat_info( $page['cat'] );
375      $page['comment']       = $result['comment'];
376      $page['cat_dir']       = $result['dir'];
377      $page['cat_name']      = $result['name'];
378      $page['cat_nb_images'] = $result['nb_images'];
379      $page['cat_site_id']   = $result['site_id'];
380      $page['title'] = get_cat_display_name( $page['cat_name'], ' - ', '' );
381      $page['where'] = ' where cat_id = '.$page['cat'];
382    }
383    else
384    {
385      $query = '';
386      // search result
387      if ( $page['cat'] == 'search' )
388      {
389        $page['title'] = $lang['search_result'];
390        if ( $calling_page == 'picture' )
391        {
392          $page['title'].= ' : <span style="font-style:italic;">';
393          $page['title'].= $_GET['search']."</span>";
394        }
395        $page['where'] = " where ( file like '%".$_GET['search']."%'";
396        $page['where'].= " or name like '%".$_GET['search']."%'";
397        $page['where'].= " or comment like '%".$_GET['search']."%' )";
398
399        $query = 'select count(*) as nb_total_images';
400        $query.= ' from '.$prefixeTable.'images';
401        $query.= $page['where'];
402        $query.= ';';
403
404        $url.= '&amp;search='.$_GET['search'];
405      }
406      // favorites displaying
407      else if ( $page['cat'] == 'fav' )
408      {
409        $page['title'] = $lang['favorites'];
410
411        $page['where'] = ', '.$prefixeTable.'favorites';
412        $page['where'].= ' where user_id = '.$user['id'];
413        $page['where'].= ' and image_id = id';
414     
415        $query = 'select count(*) as nb_total_images';
416        $query.= ' from '.$prefixeTable.'favorites';
417        $query.= ' where user_id = '.$user['id'];
418        $query.= ';';
419      }
420      // pictures within the short period
421      else if ( $page['cat'] == 'recent' )
422      {
423        $page['title'] = $lang['recent_cat_title'];
424        // We must find the date corresponding to :
425        // today - $conf['periode_courte']
426        $date = time() - 60*60*24*$user['short_period'];
427        $page['where'] = " where date_available > '";
428        $page['where'].= date( 'Y-m-d', $date )."'";
429
430        $query = 'select count(*) as nb_total_images';
431        $query.= ' from '.$prefixeTable.'images';
432        $query.= $page['where'];
433        $query.= ';';
434      }
435      // most visited pictures
436      else if ( $page['cat'] == 'most_visited' )
437      {
438        $page['title'] = $conf['top_number'].' '.$lang['most_visited_cat'];
439        $page['where'] = ' where cat_id != -1';
440        $conf['order_by'] = ' order by hit desc, file asc';
441        $page['cat_nb_images'] = $conf['top_number'];
442        if ( $page['start'] + $user['nb_image_page'] >= $conf['top_number'] )
443        {
444          $page['nb_image_page'] = $conf['top_number'] - $page['start'];
445        }
446      }
447     
448      if ( $query != '' )
449      {
450        $result = mysql_query( $query );
451        $row = mysql_fetch_array( $result );
452        $page['cat_nb_images'] = $row['nb_total_images'];
453      }
454     
455      if ( $page['cat'] == 'search' or $page['cat'] == 'most_visited'
456           or $page['cat'] == 'recent' or $page['cat'] == 'best_rated' )
457      {
458        // we must not show pictures of a forbidden category
459        $restricted_cat = get_all_restrictions( $user['id'], $user['status'] );
460        if ( sizeof( $restricted_cat ) > 0 )
461        {
462          for ( $i = 0; $i < sizeof( $restricted_cat ); $i++ )
463          {
464            $page['where'].= ' and cat_id != '.$restricted_cat[$i];
465          }
466        }
467      }
468    }
469    if ( $calling_page == 'category' )
470    {
471      $page['navigation_bar'] =
472        create_navigation_bar( $url, $page['cat_nb_images'], $page['start'],
473                               $user['nb_image_page'], 'back' );
474    }
475  }
476  else
477  {
478    $page['title'] = $lang['diapo_default_page_title'];
479  }
480}
481?>
Note: See TracBrowser for help on using the repository browser.