source: extensions/SmartAlbums/init_cat_modify.php @ 11333

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

clean code, use TokenInput

File size: 4.1 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;
10  include_once(SMART_PATH.'include/functions.inc.php');
11 
12  $cat_id = $_GET['cat_id'];
13  list($cat_dir) = pwg_db_fetch_row(pwg_query('SELECT dir FROM '.CATEGORIES_TABLE.' WHERE id = '.$cat_id.';'));
14 
15  // category must be virtual
16  if ($cat_dir != NULL)
17  {
18    return;
19  }
20 
21  /* SAVE FILTERS */
22  if (isset($_POST['submitFilters']))
23  {
24    // test if it was a Smart Album
25    $result = pwg_query('SELECT DISTINCT category_id FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
26    $was_smart = pwg_db_num_rows($result);
27   
28    /* this album is no longer a SmartAlbum */
29    if ($was_smart AND !isset($_POST['is_smart']))
30    {
31      pwg_query('DELETE FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;');
32      pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$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      invalidate_user_cache(true);
64      $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', count($associated_images)));
65    }
66  }
67     
68  /* select options, for html_options */
69  $template->assign(
70    'options', 
71    array(
72      'tags' => array(
73        'all' => l10n('All these tags'),
74        'one' => l10n('One of these tags'),
75        'none' => l10n('None of these tags'),
76        'only' => l10n('Only these tags'),
77        ),
78      'date' => array(
79        'the' => l10n('Added the'),
80        'before' => l10n('Added before the'),
81        'after' => l10n('Added after the'),
82        ),
83      'limit' => array('limit' => 'limit'), // second filter not used
84      )
85    );
86 
87  /* get filters for this album */
88  $filters = pwg_query('SELECT * FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.' ORDER BY type ASC, cond ASC;');
89  while ($filter = pwg_db_fetch_assoc($filters))
90  {
91    // get tags name and id
92    if ($filter['type'] == 'tags')
93    {
94      $query = '
95SELECT
96    id AS tag_id,
97    name AS tag_name
98  FROM '.TAGS_TABLE.'
99  WHERE id IN('.$filter['value'].')
100';
101      $filter['value'] = get_taglist($query); 
102    }
103   
104    $template->append('filters', array(
105      'TYPE' => $filter['type'],
106      'COND' => $filter['cond'],
107      'VALUE' => $filter['value'],
108    ));
109  }
110 
111  /* all tags */
112  $query = '
113SELECT
114    id AS tag_id,
115    name AS tag_name
116  FROM '.TAGS_TABLE.'
117;';
118  $tags = get_taglist($query);
119 
120  /* get image number */
121  if ($template->get_template_vars('IMAGE_COUNT') == null)
122  {
123    list($image_num) = pwg_db_fetch_row(pwg_query('SELECT count(*) FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;'));
124    $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', $image_num));
125  }
126 
127  $template->assign(array(
128    'SMART_PATH' => SMART_PATH,
129    'COUNT_SCRIPT_URL' => SMART_PATH.'include/count_images.php',
130    'tags' => $tags,
131  )); 
132  $template->set_prefilter('categories', 'smart_cat_modify_prefilter');
133}
134
135
136function smart_cat_modify_prefilter($content, &$smarty)
137{
138  $search = '<form action="{$F_ACTION}" method="POST" id="links">';
139  $replacement = file_get_contents(SMART_PATH.'template/cat_modify.tpl')."\n".$search;
140  return str_replace($search, $replacement, $content);
141}
142
143?>
Note: See TracBrowser for help on using the repository browser.