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

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

replace toggle_image.php by webservices

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