source: trunk/admin/element_set_ranks.php @ 12259

Last change on this file since 12259 was 11893, checked in by rvelices, 13 years ago

rename #images.average_rate to rating_score

  • Property svn:eol-style set to LF
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);
211if (pwg_db_num_rows($result) > 0)
212{
213        // template thumbnail initialization
214        $current_rank = 1;
215        $thumbnail_info=array();
216        $clipping=array();
217        while ($row = pwg_db_fetch_assoc($result))
218        {
219                $src = get_thumbnail_url($row);
220
221                $thumbnail_size = getimagesize($src);
222                if ( !empty( $row['name'] ) )
223                {
224                        $thumbnail_name = $row['name'];
225                }
226                else
227                {
228                        $file_wo_ext = get_filename_wo_extension($row['file']);
229                        $thumbnail_name = str_replace('_', ' ', $file_wo_ext);
230                }
231                $thumbnail_info[] = array(
232                        'name'  => $thumbnail_name,
233                        'width' => $thumbnail_size[0],
234                        'height'        => $thumbnail_size[1],
235                        'id'    => $row['id'],
236                        'tn_src'        => $src,
237                        'rank'  => $current_rank * 10,
238                        );
239                if ($thumbnail_size[0]<=128 and $thumbnail_size[1]<=128)
240                {
241                        $clipping[]= min($thumbnail_size[0],$thumbnail_size[1]);
242                }
243                else
244                {
245                        $clipping[]= min($thumbnail_size[0]*0.75,$thumbnail_size[1]*0.75);
246                }
247                $current_rank++;
248        }
249        $clipping=array_sum($clipping)/count($clipping);
250        foreach ($thumbnail_info as $thumbnails_info)
251        {
252                $thumbnail_x_center = $thumbnails_info['width']/2;
253                $thumbnail_y_center = $thumbnails_info['height']/2;
254                $template->append(
255                        'thumbnails',
256                        array(
257                                'ID' => $thumbnails_info['id'],
258                                'NAME' => $thumbnails_info['name'],
259                                'TN_SRC' => $thumbnails_info['tn_src'],
260                                'RANK' => $thumbnails_info['rank'],
261                                'CLIPING' => round($clipping),
262                                'CLIPING_li' => round($clipping+8),
263                                'CLIP_TOP' => round($thumbnail_y_center - $clipping/2),
264                                'CLIP_RIGHT' => round($thumbnail_x_center + $clipping/2),
265                                'CLIP_BOTTOM' => round($thumbnail_y_center + $clipping/2),
266                                'CLIP_LEFT' => round($thumbnail_x_center - $clipping/2)
267                                )
268                        );
269        }
270}
271// image order management
272$sort_fields = array(
273  '' => '',
274  'date_creation' => l10n('Creation date'),
275  'date_available' => l10n('Post date'),
276  'rating_score' => l10n('Rating score'),
277  'hit' => l10n('Most visited'),
278  'file' => l10n('File name'),
279  'id' => 'Id',
280  'rank' => l10n('Rank'),
281  );
282
283$sort_directions = array(
284  'ASC' => l10n('ascending'),
285  'DESC' => l10n('descending'),
286  );
287
288$template->assign('image_order_field_options', $sort_fields);
289$template->assign('image_order_direction_options', $sort_directions);
290
291$matches = array();
292if ( !empty( $category['image_order'] ) )
293{
294  preg_match_all('/([a-z_]+) *(?:(asc|desc)(?:ending)?)? *(?:, *|$)/i',
295    $category['image_order'], $matches);
296}
297
298for ($i=0; $i<3; $i++) // 3 fields
299{
300  $tpl_image_order_select = array(
301      'ID' => $i+1,
302      'FIELD' => array(''),
303      'DIRECTION' => array('ASC'),
304    );
305
306  if ( isset($matches[1][$i]) )
307  {
308    $tpl_image_order_select['FIELD'] = array($matches[1][$i]);
309  }
310
311  if (isset($matches[2][$i]) and strcasecmp($matches[2][$i],'DESC')==0)
312  {
313    $tpl_image_order_select['DIRECTION'] = array('DESC');
314  }
315  $template->append( 'image_orders', $tpl_image_order_select);
316}
317
318$template->assign('image_order_choice', $image_order_choice);
319
320
321// +-----------------------------------------------------------------------+
322// |                          sending html code                            |
323// +-----------------------------------------------------------------------+
324
325$template->assign_var_from_handle('ADMIN_CONTENT', 'element_set_ranks');
326?>
Note: See TracBrowser for help on using the repository browser.