source: trunk/admin/element_set_ranks.php @ 18456

Last change on this file since 18456 was 18456, checked in by rvelices, 11 years ago

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
File size: 8.5 KB
RevLine 
[2517]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2517]4// +-----------------------------------------------------------------------+
[12922]5// | Copyright(C) 2008-2012 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
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
[5759]87$image_order_choices = array('default', 'rank', 'user_define');
88$image_order_choice = 'default';
89
[2517]90if (isset($_POST['submit']))
91{
[9051]92  if (isset($_POST['rank_of_image']))
93  {
94    asort($_POST['rank_of_image'], SORT_NUMERIC);
[11893]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
[11893]107  if (!empty($_POST['image_order_choice'])
[5759]108      && in_array($_POST['image_order_choice'], $image_order_choices))
109  {
110    $image_order_choice = $_POST['image_order_choice'];
111  }
[11893]112
[13087]113  $image_order = null;
[5759]114  if ($image_order_choice=='user_define')
115  {
[13087]116    for ($i=0; $i<3; $i++)
[5759]117    {
[13087]118      if (!empty($_POST['image_order'][$i]))
[5759]119      {
[13087]120        if (!empty($image_order)) $image_order.= ',';
121        $image_order.= $_POST['image_order'][$i];
[5759]122      }
123    }
124  }
125  elseif ($image_order_choice=='rank')
126  {
127    $image_order = 'rank';
128  }
129  $query = '
[13087]130UPDATE '.CATEGORIES_TABLE.'
[18456]131  SET image_order = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').'
[5759]132  WHERE id='.$page['category_id'];
133  pwg_query($query);
[9051]134
135  if (isset($_POST['image_order_subcats']))
136  {
137    $cat_info = get_cat_info($page['category_id']);
[11893]138
[9051]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'));
[2517]147}
148
149// +-----------------------------------------------------------------------+
150// |                             template init                             |
151// +-----------------------------------------------------------------------+
152$template->set_filenames(
[2530]153  array('element_set_ranks' => 'element_set_ranks.tpl')
[2517]154  );
155
156$base_url = get_root_url().'admin.php';
157
158$query = '
[5759]159SELECT *
[2517]160  FROM '.CATEGORIES_TABLE.'
161  WHERE id = '.$page['category_id'].'
162;';
[4325]163$category = pwg_db_fetch_assoc(pwg_query($query));
[2517]164
[5759]165if ($category['image_order']=='rank')
166{
167  $image_order_choice = 'rank';
168}
[11893]169elseif ($category['image_order']!='')
[5759]170{
171  $image_order_choice = 'user_define';
172}
173
[2517]174// Navigation path
175$navigation = get_cat_display_name_cache(
176  $category['uppercats'],
[13013]177  get_root_url().'admin.php?page=album-'
[2517]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,
[9868]194    file,
[2517]195    path,
[12796]196    representative_ext,
[14198]197    width, height, rotation,
[9868]198    name,
[2517]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);
[10513]206if (pwg_db_num_rows($result) > 0)
[2517]207{
[10513]208        // template thumbnail initialization
209        $current_rank = 1;
[12796]210  $derivativeParams = ImageStdParams::get_by_type(IMG_SQUARE);
[10513]211        while ($row = pwg_db_fetch_assoc($result))
[9865]212        {
[12796]213    $derivative = new DerivativeImage($derivativeParams, new SrcImage($row));
[11893]214
[10513]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(
[14205]228                                'ID' => $row['id'],
229                                'NAME' => $thumbnail_name,
230                                'TN_SRC' => $derivative->get_url(),
231                                'RANK' => $current_rank * 10,
232                                'SIZE' => $derivative->get_size(),
[10513]233                                )
234                        );
[9865]235        }
[9864]236}
[5759]237// image order management
238$sort_fields = array(
[13087]239  ''                    => '',
[17289]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'),
[5759]255  );
256
[13087]257$template->assign('image_order_options', $sort_fields);
[5759]258
[13087]259$image_order = explode(',', $category['image_order']);
[5759]260
261for ($i=0; $i<3; $i++) // 3 fields
262{
[13087]263  if ( isset($image_order[$i]) )
[5759]264  {
[13087]265    $template->append('image_order', $image_order[$i]);
[5759]266  }
[13087]267  else
[5759]268  {
[13087]269    $template->append('image_order', '');
[5759]270  }
271}
272
273$template->assign('image_order_choice', $image_order_choice);
274
275
[2517]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.