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

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

compatible with Simple, new export options

File size: 8.4 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 
102  // template vars
103  $url = duplicate_index_url(array(), array('collection_toggle')); 
104 
105  foreach ($tpl_thumbnails_var as &$thumbnail)
106  {
107    if (in_array($thumbnail['id'], $collection))
108    {
109      $thumbnail['COLLECTION_SELECTED'] = true;
110    }
111  }
112  unset($thumbnail);
113 
114  $template->assign(array(
115    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
116    'collection_toggle_url' =>  add_url_params($url, array('collection_toggle'=>'')),
117    ));
118 
119  // thumbnails buttons
120  $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_prefilter');
121 
122  return $tpl_thumbnails_var;
123}
124
125function user_collections_thumbnails_list_prefilter($content, &$smarty)
126{
127  // add links
128  $search = '#(<li>|<li class="gthumb">)#';
129  $replace = '$1
130{strip}<a class="addCollection" href="{$collection_toggle_url}{$thumbnail.id}" data-id="{$thumbnail.id}" rel="nofollow">
131<span class="uc_remove" {if not $COL_ID and not $thumbnail.COLLECTION_SELECTED}style="display:none;"{/if}>
132{\'Remove from collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_delete.png" title="{\'Remove from collection\'|@translate}">
133</span>
134<span class="uc_add" {if $COL_ID or $thumbnail.COLLECTION_SELECTED}style="display:none;"{/if}>
135{\'Add to collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/resources/image_add.png" title="{\'Add to collection\'|@translate}">
136</span>
137</a>{/strip}';
138
139  // custom CSS and AJAX request
140  $content.= file_get_contents(USER_COLLEC_PATH.'template/thumbnails_css_js.tpl');
141 
142  return preg_replace($search, $replace, $content);
143}
144
145
146// +-----------------------------------------------------------------------+
147// | PICTURE PAGE
148// +-----------------------------------------------------------------------+
149/* add button on picture page */
150function user_collections_picture_page()
151{
152  if (is_a_guest()) return;
153 
154  global $template, $picture, $UserCollection;
155 
156  // add image to collection list
157  if ( isset($_GET['action']) and $_GET['action'] == 'collection_toggle' )
158  {
159    if (empty($UserCollection))
160    {
161      $UserCollection = new UserCollection(get_current_collection_id(true));
162    }
163   
164    $UserCollection->toggleImage($picture['current']['id']);
165    redirect(duplicate_picture_url());
166  }
167 
168  // get existing collection
169  if (empty($UserCollection) and ($col_id = get_current_collection_id(false)) !== false)
170  {
171    $UserCollection = new UserCollection($col_id);
172    $collection = $UserCollection->isInSet($picture['current']['id']);
173  }
174  else if (!empty($UserCollection))
175  {
176    $collection = $UserCollection->isInSet($picture['current']['id']);
177  }
178  else
179  {
180    $collection = false;
181  } 
182 
183  $url = add_url_params(duplicate_picture_url(), array('action'=>'collection_toggle'));   
184 
185  $button = '
186<a href="'.$url.'" title="'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'" class="pwg-state-default pwg-button" rel="nofollow">
187  <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>
188  <span class="pwg-button-text">'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'</span>
189</a>';
190  // $template->add_picture_button($button, 50);
191  $template->concat('PLUGIN_PICTURE_ACTIONS', $button);
192}
193
194
195// +-----------------------------------------------------------------------+
196// | MENU BLOCK
197// +-----------------------------------------------------------------------+
198/* register block */
199function user_collections_add_menublock($menu_ref_arr)
200{
201  if (is_a_guest()) return;
202 
203  $menu = &$menu_ref_arr[0];
204  if ($menu->get_id() != 'menubar') return;
205   
206  $menu->register_block(new RegisteredBlock('mbUserCollection', l10n('Collections'), 'UserCollection'));
207}
208
209/* fill block */
210function user_collections_applymenu($menu_ref_arr)
211{
212  $max = 6;
213 
214  global $template, $page, $conf, $user, $UserCollection;
215  $menu = &$menu_ref_arr[0];
216 
217  // the editable counter is for the active collection, except if we are currently editing a collection
218  $col_in_edit = 0;
219  if ( @$page['section'] == 'collections' and @$page['sub_section']=='edit' and !empty($page['col_id']) )
220  {
221    $col_in_edit = $page['col_id'];
222  }
223 
224  if (($block = $menu->get_block('mbUserCollection')) != null)
225  {
226    $query = '
227SELECT *
228  FROM '.COLLECTIONS_TABLE.'
229  WHERE user_id = '.$user['id'].'
230  ORDER BY
231    active DESC,
232    date_creation DESC
233;';
234    $collections = array_values(hash_from_query($query, 'id'));
235   
236    $data['collections'] = array();
237    for ($i=0; $i<$max && $i<count($collections); $i++)
238    {
239      $collections[$i]['count_handler'] = $col_in_edit!=0 ? $collections[$i]['id']==$col_in_edit : $collections[$i]['active'];
240      $collections[$i]['U_EDIT'] = USER_COLLEC_PUBLIC.'edit/'.$collections[$i]['id'];
241      array_push($data['collections'], $collections[$i]);
242    }
243   
244    $data['NB_COL'] = count($collections);
245    if ($data['NB_COL'] > $max)
246    {
247      $data['MORE'] = count($collections)-$max;
248    }
249   
250    $data['U_LIST'] = USER_COLLEC_PUBLIC;
251    $data['U_CREATE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'new','col_id'=>'0','redirect'=>'true'));
252   
253    $template->set_template_dir(USER_COLLEC_PATH . 'template/');
254    $block->set_title('<a href="'.USER_COLLEC_PUBLIC.'">'.l10n('Collections').'</a>');
255    $block->template = 'menublock_user_collec.tpl';
256    $block->data = $data;
257  }
258}
259
260?>
Note: See TracBrowser for help on using the repository browser.