source: branches/branch-1_6/include/functions_category.inc.php @ 1287

Last change on this file since 1287 was 1287, checked in by rvelices, 18 years ago

bug 349: Nicer display messages instead of "die" when urls cannot be
solved (also set 404 status code for bots)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.2 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2005 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $Id: functions_category.inc.php 1287 2006-04-28 05:11:46Z rvelices $
9// | last update   : $Date: 2006-04-28 05:11:46 +0000 (Fri, 28 Apr 2006) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 1287 $
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;
46
47  if (in_array($category_id, explode(',', $user['forbidden_categories'])))
48  {
49    access_denied();
50  }
51}
52
53function get_categories_menu()
54{
55  global $page,$user;
56
57  $infos = array('');
58
59  $query = '
60SELECT name,id,date_last,nb_images,global_rank
61  FROM '.CATEGORIES_TABLE.'
62  WHERE 1 = 1'; // stupid but permit using AND after it !
63  if (!$user['expand'])
64  {
65    $query.= '
66    AND (id_uppercat is NULL';
67    if (isset($page['category']))
68    {
69      $query.= ' OR id_uppercat IN ('.$page['uppercats'].')';
70    }
71    $query.= ')';
72  }
73  if ($user['forbidden_categories'] != '')
74  {
75    $query.= '
76    AND id NOT IN ('.$user['forbidden_categories'].')';
77  }
78  $query.= '
79;';
80
81  $result = pwg_query($query);
82  $cats = array();
83  while ($row = mysql_fetch_array($result))
84  {
85    array_push($cats, $row);
86  }
87  usort($cats, 'global_rank_compare');
88
89  return get_html_menu_category($cats);
90}
91
92/**
93 * Retrieve informations about a category in the database
94 *
95 * Returns an array with following keys :
96 *
97 *  - comment
98 *  - dir : directory, might be empty for virtual categories
99 *  - name : an array with indexes from 0 (lowest cat name) to n (most
100 *           uppercat name findable)
101 *  - nb_images
102 *  - id_uppercat
103 *  - site_id
104 *  -
105 *
106 * @param int category id
107 * @return array
108 */
109function get_cat_info( $id )
110{
111  $infos = array('nb_images','id_uppercat','comment','site_id'
112                 ,'dir','date_last','uploadable','status','visible'
113                 ,'representative_picture_id','uppercats','commentable');
114
115  $query = '
116SELECT '.implode(',', $infos).'
117  FROM '.CATEGORIES_TABLE.'
118  WHERE id = '.$id.'
119;';
120  $row = mysql_fetch_array(pwg_query($query));
121  if (empty($row))
122    return null;
123
124  $cat = array();
125  foreach ($infos as $info)
126  {
127    if (isset($row[$info]))
128    {
129      $cat[$info] = $row[$info];
130    }
131    else
132    {
133      $cat[$info] = '';
134    }
135    // If the field is true or false, the variable is transformed into a
136    // boolean value.
137    if ($cat[$info] == 'true' or $cat[$info] == 'false')
138    {
139      $cat[$info] = get_boolean( $cat[$info] );
140    }
141  }
142  global $conf;
143  if ( !( $conf['allow_html_descriptions'] and
144          preg_match('/<(div|br|img).*>/i', $cat['comment']) ) )
145  {
146    $cat['comment'] = nl2br($cat['comment']);
147  }
148
149  $names = array();
150  $query = '
151SELECT name,id
152  FROM '.CATEGORIES_TABLE.'
153  WHERE id IN ('.$cat['uppercats'].')
154;';
155  $result = pwg_query($query);
156  while($row = mysql_fetch_array($result))
157  {
158    $names[$row['id']] = $row['name'];
159  }
160
161  // category names must be in the same order than uppercats list
162  $cat['name'] = array();
163  foreach (explode(',', $cat['uppercats']) as $cat_id)
164  {
165    $cat['name'][$cat_id] = $names[$cat_id];
166  }
167
168  return $cat;
169}
170
171// get_complete_dir returns the concatenation of get_site_url and
172// get_local_dir
173// Example : "pets > rex > 1_year_old" is on the the same site as the
174// PhpWebGallery files and this category has 22 for identifier
175// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
176function get_complete_dir( $category_id )
177{
178  return get_site_url($category_id).get_local_dir($category_id);
179}
180
181// get_local_dir returns an array with complete path without the site url
182// Example : "pets > rex > 1_year_old" is on the the same site as the
183// PhpWebGallery files and this category has 22 for identifier
184// get_local_dir(22) returns "pets/rex/1_year_old/"
185function get_local_dir( $category_id )
186{
187  global $page;
188
189  $uppercats = '';
190  $local_dir = '';
191
192  if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
193  {
194    $uppercats = $page['plain_structure'][$category_id]['uppercats'];
195  }
196  else
197  {
198    $query = 'SELECT uppercats';
199    $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
200    $query.= ';';
201    $row = mysql_fetch_array( pwg_query( $query ) );
202    $uppercats = $row['uppercats'];
203  }
204
205  $upper_array = explode( ',', $uppercats );
206
207  $database_dirs = array();
208  $query = 'SELECT id,dir';
209  $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
210  $query.= ';';
211  $result = pwg_query( $query );
212  while( $row = mysql_fetch_array( $result ) )
213  {
214    $database_dirs[$row['id']] = $row['dir'];
215  }
216  foreach ($upper_array as $id)
217  {
218    $local_dir.= $database_dirs[$id].'/';
219  }
220
221  return $local_dir;
222}
223
224// retrieving the site url : "http://domain.com/gallery/" or
225// simply "./galleries/"
226function get_site_url($category_id)
227{
228  global $page;
229
230  $query = '
231SELECT galleries_url
232  FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
233  WHERE s.id = c.site_id
234    AND c.id = '.$category_id.'
235;';
236  $row = mysql_fetch_array(pwg_query($query));
237  return $row['galleries_url'];
238}
239
240// returns an array of image orders available for users/visitors
241function get_category_preferred_image_orders()
242{
243  global $conf;
244  return array(
245    array(l10n('default_sort'), '', true),
246    array(l10n('Average rate'), 'average_rate DESC', $conf['rate']),
247    array(l10n('most_visited_cat'), 'hit DESC', true),
248    array(l10n('Creation date'), 'date_creation DESC', true),
249    array(l10n('Post date'), 'date_available DESC', true),
250    array(l10n('File name'), 'file ASC', true)
251  );
252}
253
254function display_select_categories($categories,
255                                   $selecteds,
256                                   $blockname,
257                                   $fullname = true)
258{
259  global $template;
260
261  foreach ($categories as $category)
262  {
263    $selected = '';
264    if (in_array($category['id'], $selecteds))
265    {
266      $selected = ' selected="selected"';
267    }
268
269    if ($fullname)
270    {
271      $option = get_cat_display_name_cache($category['uppercats'],
272                                           null,
273                                           false);
274    }
275    else
276    {
277      $option = str_repeat('&nbsp;',
278                           (3 * substr_count($category['global_rank'], '.')));
279      $option.= '- '.$category['name'];
280    }
281
282    $template->assign_block_vars(
283      $blockname,
284      array('SELECTED'=>$selected,
285            'VALUE'=>$category['id'],
286            'OPTION'=>$option
287        ));
288  }
289}
290
291function display_select_cat_wrapper($query, $selecteds, $blockname,
292                                    $fullname = true)
293{
294  $result = pwg_query($query);
295  $categories = array();
296  if (!empty($result))
297  {
298    while ($row = mysql_fetch_array($result))
299    {
300      array_push($categories, $row);
301    }
302  }
303  usort($categories, 'global_rank_compare');
304  display_select_categories($categories, $selecteds, $blockname, $fullname);
305}
306
307/**
308 * returns all subcategory identifiers of given category ids
309 *
310 * @param array ids
311 * @return array
312 */
313function get_subcat_ids($ids)
314{
315  $query = '
316SELECT DISTINCT(id)
317  FROM '.CATEGORIES_TABLE.'
318  WHERE ';
319  foreach ($ids as $num => $category_id)
320  {
321    if ($num > 0)
322    {
323      $query.= '
324    OR ';
325    }
326    $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'';
327  }
328  $query.= '
329;';
330  $result = pwg_query($query);
331
332  $subcats = array();
333  while ($row = mysql_fetch_array($result))
334  {
335    array_push($subcats, $row['id']);
336  }
337  return $subcats;
338}
339
340function global_rank_compare($a, $b)
341{
342  return strnatcasecmp($a['global_rank'], $b['global_rank']);
343}
344
345function rank_compare($a, $b)
346{
347  if ($a['rank'] == $b['rank'])
348  {
349    return 0;
350  }
351
352  return ($a['rank'] < $b['rank']) ? -1 : 1;
353}
354?>
Note: See TracBrowser for help on using the repository browser.