source: extensions/SmartAlbums/include/init_cat_modify.php @ 12031

Last change on this file since 12031 was 12031, checked in by mistic100, 13 years ago

fix an error with TokenInput display

File size: 4.5 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3/**
4 * Add the SmartAlbums configuration tool to virtual cats' configuration page
5 */
6 
7function smart_cat_modify()
8{
9  global $template, $page;
10 
11  $cat_id = $_GET['cat_id'];
12  list($cat_dir) = pwg_db_fetch_row(pwg_query('SELECT dir FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat_id.';'));
13 
14  // category must be virtual
15  if ($cat_dir != NULL)
16  {
17    return;
18  }
19 
20  /* SAVE FILTERS */
21  if (isset($_POST['submitFilters']))
22  {
23    // test if it was a Smart Album
24    $result = pwg_query('SELECT DISTINCT category_id FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
25    $was_smart = pwg_db_num_rows($result);
26   
27    /* this album is no longer a SmartAlbum */
28    if ($was_smart AND !isset($_POST['is_smart']))
29    {
30      pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;');
31      pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
32      set_random_representant(array($cat_id));
33    }
34    /* no filter selected */
35    else if (isset($_POST['is_smart']) AND !isset($_POST['filters']))
36    {
37      array_push($page['errors'], l10n('No filter selected'));
38    }
39    /* everything is fine */
40    else if (isset($_POST['is_smart']) AND count($_POST['filters']) > 0)
41    {
42      pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
43     
44      $limit_is_set = false;
45      foreach ($_POST['filters'] as $filter)
46      {
47        if (($filter = smart_check_filter($filter)) != false)
48        {
49          $query = '
50INSERT INTO '.CATEGORY_FILTERS_TABLE.'
51  VALUES(
52    '.$cat_id.',
53    "'.$filter['type'].'",
54    "'.$filter['cond'].'",
55    "'.$filter['value'].'"
56  )
57;';
58        pwg_query($query);
59        }
60      }
61     
62      $associated_images = smart_make_associations($cat_id);
63      $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', count($associated_images)));
64     
65      array_push(
66          $page['infos'], 
67          sprintf(
68            l10n('%d photos associated to album %s'), 
69            count($associated_images), 
70            ''
71            )
72          );
73     
74      define('SMART_NOT_UPDATE', 1);
75      invalidate_user_cache();
76    }
77  }
78     
79  /* select options, for html_options */
80  $template->assign(
81    'options', 
82    array(
83      'tags' => array(
84        'all' => l10n('All these tags'),
85        'one' => l10n('One of these tags'),
86        'none' => l10n('None of these tags'),
87        'only' => l10n('Only these tags'),
88        ),
89      'date' => array(
90        'the' => l10n('Added on'),
91        'before' => l10n('Added before'),
92        'after' => l10n('Added after'),
93        'the_crea' => l10n('Created on'),
94        'before_crea' => l10n('Created before'),
95        'after_crea' => l10n('Created after'),
96        ),
97      'limit' => array('limit' => 'limit'), // second filter not used
98      // TODO : new filter by album
99      )
100    );
101 
102  /* get filters for this album */
103  $filters = pwg_query('SELECT * FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.' ORDER BY type ASC, cond ASC;');
104  while ($filter = pwg_db_fetch_assoc($filters))
105  {
106    // get tags name and id
107    if ($filter['type'] == 'tags')
108    {
109      $query = '
110SELECT
111    id,
112    name
113  FROM '.TAGS_TABLE.'
114  WHERE id IN('.$filter['value'].')
115';
116      $filter['value'] = get_taglist($query); 
117    }
118   
119    $template->append('filters', array(
120      'TYPE' => $filter['type'],
121      'COND' => $filter['cond'],
122      'VALUE' => $filter['value'],
123    ));
124  }
125 
126  /* all tags */
127  $query = '
128SELECT
129    id,
130    name
131  FROM '.TAGS_TABLE.'
132;';
133  $tags = get_taglist($query);
134 
135  /* get image number */
136  if ($template->get_template_vars('IMAGE_COUNT') == null)
137  {
138    list($image_num) = pwg_db_fetch_row(pwg_query('SELECT count(*) FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;'));
139    $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', $image_num));
140  }
141 
142  if (isset($_GET['new_smart']))
143  {
144    $template->assign('new_smart', true);
145  }
146 
147  $template->assign(array(
148    'SMART_PATH' => SMART_PATH,
149    'COUNT_SCRIPT_URL' => SMART_PATH.'include/count_images.php',
150    'tags' => $tags,
151  )); 
152  $template->set_prefilter('categories', 'smart_cat_modify_prefilter');
153}
154
155
156function smart_cat_modify_prefilter($content, &$smarty)
157{
158  $search = '<form action="{$F_ACTION}" method="POST" id="catModify">';
159  $replacement = file_get_contents(SMART_PATH.'template/cat_modify.tpl')."\n".$search;
160  return str_replace($search, $replacement, $content);
161}
162
163?>
Note: See TracBrowser for help on using the repository browser.