source: extensions/UserCollections/include/collections.inc.php @ 17657

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

consolidate upgrade process, remote filter_var function (PHP 5.2)

File size: 8.8 KB
Line 
1<?php
2defined('USER_COLLEC_PATH') or die('Hacking attempt!');
3
4# this file is called on basket public page #
5
6global $page, $template, $conf, $user, $tokens, $pwg_loaded_plugins;
7
8switch ($page['sub_section'])
9{
10  /* list */
11  case 'list':
12  {
13    // security
14    if (is_a_guest()) access_denied();
15   
16    $template->set_filename('index', dirname(__FILE__) . '/../template/list.tpl');
17   
18    // actions
19    if ( isset($_GET['action']) and preg_match('#^([0-9]+)$#', $_GET['col_id']) )
20    {
21      switch ($_GET['action'])
22      {
23        // new
24        case 'new':
25        {
26          $UserCollection = new UserCollection('new', array(), empty($_GET['name']) ? 'temp' : $_GET['name'], 1);
27         
28          if (isset($_GET['redirect']))
29          {
30            $redirect = USER_COLLEC_PUBLIC.'edit/'.$UserCollection->getParam('id');
31          }
32          else
33          {
34            $redirect = USER_COLLEC_PUBLIC;
35          }
36          redirect($redirect);
37          break;
38        }
39         
40        // delete
41        case 'delete':
42        {
43          $query = '
44DELETE ci, c
45  FROM '.COLLECTION_IMAGES_TABLE.' AS ci
46    RIGHT JOIN '.COLLECTIONS_TABLE.' AS c
47    ON ci.col_id = c.id
48  WHERE
49    c.user_id = '.$user['id'].'
50    AND c.id = '.$_GET['col_id'].'
51;';
52          pwg_query($query);
53     
54          redirect(USER_COLLEC_PUBLIC);
55          break;
56        }
57       
58        // save
59        case 'save':
60        {
61          if (empty($_GET['name']))
62          {
63            array_push($page['errors'], l10n('Please give a name'));
64          }
65          else
66          {
67            $query = '
68UPDATE '.COLLECTIONS_TABLE.'
69  SET
70    name = "'.pwg_db_real_escape_string($_GET['name']).'",
71    active = 0
72  WHERE
73    user_id = '.$user['id'].'
74    AND id = '.$_GET['col_id'].'
75;';
76            pwg_query($query);
77           
78            redirect(USER_COLLEC_PUBLIC);
79          }
80          break;
81        }
82       
83        // set active
84        case 'set_active':
85        {
86          $query = '
87UPDATE '.COLLECTIONS_TABLE.'
88  SET active = 0
89  WHERE user_id = '.$user['id'].'
90;';
91          pwg_query($query);
92         
93          $query = '
94UPDATE '.COLLECTIONS_TABLE.'
95  SET active = 1
96  WHERE
97    user_id = '.$user['id'].'
98    AND id = '.$_GET['col_id'].'
99;';
100          pwg_query($query);
101         
102          redirect(USER_COLLEC_PUBLIC);
103          break;
104        }
105      }
106    }
107   
108   
109    // get collections
110    $query = '
111SELECT *
112  FROM '.COLLECTIONS_TABLE.'
113  WHERE user_id = '.$user['id'].'
114  ORDER BY date_creation DESC
115';
116    $collections = hash_from_query($query, 'id');
117   
118    foreach ($collections as $col)
119    {
120      $col['date_creation'] = format_date($col['date_creation'], true);
121      $col['U_EDIT'] = USER_COLLEC_PUBLIC.'edit/'.$col['id'];
122      $col['U_ACTIVE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'set_active','col_id'=>$col['id']));
123      $col['U_DELETE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'delete','col_id'=>$col['id']));
124     
125      if (isset($pwg_loaded_plugins['BatchDownloader']))
126      {
127        $col['U_DOWNLOAD'] = add_url_params(USER_COLLEC_PUBLIC.'view/'.$col['public_id'], array('action'=>'advdown_set'));
128      }
129     
130      // temporary collections are above save collections
131      if ($col['name'] == 'temp')
132      {
133        $col['name'] = 'temp #'.$col['id'];
134        $col['U_SAVE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'save','col_id'=>$col['id']));
135        $template->append('temp_col', $col);
136      }
137      else
138      {
139        $template->append('collections', $col);
140      }
141    }
142   
143    $template->assign('U_CREATE', add_url_params(USER_COLLEC_PUBLIC, array('action'=>'new','col_id'=>'0')));
144    break;
145  }
146 
147  /* edit */
148  case 'edit':
149  {
150    // security
151    if (empty($page['col_id']))
152    {
153      $_SESSION['page_errors'][] = l10n('Invalid collection');
154      redirect(USER_COLLEC_PUBLIC);
155    }
156   
157    $template->set_filename('index', dirname(__FILE__).'/../template/edit.tpl');
158   
159    $self_url = USER_COLLEC_PUBLIC . 'edit/'.$page['col_id'];
160    $template->assign(array(
161      'USER_COLLEC_PATH' => USER_COLLEC_PATH,
162      'F_ACTION' => $self_url,
163      'collection_toggle_url' => $self_url,
164      'U_LIST' => USER_COLLEC_PUBLIC,
165      'COL_ID' => $page['col_id'],
166      ));
167   
168    try {
169      $UserCollection = new UserCollection($page['col_id']);
170     
171      // security
172      if ( !is_admin() and $UserCollection->getParam('user_id') != $user['id'] )
173      {
174        access_denied();
175      }
176     
177      // save properties
178      if (isset($_POST['save_col']))
179      {
180        $UserCollection->updateParam('name', $_POST['name']);
181        $UserCollection->updateParam('public', $_POST['public']);
182      }
183     
184      // clear
185      if ( isset($_GET['action']) and $_GET['action'] == 'clear' )
186      {
187        $UserCollection->clearImages();
188      }
189     
190      // remove an element
191      if ( isset($_GET['collection_toggle']) and preg_match('#^[0-9]+$#', $_GET['collection_toggle']) )
192      {
193        $UserCollection->removeImages(array($_GET['collection_toggle']));
194        unset($_GET['collection_toggle']);
195      }
196     
197      // special template
198      add_event_handler('loc_end_index_thumbnails', 'user_collections_thumbnails_in_collection', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 2); // +10 to overload GThumb+
199      $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_prefilter');
200     
201      // collection content
202      $col = $UserCollection->getCollectionInfo();
203      $col['U_CLEAR'] = $self_url.'&amp;action=clear';
204      $col['U_DELETE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'delete','col_id'=>$page['col_id']));
205      $template->assign('collection', $col);
206     
207      $page['items'] = $UserCollection->getImages();
208     
209      // navigation bar
210      $page['start'] = isset($_GET['start']) ? $_GET['start'] : 0;
211      if (count($page['items']) > $page['nb_image_page'])
212      {
213        $page['navigation_bar'] = create_navigation_bar(
214          $self_url,
215          count($page['items']),
216          $page['start'],
217          $page['nb_image_page'],
218          false
219          );
220        $template->assign('navbar', $page['navigation_bar']);
221      }
222     
223      // display
224      include(PHPWG_ROOT_PATH . 'include/category_default.inc.php');
225     
226      $template->concat('TITLE', $conf['level_separator'].$UserCollection->getParam('name'));
227    }
228    catch (Exception $e)
229    {
230      array_push($page['errors'], $e->getMessage());
231    }
232   
233    break;
234  }
235 
236  /* view */
237  case 'view':
238  {
239    // security
240    if ( empty($page['col_id']) or strlen($page['col_id']) != 10 or strpos($page['col_id'], 'uc') === false )
241    {
242      $_SESSION['page_errors'][] = l10n('Invalid collection');
243      redirect('index.php');
244    }
245   
246    $template->set_filename('index', dirname(__FILE__).'/../template/view.tpl');
247   
248    $self_url = USER_COLLEC_PUBLIC . 'view/'.$page['col_id'];
249   
250    try {
251      $UserCollection = new UserCollection($page['col_id']);
252     
253      // special template
254      add_event_handler('loc_end_index_thumbnails', 'user_collections_thumbnails_in_collection', EVENT_HANDLER_PRIORITY_NEUTRAL+10, 2); // +10 to overload GThumb+
255     
256      // collection content
257      $page['items'] = $UserCollection->getImages();
258     
259      // navigation bar
260      $page['start'] = isset($_GET['start']) ? $_GET['start'] : 0;
261      if (count($page['items']) > $page['nb_image_page'])
262      {
263        $page['navigation_bar'] = create_navigation_bar(
264          $self_url,
265          count($page['items']),
266          $page['start'],
267          $page['nb_image_page'],
268          false
269          );
270        $template->assign('navbar', $page['navigation_bar']);
271      }
272     
273      // display
274      include(PHPWG_ROOT_PATH . 'include/category_default.inc.php');
275     
276      include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
277      $template->concat('TITLE', 
278        $conf['level_separator'].$UserCollection->getParam('name').
279        ' ('.sprintf(l10n('by %s'), get_username($UserCollection->getParam('user_id'))).')'
280        );
281    }
282    catch (Exception $e)
283    {
284      access_denied();
285    }
286   
287    break;
288  }
289}
290
291$template->assign(array(
292  'USER_COLLEC_PATH' => USER_COLLEC_PATH,
293  'USER_COLLEC_ABS_PATH' => realpath(USER_COLLEC_PATH).'/',
294  ));
295
296
297function user_collections_thumbnails_in_collection($tpl_thumbnails_var, $pictures)
298{
299  global $template, $page;
300 
301  $template->set_filename('index_thumbnails', dirname(__FILE__).'/../template/thumbnails.tpl');
302 
303  foreach ($tpl_thumbnails_var as &$thumbnail)
304  {
305    $src_image = new SrcImage($thumbnail);
306   
307    $thumbnail['FILE_SRC'] = DerivativeImage::url(IMG_LARGE, $src_image);
308    $thumbnail['URL'] = duplicate_picture_url(
309        array(
310          'image_id' => $thumbnail['id'],
311          'image_file' => $thumbnail['file'],
312          'section' => 'none',
313        ),
314        array('start')
315      );
316  }
317 
318  return $tpl_thumbnails_var;
319}
320
321?>
Note: See TracBrowser for help on using the repository browser.