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

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

-rework the main menu
-bug when a collection is create through AJAX
-add "Remove from collection" on colorbox
-minor interfaces changes

File size: 7.5 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  // if ( script_basename() == 'picture' and @$tokens[1] == 'collections' and preg_match('#^[0-9]+$#', @$tokens[2]) )
39  // {
40    // try
41    // {
42      // $UserCollection = new UserCollection($tokens[2]);
43      // $page['title'].= $conf['level_separator'].l10n('Collection').': <a href="'.USER_COLLEC_PUBLIC . 'view/'.$tokens[2].'">'.$UserCollection->getParam('name').'</a>';
44      // $page['items'] = $UserCollection->getImages();
45      // $page['col_id'] = $tokens[2];
46    // } catch (Exception $e) {}
47  // }
48}
49
50/* collections section */
51function user_collections_page()
52{
53  global $page;
54
55  if (isset($page['section']) and $page['section'] == 'collections')
56  {
57    include(USER_COLLEC_PATH . '/include/collections.inc.php');
58  }
59}
60
61
62/* add buttons on thumbnails list */
63function user_collections_index_actions()
64{
65  if (is_a_guest()) return;
66 
67  global $page, $UserCollection;
68     
69  // add image to collection list
70  if ( isset($_GET['collection_toggle']) and  preg_match('#^[0-9]+$#', $_GET['collection_toggle']) )
71  {
72    if (empty($UserCollection))
73    {
74      $UserCollection = new UserCollection(get_current_collection_id(true));
75    }
76    $UserCollection->toggleImage($_GET['collection_toggle']);
77    redirect(duplicate_index_url(array(), array('collection_toggle')));
78  }
79}
80
81function user_collections_thumbnails_list($tpl_thumbnails_var, $pictures)
82{
83  if (is_a_guest()) return $tpl_thumbnails_var;
84 
85  global $page, $template, $UserCollection;
86 
87  // the prefilter is different on collection page
88  if (isset($page['section']) and ($page['section'] == 'collections' or $page['section'] == 'download')) return $tpl_thumbnails_var;
89 
90  // get existing collections
91  if (empty($UserCollection) and ($col_id = get_current_collection_id(false)) !== 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 
106  $self_url = duplicate_index_url(array(), array('collection_toggle')); 
107 
108  foreach ($tpl_thumbnails_var as &$thumbnail)
109  {
110    if (in_array($thumbnail['id'], $collection))
111    {
112      $thumbnail['COLLECTION_SELECTED'] = true;
113    }
114  }
115  unset($thumbnail);
116 
117  // thumbnails buttons
118  $template->assign(array(
119    'USER_COLLEC_PATH' => USER_COLLEC_PATH,
120    'collection_toggle_url' =>  $self_url,
121    ));
122  $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_prefilter');
123 
124  return $tpl_thumbnails_var;
125}
126
127function user_collections_thumbnails_list_prefilter($content, &$smarty)
128{
129  // add links
130  $search = '<span class="wrap1">';
131  $replace = $search.'
132{strip}<a class="addCollection" href="{$collection_toggle_url}&amp;collection_toggle={$thumbnail.id}" data-id="{$thumbnail.id}" rel="nofollow">
133{if $COL_ID or $thumbnail.COLLECTION_SELECTED}
134{\'Remove from collection\'|@translate}&nbsp;<img src="{$USER_COLLEC_PATH}template/image_delete.png" title="{\'Remove from collection\'|@translate}">
135{else}
136{\'Add to collection\'|@translate}&nbsp;<img src="{$USER_COLLEC_PATH}template/image_add.png" title="{\'Add to collection\'|@translate}">
137{/if}
138</a>{/strip}';
139
140  // custom CSS and AJAX request
141  $content.= file_get_contents(USER_COLLEC_PATH.'template/thumbnails_css_js.tpl');
142
143  return str_replace($search, $replace, $content);
144}
145
146
147/* add button on picture page */
148function user_collections_picture_page()
149{
150  if (is_a_guest()) return;
151 
152  global $template, $picture, $UserCollection;
153 
154  // add image to collection list
155  if ( isset($_GET['action']) and $_GET['action'] == 'collection_toggle' )
156  {
157    if (empty($UserCollection))
158    {
159      $UserCollection = new UserCollection(get_current_collection_id(true));
160    }
161   
162    $UserCollection->toggleImage($picture['current']['id']);
163    redirect(duplicate_picture_url());
164  }
165 
166  // get existing collection
167  if (empty($UserCollection) and ($col_id = get_current_collection_id(false)) !== false)
168  {
169    $UserCollection = new UserCollection($col_id);
170    $collection = $UserCollection->isInSet($picture['current']['id']);
171  }
172  else if (!empty($UserCollection))
173  {
174    $collection = $UserCollection->isInSet($picture['current']['id']);
175  }
176  else
177  {
178    $collection = false;
179  } 
180 
181  $url = duplicate_picture_url().'&amp;action=collection_toggle';   
182 
183  $button = '
184<a href="'.$url.'" title="'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'" class="pwg-state-default pwg-button" rel="nofollow">
185  <span class="pwg-icon" style="background:url(\''.USER_COLLEC_PATH.'template/image_'.($collection?'delete':'add').'.png\') center center no-repeat;"> </span>
186  <span class="pwg-button-text">'.($collection?l10n('Remove from collection'):l10n('Add to collection')).'</span>
187</a>';
188   
189  $template->concat('PLUGIN_PICTURE_ACTIONS', $button);
190}
191
192
193/* menu block */
194function user_collections_add_menublock($menu_ref_arr)
195{
196  if (is_a_guest()) return;
197 
198  global $user;
199 
200  $menu = &$menu_ref_arr[0];
201  if ($menu->get_id() != 'menubar') return;
202 
203  $query = '
204SELECT id
205  FROM '.COLLECTIONS_TABLE.'
206  WHERE user_id = '.$user['id'].'
207  LIMIT 1
208;';
209  $result = pwg_query($query);
210 
211  if (!pwg_db_num_rows($result)) return;
212 
213  $menu->register_block(new RegisteredBlock('mbUserCollection', l10n('Collections'), 'UserCollection'));
214}
215
216function user_collections_applymenu($menu_ref_arr)
217{
218  $max = 6;
219 
220  global $template, $conf, $user, $UserCollection;
221  $menu = &$menu_ref_arr[0];
222 
223  if (($block = $menu->get_block('mbUserCollection')) != null)
224  {
225    $query = '
226SELECT *
227  FROM '.COLLECTIONS_TABLE.'
228  WHERE user_id = '.$user['id'].'
229  ORDER BY
230    active DESC,
231    date_creation DESC
232;';
233    $collections = array_values(hash_from_query($query, 'id'));
234   
235    $data['collections'] = array();
236    for ($i=0; $i<$max && $i<count($collections); $i++)
237    {
238      $collections[$i]['U_EDIT'] = USER_COLLEC_PUBLIC.'edit/'.$collections[$i]['id'];
239      array_push($data['collections'], $collections[$i]);
240    }
241   
242    $data['NB_COL'] = count($collections);
243    if ($data['NB_COL'] > $max)
244    {
245      $data['MORE'] = count($collections)-$max;
246    }
247   
248    $data['U_LIST'] = USER_COLLEC_PUBLIC;
249    $data['U_CREATE'] = USER_COLLEC_PUBLIC.'&amp;action=new&amp;col_id=0&amp;redirect=true';
250   
251    $template->set_template_dir(USER_COLLEC_PATH . 'template/');
252    $block->set_title('<a href="'.USER_COLLEC_PUBLIC.'">'.l10n('Collections').'</a>');
253    $block->template = 'menublock_user_collec.tpl';
254    $block->data = $data;
255  }
256}
257
258?>
Note: See TracBrowser for help on using the repository browser.