source: extensions/SmartAlbums/include/events.inc.php @ 26442

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

update for Piwigo 2.6

File size: 3.4 KB
Line 
1<?php
2defined('SMART_PATH') or die('Hacking attempt!');
3
4/**
5 * clean table when categories are deleted
6 */
7function smart_delete_categories($ids)
8{
9  $query = '
10DELETE FROM '.CATEGORY_FILTERS_TABLE.'
11  WHERE category_id IN('.implode(',', $ids).')
12;';
13  pwg_query($query);
14}
15
16/**
17 * update images list periodically
18 */
19function smart_periodic_update()
20{
21  global $conf;
22
23  // we only search for old albums every hour, nevermind which user is connected
24  if ($conf['SmartAlbums']['last_update'] > time() - 3600) return;
25
26  $conf['SmartAlbums']['last_update'] = time();
27  conf_update_param('SmartAlbums', serialize($conf['SmartAlbums']));
28
29  // get categories with smart filters
30  $query = '
31SELECT DISTINCT category_id
32  FROM '.CATEGORY_FILTERS_TABLE.'
33  WHERE updated < DATE_SUB(NOW(), INTERVAL '.$conf['SmartAlbums']['update_timeout'].' DAY)
34;';
35
36  // regenerate photo list
37  $smart_cats = query2array($query, null, 'category_id');
38  array_map('smart_make_associations', $smart_cats);
39}
40
41/**
42 * Remove picture that must not be displayed from $page['items']
43 *
44 * here we get all pictures that current user could see
45 * if SmartAlbums doesn't exist, and make intersect with pictures
46 * actually displayed
47 */
48function smart_init_page_items()
49{
50  global $user, $page, $conf;
51
52  if (
53    ('categories' == $page['section']) and
54    (!isset($page['chronology_field'])) and
55    (
56      (isset($page['category'])) or
57      (isset($page['flat']))
58    )
59  ) {
60
61    $query = '
62SELECT DISTINCT category_id
63  FROM '.CATEGORY_FILTERS_TABLE.'
64;';
65    $smart_albums = query2array($query, null, 'category_id');
66
67    if (count($smart_albums) > 0 and !is_admin())
68    {
69      // add SmartAlbums to forbidden categories
70      $user['forbidden_categories_old'] = $user['forbidden_categories'];
71      $user['forbidden_categories'] = explode(',', $user['forbidden_categories']);
72      $user['forbidden_categories'] = array_unique(array_merge($user['forbidden_categories'], $smart_albums));
73      $user['forbidden_categories'] = implode(',', $user['forbidden_categories']);
74
75      if (isset($page['category']))
76      {
77        $query = '
78SELECT id
79  FROM '.CATEGORIES_TABLE.'
80  WHERE
81    '.get_sql_condition_FandF(
82      array(
83        'forbidden_categories' => 'id',
84        'visible_categories' => 'id',
85        )
86      );
87        $subcat_ids = query2array($query, null, 'id');
88        $subcat_ids[] = 0;
89        $where_sql = 'category_id IN ('.implode(',',$subcat_ids).')';
90        // remove categories from forbidden because just checked above
91        $forbidden = get_sql_condition_FandF(
92          array(
93            'visible_images' => 'id'
94            ),
95          'AND'
96          );
97      }
98      else
99      {
100        $where_sql = '1=1';
101        $forbidden = get_sql_condition_FandF(
102          array(
103            'forbidden_categories' => 'category_id',
104            'visible_categories' => 'category_id',
105            'visible_images' => 'id'
106            ),
107          'AND'
108          );
109      }
110
111      // Main query
112      $query = '
113SELECT DISTINCT(image_id)
114  FROM '.IMAGE_CATEGORY_TABLE.'
115    INNER JOIN '.IMAGES_TABLE.' ON id = image_id
116  WHERE
117    '.$where_sql.'
118'.$forbidden.'
119  '.$conf['order_by'].'
120;';
121
122      $page['items_wo_sa'] = query2array($query, null, 'image_id');
123      $page['items'] = array_intersect($page['items'], $page['items_wo_sa']);
124
125      // restore forbidden categories
126      $user['forbidden_categories'] = $user['forbidden_categories_old'];
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.