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

Last change on this file since 23361 was 23361, checked in by mistic100, 11 years ago

all collections are "active", display a menu when adding to a collection

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