[2517] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2517] | 4 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 5 | // | Copyright(C) 2008-2011 Piwigo Team http://piwigo.org | |
---|
[2517] | 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 | // +-----------------------------------------------------------------------+ |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * Change rank of images inside a category |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | if (!defined('PHPWG_ROOT_PATH')) |
---|
| 30 | { |
---|
| 31 | die('Hacking attempt!'); |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | include_once(PHPWG_ROOT_PATH.'admin/include/functions.php'); |
---|
| 35 | |
---|
| 36 | // +-----------------------------------------------------------------------+ |
---|
| 37 | // | Check Access and exit when user status is not ok | |
---|
| 38 | // +-----------------------------------------------------------------------+ |
---|
| 39 | check_status(ACCESS_ADMINISTRATOR); |
---|
| 40 | |
---|
| 41 | if (!isset($_GET['cat_id']) or !is_numeric($_GET['cat_id'])) |
---|
| 42 | { |
---|
| 43 | trigger_error('missing cat_id param', E_USER_ERROR); |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | $page['category_id'] = $_GET['cat_id']; |
---|
| 47 | |
---|
| 48 | // +-----------------------------------------------------------------------+ |
---|
| 49 | // | functions | |
---|
| 50 | // +-----------------------------------------------------------------------+ |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * save the rank depending on given images order |
---|
| 54 | * |
---|
| 55 | * The list of ordered images id is supposed to be in the same parent |
---|
| 56 | * category |
---|
| 57 | * |
---|
| 58 | * @param array categories |
---|
| 59 | * @return void |
---|
| 60 | */ |
---|
| 61 | function save_images_order($category_id, $images) |
---|
| 62 | { |
---|
| 63 | $current_rank = 0; |
---|
| 64 | $datas = array(); |
---|
| 65 | foreach ($images as $id) |
---|
| 66 | { |
---|
| 67 | array_push( |
---|
| 68 | $datas, |
---|
| 69 | array( |
---|
| 70 | 'category_id' => $category_id, |
---|
| 71 | 'image_id' => $id, |
---|
| 72 | 'rank' => ++$current_rank, |
---|
| 73 | ) |
---|
| 74 | ); |
---|
| 75 | } |
---|
| 76 | $fields = array( |
---|
| 77 | 'primary' => array('image_id', 'category_id'), |
---|
| 78 | 'update' => array('rank') |
---|
| 79 | ); |
---|
| 80 | mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | // +-----------------------------------------------------------------------+ |
---|
| 84 | // | global mode form submission | |
---|
| 85 | // +-----------------------------------------------------------------------+ |
---|
| 86 | |
---|
[5759] | 87 | $image_order_choices = array('default', 'rank', 'user_define'); |
---|
| 88 | $image_order_choice = 'default'; |
---|
| 89 | |
---|
[2517] | 90 | if (isset($_POST['submit'])) |
---|
| 91 | { |
---|
[9051] | 92 | if (isset($_POST['rank_of_image'])) |
---|
| 93 | { |
---|
| 94 | asort($_POST['rank_of_image'], SORT_NUMERIC); |
---|
[2517] | 95 | |
---|
[9051] | 96 | save_images_order( |
---|
| 97 | $page['category_id'], |
---|
| 98 | array_keys($_POST['rank_of_image']) |
---|
| 99 | ); |
---|
[2517] | 100 | |
---|
[9051] | 101 | array_push( |
---|
| 102 | $page['infos'], |
---|
| 103 | l10n('Images manual order was saved') |
---|
| 104 | ); |
---|
| 105 | } |
---|
[5759] | 106 | |
---|
| 107 | $image_order = null; |
---|
| 108 | if (!empty($_POST['image_order_choice']) |
---|
| 109 | && in_array($_POST['image_order_choice'], $image_order_choices)) |
---|
| 110 | { |
---|
| 111 | $image_order_choice = $_POST['image_order_choice']; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | if ($image_order_choice=='user_define') |
---|
| 115 | { |
---|
| 116 | for ($i=1; $i<=3; $i++) |
---|
| 117 | { |
---|
| 118 | if ( !empty($_POST['order_field_'.$i]) ) |
---|
| 119 | { |
---|
| 120 | if (! empty($image_order) ) |
---|
| 121 | { |
---|
| 122 | $image_order .= ','; |
---|
| 123 | } |
---|
| 124 | $image_order .= $_POST['order_field_'.$i]; |
---|
| 125 | if ($_POST['order_direction_'.$i]=='DESC') |
---|
| 126 | { |
---|
| 127 | $image_order .= ' DESC'; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | elseif ($image_order_choice=='rank') |
---|
| 133 | { |
---|
| 134 | $image_order = 'rank'; |
---|
| 135 | } |
---|
| 136 | $query = ' |
---|
| 137 | UPDATE '.CATEGORIES_TABLE.' SET image_order=\''.$image_order.'\' |
---|
| 138 | WHERE id='.$page['category_id']; |
---|
| 139 | pwg_query($query); |
---|
[9051] | 140 | |
---|
| 141 | if (isset($_POST['image_order_subcats'])) |
---|
| 142 | { |
---|
| 143 | $cat_info = get_cat_info($page['category_id']); |
---|
| 144 | |
---|
| 145 | $query = ' |
---|
| 146 | UPDATE '.CATEGORIES_TABLE.' |
---|
| 147 | SET image_order = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').' |
---|
| 148 | WHERE uppercats LIKE \''.$cat_info['uppercats'].',%\''; |
---|
| 149 | pwg_query($query); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | array_push($page['infos'], l10n('Your configuration settings are saved')); |
---|
[2517] | 153 | } |
---|
| 154 | |
---|
| 155 | // +-----------------------------------------------------------------------+ |
---|
| 156 | // | template init | |
---|
| 157 | // +-----------------------------------------------------------------------+ |
---|
| 158 | $template->set_filenames( |
---|
[2530] | 159 | array('element_set_ranks' => 'element_set_ranks.tpl') |
---|
[2517] | 160 | ); |
---|
| 161 | |
---|
| 162 | $base_url = get_root_url().'admin.php'; |
---|
| 163 | |
---|
| 164 | $query = ' |
---|
[5759] | 165 | SELECT * |
---|
[2517] | 166 | FROM '.CATEGORIES_TABLE.' |
---|
| 167 | WHERE id = '.$page['category_id'].' |
---|
| 168 | ;'; |
---|
[4325] | 169 | $category = pwg_db_fetch_assoc(pwg_query($query)); |
---|
[2517] | 170 | |
---|
[5759] | 171 | if ($category['image_order']=='rank') |
---|
| 172 | { |
---|
| 173 | $image_order_choice = 'rank'; |
---|
| 174 | } |
---|
| 175 | elseif ($category['image_order']!='') |
---|
| 176 | { |
---|
| 177 | $image_order_choice = 'user_define'; |
---|
| 178 | } |
---|
| 179 | |
---|
[2517] | 180 | // Navigation path |
---|
| 181 | $navigation = get_cat_display_name_cache( |
---|
| 182 | $category['uppercats'], |
---|
| 183 | get_root_url().'admin.php?page=cat_modify&cat_id=' |
---|
| 184 | ); |
---|
| 185 | |
---|
| 186 | $template->assign( |
---|
| 187 | array( |
---|
| 188 | 'CATEGORIES_NAV' => $navigation, |
---|
| 189 | 'F_ACTION' => $base_url.get_query_string_diff(array()), |
---|
| 190 | ) |
---|
| 191 | ); |
---|
| 192 | |
---|
| 193 | // +-----------------------------------------------------------------------+ |
---|
| 194 | // | thumbnails | |
---|
| 195 | // +-----------------------------------------------------------------------+ |
---|
| 196 | |
---|
| 197 | $query = ' |
---|
| 198 | SELECT |
---|
| 199 | id, |
---|
| 200 | path, |
---|
| 201 | tn_ext, |
---|
| 202 | rank |
---|
| 203 | FROM '.IMAGES_TABLE.' |
---|
| 204 | JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id |
---|
| 205 | WHERE category_id = '.$page['category_id'].' |
---|
| 206 | ORDER BY rank |
---|
| 207 | ;'; |
---|
| 208 | $result = pwg_query($query); |
---|
| 209 | |
---|
| 210 | // template thumbnail initialization |
---|
| 211 | $current_rank = 1; |
---|
[9864] | 212 | $thumbnail_info=array(); |
---|
| 213 | $clipping=array(); |
---|
[4325] | 214 | while ($row = pwg_db_fetch_assoc($result)) |
---|
[2517] | 215 | { |
---|
| 216 | $src = get_thumbnail_url($row); |
---|
[9864] | 217 | |
---|
| 218 | $thumbnail_size = getimagesize($src); |
---|
| 219 | $thumbnail_info[] = array( |
---|
| 220 | 'width' => $thumbnail_size[0], |
---|
| 221 | 'height' => $thumbnail_size[1], |
---|
| 222 | 'id' => $row['id'], |
---|
| 223 | 'tn_src' => $src, |
---|
| 224 | 'rank' => $current_rank * 10, |
---|
| 225 | ); |
---|
[9865] | 226 | if ($thumbnail_size[0]<=128 and $thumbnail_size[1]<=128) |
---|
| 227 | { |
---|
| 228 | $clipping[]= min($thumbnail_size[0],$thumbnail_size[1]); |
---|
| 229 | } |
---|
| 230 | else |
---|
| 231 | { |
---|
| 232 | $clipping[]= min($thumbnail_size[0]*0.75,$thumbnail_size[1]*0.75); |
---|
| 233 | } |
---|
[9864] | 234 | $current_rank++; |
---|
| 235 | } |
---|
| 236 | $clipping=array_sum($clipping)/count($clipping); |
---|
| 237 | foreach ($thumbnail_info as $thumbnails_info) |
---|
| 238 | { |
---|
| 239 | $thumbnail_x_center = $thumbnails_info['width']/2; |
---|
| 240 | $thumbnail_y_center = $thumbnails_info['height']/2; |
---|
[2517] | 241 | $template->append( |
---|
| 242 | 'thumbnails', |
---|
| 243 | array( |
---|
[9864] | 244 | 'ID' => $thumbnails_info['id'], |
---|
| 245 | 'TN_SRC' => $thumbnails_info['tn_src'], |
---|
| 246 | 'RANK' => $thumbnails_info['rank'], |
---|
| 247 | 'CLIPING' => round($clipping), |
---|
| 248 | 'CLIPING_li' => round($clipping+8), |
---|
| 249 | 'CLIP_TOP' => round($thumbnail_y_center - $clipping/2), |
---|
| 250 | 'CLIP_RIGHT' => round($thumbnail_x_center + $clipping/2), |
---|
| 251 | 'CLIP_BOTTOM' => round($thumbnail_y_center + $clipping/2), |
---|
| 252 | 'CLIP_LEFT' => round($thumbnail_x_center - $clipping/2) |
---|
[2517] | 253 | ) |
---|
| 254 | ); |
---|
| 255 | } |
---|
[5759] | 256 | // image order management |
---|
| 257 | $sort_fields = array( |
---|
| 258 | '' => '', |
---|
| 259 | 'date_creation' => l10n('Creation date'), |
---|
| 260 | 'date_available' => l10n('Post date'), |
---|
| 261 | 'average_rate' => l10n('Average rate'), |
---|
| 262 | 'hit' => l10n('Most visited'), |
---|
| 263 | 'file' => l10n('File name'), |
---|
| 264 | 'id' => 'Id', |
---|
| 265 | 'rank' => l10n('Rank'), |
---|
| 266 | ); |
---|
| 267 | |
---|
| 268 | $sort_directions = array( |
---|
| 269 | 'ASC' => l10n('ascending'), |
---|
| 270 | 'DESC' => l10n('descending'), |
---|
| 271 | ); |
---|
| 272 | |
---|
| 273 | $template->assign('image_order_field_options', $sort_fields); |
---|
| 274 | $template->assign('image_order_direction_options', $sort_directions); |
---|
| 275 | |
---|
| 276 | $matches = array(); |
---|
| 277 | if ( !empty( $category['image_order'] ) ) |
---|
| 278 | { |
---|
| 279 | preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i', |
---|
| 280 | $category['image_order'], $matches); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | for ($i=0; $i<3; $i++) // 3 fields |
---|
| 284 | { |
---|
| 285 | $tpl_image_order_select = array( |
---|
| 286 | 'ID' => $i+1, |
---|
| 287 | 'FIELD' => array(''), |
---|
| 288 | 'DIRECTION' => array('ASC'), |
---|
| 289 | ); |
---|
| 290 | |
---|
| 291 | if ( isset($matches[1][$i]) ) |
---|
| 292 | { |
---|
| 293 | $tpl_image_order_select['FIELD'] = array($matches[1][$i]); |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0) |
---|
| 297 | { |
---|
| 298 | $tpl_image_order_select['DIRECTION'] = array('DESC'); |
---|
| 299 | } |
---|
| 300 | $template->append( 'image_orders', $tpl_image_order_select); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | $template->assign('image_order_choice', $image_order_choice); |
---|
| 304 | |
---|
| 305 | |
---|
[2517] | 306 | // +-----------------------------------------------------------------------+ |
---|
| 307 | // | sending html code | |
---|
| 308 | // +-----------------------------------------------------------------------+ |
---|
| 309 | |
---|
| 310 | $template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks'); |
---|
| 311 | ?> |
---|