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

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

fix mysql error when no photos on the page, remove "create collection" link in menu

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