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

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

private items are not displayed in SmartAlbums (according to user permissions)

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, $page;
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      set_random_representant(array($cat_id));
34    }
35    /* no filter selected */
36    else if (isset($_POST['is_smart']) AND !isset($_POST['filters']))
37    {
38      array_push($page['errors'], l10n('No filter selected'));
39    }
40    /* everything is fine */
41    else if (isset($_POST['is_smart']) AND count($_POST['filters']) > 0)
42    {
43      pwg_query('DELETE FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.';');
44     
45      $limit_is_set = false;
46      foreach ($_POST['filters'] as $filter)
47      {
48        if (($filter = smart_check_filter($filter)) != false)
49        {
50          $query = '
51INSERT INTO '.CATEGORY_FILTERS_TABLE.'
52  VALUES(
53    '.$cat_id.',
54    "'.$filter['type'].'",
55    "'.$filter['cond'].'",
56    "'.$filter['value'].'"
57  )
58;';
59        pwg_query($query);
60        }
61      }
62     
63      $associated_images = smart_make_associations($cat_id);
64      invalidate_user_cache(true);
65      $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', count($associated_images)));
66    }
67  }
68     
69  /* select options, for html_options */
70  $template->assign(
71    'options', 
72    array(
73      'tags' => array(
74        'all' => l10n('All these tags'),
75        'one' => l10n('One of these tags'),
76        'none' => l10n('None of these tags'),
77        'only' => l10n('Only these tags'),
78        ),
79      'date' => array(
80        'the' => l10n('Added the'),
81        'before' => l10n('Added before the'),
82        'after' => l10n('Added after the'),
83        ),
84      'limit' => array('limit' => 'limit'), // second filter not used
85      )
86    );
87 
88  /* get filters for this album */
89  $filters = pwg_query('SELECT * FROM '.CATEGORY_FILTERS_TABLE.' WHERE category_id = '.$cat_id.' ORDER BY type ASC, cond ASC;');
90  while ($filter = pwg_db_fetch_assoc($filters))
91  {
92    // get tags name and id
93    if ($filter['type'] == 'tags')
94    {
95      $query = '
96SELECT
97    id AS tag_id,
98    name AS tag_name
99  FROM '.TAGS_TABLE.'
100  WHERE id IN('.$filter['value'].')
101';
102      $filter['value'] = get_taglist($query); 
103    }
104   
105    $template->append('filters', array(
106      'TYPE' => $filter['type'],
107      'COND' => $filter['cond'],
108      'VALUE' => $filter['value'],
109    ));
110  }
111 
112  /* all tags */
113  $query = '
114SELECT
115    id AS tag_id,
116    name AS tag_name
117  FROM '.TAGS_TABLE.'
118;';
119  $tags = get_taglist($query);
120 
121  /* get image number */
122  if ($template->get_template_vars('IMAGE_COUNT') == null)
123  {
124    list($image_num) = pwg_db_fetch_row(pwg_query('SELECT count(*) FROM '.IMAGE_CATEGORY_TABLE.' WHERE category_id = '.$cat_id.' AND smart = true;'));
125    $template->assign('IMAGE_COUNT', l10n_dec('%d photo', '%d photos', $image_num));
126  }
127 
128  $template->assign(array(
129    'SMART_PATH' => SMART_PATH,
130    'COUNT_SCRIPT_URL' => SMART_PATH.'include/count_images.php',
131    'tags' => $tags,
132  )); 
133  $template->set_prefilter('categories', 'smart_cat_modify_prefilter');
134}
135
136
137function smart_cat_modify_prefilter($content, &$smarty)
138{
139  $search = '<form action="{$F_ACTION}" method="POST" id="links">';
140  $replacement = file_get_contents(SMART_PATH.'template/cat_modify.tpl')."\n".$search;
141  return str_replace($search, $replacement, $content);
142}
143
144?>
Note: See TracBrowser for help on using the repository browser.