source: trunk/admin/element_set_ranks.php @ 9051

Last change on this file since 9051 was 9051, checked in by plg, 13 years ago

bug 2172 fixed: the "sort order" options are removed from the album administration main page.

bug 2173 fixed: rename "manual order" into "automatic order" and "by rank" into "manual order"

bug 2174 fixed: only show the automatic order options when it's relevant

new icon for the "manage photo order" screen

hide the numeric rank below the thumbnail (it is confusing for users)

tell users that you can drag'n drop to reorder photos

bug fixed: the "apply to sub-albums" options for photo orders was not working

  • Property svn:eol-style set to LF
File size: 8.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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  $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 = '
137UPDATE '.CATEGORIES_TABLE.' SET image_order=\''.$image_order.'\'
138  WHERE id='.$page['category_id'];
139  pwg_query($query);
140
141  if (isset($_POST['image_order_subcats']))
142  {
143    $cat_info = get_cat_info($page['category_id']);
144   
145    $query = '
146UPDATE '.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'));
153}
154
155// +-----------------------------------------------------------------------+
156// |                             template init                             |
157// +-----------------------------------------------------------------------+
158$template->set_filenames(
159  array('element_set_ranks' => 'element_set_ranks.tpl')
160  );
161
162$base_url = get_root_url().'admin.php';
163
164$query = '
165SELECT *
166  FROM '.CATEGORIES_TABLE.'
167  WHERE id = '.$page['category_id'].'
168;';
169$category = pwg_db_fetch_assoc(pwg_query($query));
170
171if ($category['image_order']=='rank')
172{
173  $image_order_choice = 'rank';
174}
175elseif ($category['image_order']!='') 
176{
177  $image_order_choice = 'user_define';
178}
179
180// Navigation path
181$navigation = get_cat_display_name_cache(
182  $category['uppercats'],
183  get_root_url().'admin.php?page=cat_modify&amp;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 = '
198SELECT
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;
212
213while ($row = pwg_db_fetch_assoc($result))
214{
215  $src = get_thumbnail_url($row);
216
217  list($thumbnail_width, $thumbnail_height) = getimagesize($src);
218  $thumbnail_x_center = $thumbnail_width/2;
219  $thumbnail_y_center = $thumbnail_height/2;
220 
221  $template->append(
222    'thumbnails',
223    array(
224      'ID' => $row['id'],
225      'TN_SRC' => $src,
226      'RANK' => $current_rank * 10,
227      'CLIP_TOP' => round($thumbnail_y_center - 96/2),
228      'CLIP_RIGHT' => round($thumbnail_x_center + 96/2),
229      'CLIP_BOTTOM' => round($thumbnail_y_center + 96/2),
230      'CLIP_LEFT' => round($thumbnail_x_center - 96/2)
231      )
232    );
233
234  $current_rank++;
235}
236
237// image order management
238$sort_fields = array(
239  '' => '',
240  'date_creation' => l10n('Creation date'),
241  'date_available' => l10n('Post date'),
242  'average_rate' => l10n('Average rate'),
243  'hit' => l10n('Most visited'),
244  'file' => l10n('File name'),
245  'id' => 'Id',
246  'rank' => l10n('Rank'),
247  );
248
249$sort_directions = array(
250  'ASC' => l10n('ascending'),
251  'DESC' => l10n('descending'),
252  );
253
254$template->assign('image_order_field_options', $sort_fields);
255$template->assign('image_order_direction_options', $sort_directions);
256
257$matches = array();
258if ( !empty( $category['image_order'] ) )
259{
260  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
261    $category['image_order'], $matches);
262}
263
264for ($i=0; $i<3; $i++) // 3 fields
265{
266  $tpl_image_order_select = array(
267      'ID' => $i+1,
268      'FIELD' => array(''),
269      'DIRECTION' => array('ASC'),
270    );
271
272  if ( isset($matches[1][$i]) )
273  {
274    $tpl_image_order_select['FIELD'] = array($matches[1][$i]);
275  }
276
277  if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0)
278  {
279    $tpl_image_order_select['DIRECTION'] = array('DESC');
280  }
281  $template->append( 'image_orders', $tpl_image_order_select);
282}
283
284$template->assign('image_order_choice', $image_order_choice);
285
286
287// +-----------------------------------------------------------------------+
288// |                          sending html code                            |
289// +-----------------------------------------------------------------------+
290
291$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks');
292?>
Note: See TracBrowser for help on using the repository browser.