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

Last change on this file since 27266 was 27266, checked in by mistic100, 10 years ago

allow to choose the sort order when using limit filter

File size: 10.6 KB
Line 
1<?php
2defined('SMART_PATH') or die('Hacking attempt!');
3
4// +-----------------------------------------------------------------------+
5// | Basic checks                                                          |
6// +-----------------------------------------------------------------------+
7
8check_status(ACCESS_ADMINISTRATOR);
9
10$page['active_menu'] = get_active_menu('album');
11
12check_input_parameter('cat_id', $_GET, false, PATTERN_ID);
13$cat_id = $_GET['cat_id'];
14
15$admin_album_base_url = get_root_url().'admin.php?page=album-'.$cat_id;
16$self_url = SMART_ADMIN.'-album&amp;cat_id='.$cat_id;
17
18$query = '
19SELECT *
20  FROM '.CATEGORIES_TABLE.'
21  WHERE id = '.$cat_id.'
22;';
23$category = pwg_db_fetch_assoc(pwg_query($query));
24
25if (!isset($category['id']))
26{
27  die("unknown album");
28}
29
30// category must be virtual
31if ($category['dir'] != NULL)
32{
33  die("physical album");
34}
35
36
37// +-----------------------------------------------------------------------+
38// | Tabs                                                                  |
39// +-----------------------------------------------------------------------+
40
41include_once(PHPWG_ROOT_PATH.'admin/include/tabsheet.class.php');
42$tabsheet = new tabsheet();
43$tabsheet->set_id('album');
44$tabsheet->select('smartalbum');
45$tabsheet->assign();
46
47
48// +-----------------------------------------------------------------------+
49// | Save Filters                                                          |
50// +-----------------------------------------------------------------------+
51
52if (isset($_POST['submitFilters']))
53{
54  if (defined('SMART_DEBUG'))
55  {
56    var_dump($_POST['filters']);
57  }
58
59  // test if it was a Smart Album
60  $query = '
61SELECT DISTINCT category_id
62  FROM '.CATEGORY_FILTERS_TABLE.'
63  WHERE category_id = '.$cat_id.'
64;';
65  $was_smart = pwg_db_num_rows(pwg_query($query));
66
67  /* this album is no longer a SmartAlbum */
68  if ($was_smart and !isset($_POST['is_smart']))
69  {
70    pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;');
71    pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
72
73    set_random_representant(array($cat_id));
74
75    define('SMART_NOT_UPDATE', 1);
76    invalidate_user_cache();
77  }
78  /* no filter selected */
79  else if (isset($_POST['is_smart']) and empty($_POST['filters']))
80  {
81    $page['errors'][] = l10n('No filter selected');
82  }
83  /* everything is fine */
84  else if (isset($_POST['is_smart']))
85  {
86    pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
87
88    $inserts = array();
89    foreach ($_POST['filters'] as $filter)
90    {
91      if (($filter = smart_check_filter($filter)) != false)
92      {
93        $filter['category_id'] = $cat_id;
94        $inserts[] = $filter;
95      }
96    }
97
98    mass_inserts(
99      CATEGORY_FILTERS_TABLE,
100      array('category_id', 'type', 'cond', 'value'),
101      $inserts,
102      array('ignore'=>true)
103      );
104
105    $associated_images = smart_make_associations($cat_id);
106    $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', count($associated_images)));
107
108    $page['infos'][] = l10n('%d photos associated to album %s', count($associated_images), '');
109
110    define('SMART_NOT_UPDATE', 1);
111    invalidate_user_cache();
112  }
113}
114
115// +-----------------------------------------------------------------------+
116// | Display page                                                          |
117// +-----------------------------------------------------------------------+
118
119/* select options, for html_options */
120$options = array(
121  'tags' => array(
122    'name' => l10n('Tags'),
123    'options' => array(
124      'all'   => l10n('All these tags'),
125      'one'   => l10n('One of these tags'),
126      'none'  => l10n('None of these tags'),
127      'only'  => l10n('Only these tags'),
128      ),
129    ),
130  'date' => array(
131    'name' => l10n('Date'),
132    'options' => array(
133      'the_post'     => l10n('Added on'),
134      'before_post'  => l10n('Added before'),
135      'after_post'   => l10n('Added after'),
136      'the_taken'    => l10n('Created on'),
137      'before_taken' => l10n('Created before'),
138      'after_taken'  => l10n('Created after'),
139      ),
140    ),
141  'name' => array(
142    'name' => l10n('Photo name'),
143    'options' => array(
144      'contain'     => l10n('Contains'),
145      'begin'       => l10n('Begins with'),
146      'end'         => l10n('Ends with'),
147      'not_contain' => l10n('Doesn\'t contain'),
148      'not_begin'   => l10n('Doesn\'t begin with'),
149      'not_end'     => l10n('Doesn\'t end with'),
150      'regex'       => l10n('Regular expression'),
151      ),
152    ),
153  'album' => array(
154    'name' => l10n('Album'),
155    'options' => array(
156      'all'   => l10n('All these albums'),
157      'one'   => l10n('One of these albums'),
158      'none'  => l10n('None of these albums'),
159      'only'  => l10n('Only these albums'),
160      ),
161    ),
162  'dimensions' => array(
163    'name' => l10n('Dimensions'),
164    'options' => array(
165      'width'  => l10n('Width'),
166      'height' => l10n('Height'),
167      'ratio'  => l10n('Ratio').' ('.l10n('Width').'/'.l10n('Height').')',
168      ),
169    ),
170  'author' => array(
171    'name' => l10n('Author'),
172    'options' => array(
173      'is'      => l10n('Is'),
174      'in'      => l10n('Is in'),
175      'not_is'  => l10n('Is not'),
176      'not_in'  => l10n('Is not in'),
177      'regex'   => l10n('Regular expression'),
178      ),
179    ),
180  'hit' => array(
181    'name' => l10n('Hits'),
182    'options' => array(
183      'less' => l10n('Bellow'),
184      'more' => l10n('Above'),
185      ),
186    ),
187  'rating_score' => array(
188    'name' => l10n('Rating score'),
189    'options' => array(
190      'less' => l10n('Bellow'),
191      'more' => l10n('Above'),
192      ),
193    ),
194  'level' => array(
195    'name' => l10n('Privacy level'),
196    'options' => array(),
197    ),
198  'limit' => array(
199    'name' => l10n('Max. number of photos'),
200    'options' => array(
201      ''                    => '-- ' . l10n('Default') . ' --',
202      'file ASC'            => l10n('File name, A &rarr; Z'),
203      'file DESC'           => l10n('File name, Z &rarr; A'),
204      'name ASC'            => l10n('Photo title, A &rarr; Z'),
205      'name DESC'           => l10n('Photo title, Z &rarr; A'),
206      'date_creation DESC'  => l10n('Date created, new &rarr; old'),
207      'date_creation ASC'   => l10n('Date created, old &rarr; new'),
208      'date_available DESC' => l10n('Date posted, new &rarr; old'),
209      'date_available ASC'  => l10n('Date posted, old &rarr; new'),
210      'rating_score DESC'   => l10n('Rating score, high &rarr; low'),
211      'rating_score ASC'    => l10n('Rating score, low &rarr; high'),
212      'hit DESC'            => l10n('Visits, high &rarr; low'),
213      'hit ASC'             => l10n('Visits, low &rarr; high'),
214      'id ASC'              => l10n('Numeric identifier, 1 &rarr; 9'),
215      'id DESC'             => l10n('Numeric identifier, 9 &rarr; 1'),
216      ),
217    ),
218  );
219$template->assign('options', $options);
220
221
222/* get filters for this album */
223$query = '
224SELECT *
225  FROM '.CATEGORY_FILTERS_TABLE.'
226  WHERE category_id = '.$cat_id.'
227  ORDER BY
228    type ASC,
229    cond ASC
230;';
231$result = pwg_query($query);
232
233$template->assign('filter_mode', 'and');
234
235while ($filter = pwg_db_fetch_assoc($result))
236{
237  if ($filter['type'] == 'mode')
238  {
239    $template->assign('filter_mode', $filter['value']);
240    continue;
241  }
242  else if ($filter['type'] == 'tags')
243  {
244    $query = '
245SELECT id, name
246  FROM '.TAGS_TABLE.'
247  WHERE id IN('.$filter['value'].')
248;';
249    $filter['value'] = get_taglist($query);
250  }
251
252  $template->append('filters', $filter);
253}
254
255
256/* format types */
257$template->assign('format_options', array(
258  'portrait' => l10n('Portrait'),
259  'square'   => l10n('square'),
260  'lanscape' => l10n('Landscape'),
261  'panorama' => l10n('Panorama'),
262  ));
263
264/* all tags */
265$query = '
266SELECT id, name
267  FROM '.TAGS_TABLE.'
268;';
269$template->assign('all_tags', get_taglist($query));
270
271/* all albums */
272$query = '
273SELECT
274    id,
275    name,
276    uppercats,
277    global_rank
278  FROM '.CATEGORIES_TABLE.'
279;';
280display_select_cat_wrapper($query, array(), 'all_albums');
281
282// +-----------------------------------------------------------------------+
283// |                              dimensions                               |
284// +-----------------------------------------------------------------------+
285
286$widths = array();
287$heights = array();
288$ratios = array();
289
290// get all width, height and ratios
291$query = '
292SELECT
293  DISTINCT width, height
294  FROM '.IMAGES_TABLE.'
295  WHERE width IS NOT NULL
296    AND height IS NOT NULL
297;';
298$result = pwg_query($query);
299
300while ($row = pwg_db_fetch_assoc($result))
301{
302  if ($row['width']>0 && $row['height']>0)
303  {
304    $widths[] = $row['width'];
305    $heights[] = $row['height'];
306    $ratios[] = floor($row['width'] / $row['height'] * 100) / 100;
307  }
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 ($ratio_categories as $ratio_name => $ratio_values)
357{
358  if (count($ratio_values) > 0)
359  {
360    $dimensions['ratio_'.$ratio_name] = array(
361      'min' => $ratio_values[0],
362      'max' => array_pop($ratio_values),
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', realpath(SMART_PATH . 'admin/template/album.tpl'));
Note: See TracBrowser for help on using the repository browser.