source: branches/2.1/admin/element_set_ranks.php @ 6364

Last change on this file since 6364 was 6364, checked in by plg, 14 years ago

remove all svn:mergeinfo properties

  • Property svn:eol-style set to LF
File size: 8.0 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based picture gallery                                  |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2010 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  asort($_POST['rank_of_image'], SORT_NUMERIC);
93 
94  save_images_order(
95    $page['category_id'],
96    array_keys($_POST['rank_of_image'])
97    );
98
99  array_push(
100    $page['infos'],
101    l10n('Images manual order was saved')
102    );
103
104  $image_order = null;
105  if (!empty($_POST['image_order_choice']) 
106      && in_array($_POST['image_order_choice'], $image_order_choices))
107  {
108    $image_order_choice = $_POST['image_order_choice'];
109  }
110     
111  if ($image_order_choice=='user_define')
112  {
113    for ($i=1; $i<=3; $i++)
114    {
115      if ( !empty($_POST['order_field_'.$i]) )
116      {
117        if (! empty($image_order) )
118        {
119          $image_order .= ',';
120        }
121        $image_order .= $_POST['order_field_'.$i];
122        if ($_POST['order_direction_'.$i]=='DESC')
123        {
124          $image_order .= ' DESC';
125        }
126      }
127    }
128  }
129  elseif ($image_order_choice=='rank')
130  {
131    $image_order = 'rank';
132  }
133  $query = '
134UPDATE '.CATEGORIES_TABLE.' SET image_order=\''.$image_order.'\'
135  WHERE id='.$page['category_id'];
136  pwg_query($query);
137}
138
139// +-----------------------------------------------------------------------+
140// |                             template init                             |
141// +-----------------------------------------------------------------------+
142$template->set_filenames(
143  array('element_set_ranks' => 'element_set_ranks.tpl')
144  );
145
146$base_url = get_root_url().'admin.php';
147
148$query = '
149SELECT *
150  FROM '.CATEGORIES_TABLE.'
151  WHERE id = '.$page['category_id'].'
152;';
153$category = pwg_db_fetch_assoc(pwg_query($query));
154
155if ($category['image_order']=='rank')
156{
157  $image_order_choice = 'rank';
158}
159elseif ($category['image_order']!='') 
160{
161  $image_order_choice = 'user_define';
162}
163
164// Navigation path
165$navigation = get_cat_display_name_cache(
166  $category['uppercats'],
167  get_root_url().'admin.php?page=cat_modify&amp;cat_id='
168  );
169
170$template->assign(
171  array(
172    'CATEGORIES_NAV' => $navigation,
173    'F_ACTION' => $base_url.get_query_string_diff(array()),
174   )
175 );
176
177// +-----------------------------------------------------------------------+
178// |                              thumbnails                               |
179// +-----------------------------------------------------------------------+
180
181$query = '
182SELECT
183    id,
184    path,
185    tn_ext,
186    rank
187  FROM '.IMAGES_TABLE.'
188    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
189  WHERE category_id = '.$page['category_id'].'
190  ORDER BY rank
191;';
192$result = pwg_query($query);
193
194// template thumbnail initialization
195$current_rank = 1;
196
197while ($row = pwg_db_fetch_assoc($result))
198{
199  $src = get_thumbnail_url($row);
200
201  list($thumbnail_width, $thumbnail_height) = getimagesize($src);
202  $thumbnail_x_center = $thumbnail_width/2;
203  $thumbnail_y_center = $thumbnail_height/2;
204 
205  $template->append(
206    'thumbnails',
207    array(
208      'ID' => $row['id'],
209      'TN_SRC' => $src,
210      'RANK' => $current_rank * 10,
211      'CLIP_TOP' => round($thumbnail_y_center - 96/2),
212      'CLIP_RIGHT' => round($thumbnail_x_center + 96/2),
213      'CLIP_BOTTOM' => round($thumbnail_y_center + 96/2),
214      'CLIP_LEFT' => round($thumbnail_x_center - 96/2)
215      )
216    );
217
218  $current_rank++;
219}
220
221// image order management
222$sort_fields = array(
223  '' => '',
224  'date_creation' => l10n('Creation date'),
225  'date_available' => l10n('Post date'),
226  'average_rate' => l10n('Average rate'),
227  'hit' => l10n('Most visited'),
228  'file' => l10n('File name'),
229  'id' => 'Id',
230  'rank' => l10n('Rank'),
231  );
232
233$sort_directions = array(
234  'ASC' => l10n('ascending'),
235  'DESC' => l10n('descending'),
236  );
237
238$template->assign('image_order_field_options', $sort_fields);
239$template->assign('image_order_direction_options', $sort_directions);
240
241$matches = array();
242if ( !empty( $category['image_order'] ) )
243{
244  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
245    $category['image_order'], $matches);
246}
247
248for ($i=0; $i<3; $i++) // 3 fields
249{
250  $tpl_image_order_select = array(
251      'ID' => $i+1,
252      'FIELD' => array(''),
253      'DIRECTION' => array('ASC'),
254    );
255
256  if ( isset($matches[1][$i]) )
257  {
258    $tpl_image_order_select['FIELD'] = array($matches[1][$i]);
259  }
260
261  if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0)
262  {
263    $tpl_image_order_select['DIRECTION'] = array('DESC');
264  }
265  $template->append( 'image_orders', $tpl_image_order_select);
266}
267
268$template->assign('image_order_choice', $image_order_choice);
269
270
271// +-----------------------------------------------------------------------+
272// |                          sending html code                            |
273// +-----------------------------------------------------------------------+
274
275$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks');
276?>
Note: See TracBrowser for help on using the repository browser.