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

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

Add event_handler on 'login_success' and 'invalidate_user_cache' to regenerate SmartAlbums content

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