[2] | 1 | <?php |
---|
[362] | 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[12922] | 5 | // | Copyright(C) 2008-2012 Piwigo Team http://piwigo.org | |
---|
[2297] | 6 | // | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net | |
---|
| 7 | // | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick | |
---|
| 8 | // +-----------------------------------------------------------------------+ |
---|
| 9 | // | This program is free software; you can redistribute it and/or modify | |
---|
| 10 | // | it under the terms of the GNU General Public License as published by | |
---|
| 11 | // | the Free Software Foundation | |
---|
| 12 | // | | |
---|
| 13 | // | This program is distributed in the hope that it will be useful, but | |
---|
| 14 | // | WITHOUT ANY WARRANTY; without even the implied warranty of | |
---|
| 15 | // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
---|
| 16 | // | General Public License for more details. | |
---|
| 17 | // | | |
---|
| 18 | // | You should have received a copy of the GNU General Public License | |
---|
| 19 | // | along with this program; if not, write to the Free Software | |
---|
| 20 | // | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, | |
---|
| 21 | // | USA. | |
---|
| 22 | // +-----------------------------------------------------------------------+ |
---|
[2] | 23 | |
---|
[423] | 24 | /** |
---|
| 25 | * Provides functions to handle categories. |
---|
| 26 | * |
---|
[1059] | 27 | * |
---|
[423] | 28 | */ |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * Is the category accessible to the connected user ? |
---|
| 32 | * |
---|
| 33 | * Note : if the user is not authorized to see this category, page creation |
---|
| 34 | * ends (exit command in this function) |
---|
| 35 | * |
---|
| 36 | * @param int category id to verify |
---|
| 37 | * @return void |
---|
| 38 | */ |
---|
[808] | 39 | function check_restrictions($category_id) |
---|
[2] | 40 | { |
---|
[1113] | 41 | global $user; |
---|
[2] | 42 | |
---|
[1677] | 43 | // $filter['visible_categories'] and $filter['visible_images'] |
---|
| 44 | // are not used because it's not necessary (filter <> restriction) |
---|
[808] | 45 | if (in_array($category_id, explode(',', $user['forbidden_categories']))) |
---|
[2] | 46 | { |
---|
[1113] | 47 | access_denied(); |
---|
[2] | 48 | } |
---|
| 49 | } |
---|
[345] | 50 | |
---|
[614] | 51 | function get_categories_menu() |
---|
[2] | 52 | { |
---|
[11285] | 53 | global $page, $user, $filter, $conf; |
---|
[1059] | 54 | |
---|
[1624] | 55 | $query = ' |
---|
| 56 | SELECT '; |
---|
| 57 | // From CATEGORIES_TABLE |
---|
| 58 | $query.= ' |
---|
[1866] | 59 | id, name, permalink, nb_images, global_rank,'; |
---|
[1624] | 60 | // From USER_CACHE_CATEGORIES_TABLE |
---|
| 61 | $query.= ' |
---|
[1641] | 62 | date_last, max_date_last, count_images, count_categories'; |
---|
[1059] | 63 | |
---|
[1624] | 64 | // $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE |
---|
| 65 | $query.= ' |
---|
[1677] | 66 | FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.' |
---|
[1624] | 67 | ON id = cat_id and user_id = '.$user['id']; |
---|
[1677] | 68 | |
---|
| 69 | // Always expand when filter is activated |
---|
| 70 | if (!$user['expand'] and !$filter['enabled']) |
---|
[345] | 71 | { |
---|
[2070] | 72 | $where = ' |
---|
[1677] | 73 | (id_uppercat is NULL'; |
---|
| 74 | if (isset($page['category'])) |
---|
| 75 | { |
---|
[2070] | 76 | $where .= ' OR id_uppercat IN ('.$page['category']['uppercats'].')'; |
---|
[1677] | 77 | } |
---|
[2070] | 78 | $where .= ')'; |
---|
[1648] | 79 | } |
---|
| 80 | else |
---|
| 81 | { |
---|
[2070] | 82 | $where = ' |
---|
[1677] | 83 | '.get_sql_condition_FandF |
---|
| 84 | ( |
---|
| 85 | array |
---|
| 86 | ( |
---|
| 87 | 'visible_categories' => 'id', |
---|
| 88 | ), |
---|
[2070] | 89 | null, |
---|
| 90 | true |
---|
[1677] | 91 | ); |
---|
[2] | 92 | } |
---|
[1677] | 93 | |
---|
[2070] | 94 | $where = trigger_event('get_categories_menu_sql_where', |
---|
| 95 | $where, $user['expand'], $filter['enabled'] ); |
---|
| 96 | |
---|
[589] | 97 | $query.= ' |
---|
[2070] | 98 | WHERE '.$where.' |
---|
[589] | 99 | ;'; |
---|
[26] | 100 | |
---|
[589] | 101 | $result = pwg_query($query); |
---|
[614] | 102 | $cats = array(); |
---|
[3171] | 103 | $selected_category = isset($page['category']) ? $page['category'] : null; |
---|
[4325] | 104 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[2] | 105 | { |
---|
[3171] | 106 | $child_date_last = @$row['max_date_last']> @$row['date_last']; |
---|
| 107 | $row = array_merge($row, |
---|
| 108 | array( |
---|
| 109 | 'NAME' => trigger_event( |
---|
| 110 | 'render_category_name', |
---|
| 111 | $row['name'], |
---|
| 112 | 'get_categories_menu' |
---|
[11285] | 113 | ), |
---|
[3171] | 114 | 'TITLE' => get_display_images_count( |
---|
| 115 | $row['nb_images'], |
---|
| 116 | $row['count_images'], |
---|
| 117 | $row['count_categories'], |
---|
| 118 | false, |
---|
| 119 | ' / ' |
---|
[11285] | 120 | ), |
---|
[3171] | 121 | 'URL' => make_index_url(array('category' => $row)), |
---|
| 122 | 'LEVEL' => substr_count($row['global_rank'], '.') + 1, |
---|
| 123 | 'SELECTED' => $selected_category['id'] == $row['id'] ? true : false, |
---|
| 124 | 'IS_UPPERCAT' => $selected_category['id_uppercat'] == $row['id'] ? true : false, |
---|
[11285] | 125 | ) |
---|
| 126 | ); |
---|
| 127 | if ($conf['index_new_icon']) |
---|
| 128 | { |
---|
| 129 | $row['icon_ts'] = get_icon($row['max_date_last'], $child_date_last); |
---|
| 130 | } |
---|
[18579] | 131 | $cats[] = $row; |
---|
[2586] | 132 | if ($row['id']==@$page['category']['id']) //save the number of subcats for later optim |
---|
| 133 | $page['category']['count_categories'] = $row['count_categories']; |
---|
[26] | 134 | } |
---|
[614] | 135 | usort($cats, 'global_rank_compare'); |
---|
[2] | 136 | |
---|
[1677] | 137 | // Update filtered data |
---|
[1722] | 138 | if (function_exists('update_cats_with_filtered_data')) |
---|
| 139 | { |
---|
| 140 | update_cats_with_filtered_data($cats); |
---|
| 141 | } |
---|
[1677] | 142 | |
---|
[3171] | 143 | return $cats; |
---|
[26] | 144 | } |
---|
[2] | 145 | |
---|
[1624] | 146 | |
---|
[761] | 147 | /** |
---|
[423] | 148 | * Retrieve informations about a category in the database |
---|
| 149 | * |
---|
| 150 | * Returns an array with following keys : |
---|
| 151 | * |
---|
| 152 | * - comment |
---|
| 153 | * - dir : directory, might be empty for virtual categories |
---|
| 154 | * - name : an array with indexes from 0 (lowest cat name) to n (most |
---|
| 155 | * uppercat name findable) |
---|
| 156 | * - nb_images |
---|
| 157 | * - id_uppercat |
---|
| 158 | * - site_id |
---|
[1059] | 159 | * - |
---|
[423] | 160 | * |
---|
| 161 | * @param int category id |
---|
| 162 | * @return array |
---|
| 163 | */ |
---|
[2] | 164 | function get_cat_info( $id ) |
---|
| 165 | { |
---|
[603] | 166 | $query = ' |
---|
[1861] | 167 | SELECT * |
---|
[603] | 168 | FROM '.CATEGORIES_TABLE.' |
---|
| 169 | WHERE id = '.$id.' |
---|
| 170 | ;'; |
---|
[4325] | 171 | $cat = pwg_db_fetch_assoc(pwg_query($query)); |
---|
[1861] | 172 | if (empty($cat)) |
---|
[1288] | 173 | return null; |
---|
[345] | 174 | |
---|
[1861] | 175 | foreach ($cat as $k => $v) |
---|
[603] | 176 | { |
---|
[345] | 177 | // If the field is true or false, the variable is transformed into a |
---|
| 178 | // boolean value. |
---|
[1861] | 179 | if ($cat[$k] == 'true' or $cat[$k] == 'false') |
---|
[345] | 180 | { |
---|
[1861] | 181 | $cat[$k] = get_boolean( $cat[$k] ); |
---|
[345] | 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[1866] | 185 | $upper_ids = explode(',', $cat['uppercats']); |
---|
| 186 | if ( count($upper_ids)==1 ) |
---|
| 187 | {// no need to make a query for level 1 |
---|
| 188 | $cat['upper_names'] = array( |
---|
| 189 | array( |
---|
| 190 | 'id' => $cat['id'], |
---|
| 191 | 'name' => $cat['name'], |
---|
| 192 | 'permalink' => $cat['permalink'], |
---|
| 193 | ) |
---|
| 194 | ); |
---|
| 195 | } |
---|
| 196 | else |
---|
[2] | 197 | { |
---|
[1866] | 198 | $query = ' |
---|
| 199 | SELECT id, name, permalink |
---|
| 200 | FROM '.CATEGORIES_TABLE.' |
---|
| 201 | WHERE id IN ('.$cat['uppercats'].') |
---|
| 202 | ;'; |
---|
| 203 | $names = hash_from_query($query, 'id'); |
---|
[672] | 204 | |
---|
[1866] | 205 | // category names must be in the same order than uppercats list |
---|
| 206 | $cat['upper_names'] = array(); |
---|
| 207 | foreach ($upper_ids as $cat_id) |
---|
| 208 | { |
---|
[18579] | 209 | $cat['upper_names'][] = $names[$cat_id]; |
---|
[1866] | 210 | } |
---|
[672] | 211 | } |
---|
[2] | 212 | return $cat; |
---|
| 213 | } |
---|
[61] | 214 | |
---|
| 215 | |
---|
| 216 | |
---|
[1020] | 217 | // returns an array of image orders available for users/visitors |
---|
| 218 | function get_category_preferred_image_orders() |
---|
| 219 | { |
---|
[2517] | 220 | global $conf, $page; |
---|
[18579] | 221 | |
---|
[13839] | 222 | return trigger_event('get_category_preferred_image_orders', array( |
---|
| 223 | array(l10n('Default'), '', true), |
---|
| 224 | array(l10n('Photo title, A → Z'), 'name ASC', true), |
---|
| 225 | array(l10n('Photo title, Z → A'), 'name DESC', true), |
---|
| 226 | array(l10n('Date created, new → old'), 'date_creation DESC', true), |
---|
[18579] | 227 | array(l10n('Date created, old → new'), 'date_creation ASC', true), |
---|
[13839] | 228 | array(l10n('Date posted, new → old'), 'date_available DESC', true), |
---|
| 229 | array(l10n('Date posted, old → new'), 'date_available ASC', true), |
---|
| 230 | array(l10n('Rating score, high → low'), 'rating_score DESC', $conf['rate']), |
---|
| 231 | array(l10n('Rating score, low → high'), 'rating_score ASC', $conf['rate']), |
---|
| 232 | array(l10n('Visits, high → low'), 'hit DESC', true), |
---|
| 233 | array(l10n('Visits, low → high'), 'hit ASC', true), |
---|
| 234 | array(l10n('Permissions'), 'level DESC', is_admin()), |
---|
[2521] | 235 | )); |
---|
[1020] | 236 | } |
---|
| 237 | |
---|
[589] | 238 | function display_select_categories($categories, |
---|
| 239 | $selecteds, |
---|
[602] | 240 | $blockname, |
---|
[614] | 241 | $fullname = true) |
---|
[589] | 242 | { |
---|
[614] | 243 | global $template; |
---|
[589] | 244 | |
---|
[2223] | 245 | $tpl_cats = array(); |
---|
| 246 | foreach ($categories as $category) |
---|
| 247 | { |
---|
| 248 | if ($fullname) |
---|
| 249 | { |
---|
[11512] | 250 | $option = strip_tags( |
---|
| 251 | get_cat_display_name_cache( |
---|
| 252 | $category['uppercats'], |
---|
| 253 | null, |
---|
| 254 | false |
---|
| 255 | ) |
---|
| 256 | ); |
---|
[2223] | 257 | } |
---|
| 258 | else |
---|
| 259 | { |
---|
| 260 | $option = str_repeat(' ', |
---|
| 261 | (3 * substr_count($category['global_rank'], '.'))); |
---|
[2433] | 262 | $option.= '- '; |
---|
| 263 | $option.= strip_tags( |
---|
| 264 | trigger_event( |
---|
| 265 | 'render_category_name', |
---|
| 266 | $category['name'], |
---|
| 267 | 'display_select_categories' |
---|
| 268 | ) |
---|
| 269 | ); |
---|
[2223] | 270 | } |
---|
| 271 | $tpl_cats[ $category['id'] ] = $option; |
---|
| 272 | } |
---|
| 273 | |
---|
[2290] | 274 | $template->assign( $blockname, $tpl_cats); |
---|
| 275 | $template->assign( $blockname.'_selected', $selecteds); |
---|
[589] | 276 | } |
---|
[603] | 277 | |
---|
[614] | 278 | function display_select_cat_wrapper($query, $selecteds, $blockname, |
---|
| 279 | $fullname = true) |
---|
| 280 | { |
---|
[18579] | 281 | $categories = array_from_query($query); |
---|
[614] | 282 | usort($categories, 'global_rank_compare'); |
---|
| 283 | display_select_categories($categories, $selecteds, $blockname, $fullname); |
---|
| 284 | } |
---|
| 285 | |
---|
[603] | 286 | /** |
---|
| 287 | * returns all subcategory identifiers of given category ids |
---|
| 288 | * |
---|
| 289 | * @param array ids |
---|
| 290 | * @return array |
---|
| 291 | */ |
---|
| 292 | function get_subcat_ids($ids) |
---|
| 293 | { |
---|
| 294 | $query = ' |
---|
| 295 | SELECT DISTINCT(id) |
---|
| 296 | FROM '.CATEGORIES_TABLE.' |
---|
| 297 | WHERE '; |
---|
| 298 | foreach ($ids as $num => $category_id) |
---|
| 299 | { |
---|
[1861] | 300 | is_numeric($category_id) |
---|
| 301 | or trigger_error( |
---|
| 302 | 'get_subcat_ids expecting numeric, not '.gettype($category_id), |
---|
| 303 | E_USER_WARNING |
---|
| 304 | ); |
---|
[603] | 305 | if ($num > 0) |
---|
| 306 | { |
---|
| 307 | $query.= ' |
---|
| 308 | OR '; |
---|
| 309 | } |
---|
[4367] | 310 | $query.= 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\''; |
---|
[603] | 311 | } |
---|
| 312 | $query.= ' |
---|
| 313 | ;'; |
---|
[18579] | 314 | return array_from_query($query, 'id'); |
---|
[603] | 315 | } |
---|
[614] | 316 | |
---|
[2047] | 317 | /** finds a matching category id from a potential list of permalinks |
---|
| 318 | * @param array permalinks example: holiday holiday/france holiday/france/paris |
---|
| 319 | * @param int idx - output of the index in $permalinks that matches |
---|
| 320 | * return category id or null if no match |
---|
[1866] | 321 | */ |
---|
[2047] | 322 | function get_cat_id_from_permalinks( $permalinks, &$idx ) |
---|
[1866] | 323 | { |
---|
[2047] | 324 | $in = ''; |
---|
| 325 | foreach($permalinks as $permalink) |
---|
[1866] | 326 | { |
---|
[2047] | 327 | if ( !empty($in) ) $in.=', '; |
---|
[6664] | 328 | $in .= '\''.$permalink.'\''; |
---|
[1866] | 329 | } |
---|
[2047] | 330 | $query =' |
---|
[2356] | 331 | SELECT cat_id AS id, permalink, 1 AS is_old |
---|
| 332 | FROM '.OLD_PERMALINKS_TABLE.' |
---|
| 333 | WHERE permalink IN ('.$in.') |
---|
[2047] | 334 | UNION |
---|
| 335 | SELECT id, permalink, 0 AS is_old |
---|
| 336 | FROM '.CATEGORIES_TABLE.' |
---|
| 337 | WHERE permalink IN ('.$in.') |
---|
| 338 | ;'; |
---|
| 339 | $perma_hash = hash_from_query($query, 'permalink'); |
---|
[1866] | 340 | |
---|
[2047] | 341 | if ( empty($perma_hash) ) |
---|
| 342 | return null; |
---|
| 343 | for ($i=count($permalinks)-1; $i>=0; $i--) |
---|
[1866] | 344 | { |
---|
[2047] | 345 | if ( isset( $perma_hash[ $permalinks[$i] ] ) ) |
---|
| 346 | { |
---|
| 347 | $idx = $i; |
---|
| 348 | $cat_id = $perma_hash[ $permalinks[$i] ]['id']; |
---|
| 349 | if ($perma_hash[ $permalinks[$i] ]['is_old']) |
---|
| 350 | { |
---|
| 351 | $query=' |
---|
[1866] | 352 | UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1 |
---|
[6654] | 353 | WHERE permalink=\''.$permalinks[$i].'\' AND cat_id='.$cat_id.' |
---|
[1866] | 354 | LIMIT 1'; |
---|
[2047] | 355 | pwg_query($query); |
---|
| 356 | } |
---|
| 357 | return $cat_id; |
---|
| 358 | } |
---|
[1866] | 359 | } |
---|
[2047] | 360 | return null; |
---|
[1866] | 361 | } |
---|
| 362 | |
---|
[614] | 363 | function global_rank_compare($a, $b) |
---|
| 364 | { |
---|
| 365 | return strnatcasecmp($a['global_rank'], $b['global_rank']); |
---|
| 366 | } |
---|
[1036] | 367 | |
---|
| 368 | function rank_compare($a, $b) |
---|
| 369 | { |
---|
[13240] | 370 | return $a['rank'] - $b['rank']; |
---|
[1036] | 371 | } |
---|
[1624] | 372 | |
---|
| 373 | /** |
---|
| 374 | * returns display text for information images of category |
---|
| 375 | * |
---|
| 376 | * @param array categories |
---|
| 377 | * @return string |
---|
| 378 | */ |
---|
[1851] | 379 | function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $Separator = '\n') |
---|
[1624] | 380 | { |
---|
| 381 | $display_text = ''; |
---|
| 382 | |
---|
[1851] | 383 | if ($cat_count_images > 0) |
---|
| 384 | { |
---|
| 385 | if ($cat_nb_images > 0 and $cat_nb_images < $cat_count_images) |
---|
| 386 | { |
---|
| 387 | $display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $Separator).$Separator; |
---|
| 388 | $cat_count_images-= $cat_nb_images; |
---|
| 389 | $cat_nb_images = 0; |
---|
| 390 | } |
---|
[2117] | 391 | |
---|
[1851] | 392 | //at least one image direct or indirect |
---|
[8665] | 393 | $display_text.= l10n_dec('%d photo', '%d photos', $cat_count_images); |
---|
[1624] | 394 | |
---|
[1851] | 395 | if ($cat_count_categories == 0 or $cat_nb_images == $cat_count_images) |
---|
| 396 | { |
---|
| 397 | //no descendant categories or descendants do not contain images |
---|
[1624] | 398 | if (! $short_message) |
---|
| 399 | { |
---|
[6951] | 400 | $display_text.= ' '.l10n('in this album'); |
---|
[1624] | 401 | } |
---|
| 402 | } |
---|
| 403 | else |
---|
| 404 | { |
---|
[6951] | 405 | $display_text.= ' '.l10n_dec('in %d sub-album', 'in %d sub-albums', $cat_count_categories); |
---|
[1624] | 406 | } |
---|
| 407 | } |
---|
| 408 | |
---|
| 409 | return $display_text; |
---|
| 410 | } |
---|
| 411 | |
---|
[8802] | 412 | /** |
---|
| 413 | * Find a random photo among all photos below a given album in the tree (not |
---|
| 414 | * only photo directly associated to the album but also to sub-albums) |
---|
| 415 | * |
---|
| 416 | * we need $category['uppercats'], $category['id'], $category['count_images'] |
---|
| 417 | */ |
---|
[11481] | 418 | function get_random_image_in_category($category, $recursive=true) |
---|
[8802] | 419 | { |
---|
| 420 | $image_id = null; |
---|
| 421 | if ($category['count_images']>0) |
---|
| 422 | { |
---|
| 423 | $query = ' |
---|
| 424 | SELECT image_id |
---|
| 425 | FROM '.CATEGORIES_TABLE.' AS c |
---|
| 426 | INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.category_id = c.id |
---|
[11481] | 427 | WHERE '; |
---|
| 428 | if ($recursive) |
---|
| 429 | { |
---|
| 430 | $query.= ' |
---|
| 431 | (c.id='.$category['id'].' OR uppercats LIKE \''.$category['uppercats'].',%\')'; |
---|
| 432 | } |
---|
| 433 | else |
---|
| 434 | { |
---|
| 435 | $query.= ' |
---|
| 436 | c.id='.$category['id']; |
---|
| 437 | } |
---|
| 438 | $query.= ' |
---|
| 439 | '.get_sql_condition_FandF |
---|
[8802] | 440 | ( |
---|
| 441 | array |
---|
| 442 | ( |
---|
| 443 | 'forbidden_categories' => 'c.id', |
---|
| 444 | 'visible_categories' => 'c.id', |
---|
| 445 | 'visible_images' => 'image_id', |
---|
| 446 | ), |
---|
| 447 | "\n AND" |
---|
| 448 | ).' |
---|
| 449 | ORDER BY '.DB_RANDOM_FUNCTION.'() |
---|
| 450 | LIMIT 1 |
---|
| 451 | ;'; |
---|
| 452 | $result = pwg_query($query); |
---|
| 453 | if (pwg_db_num_rows($result) > 0) |
---|
| 454 | { |
---|
| 455 | list($image_id) = pwg_db_fetch_row($result); |
---|
| 456 | } |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | return $image_id; |
---|
| 460 | } |
---|
[11155] | 461 | |
---|
[11893] | 462 | |
---|
[2770] | 463 | ?> |
---|