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

Last change on this file since 20090 was 20090, checked in by mistic100, 11 years ago
  • add webservices
  • add mail function
  • add admin list
  • add admin export function
  • try to deal with Gthumb+
  • activate multisize dropdown menu of collection page

TODO : use webservices instead of toggle_image.php

File size: 11.2 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_collections' => $conf['user_collections'],
162      'USER_COLLEC_PATH' => USER_COLLEC_PATH,
163      'F_ACTION' => $self_url,
164      'collection_toggle_url' => $self_url,
165      'U_LIST' => USER_COLLEC_PUBLIC,
166      'COL_ID' => $page['col_id'],
167      ));
168   
169    try {
170      $UserCollection = new UserCollection($page['col_id']);
171     
172      // security
173      if ( !is_admin() and $UserCollection->getParam('user_id') != $user['id'] )
174      {
175        access_denied();
176      }
177     
178      // save properties
179      if (isset($_POST['save_col']))
180      {
181        if (empty($_POST['name']))
182        {
183          array_push($page['errors'], l10n('Please give a name'));
184        }
185        else
186        {
187          $UserCollection->updateParam('name', stripslashes($_POST['name']));
188        }
189        if (!$conf['user_collections']['allow_public'])
190        {
191          $_POST['public'] = '0';
192        }
193        $UserCollection->updateParam('public', $_POST['public']);
194      }
195     
196      // send mail
197      if ( $conf['user_collections']['allow_public'] and $conf['user_collections']['allow_mails'] )
198      {
199        $contact = array(
200            'sender_name' => $user['username'],
201            'sender_email' => $user['email'],
202            'recipient_name' => null,
203            'recipient_email' => null,
204            'nb_images' => 4,
205            'message' => null,
206            );
207           
208        if ( isset($_POST['send_mail']) and (bool)$UserCollection->getParam('public') )
209        {
210          $contact = array(
211            'sender_email' => trim($_POST['sender_email']),
212            'sender_name' => trim($_POST['sender_name']),
213            'recipient_email' => trim($_POST['recipient_email']),
214            'recipient_name' => trim($_POST['recipient_name']),
215            'nb_images' => $_POST['nb_images'],
216            'message' => $_POST['message'],
217            );
218           
219          $errors = $UserCollection->sendEmail($contact, @$_POST['key']);
220          if (count($errors))
221          {
222            $template->assign('uc_mail_errors', $errors);
223          }
224        }
225       
226        $contact['KEY'] = get_ephemeral_key(3);
227        $template->assign('contact', $contact);
228      }
229     
230      // clear
231      if ( isset($_GET['action']) and $_GET['action'] == 'clear' )
232      {
233        $UserCollection->clearImages();
234      }
235     
236      // remove an element
237      if ( isset($_GET['collection_toggle']) and preg_match('#^[0-9]+$#', $_GET['collection_toggle']) )
238      {
239        $UserCollection->removeImages(array($_GET['collection_toggle']));
240        unset($_GET['collection_toggle']);
241      }
242     
243      // add links for colorbox
244      add_event_handler('loc_end_index_thumbnails', 'user_collections_thumbnails_in_collection', EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
245     
246      // add remove item links
247      $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_prefilter');
248     
249      // collection content
250      $col = $UserCollection->getCollectionInfo();
251      $col['DATE_CREATION'] = format_date($col['DATE_CREATION'], true);
252      $col['U_CLEAR'] = $self_url.'&amp;action=clear';
253      $col['U_DELETE'] = add_url_params(USER_COLLEC_PUBLIC, array('action'=>'delete','col_id'=>$page['col_id']));
254      $template->assign('collection', $col);
255      $page['items'] = $UserCollection->getImages();
256     
257      // toolbar buttons
258      $buttons = null;
259      if ($conf['user_collections']['allow_public'] and $conf['user_collections']['allow_mails'])
260      {
261        $buttons.= '<li><a class="mail_colorbox_open" href="#mail_form" title="'.l10n('Send this collection my mail').'" class="pwg-state-default pwg-button" rel="nofollow" '.(!$col['PUBLIC']?'onClick="alert(\''.l10n('The collection must be public in order to send it').'\');return false;"':null).'>
262          <span class="pwg-icon user-collections-delete-icon" style="background:url(\'' . get_root_url().USER_COLLEC_PATH . 'template/resources/mail.png\') center center no-repeat;">&nbsp;</span><span class="pwg-button-text">'.l10n('Send').'</span>
263        </a></li>';
264      }
265      $buttons.= '
266        <li><a href="'. $col['U_CLEAR'] .'" title="'.l10n('Clear this collection').'" class="pwg-state-default pwg-button" rel="nofollow" onClick="return confirm(\''.l10n('Are you sure?').'\');">
267          <span class="pwg-icon user-collections-delete-icon" style="background:url(\'' . get_root_url().USER_COLLEC_PATH . 'template/resources/bin.png\') center center no-repeat;">&nbsp;</span><span class="pwg-button-text">'.l10n('Clear').'</span>
268        </a></li>
269        <li><a href="'. $col['U_DELETE'] .'" title="'.l10n('Delete this collection').'" class="pwg-state-default pwg-button" rel="nofollow" onClick="return confirm(\''.l10n('Are you sure?').'\');">
270          <span class="pwg-icon user-collections-delete-icon" style="background:url(\'' . get_root_url().USER_COLLEC_PATH . 'template/resources/delete.png\') center center no-repeat;">&nbsp;</span><span class="pwg-button-text">'.l10n('Delete').'</span>
271        </a></li>';
272      $template->concat('COLLECTION_ACTIONS', $buttons);
273     
274      // thumbnails
275      define('USER_COLLEC_REMOVE_GTHUMB', true);
276      include(USER_COLLEC_PATH . '/include/display_thumbnails.inc.php');
277     
278      $template->concat('TITLE', $conf['level_separator'].$UserCollection->getParam('name'));
279    }
280    catch (Exception $e)
281    {
282      array_push($page['errors'], $e->getMessage());
283    }
284   
285    break;
286  }
287 
288  /* view */
289  case 'view':
290  {
291    // security
292    if ( empty($page['col_id']) or strlen($page['col_id']) != 10 or strpos($page['col_id'], 'uc') === false or !$conf['user_collections']['allow_public'] )
293    {
294      $_SESSION['page_errors'][] = l10n('Invalid collection');
295      redirect('index.php');
296    }
297   
298    $template->set_filename('index', dirname(__FILE__).'/../template/view.tpl');
299   
300    $self_url = USER_COLLEC_PUBLIC . 'view/'.$page['col_id'];
301   
302    try {
303      $UserCollection = new UserCollection($page['col_id']);
304      $page['items'] = $UserCollection->getImages();
305     
306      // thumbnails
307      include(USER_COLLEC_PATH . '/include/display_thumbnails.inc.php');
308     
309      // add username in title
310      include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
311      $template->concat('TITLE', 
312        $conf['level_separator'].$UserCollection->getParam('name').
313        ' ('.sprintf(l10n('by %s'), get_username($UserCollection->getParam('user_id'))).')'
314        );
315    }
316    catch (Exception $e)
317    {
318      access_denied();
319    }
320   
321    break;
322  }
323}
324
325
326$template->assign(array(
327  'USER_COLLEC_PATH' => USER_COLLEC_PATH,
328  'USER_COLLEC_ABS_PATH' => realpath(USER_COLLEC_PATH).'/',
329  ));
330
331
332function user_collections_thumbnails_in_collection($tpl_thumbnails_var, $pictures)
333{
334  global $template, $page;
335 
336  $template->set_filename('index_thumbnails', dirname(__FILE__).'/../template/thumbnails.tpl');
337 
338  foreach ($tpl_thumbnails_var as &$thumbnail)
339  {
340    $src_image = new SrcImage($thumbnail);
341   
342    $thumbnail['FILE_SRC'] = DerivativeImage::url(IMG_LARGE, $src_image);
343    $thumbnail['URL'] = duplicate_picture_url(
344        array(
345          'image_id' => $thumbnail['id'],
346          'image_file' => $thumbnail['file'],
347          'section' => 'none',
348        ),
349        array('start')
350      );
351  }
352 
353  return $tpl_thumbnails_var;
354}
355
356?>
Note: See TracBrowser for help on using the repository browser.