[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[593] | 3 | // | PhpWebGallery - a PHP based picture gallery | |
---|
| 4 | // | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net | |
---|
[1677] | 5 | // | Copyright (C) 2003-2006 PhpWebGallery Team - http://phpwebgallery.net | |
---|
[362] | 6 | // +-----------------------------------------------------------------------+ |
---|
[593] | 7 | // | branch : BSF (Best So Far) |
---|
[1113] | 8 | // | file : $Id: functions_category.inc.php 1722 2007-01-15 00:09:14Z rub $ |
---|
[362] | 9 | // | last update : $Date: 2007-01-15 00:09:14 +0000 (Mon, 15 Jan 2007) $ |
---|
| 10 | // | last modifier : $Author: rub $ |
---|
| 11 | // | revision : $Revision: 1722 $ |
---|
| 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 | // +-----------------------------------------------------------------------+ |
---|
[2] | 27 | |
---|
[423] | 28 | /** |
---|
| 29 | * Provides functions to handle categories. |
---|
| 30 | * |
---|
[1059] | 31 | * |
---|
[423] | 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 | */ |
---|
[808] | 43 | function check_restrictions($category_id) |
---|
[2] | 44 | { |
---|
[1113] | 45 | global $user; |
---|
[2] | 46 | |
---|
[1677] | 47 | // $filter['visible_categories'] and $filter['visible_images'] |
---|
| 48 | // are not used because it's not necessary (filter <> restriction) |
---|
[808] | 49 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
[2] | 50 | { |
---|
[1113] | 51 | access_denied(); |
---|
[2] | 52 | } |
---|
| 53 | } |
---|
[345] | 54 | |
---|
[614] | 55 | function get_categories_menu() |
---|
[2] | 56 | { |
---|
[1677] | 57 | global $page, $user, $filter; |
---|
[1059] | 58 | |
---|
[1624] | 59 | $query = ' |
---|
| 60 | SELECT '; |
---|
| 61 | // From CATEGORIES_TABLE |
---|
| 62 | $query.= ' |
---|
| 63 | name, id, nb_images, global_rank,'; |
---|
| 64 | // From USER_CACHE_CATEGORIES_TABLE |
---|
| 65 | $query.= ' |
---|
[1641] | 66 | date_last, max_date_last, count_images, count_categories'; |
---|
[1059] | 67 | |
---|
[1624] | 68 | // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE |
---|
| 69 | $query.= ' |
---|
[1677] | 70 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
---|
[1624] | 71 | ON id = cat_id and user_id = '.$user['id']; |
---|
[1677] | 72 | |
---|
| 73 | // Always expand when filter is activated |
---|
| 74 | if (!$user['expand'] and !$filter['enabled']) |
---|
[345] | 75 | { |
---|
[589] | 76 | $query.= ' |
---|
[1677] | 77 | WHERE |
---|
| 78 | (id_uppercat is NULL'; |
---|
| 79 | if (isset($page['category'])) |
---|
| 80 | { |
---|
| 81 | $query.= ' OR id_uppercat IN ('.$page['uppercats'].')'; |
---|
| 82 | } |
---|
| 83 | $query.= ')'; |
---|
[1648] | 84 | } |
---|
| 85 | else |
---|
| 86 | { |
---|
[1677] | 87 | $query.= ' |
---|
| 88 | '.get_sql_condition_FandF |
---|
| 89 | ( |
---|
| 90 | array |
---|
| 91 | ( |
---|
| 92 | 'visible_categories' => 'id', |
---|
| 93 | ), |
---|
| 94 | 'WHERE' |
---|
| 95 | ); |
---|
[2] | 96 | } |
---|
[1677] | 97 | |
---|
[589] | 98 | $query.= ' |
---|
| 99 | ;'; |
---|
[26] | 100 | |
---|
[589] | 101 | $result = pwg_query($query); |
---|
[614] | 102 | $cats = array(); |
---|
[1641] | 103 | while ($row = mysql_fetch_assoc($result)) |
---|
[2] | 104 | { |
---|
[614] | 105 | array_push($cats, $row); |
---|
[26] | 106 | } |
---|
[614] | 107 | usort($cats, 'global_rank_compare'); |
---|
[2] | 108 | |
---|
[1677] | 109 | // Update filtered data |
---|
[1722] | 110 | if (function_exists('update_cats_with_filtered_data')) |
---|
| 111 | { |
---|
| 112 | update_cats_with_filtered_data($cats); |
---|
| 113 | } |
---|
[1677] | 114 | |
---|
[614] | 115 | return get_html_menu_category($cats); |
---|
[26] | 116 | } |
---|
[2] | 117 | |
---|
[1624] | 118 | |
---|
[761] | 119 | /** |
---|
[423] | 120 | * Retrieve informations about a category in the database |
---|
| 121 | * |
---|
| 122 | * Returns an array with following keys : |
---|
| 123 | * |
---|
| 124 | * - comment |
---|
| 125 | * - dir : directory, might be empty for virtual categories |
---|
| 126 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
| 127 | * uppercat name findable) |
---|
| 128 | * - nb_images |
---|
| 129 | * - id_uppercat |
---|
| 130 | * - site_id |
---|
[1059] | 131 | * - |
---|
[423] | 132 | * |
---|
| 133 | * @param int category id |
---|
| 134 | * @return array |
---|
| 135 | */ |
---|
[2] | 136 | function get_cat_info( $id ) |
---|
| 137 | { |
---|
[603] | 138 | $infos = array('nb_images','id_uppercat','comment','site_id' |
---|
| 139 | ,'dir','date_last','uploadable','status','visible' |
---|
[1500] | 140 | ,'representative_picture_id','uppercats','commentable' |
---|
| 141 | ,'image_order'); |
---|
[1059] | 142 | |
---|
[603] | 143 | $query = ' |
---|
| 144 | SELECT '.implode(',', $infos).' |
---|
| 145 | FROM '.CATEGORIES_TABLE.' |
---|
| 146 | WHERE id = '.$id.' |
---|
| 147 | ;'; |
---|
| 148 | $row = mysql_fetch_array(pwg_query($query)); |
---|
[1288] | 149 | if (empty($row)) |
---|
| 150 | return null; |
---|
[345] | 151 | |
---|
| 152 | $cat = array(); |
---|
[603] | 153 | foreach ($infos as $info) |
---|
| 154 | { |
---|
| 155 | if (isset($row[$info])) |
---|
| 156 | { |
---|
| 157 | $cat[$info] = $row[$info]; |
---|
| 158 | } |
---|
| 159 | else |
---|
| 160 | { |
---|
| 161 | $cat[$info] = ''; |
---|
| 162 | } |
---|
[345] | 163 | // If the field is true or false, the variable is transformed into a |
---|
| 164 | // boolean value. |
---|
[603] | 165 | if ($cat[$info] == 'true' or $cat[$info] == 'false') |
---|
[345] | 166 | { |
---|
| 167 | $cat[$info] = get_boolean( $cat[$info] ); |
---|
| 168 | } |
---|
| 169 | } |
---|
[1046] | 170 | global $conf; |
---|
| 171 | if ( !( $conf['allow_html_descriptions'] and |
---|
[1573] | 172 | preg_match('/<(div|br|img|script).*>/i', $cat['comment']) ) ) |
---|
[1046] | 173 | { |
---|
| 174 | $cat['comment'] = nl2br($cat['comment']); |
---|
| 175 | } |
---|
[345] | 176 | |
---|
[672] | 177 | $names = array(); |
---|
[603] | 178 | $query = ' |
---|
| 179 | SELECT name,id |
---|
| 180 | FROM '.CATEGORIES_TABLE.' |
---|
| 181 | WHERE id IN ('.$cat['uppercats'].') |
---|
| 182 | ;'; |
---|
| 183 | $result = pwg_query($query); |
---|
| 184 | while($row = mysql_fetch_array($result)) |
---|
[2] | 185 | { |
---|
[672] | 186 | $names[$row['id']] = $row['name']; |
---|
[2] | 187 | } |
---|
[672] | 188 | |
---|
| 189 | // category names must be in the same order than uppercats list |
---|
| 190 | $cat['name'] = array(); |
---|
| 191 | foreach (explode(',', $cat['uppercats']) as $cat_id) |
---|
| 192 | { |
---|
| 193 | $cat['name'][$cat_id] = $names[$cat_id]; |
---|
| 194 | } |
---|
[1059] | 195 | |
---|
[2] | 196 | return $cat; |
---|
| 197 | } |
---|
[61] | 198 | |
---|
| 199 | // get_complete_dir returns the concatenation of get_site_url and |
---|
| 200 | // get_local_dir |
---|
| 201 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
| 202 | // PhpWebGallery files and this category has 22 for identifier |
---|
| 203 | // get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/" |
---|
| 204 | function get_complete_dir( $category_id ) |
---|
| 205 | { |
---|
[579] | 206 | return get_site_url($category_id).get_local_dir($category_id); |
---|
[61] | 207 | } |
---|
| 208 | |
---|
| 209 | // get_local_dir returns an array with complete path without the site url |
---|
| 210 | // Example : "pets > rex > 1_year_old" is on the the same site as the |
---|
| 211 | // PhpWebGallery files and this category has 22 for identifier |
---|
| 212 | // get_local_dir(22) returns "pets/rex/1_year_old/" |
---|
| 213 | function get_local_dir( $category_id ) |
---|
| 214 | { |
---|
| 215 | global $page; |
---|
| 216 | |
---|
[345] | 217 | $uppercats = ''; |
---|
| 218 | $local_dir = ''; |
---|
| 219 | |
---|
| 220 | if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) ) |
---|
[61] | 221 | { |
---|
[345] | 222 | $uppercats = $page['plain_structure'][$category_id]['uppercats']; |
---|
[61] | 223 | } |
---|
[345] | 224 | else |
---|
| 225 | { |
---|
| 226 | $query = 'SELECT uppercats'; |
---|
| 227 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id; |
---|
| 228 | $query.= ';'; |
---|
[587] | 229 | $row = mysql_fetch_array( pwg_query( $query ) ); |
---|
[345] | 230 | $uppercats = $row['uppercats']; |
---|
| 231 | } |
---|
| 232 | |
---|
| 233 | $upper_array = explode( ',', $uppercats ); |
---|
| 234 | |
---|
| 235 | $database_dirs = array(); |
---|
| 236 | $query = 'SELECT id,dir'; |
---|
| 237 | $query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')'; |
---|
| 238 | $query.= ';'; |
---|
[587] | 239 | $result = pwg_query( $query ); |
---|
[345] | 240 | while( $row = mysql_fetch_array( $result ) ) |
---|
| 241 | { |
---|
| 242 | $database_dirs[$row['id']] = $row['dir']; |
---|
| 243 | } |
---|
[579] | 244 | foreach ($upper_array as $id) |
---|
| 245 | { |
---|
[345] | 246 | $local_dir.= $database_dirs[$id].'/'; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | return $local_dir; |
---|
[61] | 250 | } |
---|
| 251 | |
---|
| 252 | // retrieving the site url : "http://domain.com/gallery/" or |
---|
| 253 | // simply "./galleries/" |
---|
[579] | 254 | function get_site_url($category_id) |
---|
[61] | 255 | { |
---|
| 256 | global $page; |
---|
| 257 | |
---|
[579] | 258 | $query = ' |
---|
| 259 | SELECT galleries_url |
---|
| 260 | FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c |
---|
| 261 | WHERE s.id = c.site_id |
---|
| 262 | AND c.id = '.$category_id.' |
---|
| 263 | ;'; |
---|
[587] | 264 | $row = mysql_fetch_array(pwg_query($query)); |
---|
[61] | 265 | return $row['galleries_url']; |
---|
| 266 | } |
---|
| 267 | |
---|
[1020] | 268 | // returns an array of image orders available for users/visitors |
---|
| 269 | function get_category_preferred_image_orders() |
---|
| 270 | { |
---|
[1029] | 271 | global $conf; |
---|
[1020] | 272 | return array( |
---|
[1059] | 273 | array(l10n('default_sort'), '', true), |
---|
[1042] | 274 | array(l10n('Average rate'), 'average_rate DESC', $conf['rate']), |
---|
[1031] | 275 | array(l10n('most_visited_cat'), 'hit DESC', true), |
---|
| 276 | array(l10n('Creation date'), 'date_creation DESC', true), |
---|
[1059] | 277 | array(l10n('Post date'), 'date_available DESC', true), |
---|
[1031] | 278 | array(l10n('File name'), 'file ASC', true) |
---|
[1020] | 279 | ); |
---|
| 280 | } |
---|
| 281 | |
---|
[589] | 282 | function display_select_categories($categories, |
---|
| 283 | $selecteds, |
---|
[602] | 284 | $blockname, |
---|
[614] | 285 | $fullname = true) |
---|
[589] | 286 | { |
---|
[614] | 287 | global $template; |
---|
[589] | 288 | |
---|
| 289 | foreach ($categories as $category) |
---|
| 290 | { |
---|
[614] | 291 | $selected = ''; |
---|
| 292 | if (in_array($category['id'], $selecteds)) |
---|
[589] | 293 | { |
---|
[614] | 294 | $selected = ' selected="selected"'; |
---|
| 295 | } |
---|
[589] | 296 | |
---|
[614] | 297 | if ($fullname) |
---|
| 298 | { |
---|
| 299 | $option = get_cat_display_name_cache($category['uppercats'], |
---|
[1092] | 300 | null, |
---|
[614] | 301 | false); |
---|
[589] | 302 | } |
---|
[614] | 303 | else |
---|
| 304 | { |
---|
| 305 | $option = str_repeat(' ', |
---|
| 306 | (3 * substr_count($category['global_rank'], '.'))); |
---|
| 307 | $option.= '- '.$category['name']; |
---|
| 308 | } |
---|
[1059] | 309 | |
---|
[614] | 310 | $template->assign_block_vars( |
---|
| 311 | $blockname, |
---|
| 312 | array('SELECTED'=>$selected, |
---|
| 313 | 'VALUE'=>$category['id'], |
---|
| 314 | 'OPTION'=>$option |
---|
| 315 | )); |
---|
[589] | 316 | } |
---|
| 317 | } |
---|
[603] | 318 | |
---|
[614] | 319 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
| 320 | $fullname = true) |
---|
| 321 | { |
---|
| 322 | $result = pwg_query($query); |
---|
| 323 | $categories = array(); |
---|
[655] | 324 | if (!empty($result)) |
---|
| 325 | { |
---|
[657] | 326 | while ($row = mysql_fetch_array($result)) |
---|
| 327 | { |
---|
| 328 | array_push($categories, $row); |
---|
| 329 | } |
---|
[614] | 330 | } |
---|
| 331 | usort($categories, 'global_rank_compare'); |
---|
| 332 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
| 333 | } |
---|
| 334 | |
---|
[603] | 335 | /** |
---|
| 336 | * returns all subcategory identifiers of given category ids |
---|
| 337 | * |
---|
| 338 | * @param array ids |
---|
| 339 | * @return array |
---|
| 340 | */ |
---|
| 341 | function get_subcat_ids($ids) |
---|
| 342 | { |
---|
| 343 | $query = ' |
---|
| 344 | SELECT DISTINCT(id) |
---|
| 345 | FROM '.CATEGORIES_TABLE.' |
---|
| 346 | WHERE '; |
---|
| 347 | foreach ($ids as $num => $category_id) |
---|
| 348 | { |
---|
| 349 | if ($num > 0) |
---|
| 350 | { |
---|
| 351 | $query.= ' |
---|
| 352 | OR '; |
---|
| 353 | } |
---|
| 354 | $query.= 'uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\''; |
---|
| 355 | } |
---|
| 356 | $query.= ' |
---|
| 357 | ;'; |
---|
| 358 | $result = pwg_query($query); |
---|
| 359 | |
---|
| 360 | $subcats = array(); |
---|
| 361 | while ($row = mysql_fetch_array($result)) |
---|
| 362 | { |
---|
| 363 | array_push($subcats, $row['id']); |
---|
| 364 | } |
---|
| 365 | return $subcats; |
---|
| 366 | } |
---|
[614] | 367 | |
---|
| 368 | function global_rank_compare($a, $b) |
---|
| 369 | { |
---|
| 370 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
| 371 | } |
---|
[1036] | 372 | |
---|
| 373 | function rank_compare($a, $b) |
---|
| 374 | { |
---|
| 375 | if ($a['rank'] == $b['rank']) |
---|
| 376 | { |
---|
| 377 | return 0; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | return ($a['rank'] < $b['rank']) ? -1 : 1; |
---|
| 381 | } |
---|
[1624] | 382 | |
---|
| 383 | /** |
---|
| 384 | * returns display text for information images of category |
---|
| 385 | * |
---|
| 386 | * @param array categories |
---|
| 387 | * @return string |
---|
| 388 | */ |
---|
| 389 | function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true) |
---|
| 390 | { |
---|
| 391 | $display_text = ''; |
---|
| 392 | |
---|
| 393 | // Count of category is main |
---|
| 394 | // if not picture on categorie, test on sub-categories |
---|
| 395 | $count = ($cat_nb_images > 0 ? $cat_nb_images : $cat_count_images); |
---|
| 396 | |
---|
| 397 | if ($count > 0) |
---|
| 398 | { |
---|
[1637] | 399 | $display_text.= l10n_dec('image_available', 'images_available', $count); |
---|
[1624] | 400 | |
---|
| 401 | if ($cat_nb_images > 0) |
---|
| 402 | { |
---|
| 403 | if (! $short_message) |
---|
| 404 | { |
---|
| 405 | $display_text.= ' '.l10n('images_available_cpl'); |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | else |
---|
| 409 | { |
---|
[1637] | 410 | $display_text.= ' '.l10n_dec('images_available_cat', 'images_available_cats', $cat_count_categories); |
---|
[1624] | 411 | } |
---|
| 412 | } |
---|
| 413 | |
---|
| 414 | return $display_text; |
---|
| 415 | } |
---|
| 416 | |
---|
[1641] | 417 | ?> |
---|