source: trunk/admin/element_set_ranks.php @ 18456

Revision 18456, 8.5 KB checked in by rvelices, 7 years ago (diff)

fix - when applying default image order to an album , same behaviour (null versus empty string db field) for parent and children

  • Property svn:eol-style set to LF
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 = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').'
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, rotation,
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  $derivativeParams = ImageStdParams::get_by_type(IMG_SQUARE);
211        while ($row = pwg_db_fetch_assoc($result))
212        {
213    $derivative = new DerivativeImage($derivativeParams, new SrcImage($row));
214
215                if ( !empty( $row['name'] ) )
216                {
217                        $thumbnail_name = $row['name'];
218                }
219                else
220                {
221                        $file_wo_ext = get_filename_wo_extension($row['file']);
222                        $thumbnail_name = str_replace('_', ' ', $file_wo_ext);
223                }
224                $current_rank++;
225                $template->append(
226                        'thumbnails',
227                        array(
228                                'ID' => $row['id'],
229                                'NAME' => $thumbnail_name,
230                                'TN_SRC' => $derivative->get_url(),
231                                'RANK' => $current_rank * 10,
232                                'SIZE' => $derivative->get_size(),
233                                )
234                        );
235        }
236}
237// image order management
238$sort_fields = array(
239  ''                    => '',
240  'file'                => l10n('File name, A &rarr; Z'),
241  'file DESC'           => l10n('File name, Z &rarr; A'),
242  'name'                => l10n('Photo title, A &rarr; Z'),
243  'name DESC'           => l10n('Photo title, Z &rarr; A'),
244  'date_creation DESC'  => l10n('Date created, new &rarr; old'),
245  'date_creation'       => l10n('Date created, old &rarr; new'),
246  'date_available DESC' => l10n('Date posted, new &rarr; old'),
247  'date_available'      => l10n('Date posted, old &rarr; new'),
248  'rating_score DESC'   => l10n('Rating score, high &rarr; low'),
249  'rating_score'        => l10n('Rating score, low &rarr; high'),
250  'hit DESC'            => l10n('Visits, high &rarr; low'),
251  'hit'                 => l10n('Visits, low &rarr; high'),
252  'id'                  => l10n('Numeric identifier, 1 &rarr; 9'),
253  'id DESC'             => l10n('Numeric identifier, 9 &rarr; 1'),
254  'rank'                => l10n('Manual sort order'),
255  );
256
257$template->assign('image_order_options', $sort_fields);
258
259$image_order = explode(',', $category['image_order']);
260
261for ($i=0; $i<3; $i++) // 3 fields
262{
263  if ( isset($image_order[$i]) )
264  {
265    $template->append('image_order', $image_order[$i]);
266  }
267  else
268  {
269    $template->append('image_order', '');
270  }
271}
272
273$template->assign('image_order_choice', $image_order_choice);
274
275
276// +-----------------------------------------------------------------------+
277// |                          sending html code                            |
278// +-----------------------------------------------------------------------+
279
280$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks');
281?>
Note: See TracBrowser for help on using the repository browser.