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

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

fix caddie button,
fix fatal error for Back2Front,
update ZeroClipboard,
fix breadcrumb and body_id,
unactive for mobile themes,
fix display issues

File size: 8.8 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    }
24    else
25    {
26      $page['sub_section'] = 'list';
27    }
28   
29    if (!empty($tokens[2]))
30    {
31      $page['col_id'] = $tokens[2];
32    }
33  }
34}
35
36function user_collections_page_header()
37{
38  global $page;
39  $page['body_id'] = 'theCollectionPage';
40}
41
42/* collections section */
43function user_collections_page()
44{
45  global $page;
46
47  if (isset($page['section']) and $page['section'] == 'collections')
48  {
49    include(USER_COLLEC_PATH . '/include/collections.inc.php');
50  }
51}
52
53
54// +-----------------------------------------------------------------------+
55// | CATEGORY PAGE
56// +-----------------------------------------------------------------------+
57/* toggle an image, in case of no javascript or first call (must create the collection) */
58function user_collections_index_actions()
59{
60  if (is_a_guest()) return;
61 
62  global $page, $UserCollection;
63     
64  // add image to collection list
65  if ( isset($_GET['collection_toggle']) and  preg_match('#^[0-9]+$#', $_GET['collection_toggle']) )
66  {
67    if (empty($UserCollection))
68    {
69      $UserCollection = new UserCollection(get_current_collection_id(true));
70    }
71    $UserCollection->toggleImage($_GET['collection_toggle']);
72    redirect(duplicate_index_url(array(), array('collection_toggle')));
73  }
74}
75
76/* add buttons on thumbnails list */
77function user_collections_thumbnails_list($tpl_thumbnails_var, $pictures)
78{
79  if (is_a_guest()) return $tpl_thumbnails_var;
80 
81  global $page, $template, $UserCollection;
82 
83  // the content is different on collection edition page and no button on batch downloader set edition page
84  if ( (@$page['section'] == 'collections' and @$page['sub_section']=='edit') or @$page['section'] == 'download')
85  {
86    return $tpl_thumbnails_var;
87  }
88 
89  // get existing collections
90  $col_id = get_current_collection_id(false);
91  if (empty($UserCollection) and $col_id !== false)
92  {
93    $UserCollection = new UserCollection($col_id);
94    $collection = $UserCollection->getImages();
95  }
96  else if (!empty($UserCollection))
97  {
98    $collection = $UserCollection->getImages();
99  }
100  else
101  {
102    $collection = array();
103  }
104 
105  // if the collection doesn't exists we don't use AJAX to force menu refresh
106  if ($col_id === false)
107  {
108    $template->assign('NO_AJAX', true);
109  }
110  else
111  {
112    $template->assign('AJAX_COL_ID', $col_id );
113  }
114 
115  // template vars
116  $url = duplicate_index_url(array(), array('collection_toggle'));
117 
118  foreach ($tpl_thumbnails_var as &$thumbnail)
119  {
120    if (in_array($thumbnail['id'], $collection))
121    {
122      $thumbnail['COLLECTION_SELECTED'] = true;
123    }
124    $thumbnail['COLLECTION_TOGGLE_URL'] = add_url_params($url, array('collection_toggle'=>$thumbnail['id']));
125  }
126  unset($thumbnail);
127 
128  $template->assign(array(
129    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
130    ));
131 
132  // thumbnails buttons
133  $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_prefilter');
134 
135  return $tpl_thumbnails_var;
136}
137
138function user_collections_thumbnails_list_prefilter($content, &$smarty)
139{
140  // add links
141  $search = '#(<li>|<li class="gthumb">)#';
142  $replace = '$1
143{strip}<a class="addCollection" href="{$thumbnail.COLLECTION_TOGGLE_URL}" data-id="{$thumbnail.id}" data-stat="{if $thumbnail.COLLECTION_SELECTED}remove{else}add{/if}" rel="nofollow">
144<span class="uc_remove" {if not $thumbnail.COLLECTION_SELECTED}style="display:none;"{/if}>
145{\'Remove from collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_delete.png" title="{\'Remove from collection\'|@translate}">
146</span>
147<span class="uc_add" {if $thumbnail.COLLECTION_SELECTED}style="display:none;"{/if}>
148{\'Add to collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_add.png" title="{\'Add to collection\'|@translate}">
149</span>
150</a>{/strip}';
151
152  // custom CSS and AJAX request
153  $content.= file_get_contents(USER_COLLEC_PATH.'template/thumbnails_css_js.tpl');
154 
155  return preg_replace($search, $replace, $content);
156}
157
158
159// +-----------------------------------------------------------------------+
160// | PICTURE PAGE
161// +-----------------------------------------------------------------------+
162/* add button on picture page */
163function user_collections_picture_page()
164{
165  if (is_a_guest()) return;
166 
167  global $template, $picture, $UserCollection;
168 
169  // add image to collection list
170  if ( isset($_GET['action']) and $_GET['action'] == 'collection_toggle' )
171  {
172    if (empty($UserCollection))
173    {
174      $UserCollection = new UserCollection(get_current_collection_id(true));
175    }
176   
177    $UserCollection->toggleImage($picture['current']['id']);
178    redirect(duplicate_picture_url());
179  }
180 
181  // get existing collection
182  if (empty($UserCollection) and ($col_id = get_current_collection_id(false)) !== false)
183  {
184    $UserCollection = new UserCollection($col_id);
185    $collection = $UserCollection->isInSet($picture['current']['id']);
186  }
187  else if (!empty($UserCollection))
188  {
189    $collection = $UserCollection->isInSet($picture['current']['id']);
190  }
191  else
192  {
193    $collection = false;
194  } 
195 
196  $url = add_url_params(duplicate_picture_url(), array('action'=>'collection_toggle'));   
197 
198  $button = '
199<a href="'.$url.'" title="'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'" class="pwg-state-default pwg-button" rel="nofollow">
200  <span class="pwg-icon" style="background:url(\''.get_root_url().USER_COLLEC_PATH.'template/resources/image_'.($collection?'delete':'add').'.png\') center center no-repeat;"> </span>
201  <span class="pwg-button-text">'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'</span>
202</a>';
203  // $template->add_picture_button($button, 50);
204  $template->concat('PLUGIN_PICTURE_ACTIONS', $button);
205}
206
207
208// +-----------------------------------------------------------------------+
209// | MENU BLOCK
210// +-----------------------------------------------------------------------+
211/* register block */
212function user_collections_add_menublock($menu_ref_arr)
213{
214  if (is_a_guest()) return;
215 
216  $menu = &$menu_ref_arr[0];
217  if ($menu->get_id() != 'menubar') return;
218   
219  $menu->register_block(new RegisteredBlock('mbUserCollection', l10n('Collections'), 'UserCollection'));
220}
221
222/* fill block */
223function user_collections_applymenu($menu_ref_arr)
224{
225  $max = 6;
226 
227  global $template, $page, $conf, $user, $UserCollection;
228  $menu = &$menu_ref_arr[0];
229 
230  // the editable counter is for the active collection, except if we are currently editing a collection
231  $col_in_edit = 0;
232  if ( @$page['section'] == 'collections' and @$page['sub_section']=='edit' and !empty($page['col_id']) )
233  {
234    $col_in_edit = $page['col_id'];
235  }
236 
237  if (($block = $menu->get_block('mbUserCollection')) != null)
238  {
239    $query = '
240SELECT *
241  FROM '.COLLECTIONS_TABLE.'
242  WHERE user_id = '.$user['id'].'
243  ORDER BY
244    active DESC,
245    date_creation DESC
246;';
247    $collections = array_values(hash_from_query($query, 'id'));
248   
249    $data['collections'] = array();
250    for ($i=0; $i<$max && $i<count($collections); $i++)
251    {
252      $collections[$i]['count_handler'] = $col_in_edit!=0 ? $collections[$i]['id']==$col_in_edit : $collections[$i]['active'];
253      $collections[$i]['U_EDIT'] = USER_COLLEC_PUBLIC.'edit/'.$collections[$i]['id'];
254      array_push($data['collections'], $collections[$i]);
255    }
256   
257    $data['NB_COL'] = count($collections);
258    if ($data['NB_COL'] > $max)
259    {
260      $data['MORE'] = count($collections)-$max;
261    }
262   
263    $data['U_LIST'] = USER_COLLEC_PUBLIC;
264    $data['U_CREATE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'new','col_id'=>'0','redirect'=>'true'));
265   
266    $template->set_template_dir(USER_COLLEC_PATH . 'template/');
267    $block->set_title('<a href="'.USER_COLLEC_PUBLIC.'">'.l10n('Collections').'</a>');
268    $block->template = 'menublock_user_collec.tpl';
269    $block->data = $data;
270  }
271}
272
273?>
Note: See TracBrowser for help on using the repository browser.