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 1113 2006-03-30 00:37:07Z rvelices $ |
---|
9 | // | last update : $Date: 2006-03-30 00:37:07 +0000 (Thu, 30 Mar 2006) $ |
---|
10 | // | last modifier : $Author: rvelices $ |
---|
11 | // | revision : $Revision: 1113 $ |
---|
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 | */ |
---|
43 | function 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 | |
---|
53 | function get_categories_menu() |
---|
54 | { |
---|
55 | global $page,$user; |
---|
56 | |
---|
57 | $infos = array(''); |
---|
58 | |
---|
59 | $query = ' |
---|
60 | SELECT 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 | */ |
---|
109 | function 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 = ' |
---|
116 | SELECT '.implode(',', $infos).' |
---|
117 | FROM '.CATEGORIES_TABLE.' |
---|
118 | WHERE id = '.$id.' |
---|
119 | ;'; |
---|
120 | $row = mysql_fetch_array(pwg_query($query)); |
---|
121 | |
---|
122 | $cat = array(); |
---|
123 | foreach ($infos as $info) |
---|
124 | { |
---|
125 | if (isset($row[$info])) |
---|
126 | { |
---|
127 | $cat[$info] = $row[$info]; |
---|
128 | } |
---|
129 | else |
---|
130 | { |
---|
131 | $cat[$info] = ''; |
---|
132 | } |
---|
133 | // If the field is true or false, the variable is transformed into a |
---|
134 | // boolean value. |
---|
135 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
136 | { |
---|
137 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
138 | } |
---|
139 | } |
---|
140 | global $conf; |
---|
141 | if ( !( $conf['allow_html_descriptions'] and |
---|
142 | preg_match('/<(div|br|img).*>/i', $cat['comment']) ) ) |
---|
143 | { |
---|
144 | $cat['comment'] = nl2br($cat['comment']); |
---|
145 | } |
---|
146 | |
---|
147 | $names = array(); |
---|
148 | $query = ' |
---|
149 | SELECT name,id |
---|
150 | FROM '.CATEGORIES_TABLE.' |
---|
151 | WHERE id IN ('.$cat['uppercats'].') |
---|
152 | ;'; |
---|
153 | $result = pwg_query($query); |
---|
154 | while($row = mysql_fetch_array($result)) |
---|
155 | { |
---|
156 | $names[$row['id']] = $row['name']; |
---|
157 | } |
---|
158 | |
---|
159 | // category names must be in the same order than uppercats list |
---|
160 | $cat['name'] = array(); |
---|
161 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
162 | { |
---|
163 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
164 | } |
---|
165 | |
---|
166 | return $cat; |
---|
167 | } |
---|
168 | |
---|
169 | // get_complete_dir returns the concatenation of get_site_url and |
---|
170 | // get_local_dir |
---|
171 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
172 | // PhpWebGallery files and this category has 22 for identifier |
---|
173 | // get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
174 | function get_complete_dir( $category_id ) |
---|
175 | { |
---|
176 | return get_site_url($category_id).get_local_dir($category_id); |
---|
177 | } |
---|
178 | |
---|
179 | // get_local_dir returns an array with complete path without the site url |
---|
180 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
181 | // PhpWebGallery files and this category has 22 for identifier |
---|
182 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
183 | function get_local_dir( $category_id ) |
---|
184 | { |
---|
185 | global $page; |
---|
186 | |
---|
187 | $uppercats = ''; |
---|
188 | $local_dir = ''; |
---|
189 | |
---|
190 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
191 | { |
---|
192 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
193 | } |
---|
194 | else |
---|
195 | { |
---|
196 | $query = 'SELECT uppercats'; |
---|
197 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
198 | $query.= ';'; |
---|
199 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
200 | $uppercats = $row['uppercats']; |
---|
201 | } |
---|
202 | |
---|
203 | $upper_array = explode( ',', $uppercats ); |
---|
204 | |
---|
205 | $database_dirs = array(); |
---|
206 | $query = 'SELECT id,dir'; |
---|
207 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
208 | $query.= ';'; |
---|
209 | $result = pwg_query( $query ); |
---|
210 | while( $row = mysql_fetch_array( $result ) ) |
---|
211 | { |
---|
212 | $database_dirs[$row['id']] = $row['dir']; |
---|
213 | } |
---|
214 | foreach ($upper_array as $id) |
---|
215 | { |
---|
216 | $local_dir.= $database_dirs[$id].'/'; |
---|
217 | } |
---|
218 | |
---|
219 | return $local_dir; |
---|
220 | } |
---|
221 | |
---|
222 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
223 | // simply "./galleries/" |
---|
224 | function get_site_url($category_id) |
---|
225 | { |
---|
226 | global $page; |
---|
227 | |
---|
228 | $query = ' |
---|
229 | SELECT galleries_url |
---|
230 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
231 | WHERE s.id = c.site_id |
---|
232 | AND c.id = '.$category_id.' |
---|
233 | ;'; |
---|
234 | $row = mysql_fetch_array(pwg_query($query)); |
---|
235 | return $row['galleries_url']; |
---|
236 | } |
---|
237 | |
---|
238 | // returns an array of image orders available for users/visitors |
---|
239 | function get_category_preferred_image_orders() |
---|
240 | { |
---|
241 | global $conf; |
---|
242 | return array( |
---|
243 | array(l10n('default_sort'), '', true), |
---|
244 | array(l10n('Average rate'), 'average_rate DESC', $conf['rate']), |
---|
245 | array(l10n('most_visited_cat'), 'hit DESC', true), |
---|
246 | array(l10n('Creation date'), 'date_creation DESC', true), |
---|
247 | array(l10n('Post date'), 'date_available DESC', true), |
---|
248 | array(l10n('File name'), 'file ASC', true) |
---|
249 | ); |
---|
250 | } |
---|
251 | |
---|
252 | function display_select_categories($categories, |
---|
253 | $selecteds, |
---|
254 | $blockname, |
---|
255 | $fullname = true) |
---|
256 | { |
---|
257 | global $template; |
---|
258 | |
---|
259 | foreach ($categories as $category) |
---|
260 | { |
---|
261 | $selected = ''; |
---|
262 | if (in_array($category['id'], $selecteds)) |
---|
263 | { |
---|
264 | $selected = ' selected="selected"'; |
---|
265 | } |
---|
266 | |
---|
267 | if ($fullname) |
---|
268 | { |
---|
269 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
270 | null, |
---|
271 | false); |
---|
272 | } |
---|
273 | else |
---|
274 | { |
---|
275 | $option = str_repeat(' ', |
---|
276 | (3 * substr_count($category['global_rank'], '.'))); |
---|
277 | $option.= '- '.$category['name']; |
---|
278 | } |
---|
279 | |
---|
280 | $template->assign_block_vars( |
---|
281 | $blockname, |
---|
282 | array('SELECTED'=>$selected, |
---|
283 | 'VALUE'=>$category['id'], |
---|
284 | 'OPTION'=>$option |
---|
285 | )); |
---|
286 | } |
---|
287 | } |
---|
288 | |
---|
289 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
290 | $fullname = true) |
---|
291 | { |
---|
292 | $result = pwg_query($query); |
---|
293 | $categories = array(); |
---|
294 | if (!empty($result)) |
---|
295 | { |
---|
296 | while ($row = mysql_fetch_array($result)) |
---|
297 | { |
---|
298 | array_push($categories, $row); |
---|
299 | } |
---|
300 | } |
---|
301 | usort($categories, 'global_rank_compare'); |
---|
302 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
303 | } |
---|
304 | |
---|
305 | /** |
---|
306 | * returns all subcategory identifiers of given category ids |
---|
307 | * |
---|
308 | * @param array ids |
---|
309 | * @return array |
---|
310 | */ |
---|
311 | function get_subcat_ids($ids) |
---|
312 | { |
---|
313 | $query = ' |
---|
314 | SELECT DISTINCT(id) |
---|
315 | FROM '.CATEGORIES_TABLE.' |
---|
316 | WHERE '; |
---|
317 | foreach ($ids as $num => $category_id) |
---|
318 | { |
---|
319 | if ($num > 0) |
---|
320 | { |
---|
321 | $query.= ' |
---|
322 | OR '; |
---|
323 | } |
---|
324 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
325 | } |
---|
326 | $query.= ' |
---|
327 | ;'; |
---|
328 | $result = pwg_query($query); |
---|
329 | |
---|
330 | $subcats = array(); |
---|
331 | while ($row = mysql_fetch_array($result)) |
---|
332 | { |
---|
333 | array_push($subcats, $row['id']); |
---|
334 | } |
---|
335 | return $subcats; |
---|
336 | } |
---|
337 | |
---|
338 | function global_rank_compare($a, $b) |
---|
339 | { |
---|
340 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
341 | } |
---|
342 | |
---|
343 | function rank_compare($a, $b) |
---|
344 | { |
---|
345 | if ($a['rank'] == $b['rank']) |
---|
346 | { |
---|
347 | return 0; |
---|
348 | } |
---|
349 | |
---|
350 | return ($a['rank'] < $b['rank']) ? -1 : 1; |
---|
351 | } |
---|
352 | ?> |
---|