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

Last change on this file since 17519 was 17519, checked in by mistic100, 12 years ago

clean definition of USER_COLLEC_PUBLIC
missing one template var for themes compatibility

File size: 7.1 KB
Line 
1<?php
2defined('USER_COLLEC_PATH') or die('Hacking attempt!');
3
4# this file contains all functions directly called by the triggers #
5
6/* unserialize conf and load language */
7function user_collections_init()
8{
9  load_language('plugin.lang', USER_COLLEC_PATH);
10}
11
12
13/* define page section from url */
14function user_collections_section_init()
15{
16  global $tokens, $page, $conf;
17
18  if ($tokens[0] == 'collections')
19  {
20    $page['section'] = 'collections';
21    $page['title'] = '<a href="'.get_absolute_root_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].'<a href="'.USER_COLLEC_PUBLIC.'">'.l10n('Collections').'</a>';
22   
23    if (in_array(@$tokens[1], array('edit','view','list')))
24    {
25       $page['sub_section'] = $tokens[1];
26    }
27    else
28    {
29      $page['sub_section'] = 'list';
30    }
31   
32    if (!empty($tokens[2]))
33    {
34      $page['col_id'] = $tokens[2];
35    }
36  }
37}
38
39/* collections section */
40function user_collections_page()
41{
42  global $page;
43
44  if (isset($page['section']) and $page['section'] == 'collections')
45  {
46    include(USER_COLLEC_PATH . '/include/collections.inc.php');
47  }
48}
49
50
51/* add buttons on thumbnails list */
52function user_collections_index_actions()
53{
54  if (is_a_guest()) return;
55 
56  global $page, $UserCollection;
57     
58  // add image to collection list
59  if ( isset($_GET['collection_toggle']) and  preg_match('#^[0-9]+$#', $_GET['collection_toggle']) )
60  {
61    if (empty($UserCollection))
62    {
63      $UserCollection = new UserCollection(get_current_collection_id(true));
64    }
65    $UserCollection->toggleImage($_GET['collection_toggle']);
66    redirect(duplicate_index_url(array(), array('collection_toggle')));
67  }
68}
69
70function user_collections_thumbnails_list($tpl_thumbnails_var, $pictures)
71{
72  if (is_a_guest()) return $tpl_thumbnails_var;
73 
74  global $page, $template, $UserCollection;
75 
76  // the prefilter is different on collection page
77  if (isset($page['section']) and ($page['section'] == 'collections' or $page['section'] == 'download')) return $tpl_thumbnails_var;
78 
79  // get existing collections
80  $col_id = get_current_collection_id(false);
81  if (empty($UserCollection) and $col_id !== false)
82  {
83    $UserCollection = new UserCollection($col_id);
84    $collection = $UserCollection->getImages();
85  }
86  else if (!empty($UserCollection))
87  {
88    $collection = $UserCollection->getImages();
89  }
90  else
91  {
92    $collection = array();
93  }
94 
95  // if the collection is created we don't use AJAX to force menu refresh
96  if ($col_id === false)
97  {
98    $template->assign('NO_AJAX', true);
99  }
100 
101 
102  $self_url = duplicate_index_url(array(), array('collection_toggle')); 
103 
104  foreach ($tpl_thumbnails_var as &$thumbnail)
105  {
106    if (in_array($thumbnail['id'], $collection))
107    {
108      $thumbnail['COLLECTION_SELECTED'] = true;
109    }
110  }
111  unset($thumbnail);
112 
113  // thumbnails buttons
114  $template->assign(array(
115    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
116    'collection_toggle_url' =>  add_url_params($self_url, array('collection_toggle'=>'')),
117    ));
118  $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_prefilter');
119 
120  return $tpl_thumbnails_var;
121}
122
123function user_collections_thumbnails_list_prefilter($content, &$smarty)
124{
125  // add links
126  $search = '<span class="wrap1">';
127  $replace = $search.'
128{strip}<a class="addCollection" href="{$collection_toggle_url}{$thumbnail.id}" data-id="{$thumbnail.id}" rel="nofollow">
129{if $COL_ID or $thumbnail.COLLECTION_SELECTED}
130{\'Remove from collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/image_delete.png" title="{\'Remove from collection\'|@translate}">
131{else}
132{\'Add to collection\'|@translate}&nbsp;<img src="{$ROOT_URL}{$USER_COLLEC_PATH}template/image_add.png" title="{\'Add to collection\'|@translate}">
133{/if}
134</a>{/strip}';
135
136  // custom CSS and AJAX request
137  $content.= file_get_contents(USER_COLLEC_PATH.'template/thumbnails_css_js.tpl');
138
139  return str_replace($search, $replace, $content);
140}
141
142
143/* add button on picture page */
144function user_collections_picture_page()
145{
146  if (is_a_guest()) return;
147 
148  global $template, $picture, $UserCollection;
149 
150  // add image to collection list
151  if ( isset($_GET['action']) and $_GET['action'] == 'collection_toggle' )
152  {
153    if (empty($UserCollection))
154    {
155      $UserCollection = new UserCollection(get_current_collection_id(true));
156    }
157   
158    $UserCollection->toggleImage($picture['current']['id']);
159    redirect(duplicate_picture_url());
160  }
161 
162  // get existing collection
163  if (empty($UserCollection) and ($col_id = get_current_collection_id(false)) !== false)
164  {
165    $UserCollection = new UserCollection($col_id);
166    $collection = $UserCollection->isInSet($picture['current']['id']);
167  }
168  else if (!empty($UserCollection))
169  {
170    $collection = $UserCollection->isInSet($picture['current']['id']);
171  }
172  else
173  {
174    $collection = false;
175  } 
176 
177  $url = add_url_params(duplicate_picture_url(), array('action'=>'collection_toggle'));   
178 
179  $button = '
180<a href="'.$url.'" title="'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'" class="pwg-state-default pwg-button" rel="nofollow">
181  <span class="pwg-icon" style="background:url(\''.get_root_url().USER_COLLEC_PATH.'template/image_'.($collection?'delete':'add').'.png\') center center no-repeat;"> </span>
182  <span class="pwg-button-text">'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'</span>
183</a>';
184  // $template->add_picture_button($button, 50);
185  $template->concat('PLUGIN_PICTURE_ACTIONS', $button);
186}
187
188
189/* menu block */
190function user_collections_add_menublock($menu_ref_arr)
191{
192  if (is_a_guest()) return;
193 
194  $menu = &$menu_ref_arr[0];
195  if ($menu->get_id() != 'menubar') return;
196   
197  $menu->register_block(new RegisteredBlock('mbUserCollection', l10n('Collections'), 'UserCollection'));
198}
199
200function user_collections_applymenu($menu_ref_arr)
201{
202  $max = 6;
203 
204  global $template, $conf, $user, $UserCollection;
205  $menu = &$menu_ref_arr[0];
206 
207  if (($block = $menu->get_block('mbUserCollection')) != null)
208  {
209    $query = '
210SELECT *
211  FROM '.COLLECTIONS_TABLE.'
212  WHERE user_id = '.$user['id'].'
213  ORDER BY
214    active DESC,
215    date_creation DESC
216;';
217    $collections = array_values(hash_from_query($query, 'id'));
218   
219    $data['collections'] = array();
220    for ($i=0; $i<$max && $i<count($collections); $i++)
221    {
222      $collections[$i]['U_EDIT'] = USER_COLLEC_PUBLIC.'edit/'.$collections[$i]['id'];
223      array_push($data['collections'], $collections[$i]);
224    }
225   
226    $data['NB_COL'] = count($collections);
227    if ($data['NB_COL'] > $max)
228    {
229      $data['MORE'] = count($collections)-$max;
230    }
231   
232    $data['U_LIST'] = USER_COLLEC_PUBLIC;
233    $data['U_CREATE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'new','col_id'=>'0','redirect'=>'true'));
234   
235    $template->set_template_dir(USER_COLLEC_PATH . 'template/');
236    $block->set_title('<a href="'.USER_COLLEC_PUBLIC.'">'.l10n('Collections').'</a>');
237    $block->template = 'menublock_user_collec.tpl';
238    $block->data = $data;
239  }
240}
241
242?>
Note: See TracBrowser for help on using the repository browser.