source: branches/2.2/admin/element_set_ranks.php @ 11040

Last change on this file since 11040 was 10711, checked in by patdenice, 13 years ago

Division by zero corrected on element_set_ranks.php if no thumbnails to display.

File size: 9.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 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  $image_order = null;
108  if (!empty($_POST['image_order_choice']) 
109      && in_array($_POST['image_order_choice'], $image_order_choices))
110  {
111    $image_order_choice = $_POST['image_order_choice'];
112  }
113     
114  if ($image_order_choice=='user_define')
115  {
116    for ($i=1; $i<=3; $i++)
117    {
118      if ( !empty($_POST['order_field_'.$i]) )
119      {
120        if (! empty($image_order) )
121        {
122          $image_order .= ',';
123        }
124        $image_order .= $_POST['order_field_'.$i];
125        if ($_POST['order_direction_'.$i]=='DESC')
126        {
127          $image_order .= ' DESC';
128        }
129      }
130    }
131  }
132  elseif ($image_order_choice=='rank')
133  {
134    $image_order = 'rank';
135  }
136  $query = '
137UPDATE '.CATEGORIES_TABLE.' SET image_order=\''.$image_order.'\'
138  WHERE id='.$page['category_id'];
139  pwg_query($query);
140
141  if (isset($_POST['image_order_subcats']))
142  {
143    $cat_info = get_cat_info($page['category_id']);
144   
145    $query = '
146UPDATE '.CATEGORIES_TABLE.'
147  SET image_order = '.(isset($image_order) ? '\''.$image_order.'\'' : 'NULL').'
148  WHERE uppercats LIKE \''.$cat_info['uppercats'].',%\'';
149    pwg_query($query);
150  }
151
152  array_push($page['infos'], l10n('Your configuration settings are saved'));
153}
154
155// +-----------------------------------------------------------------------+
156// |                             template init                             |
157// +-----------------------------------------------------------------------+
158$template->set_filenames(
159  array('element_set_ranks' => 'element_set_ranks.tpl')
160  );
161
162$base_url = get_root_url().'admin.php';
163
164$query = '
165SELECT *
166  FROM '.CATEGORIES_TABLE.'
167  WHERE id = '.$page['category_id'].'
168;';
169$category = pwg_db_fetch_assoc(pwg_query($query));
170
171if ($category['image_order']=='rank')
172{
173  $image_order_choice = 'rank';
174}
175elseif ($category['image_order']!='') 
176{
177  $image_order_choice = 'user_define';
178}
179
180// Navigation path
181$navigation = get_cat_display_name_cache(
182  $category['uppercats'],
183  get_root_url().'admin.php?page=cat_modify&amp;cat_id='
184  );
185
186$template->assign(
187  array(
188    'CATEGORIES_NAV' => $navigation,
189    'F_ACTION' => $base_url.get_query_string_diff(array()),
190   )
191 );
192
193// +-----------------------------------------------------------------------+
194// |                              thumbnails                               |
195// +-----------------------------------------------------------------------+
196
197$query = '
198SELECT
199    id,
200    file,
201    path,
202    tn_ext,
203    name,
204    rank
205  FROM '.IMAGES_TABLE.'
206    JOIN '.IMAGE_CATEGORY_TABLE.' ON image_id = id
207  WHERE category_id = '.$page['category_id'].'
208  ORDER BY rank
209;';
210$result = pwg_query($query);
211
212// template thumbnail initialization
213$current_rank = 1;
214$thumbnail_info=array();
215$clipping=array();
216while ($row = pwg_db_fetch_assoc($result))
217{
218  $src = get_thumbnail_url($row);
219       
220        $thumbnail_size = getimagesize($src);
221  if ( !empty( $row['name'] ) )
222  {
223    $thumbnail_name = $row['name'];
224  }
225  else
226  {
227    $file_wo_ext = get_filename_wo_extension($row['file']);
228        $thumbnail_name = str_replace('_', ' ', $file_wo_ext);
229  }
230        $thumbnail_info[] = array(
231                'name'  => $thumbnail_name,
232                'width' => $thumbnail_size[0],
233                'height'        => $thumbnail_size[1],
234                'id'    => $row['id'],
235                'tn_src'        => $src,
236                'rank'  => $current_rank * 10,
237                );
238        if ($thumbnail_size[0]<=128 and $thumbnail_size[1]<=128)
239        {
240                $clipping[]= min($thumbnail_size[0],$thumbnail_size[1]);
241        }
242        else
243        {
244                $clipping[]= min($thumbnail_size[0]*0.75,$thumbnail_size[1]*0.75);
245        }
246  $current_rank++;
247}
248if (count($clipping) > 0)
249{
250  $clipping=array_sum($clipping)/count($clipping);
251}
252foreach ($thumbnail_info as $thumbnails_info)
253{
254  $thumbnail_x_center = $thumbnails_info['width']/2;
255  $thumbnail_y_center = $thumbnails_info['height']/2;
256  $template->append(
257    'thumbnails',
258    array(
259      'ID' => $thumbnails_info['id'],
260      'NAME' => $thumbnails_info['name'],
261      'TN_SRC' => $thumbnails_info['tn_src'],
262      'RANK' => $thumbnails_info['rank'],
263      'CLIPING' => round($clipping),
264      'CLIPING_li' => round($clipping+8),
265      'CLIP_TOP' => round($thumbnail_y_center - $clipping/2),
266      'CLIP_RIGHT' => round($thumbnail_x_center + $clipping/2),
267      'CLIP_BOTTOM' => round($thumbnail_y_center + $clipping/2),
268      'CLIP_LEFT' => round($thumbnail_x_center - $clipping/2)
269      )
270    );
271}
272// image order management
273$sort_fields = array(
274  '' => '',
275  'date_creation' => l10n('Creation date'),
276  'date_available' => l10n('Post date'),
277  'average_rate' => l10n('Average rate'),
278  'hit' => l10n('Most visited'),
279  'file' => l10n('File name'),
280  'id' => 'Id',
281  'rank' => l10n('Rank'),
282  );
283
284$sort_directions = array(
285  'ASC' => l10n('ascending'),
286  'DESC' => l10n('descending'),
287  );
288
289$template->assign('image_order_field_options', $sort_fields);
290$template->assign('image_order_direction_options', $sort_directions);
291
292$matches = array();
293if ( !empty( $category['image_order'] ) )
294{
295  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
296    $category['image_order'], $matches);
297}
298
299for ($i=0; $i<3; $i++) // 3 fields
300{
301  $tpl_image_order_select = array(
302      'ID' => $i+1,
303      'FIELD' => array(''),
304      'DIRECTION' => array('ASC'),
305    );
306
307  if ( isset($matches[1][$i]) )
308  {
309    $tpl_image_order_select['FIELD'] = array($matches[1][$i]);
310  }
311
312  if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0)
313  {
314    $tpl_image_order_select['DIRECTION'] = array('DESC');
315  }
316  $template->append( 'image_orders', $tpl_image_order_select);
317}
318
319$template->assign('image_order_choice', $image_order_choice);
320
321
322// +-----------------------------------------------------------------------+
323// |                          sending html code                            |
324// +-----------------------------------------------------------------------+
325
326$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks');
327?>
Note: See TracBrowser for help on using the repository browser.