source: trunk/admin/element_set_ranks.php @ 13172

Last change on this file since 13172 was 13087, checked in by mistic100, 12 years ago

feature 2568: redesigned order_by fields in Admin->Config->Options

  • Property svn:eol-style set to LF
File size: 9.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 Piwigo Team                  http://piwigo.org |
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
29if (!defined('PHPWG_ROOT_PATH'))
30{
31  die('Hacking attempt!');
32}
33
34include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
35
36// +-----------------------------------------------------------------------+
37// | Check Access and exit when user status is not ok                      |
38// +-----------------------------------------------------------------------+
39check_status(ACCESS_ADMINISTRATOR);
40
41if (!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 */
61function 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
87$image_order_choices = array('default', 'rank', 'user_define');
88$image_order_choice = 'default';
89
90if (isset($_POST['submit']))
91{
92  if (isset($_POST['rank_of_image']))
93  {
94    asort($_POST['rank_of_image'], SORT_NUMERIC);
95
96    save_images_order(
97      $page['category_id'],
98      array_keys($_POST['rank_of_image'])
99      );
100
101    array_push(
102      $page['infos'],
103      l10n('Images manual order was saved')
104      );
105  }
106
107  if (!empty($_POST['image_order_choice'])
108      && in_array($_POST['image_order_choice'], $image_order_choices))
109  {
110    $image_order_choice = $_POST['image_order_choice'];
111  }
112
113  $image_order = null;
114  if ($image_order_choice=='user_define')
115  {
116    for ($i=0; $i<3; $i++)
117    {
118      if (!empty($_POST['image_order'][$i]))
119      {
120        if (!empty($image_order)) $image_order.= ',';
121        $image_order.= $_POST['image_order'][$i];
122      }
123    }
124  }
125  elseif ($image_order_choice=='rank')
126  {
127    $image_order = 'rank';
128  }
129  $query = '
130UPDATE '.CATEGORIES_TABLE.'
131  SET image_order=\''.$image_order.'\'
132  WHERE id='.$page['category_id'];
133  pwg_query($query);
134
135  if (isset($_POST['image_order_subcats']))
136  {
137    $cat_info = get_cat_info($page['category_id']);
138
139    $query = '
140UPDATE '.CATEGORIES_TABLE.'
141  SET image_order = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').'
142  WHERE uppercats LIKE \''.$cat_info['uppercats'].',%\'';
143    pwg_query($query);
144  }
145
146  array_push($page['infos'], l10n('Your configuration settings are saved'));
147}
148
149// +-----------------------------------------------------------------------+
150// |                             template init                             |
151// +-----------------------------------------------------------------------+
152$template->set_filenames(
153  array('element_set_ranks' => 'element_set_ranks.tpl')
154  );
155
156$base_url = get_root_url().'admin.php';
157
158$query = '
159SELECT *
160  FROM '.CATEGORIES_TABLE.'
161  WHERE id = '.$page['category_id'].'
162;';
163$category = pwg_db_fetch_assoc(pwg_query($query));
164
165if ($category['image_order']=='rank')
166{
167  $image_order_choice = 'rank';
168}
169elseif ($category['image_order']!='')
170{
171  $image_order_choice = 'user_define';
172}
173
174// Navigation path
175$navigation = get_cat_display_name_cache(
176  $category['uppercats'],
177  get_root_url().'admin.php?page=album-'
178  );
179
180$template->assign(
181  array(
182    'CATEGORIES_NAV' => $navigation,
183    'F_ACTION' => $base_url.get_query_string_diff(array()),
184   )
185 );
186
187// +-----------------------------------------------------------------------+
188// |                              thumbnails                               |
189// +-----------------------------------------------------------------------+
190
191$query = '
192SELECT
193    id,
194    file,
195    path,
196    representative_ext,
197    width, height,
198    name,
199    rank
200  FROM '.IMAGES_TABLE.'
201    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
202  WHERE category_id = '.$page['category_id'].'
203  ORDER BY rank
204;';
205$result = pwg_query($query);
206if (pwg_db_num_rows($result) > 0)
207{
208        // template thumbnail initialization
209        $current_rank = 1;
210        $thumbnail_info=array();
211        $clipping=array();
212  $derivativeParams = ImageStdParams::get_by_type(IMG_SQUARE);
213        while ($row = pwg_db_fetch_assoc($result))
214        {
215    $derivative = new DerivativeImage($derivativeParams, new SrcImage($row));
216
217                $thumbnail_size = $derivative->get_size();
218                if ( !empty( $row['name'] ) )
219                {
220                        $thumbnail_name = $row['name'];
221                }
222                else
223                {
224                        $file_wo_ext = get_filename_wo_extension($row['file']);
225                        $thumbnail_name = str_replace('_', ' ', $file_wo_ext);
226                }
227                $thumbnail_info[] = array(
228                        'name'  => $thumbnail_name,
229                        'width' => $thumbnail_size[0],
230                        'height'        => $thumbnail_size[1],
231                        'id'    => $row['id'],
232                        'tn_src'        => $derivative->get_url(),
233                        'rank'  => $current_rank * 10,
234                        );
235                if ($thumbnail_size[0]<=128 and $thumbnail_size[1]<=128)
236                {
237                        $clipping[]= min($thumbnail_size[0],$thumbnail_size[1]);
238                }
239                else
240                {
241                        $clipping[]= min($thumbnail_size[0]*0.75,$thumbnail_size[1]*0.75);
242                }
243                $current_rank++;
244        }
245        $clipping=array_sum($clipping)/count($clipping);
246        foreach ($thumbnail_info as $thumbnails_info)
247        {
248                $thumbnail_x_center = $thumbnails_info['width']/2;
249                $thumbnail_y_center = $thumbnails_info['height']/2;
250                $template->append(
251                        'thumbnails',
252                        array(
253                                'ID' => $thumbnails_info['id'],
254                                'NAME' => $thumbnails_info['name'],
255                                'TN_SRC' => $thumbnails_info['tn_src'],
256                                'RANK' => $thumbnails_info['rank'],
257                                'CLIPING' => round($clipping),
258                                'CLIPING_li' => round($clipping+8),
259                                'CLIP_TOP' => round($thumbnail_y_center - $clipping/2),
260                                'CLIP_RIGHT' => round($thumbnail_x_center + $clipping/2),
261                                'CLIP_BOTTOM' => round($thumbnail_y_center + $clipping/2),
262                                'CLIP_LEFT' => round($thumbnail_x_center - $clipping/2)
263                                )
264                        );
265        }
266}
267// image order management
268$sort_fields = array(
269  ''                    => '',
270  'file'                => l10n('file name, A &rarr; Z'),
271  'file DESC'           => l10n('file name, Z &rarr; A'),
272  'name'                => l10n('photo title, A &rarr; Z'),
273  'name DESC'           => l10n('photo title, Z &rarr; A'),
274  'date_creation DESC'  => l10n('date created, new &rarr; old'),
275  'date_creation'       => l10n('date created, old &rarr; new'),
276  'date_available DESC' => l10n('date posted, new &rarr; old'),
277  'date_available'      => l10n('date posted, old &rarr; new'),
278  'rating_score DESC'   => l10n('rating score, high &rarr; low'),
279  'rating_score'        => l10n('rating score, low &rarr; high'),
280  'hit DESC'            => l10n('visits, high &rarr; low'),
281  'hit'                 => l10n('visits, low &rarr; high'),
282  'id'                  => l10n('numeric identifier, 1 &rarr; 9'),
283  'id DESC'             => l10n('numeric identifier, 9 &rarr; 1'),
284  'rank'                => l10n('manual sort order'),
285  );
286
287$template->assign('image_order_options', $sort_fields);
288
289$image_order = explode(',', $category['image_order']);
290
291for ($i=0; $i<3; $i++) // 3 fields
292{
293  if ( isset($image_order[$i]) )
294  {
295    $template->append('image_order', $image_order[$i]);
296  }
297  else
298  {
299    $template->append('image_order', '');
300  }
301}
302
303$template->assign('image_order_choice', $image_order_choice);
304
305
306// +-----------------------------------------------------------------------+
307// |                          sending html code                            |
308// +-----------------------------------------------------------------------+
309
310$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks');
311?>
Note: See TracBrowser for help on using the repository browser.