source: extensions/UserCollections/include/events.inc.php @ 26176

Last change on this file since 26176 was 26176, checked in by mistic100, 10 years ago

add readme about template variables and prefilters

File size: 7.2 KB
Line 
1<?php
2defined('USER_COLLEC_PATH') or die('Hacking attempt!');
3
4// +-----------------------------------------------------------------------+
5// | SECTION INIT
6// +-----------------------------------------------------------------------+
7/* define page section from url */
8function user_collections_section_init()
9{
10  global $tokens, $page, $conf;
11
12  if ($tokens[0] == 'collections')
13  {
14    $page['section'] = 'collections';
15    $page['title'] = l10n('Collections');
16    $page['body_id'] = 'theCollectionPage';
17    $page['is_external'] = true;
18    $page['is_homepage'] = false;
19
20    $page['section_title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'];
21    if (is_a_guest())
22    {
23      $page['section_title'].= l10n('Collections');
24    }
25    else
26    {
27      $page['section_title'].= '<a href="'.USER_COLLEC_PUBLIC.'">'.l10n('Collections').'</a>';
28    }
29
30    $page['sub_section'] = 'list';
31    if (in_array(@$tokens[1], array('edit','view','list')))
32    {
33      $page['sub_section'] = $tokens[1];
34    }
35
36    if ($page['sub_section']=='edit' && isset($conf['GThumb']) && is_array($conf['GThumb']))
37    {
38      $conf['GThumb']['big_thumb'] = false; // big thumb is buggy with removes
39    }
40
41    if (!empty($tokens[2]))
42    {
43      $page['col_id'] = $tokens[2];
44    }
45  }
46}
47
48/* collections section */
49function user_collections_page()
50{
51  global $page, $template;
52
53  if (isset($page['section']) and $page['section'] == 'collections')
54  {
55    include(USER_COLLEC_PATH . 'include/collections.inc.php');
56  }
57
58  if (!is_a_guest() && count($page['items']))
59  {
60    $template->set_filename('uc_thumbnails_cssjs', realpath(USER_COLLEC_PATH . 'template/thumbnails_css_js.tpl'));
61    $template->parse('uc_thumbnails_cssjs');
62  }
63}
64
65
66// +-----------------------------------------------------------------------+
67// | CATEGORY PAGE
68// +-----------------------------------------------------------------------+
69/* add buttons on thumbnails list */
70function user_collections_thumbnails_list($tpl_thumbnails_var, $pictures)
71{
72  if (is_a_guest()) return $tpl_thumbnails_var;
73
74  global $page, $template, $user;
75
76  // the content is different on collection edition page and no button on batch downloader set edition page
77  if (empty($pictures) or (@$page['section'] == 'collections' and @$page['sub_section']=='edit') or @$page['section'] == 'download')
78  {
79    return $tpl_thumbnails_var;
80  }
81
82  $image_ids = array_map(create_function('$i', 'return $i["id"];'), $pictures);
83
84  // get collections for each picture
85  $query = '
86SELECT
87    image_id,
88    GROUP_CONCAT(col_id) AS col_ids
89  FROM '.COLLECTION_IMAGES_TABLE.'
90  WHERE col_id IN (
91      SELECT id
92      FROM '.COLLECTIONS_TABLE.'
93      WHERE user_id = '.$user['id'].'
94    )
95    AND image_id IN('.implode(',', $image_ids).')
96  GROUP BY image_id
97;';
98  $image_collections = simple_hash_from_query($query, 'image_id', 'col_ids');
99
100  foreach ($tpl_thumbnails_var as &$thumbnail)
101  {
102    $thumbnail['COLLECTIONS'] = @$image_collections[ $thumbnail['id'] ];
103  }
104  unset($thumbnail);
105
106  // get all collections
107  $query = '
108SELECT id, name, nb_images
109  FROM '.COLLECTIONS_TABLE.'
110  WHERE user_id = '.$user['id'].'
111  ORDER BY name ASC
112;';
113  $collections = hash_from_query($query, 'id');
114
115  foreach ($collections as &$col)
116  {
117    $col["name"] = trigger_change('render_category_name', $col["name"]);
118  }
119  unset($col);
120
121  $template->assign(array(
122    'COLLECTIONS' => $collections,
123    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
124    ));
125
126  // thumbnails buttons
127  $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_button');
128
129  return $tpl_thumbnails_var;
130}
131
132// add links
133function user_collections_thumbnails_list_button($content, &$smarty)
134{
135  $search = '#(<li>|<li class="gthumb">)#';
136  $replace = '$1
137{strip}<a class="addCollection" data-id="{$thumbnail.id}" data-cols="[{$thumbnail.COLLECTIONS}]" rel="nofollow">
138{if not $UC_IN_EDIT}
139{\'Add to collection\'|translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_add.png" alt="[+]">
140{else}
141{\'Remove from collection\'|translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_delete.png" alt="[+]">
142{/if}
143</a>{/strip}';
144
145  return preg_replace($search, $replace, $content);
146}
147
148
149// +-----------------------------------------------------------------------+
150// | PICTURE PAGE
151// +-----------------------------------------------------------------------+
152/* add button on picture page */
153function user_collections_picture_page()
154{
155  if (is_a_guest())
156  {
157    return;
158  }
159
160  global $template, $picture, $user;
161
162  // get collections for this picture
163  $query = '
164SELECT GROUP_CONCAT(col_id)
165  FROM '.COLLECTION_IMAGES_TABLE.'
166  WHERE col_id IN (
167      SELECT id
168      FROM '.COLLECTIONS_TABLE.'
169      WHERE user_id = '.$user['id'].'
170    )
171    AND image_id = '.$picture['current']['id'].'
172  GROUP BY image_id
173;';
174  list($image_collections) = pwg_db_fetch_row(pwg_query($query));
175
176  // get all collections
177  $query = '
178SELECT id, name, nb_images
179  FROM '.COLLECTIONS_TABLE.'
180  WHERE user_id = '.$user['id'].'
181  ORDER BY name ASC
182;';
183  $collections = hash_from_query($query, 'id');
184
185  foreach ($collections as &$col)
186  {
187    $col["name"] = trigger_change('render_category_name', $col["name"]);
188  }
189  unset($col);
190
191  $template->assign(array(
192    'CURRENT_COLLECTIONS' => $image_collections,
193    'COLLECTIONS' => $collections,
194    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
195    'USER_COLLEC_ABS_PATH' => realpath(USER_COLLEC_PATH).'/',
196    'IN_PICTURE' => true,
197    ));
198
199  // toolbar button
200  $template->set_filename('usercol_button', realpath(USER_COLLEC_PATH.'template/picture_button.tpl'));
201  $button = $template->parse('usercol_button', true);
202  $template->add_picture_button($button, 50);
203}
204
205
206// +-----------------------------------------------------------------------+
207// | MENU BLOCK
208// +-----------------------------------------------------------------------+
209/* register block */
210function user_collections_add_menublock($menu_ref_arr)
211{
212  $menu = &$menu_ref_arr[0];
213  if (is_a_guest() || $menu->get_id() != 'menubar')
214  {
215    return;
216  }
217
218  $menu->register_block(new RegisteredBlock('mbUserCollection', l10n('Collections'), 'UserCollection'));
219}
220
221/* fill block */
222function user_collections_applymenu($menu_ref_arr)
223{
224  $max = 6;
225
226  global $template, $page, $conf, $user;
227  $menu = &$menu_ref_arr[0];
228
229  if (($block = $menu->get_block('mbUserCollection')) != null)
230  {
231    $query = '
232SELECT id, name, nb_images
233  FROM '.COLLECTIONS_TABLE.'
234  WHERE user_id = '.$user['id'].'
235  ORDER BY date_creation DESC
236;';
237    $collections = array_values(hash_from_query($query, 'id'));
238
239    $data['collections'] = array();
240    for ($i=0; $i<$max && $i<count($collections); $i++)
241    {
242      $collections[$i]['name'] = trigger_change('render_category_name', $collections[$i]['name']);
243      $collections[$i]['u_edit'] = USER_COLLEC_PUBLIC.'edit/'.$collections[$i]['id'];
244      $data['collections'][] = $collections[$i];
245    }
246
247    $data['NB_COL'] = count($collections);
248    if ($data['NB_COL'] > $max)
249    {
250      $data['MORE'] = count($collections)-$max;
251    }
252
253    $data['U_LIST'] = USER_COLLEC_PUBLIC;
254
255    $block->set_title('<a href="'.USER_COLLEC_PUBLIC.'">'.l10n('Collections').'</a>');
256    $block->template = realpath(USER_COLLEC_PATH . 'template/menublock.tpl');
257    $block->data = $data;
258  }
259}
Note: See TracBrowser for help on using the repository browser.