source: trunk/admin/element_set_ranks.php @ 29892

Last change on this file since 29892 was 28924, checked in by mistic100, 10 years ago

bug 3061: manual order becomes automatic order

  • Property svn:eol-style set to LF
File size: 8.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2014 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    $datas[] = array(
68      'category_id' => $category_id,
69      'image_id' => $id,
70      'rank' => ++$current_rank,
71      );
72  }
73  $fields = array(
74    'primary' => array('image_id', 'category_id'),
75    'update' => array('rank')
76    );
77  mass_updates(IMAGE_CATEGORY_TABLE, $fields, $datas);
78}
79
80// +-----------------------------------------------------------------------+
81// |                       global mode form submission                     |
82// +-----------------------------------------------------------------------+
83
84$image_order_choices = array('default', 'rank', 'user_define');
85$image_order_choice = 'default';
86
87if (isset($_POST['submit']))
88{
89  if (isset($_POST['rank_of_image']))
90  {
91    asort($_POST['rank_of_image'], SORT_NUMERIC);
92
93    save_images_order(
94      $page['category_id'],
95      array_keys($_POST['rank_of_image'])
96      );
97
98    $page['infos'][] = l10n('Images manual order was saved');
99  }
100
101  if (!empty($_POST['image_order_choice'])
102      && in_array($_POST['image_order_choice'], $image_order_choices))
103  {
104    $image_order_choice = $_POST['image_order_choice'];
105  }
106
107  $image_order = null;
108  if ($image_order_choice=='user_define')
109  {
110    for ($i=0; $i<3; $i++)
111    {
112      if (!empty($_POST['image_order'][$i]))
113      {
114        if (!empty($image_order)) $image_order.= ',';
115        $image_order.= $_POST['image_order'][$i];
116      }
117    }
118  }
119  elseif ($image_order_choice=='rank')
120  {
121    $image_order = 'rank ASC';
122  }
123  $query = '
124UPDATE '.CATEGORIES_TABLE.'
125  SET image_order = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').'
126  WHERE id='.$page['category_id'];
127  pwg_query($query);
128
129  if (isset($_POST['image_order_subcats']))
130  {
131    $cat_info = get_cat_info($page['category_id']);
132
133    $query = '
134UPDATE '.CATEGORIES_TABLE.'
135  SET image_order = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').'
136  WHERE uppercats LIKE \''.$cat_info['uppercats'].',%\'';
137    pwg_query($query);
138  }
139
140  $page['infos'][] = l10n('Your configuration settings are saved');
141}
142
143// +-----------------------------------------------------------------------+
144// |                             template init                             |
145// +-----------------------------------------------------------------------+
146$template->set_filenames(
147  array('element_set_ranks' => 'element_set_ranks.tpl')
148  );
149
150$base_url = get_root_url().'admin.php';
151
152$query = '
153SELECT *
154  FROM '.CATEGORIES_TABLE.'
155  WHERE id = '.$page['category_id'].'
156;';
157$category = pwg_db_fetch_assoc(pwg_query($query));
158
159if ($category['image_order']=='rank ASC')
160{
161  $image_order_choice = 'rank';
162}
163elseif ($category['image_order']!='')
164{
165  $image_order_choice = 'user_define';
166}
167
168// Navigation path
169$navigation = get_cat_display_name_cache(
170  $category['uppercats'],
171  get_root_url().'admin.php?page=album-'
172  );
173
174$template->assign(
175  array(
176    'CATEGORIES_NAV' => $navigation,
177    'F_ACTION' => $base_url.get_query_string_diff(array()),
178   )
179 );
180
181// +-----------------------------------------------------------------------+
182// |                              thumbnails                               |
183// +-----------------------------------------------------------------------+
184
185$query = '
186SELECT
187    id,
188    file,
189    path,
190    representative_ext,
191    width, height, rotation,
192    name,
193    rank
194  FROM '.IMAGES_TABLE.'
195    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
196  WHERE category_id = '.$page['category_id'].'
197  ORDER BY rank
198;';
199$result = pwg_query($query);
200if (pwg_db_num_rows($result) > 0)
201{
202        // template thumbnail initialization
203        $current_rank = 1;
204  $derivativeParams = ImageStdParams::get_by_type(IMG_SQUARE);
205        while ($row = pwg_db_fetch_assoc($result))
206        {
207    $derivative = new DerivativeImage($derivativeParams, new SrcImage($row));
208
209                if ( !empty( $row['name'] ) )
210                {
211                        $thumbnail_name = $row['name'];
212                }
213                else
214                {
215                        $file_wo_ext = get_filename_wo_extension($row['file']);
216                        $thumbnail_name = str_replace('_', ' ', $file_wo_ext);
217                }
218                $current_rank++;
219                $template->append(
220                        'thumbnails',
221                        array(
222                                'ID' => $row['id'],
223                                'NAME' => $thumbnail_name,
224                                'TN_SRC' => $derivative->get_url(),
225                                'RANK' => $current_rank * 10,
226                                'SIZE' => $derivative->get_size(),
227                                )
228                        );
229        }
230}
231// image order management
232$sort_fields = array(
233  ''                    => '',
234  'file ASC'            => l10n('File name, A &rarr; Z'),
235  'file DESC'           => l10n('File name, Z &rarr; A'),
236  'name ASC'            => l10n('Photo title, A &rarr; Z'),
237  'name DESC'           => l10n('Photo title, Z &rarr; A'),
238  'date_creation DESC'  => l10n('Date created, new &rarr; old'),
239  'date_creation ASC'   => l10n('Date created, old &rarr; new'),
240  'date_available DESC' => l10n('Date posted, new &rarr; old'),
241  'date_available ASC'  => l10n('Date posted, old &rarr; new'),
242  'rating_score DESC'   => l10n('Rating score, high &rarr; low'),
243  'rating_score ASC'    => l10n('Rating score, low &rarr; high'),
244  'hit DESC'            => l10n('Visits, high &rarr; low'),
245  'hit ASC'             => l10n('Visits, low &rarr; high'),
246  'id ASC'              => l10n('Numeric identifier, 1 &rarr; 9'),
247  'id DESC'             => l10n('Numeric identifier, 9 &rarr; 1'),
248  'rank ASC'            => l10n('Manual sort order'),
249  );
250
251$template->assign('image_order_options', $sort_fields);
252
253$image_order = explode(',', $category['image_order']);
254
255for ($i=0; $i<3; $i++) // 3 fields
256{
257  if ( isset($image_order[$i]) )
258  {
259    $template->append('image_order', $image_order[$i]);
260  }
261  else
262  {
263    $template->append('image_order', '');
264  }
265}
266
267$template->assign('image_order_choice', $image_order_choice);
268
269
270// +-----------------------------------------------------------------------+
271// |                          sending html code                            |
272// +-----------------------------------------------------------------------+
273
274$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks');
275?>
Note: See TracBrowser for help on using the repository browser.