source: extensions/SmartAlbums/init_cat_list.php @ 11333

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

clean code, use TokenInput

File size: 3.1 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3/**
4 * Add a link into categories list to regenerate associations
5 */
6$smart_count = 0;
7
8function smart_cat_list()
9{
10  global $template, $page, $smart_count;
11  include_once(SMART_PATH.'include/functions.inc.php');
12  $self_url = get_root_url().'admin.php?page=cat_list'.(isset($_GET['parent_id']) ? '&amp;parent_id='.$_GET['parent_id'] : null);
13 
14  /* get categories with smart filters */
15  $query = '
16SELECT DISTINCT id, name
17  FROM '.CATEGORIES_TABLE.' AS c
18    INNER JOIN '.CATEGORY_FILTERS_TABLE.' AS cf
19      ON c.id = cf.category_id';
20  if (!isset($_GET['parent_id']))
21  {
22    // $query.= '
23    // WHERE id_uppercat IS NULL';
24  }
25  else
26  {
27    $query .= '
28  WHERE uppercats LIKE \'%'.$_GET['parent_id'].'%\'';
29  }
30  $query .= '
31;';
32 
33  $result = pwg_query($query);
34  $smart_cats = array();
35  while ($cat = pwg_db_fetch_assoc($result))
36  {
37    $smart_cats[$cat['id']] = trigger_event('render_category_name', $cat['name']);
38  }
39 
40  $smart_count = count($smart_cats);
41 
42  if (isset($_GET['smart_generate']))
43  {
44    /* regenerate photo list | all (sub) categories */
45    if ($_GET['smart_generate'] == 'all')
46    {
47      foreach ($smart_cats as $cat => $name)
48      {
49        $associated_images = smart_make_associations($cat);
50        array_push(
51          $page['infos'], 
52          l10n_args(get_l10n_args(
53            '%d photos associated to album &laquo;%s&raquo;', 
54            array(count($associated_images), $name)
55            ))
56          );
57      }
58    }
59    /* regenerate photo list | one category */
60    else
61    {
62      $associated_images = smart_make_associations($_GET['smart_generate']);   
63      array_push(
64        $page['infos'], 
65        l10n_args(get_l10n_args(
66          '%d photos associated to album &laquo;%s&raquo;', 
67          array(count($associated_images), $smart_cats[$_GET['smart_generate']])
68          ))
69        );
70    }
71   
72    invalidate_user_cache(true);
73  }
74 
75  // create regenerate link
76  $tpl_cat = array();
77  foreach ($smart_cats as $cat => $name)
78  {
79    $tpl_cat[$cat] = $self_url.'&amp;smart_generate='.$cat;
80  }
81  $tpl_cat['all'] = $self_url.'&amp;smart_generate=all';
82 
83  $template->assign(array(
84    'SMART_URL' => $tpl_cat,
85    'SMART_PATH' => SMART_PATH,
86  ));
87 
88  $template->set_prefilter('categories', 'smart_cat_list_prefilter');
89}
90
91
92function smart_cat_list_prefilter($content, &$smarty)
93{
94  global $smart_count;
95 
96  $search[0] = '<ul class="categoryActions">';
97  $replacement[0] = $search[0].'
98{if isset($SMART_URL[$category.ID])}
99        <li><a href="{$SMART_URL[$category.ID]}" title="{\'regenerate photos list\'|@translate}"><img src="{$SMART_PATH}template/refresh.png" class="button" alt="{\'regenerate photos list\'|@translate}"></a></li>
100{/if}';
101
102  if ($smart_count > 0)
103  {
104    $search[1] = '</ul>
105</form>
106{/if}';
107    $replacement[1] = $search[1].'
108<form method="post" action="{$SMART_URL.all}">
109  <input type="hidden" name="pwg_token" value="{$PWG_TOKEN}">
110  <p><input class="submit" type="submit" value="{\'regenerate photos list of all SmartAlbums\'|@translate}"></p>
111</form>';
112  }
113
114  return str_replace($search, $replacement, $content);
115}
116
117?>
Note: See TracBrowser for help on using the repository browser.