source: extensions/SmartAlbums/admin/album.php @ 21358

Last change on this file since 21358 was 21358, checked in by mistic100, 11 years ago

smart_is_forbiden now false for default value, open good accordion menu, use global_rank on smartalbums list

File size: 11.3 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2012 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
24if(!defined("PHPWG_ROOT_PATH")) die ("Hacking attempt!");
25
26// +-----------------------------------------------------------------------+
27// | Basic checks                                                          |
28// +-----------------------------------------------------------------------+
29
30check_status(ACCESS_ADMINISTRATOR);
31
32$page['active_menu'] = get_active_menu('album');
33
34check_input_parameter('cat_id', $_GET, false, PATTERN_ID);
35
36$admin_album_base_url = get_root_url().'admin.php?page=album-'.$_GET['cat_id'];
37$self_url = SMART_ADMIN.'-album&amp;cat_id='.$_GET['cat_id'];
38
39$query = '
40SELECT *
41  FROM '.CATEGORIES_TABLE.'
42  WHERE id = '.$_GET['cat_id'].'
43;';
44$category = pwg_db_fetch_assoc(pwg_query($query));
45
46if (!isset($category['id']))
47{
48  die("unknown album");
49}
50
51// +-----------------------------------------------------------------------+
52// | Tabs                                                                  |
53// +-----------------------------------------------------------------------+
54
55include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
56$tabsheet = new tabsheet();
57$tabsheet->set_id('album');
58$tabsheet->select('smartalbum');
59$tabsheet->assign();
60
61
62$cat_id = $_GET['cat_id'];
63
64// category must be virtual
65if ($category['dir'] != NULL)
66{
67  die("physical album");
68}
69
70// +-----------------------------------------------------------------------+
71// | Save Filters                                                          |
72// +-----------------------------------------------------------------------+
73
74if (isset($_POST['submitFilters']))
75{
76  if (defined('SMART_DEBUG'))
77  {
78    var_dump($_POST['filters']);
79  }
80 
81  // test if it was a Smart Album
82  $query = '
83SELECT DISTINCT category_id
84  FROM '.CATEGORY_FILTERS_TABLE.'
85  WHERE category_id = '.$cat_id.'
86;';
87  $was_smart = pwg_db_num_rows(pwg_query($query));
88 
89  /* this album is no longer a SmartAlbum */
90  if ( $was_smart AND !isset($_POST['is_smart']) )
91  {
92    pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;');
93    pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
94    set_random_representant(array($cat_id));
95    invalidate_user_cache();
96  }
97  /* no filter selected */
98  else if ( isset($_POST['is_smart']) AND empty($_POST['filters']) )
99  {
100    array_push($page['errors'], l10n('No filter selected'));
101  }
102  /* everything is fine */
103  else if ( isset($_POST['is_smart']) )
104  {
105    pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
106   
107    $inserts = array();
108    foreach ($_POST['filters'] as $filter)
109    {
110      if (($filter = smart_check_filter($filter)) != false)
111      {
112        $filter['category_id'] = $cat_id;
113        $inserts[] = $filter;
114      }
115    }
116   
117    mass_inserts(
118      CATEGORY_FILTERS_TABLE, 
119      array('category_id', 'type', 'cond', 'value'), 
120      $inserts,
121      array('ignore'=>true)
122      );
123   
124    $associated_images = smart_make_associations($cat_id);
125    $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', count($associated_images)));
126   
127    array_push($page['infos'], sprintf(l10n('%d photos associated to album %s'), count($associated_images), ''));
128   
129    define('SMART_NOT_UPDATE', 1);
130    invalidate_user_cache();
131  }
132}
133
134// +-----------------------------------------------------------------------+
135// | Display page                                                          |
136// +-----------------------------------------------------------------------+
137
138/* select options, for html_options */
139$options = array(
140  'tags' => array(
141    'name' => l10n('Tags'),
142    'options' => array(
143      'all'   => l10n('All these tags'),
144      'one'   => l10n('One of these tags'),
145      'none'  => l10n('None of these tags'),
146      'only'  => l10n('Only these tags'),
147      ),
148    ),
149  'date' => array(
150    'name' => l10n('Date'),
151    'options' => array(
152      'the_post'     => l10n('Added on'),
153      'before_post'  => l10n('Added before'),
154      'after_post'   => l10n('Added after'),
155      'the_taken'    => l10n('Created on'),
156      'before_taken' => l10n('Created before'),
157      'after_taken'  => l10n('Created after'),
158      ),
159    ),
160  'name' => array(
161    'name' => l10n('Photo name'),
162    'options' => array(
163      'contain'     => l10n('Contains'),
164      'begin'       => l10n('Begins with'),
165      'end'         => l10n('Ends with'),
166      'not_contain' => l10n('Doesn\'t contain'),
167      'not_begin'   => l10n('Doesn\'t begin with'),
168      'not_end'     => l10n('Doesn\'t end with'),
169      'regex'       => l10n('Regular expression'),
170      ),
171    ),
172  'album' => array(
173    'name' => l10n('Album'),
174    'options' => array(
175      'all'   => l10n('All these albums'),
176      'one'   => l10n('One of these albums'),
177      'none'  => l10n('None of these albums'),
178      'only'  => l10n('Only these albums'),
179      ),
180    ),
181  'dimensions' => array(
182    'name' => l10n('Dimensions'),
183    'options' => array(
184      'width'  => l10n('Width'),
185      'height' => l10n('Height'),
186      'ratio'  => l10n('Ratio').' ('.l10n('Width').'/'.l10n('Height').')',
187      ),
188    ),
189  'author' => array(
190    'name' => l10n('Author'),
191    'options' => array(
192      'is'      => l10n('Is'),
193      'in'      => l10n('Is in'),
194      'not_is'  => l10n('Is not'),
195      'not_in'  => l10n('Is not in'),
196      'regex'   => l10n('Regular expression'),
197      ),
198    ),
199  'hit' => array(
200    'name' => l10n('Hits'),
201    'options' => array(
202      'less' => l10n('Bellow'),
203      'more' => l10n('Above'),
204      ),
205    ),
206  'rating_score' => array(
207    'name' => l10n('Rating score'),
208    'options' => array(
209      'less' => l10n('Bellow'),
210      'more' => l10n('Above'),
211      ),
212    ),
213  'level' => array(
214    'name' => l10n('Privacy level'),
215    'options' => array(),
216    ),
217  'limit' => array(
218    'name' => l10n('Max. number of photos'),
219    'options' => array(),
220    ),
221  );
222$template->assign('options', $options);
223
224
225/* get filters for this album */
226$query = '
227SELECT *
228  FROM '.CATEGORY_FILTERS_TABLE.'
229  WHERE category_id = '.$cat_id.'
230  ORDER BY
231    type ASC,
232    cond ASC
233;';
234$result = pwg_query($query);
235
236$template->assign('filter_mode', 'and');
237
238while ($filter = pwg_db_fetch_assoc($result))
239{
240  if ($filter['type'] == 'mode')
241  {
242    $template->assign('filter_mode', $filter['value']);
243    continue;
244  }
245  else if ($filter['type'] == 'tags')
246  {
247    $query = '
248SELECT id, name
249  FROM '.TAGS_TABLE.'
250  WHERE id IN('.$filter['value'].')
251;';
252    $filter['value'] = get_taglist($query); 
253  }
254 
255  $template->append('filters', $filter);
256}
257
258
259/* format types */
260$template->assign('format_options', array(
261  'portrait' => l10n('Portrait'),
262  'square'   => l10n('Square'),
263  'lanscape' => l10n('Landscape'),
264  'panorama' => l10n('Panorama'),
265  ));
266
267/* all tags */
268$query = '
269SELECT id, name
270  FROM '.TAGS_TABLE.'
271;';
272$template->assign('all_tags', get_taglist($query));
273
274/* all albums */
275$query = '
276SELECT
277    id,
278    name,
279    uppercats,
280    global_rank
281  FROM '.CATEGORIES_TABLE.'
282;';
283display_select_cat_wrapper($query, array(), 'all_albums');
284
285// +-----------------------------------------------------------------------+
286// |                              dimensions                               |
287// +-----------------------------------------------------------------------+
288
289$widths = array();
290$heights = array();
291$ratios = array();
292
293// get all width, height and ratios
294$query = '
295SELECT
296  DISTINCT width, height
297  FROM '.IMAGES_TABLE.'
298  WHERE width IS NOT NULL
299    AND height IS NOT NULL
300;';
301$result = pwg_query($query);
302
303while ($row = pwg_db_fetch_assoc($result))
304{
305  $widths[] = $row['width'];
306  $heights[] = $row['height'];
307  $ratios[] = floor($row['width'] * 100 / $row['height']) / 100;
308}
309
310$widths = array_unique($widths);
311sort($widths);
312
313$heights = array_unique($heights);
314sort($heights);
315
316$ratios = array_unique($ratios);
317sort($ratios);
318
319$dimensions['bounds'] = array(
320  'min_width' => $widths[0],
321  'max_width' => $widths[count($widths)-1],
322  'min_height' => $heights[0],
323  'max_height' => $heights[count($heights)-1],
324  'min_ratio' => $ratios[0],
325  'max_ratio' => $ratios[count($ratios)-1],
326  );
327
328// find ratio categories
329$ratio_categories = array(
330  'portrait' => array(),
331  'square' => array(),
332  'landscape' => array(),
333  'panorama' => array(),
334  );
335
336foreach ($ratios as $ratio)
337{
338  if ($ratio < 0.95)
339  {
340    $ratio_categories['portrait'][] = $ratio;
341  }
342  else if ($ratio >= 0.95 and $ratio <= 1.05)
343  {
344    $ratio_categories['square'][] = $ratio;
345  }
346  else if ($ratio > 1.05 and $ratio < 2)
347  {
348    $ratio_categories['landscape'][] = $ratio;
349  }
350  else if ($ratio >= 2)
351  {
352    $ratio_categories['panorama'][] = $ratio;
353  }
354}
355
356foreach (array_keys($ratio_categories) as $ratio_category)
357{
358  if (count($ratio_categories[$ratio_category]) > 0)
359  {
360    $dimensions['ratio_'.$ratio_category] = array(
361      'min' => $ratio_categories[$ratio_category][0],
362      'max' => $ratio_categories[$ratio_category][count($ratio_categories[$ratio_category]) - 1]
363      );
364  }
365}
366
367$template->assign('dimensions', $dimensions);
368
369/* get image number */
370if ($template->get_template_vars('IMAGE_COUNT') == null)
371{
372  $query = '
373SELECT count(1)
374  FROM '.IMAGE_CATEGORY_TABLE.'
375  WHERE
376    category_id = '.$cat_id.'
377    AND smart = true
378';
379
380  list($image_num) = pwg_db_fetch_row(pwg_query($query));
381  $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', $image_num));
382}
383
384
385/* template vars */
386if (isset($_GET['new_smart']))
387{
388  $template->assign('new_smart', true);
389}
390
391$template->assign(array(
392  'COUNT_SCRIPT_URL' => SMART_PATH.'include/count_images.php',
393  'level_options' => get_privacy_level_options(),
394  'F_ACTION' => $self_url,
395  'CATEGORIES_NAV' => get_cat_display_name_cache($category['uppercats'], SMART_ADMIN.'-album&amp;cat_id='),
396));
397
398$template->set_filename('SmartAlbums_content', dirname(__FILE__).'/template/album.tpl');
399
400?>
Note: See TracBrowser for help on using the repository browser.