source: trunk/admin/element_set_ranks.php @ 30975

Last change on this file since 30975 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
RevLine 
[2517]1<?php
2// +-----------------------------------------------------------------------+
[8728]3// | Piwigo - a PHP based photo gallery                                    |
[2517]4// +-----------------------------------------------------------------------+
[26461]5// | Copyright(C) 2008-2014 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  {
[25018]67    $datas[] = array(
68      'category_id' => $category_id,
69      'image_id' => $id,
70      'rank' => ++$current_rank,
[2517]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
[5759]84$image_order_choices = array('default', 'rank', 'user_define');
85$image_order_choice = 'default';
86
[2517]87if (isset($_POST['submit']))
88{
[9051]89  if (isset($_POST['rank_of_image']))
90  {
91    asort($_POST['rank_of_image'], SORT_NUMERIC);
[11893]92
[9051]93    save_images_order(
94      $page['category_id'],
95      array_keys($_POST['rank_of_image'])
96      );
[2517]97
[25018]98    $page['infos'][] = l10n('Images manual order was saved');
[9051]99  }
[5759]100
[11893]101  if (!empty($_POST['image_order_choice'])
[5759]102      && in_array($_POST['image_order_choice'], $image_order_choices))
103  {
104    $image_order_choice = $_POST['image_order_choice'];
105  }
[11893]106
[13087]107  $image_order = null;
[5759]108  if ($image_order_choice=='user_define')
109  {
[13087]110    for ($i=0; $i<3; $i++)
[5759]111    {
[13087]112      if (!empty($_POST['image_order'][$i]))
[5759]113      {
[13087]114        if (!empty($image_order)) $image_order.= ',';
115        $image_order.= $_POST['image_order'][$i];
[5759]116      }
117    }
118  }
119  elseif ($image_order_choice=='rank')
120  {
[23813]121    $image_order = 'rank ASC';
[5759]122  }
123  $query = '
[13087]124UPDATE '.CATEGORIES_TABLE.'
[18456]125  SET image_order = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').'
[5759]126  WHERE id='.$page['category_id'];
127  pwg_query($query);
[9051]128
129  if (isset($_POST['image_order_subcats']))
130  {
131    $cat_info = get_cat_info($page['category_id']);
[11893]132
[9051]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
[25018]140  $page['infos'][] = l10n('Your configuration settings are saved');
[2517]141}
142
143// +-----------------------------------------------------------------------+
144// |                             template init                             |
145// +-----------------------------------------------------------------------+
146$template->set_filenames(
[2530]147  array('element_set_ranks' => 'element_set_ranks.tpl')
[2517]148  );
149
150$base_url = get_root_url().'admin.php';
151
152$query = '
[5759]153SELECT *
[2517]154  FROM '.CATEGORIES_TABLE.'
155  WHERE id = '.$page['category_id'].'
156;';
[4325]157$category = pwg_db_fetch_assoc(pwg_query($query));
[2517]158
[28924]159if ($category['image_order']=='rank ASC')
[5759]160{
161  $image_order_choice = 'rank';
162}
[11893]163elseif ($category['image_order']!='')
[5759]164{
165  $image_order_choice = 'user_define';
166}
167
[2517]168// Navigation path
169$navigation = get_cat_display_name_cache(
170  $category['uppercats'],
[13013]171  get_root_url().'admin.php?page=album-'
[2517]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,
[9868]188    file,
[2517]189    path,
[12796]190    representative_ext,
[14198]191    width, height, rotation,
[9868]192    name,
[2517]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);
[10513]200if (pwg_db_num_rows($result) > 0)
[2517]201{
[10513]202        // template thumbnail initialization
203        $current_rank = 1;
[12796]204  $derivativeParams = ImageStdParams::get_by_type(IMG_SQUARE);
[10513]205        while ($row = pwg_db_fetch_assoc($result))
[9865]206        {
[12796]207    $derivative = new DerivativeImage($derivativeParams, new SrcImage($row));
[11893]208
[10513]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(
[14205]222                                'ID' => $row['id'],
223                                'NAME' => $thumbnail_name,
224                                'TN_SRC' => $derivative->get_url(),
225                                'RANK' => $current_rank * 10,
226                                'SIZE' => $derivative->get_size(),
[10513]227                                )
228                        );
[9865]229        }
[9864]230}
[5759]231// image order management
232$sort_fields = array(
[13087]233  ''                    => '',
[23813]234  'file ASC'            => l10n('File name, A &rarr; Z'),
[17289]235  'file DESC'           => l10n('File name, Z &rarr; A'),
[23813]236  'name ASC'            => l10n('Photo title, A &rarr; Z'),
[17289]237  'name DESC'           => l10n('Photo title, Z &rarr; A'),
238  'date_creation DESC'  => l10n('Date created, new &rarr; old'),
[23813]239  'date_creation ASC'   => l10n('Date created, old &rarr; new'),
[17289]240  'date_available DESC' => l10n('Date posted, new &rarr; old'),
[23813]241  'date_available ASC'  => l10n('Date posted, old &rarr; new'),
[17289]242  'rating_score DESC'   => l10n('Rating score, high &rarr; low'),
[23813]243  'rating_score ASC'    => l10n('Rating score, low &rarr; high'),
[17289]244  'hit DESC'            => l10n('Visits, high &rarr; low'),
[23813]245  'hit ASC'             => l10n('Visits, low &rarr; high'),
246  'id ASC'              => l10n('Numeric identifier, 1 &rarr; 9'),
[17289]247  'id DESC'             => l10n('Numeric identifier, 9 &rarr; 1'),
[23813]248  'rank ASC'            => l10n('Manual sort order'),
[5759]249  );
250
[13087]251$template->assign('image_order_options', $sort_fields);
[5759]252
[13087]253$image_order = explode(',', $category['image_order']);
[5759]254
255for ($i=0; $i<3; $i++) // 3 fields
256{
[13087]257  if ( isset($image_order[$i]) )
[5759]258  {
[13087]259    $template->append('image_order', $image_order[$i]);
[5759]260  }
[13087]261  else
[5759]262  {
[13087]263    $template->append('image_order', '');
[5759]264  }
265}
266
267$template->assign('image_order_choice', $image_order_choice);
268
269
[2517]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.