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

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

-menu always displayed if user has unactive collections
-better user status checks
-localizations

File size: 7.6 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;
7
8switch ($page['sub_section'])
9{
10  /* list */
11  case 'list':
12  {
13    if (is_a_guest()) access_denied();
14   
15    $template->set_filename('index', dirname(__FILE__) . '/../template/list.tpl');
16   
17    if ( isset($_GET['action']) and filter_var($_GET['col_id'], FILTER_VALIDATE_INT) !== false )
18    {
19      switch ($_GET['action'])
20      {
21        // new
22        case 'new':
23        {
24          new UserCollection('new', array(), empty($_GET['name']) ? 'temp' : $_GET['name'], 1);
25          redirect(USER_COLLEC_PUBLIC);
26          break;
27        }
28       
29        // clear
30        case 'clear':
31        {
32          $query = '
33DELETE ci
34  FROM '.COLLECTION_IMAGES_TABLE.' AS ci
35    INNER JOIN '.COLLECTIONS_TABLE.' AS c
36    ON ci.col_id = c.id
37  WHERE
38    c.user_id = '.$user['id'].'
39    AND c.id = '.$_GET['col_id'].'
40;';
41          pwg_query($query);
42         
43          if (!empty($_SERVER['HTTP_REFERER']))
44          {
45            redirect($_SERVER['HTTP_REFERER']);
46          }
47          break;
48        }
49         
50        // delete
51        case 'delete':
52        {
53          $query = '
54DELETE ci, c
55  FROM '.COLLECTION_IMAGES_TABLE.' AS ci
56    RIGHT JOIN '.COLLECTIONS_TABLE.' AS c
57    ON ci.col_id = c.id
58  WHERE
59    c.user_id = '.$user['id'].'
60    AND c.id = '.$_GET['col_id'].'
61;';
62          pwg_query($query);
63     
64          redirect(USER_COLLEC_PUBLIC);
65          break;
66        }
67       
68        // save
69        case 'save':
70        {
71          if (empty($_GET['name']))
72          {
73            array_push($page['errors'], l10n('Please give a name'));
74          }
75          else
76          {
77            $query = '
78UPDATE '.COLLECTIONS_TABLE.'
79  SET
80    name = "'.pwg_db_real_escape_string($_GET['name']).'",
81    active = 0
82  WHERE
83    user_id = '.$user['id'].'
84    AND id = '.$_GET['col_id'].'
85;';
86            pwg_query($query);
87           
88            redirect(USER_COLLEC_PUBLIC);
89          }
90          break;
91        }
92       
93        // set active
94        case 'set_active':
95        {
96          $query = '
97UPDATE '.COLLECTIONS_TABLE.'
98  SET active = 0
99  WHERE user_id = '.$user['id'].'
100;';
101          pwg_query($query);
102         
103          $query = '
104UPDATE '.COLLECTIONS_TABLE.'
105  SET active = 1
106  WHERE
107    user_id = '.$user['id'].'
108    AND id = '.$_GET['col_id'].'
109;';
110          pwg_query($query);
111         
112          redirect(USER_COLLEC_PUBLIC);
113          break;
114        }
115      }
116    }
117     
118    // get collections
119    $query = '
120SELECT *
121  FROM '.COLLECTIONS_TABLE.'
122  WHERE user_id = '.$user['id'].'
123  ORDER BY date_creation DESC
124';
125    $collections = hash_from_query($query, 'id');
126   
127    foreach ($collections as $col)
128    {
129      $col['date_creation'] = format_date($col['date_creation'], true);
130      $col['U_EDIT'] = USER_COLLEC_PUBLIC.'edit/'.$col['id'];
131      $col['U_ACTIVE'] = USER_COLLEC_PUBLIC.'&amp;action=set_active&amp;col_id='.$col['id'];
132      $col['U_DELETE'] = USER_COLLEC_PUBLIC.'&amp;action=delete&amp;col_id='.$col['id'];
133     
134      if ($col['name'] == 'temp')
135      {
136        $col['name'] = 'temp #'.$col['id'];
137        $col['U_VIEW'] = $col['U_EDIT'];
138        $col['U_SAVE'] = USER_COLLEC_PUBLIC.'&amp;action=save&amp;col_id='.$col['id'];
139        $template->append('temp_col', $col);
140      }
141      else
142      {
143        $col['U_VIEW'] = USER_COLLEC_PUBLIC.'view/'.$col['id'];
144        $template->append('collections', $col);
145      }
146    }
147   
148    $template->assign('U_CREATE', USER_COLLEC_PUBLIC.'&amp;action=new&amp;col_id=0');
149    break;
150  }
151 
152  /* edit */
153  case 'edit':
154  {
155    if (empty($page['col_id']))
156    {
157      $_SESSION['page_errors'][] = l10n('Invalid collection');
158      redirect(USER_COLLEC_PUBLIC);
159    }
160   
161    $self_url = USER_COLLEC_PUBLIC . 'edit/'.$page['col_id'];
162   
163    $template->set_filename('index', dirname(__FILE__).'/../template/edit.tpl');
164    $template->assign(array(
165      'USER_COLLEC_PATH' => USER_COLLEC_PATH,
166      'U_VIEW' => $self_url,
167      'U_LIST' => USER_COLLEC_PUBLIC,
168      ));
169   
170    try {
171      $UserCollection = new UserCollection($page['col_id']);
172     
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        $UserCollection->updateParam('name', $_POST['name']);
182        $UserCollection->updateParam('public', $_POST['public']);
183      }
184     
185      // remove an element
186      if ( isset($_GET['remove']) and preg_match('#^[0-9]+$#', $_GET['remove']) )
187      {
188        $UserCollection->removeImages(array($_GET['remove']));
189      }
190     
191      $template->assign('collection', $UserCollection->getCollectionInfo());
192     
193      $template->set_prefilter('index_thumbnails', 'user_collection_thumbnails_list_prefilter');
194     
195      $page['start'] = isset($_GET['start']) ? $_GET['start'] : 0;
196      $page['items'] = $UserCollection->getImages();
197     
198      if (count($page['items']) > $page['nb_image_page'])
199      {
200        $page['navigation_bar'] = create_navigation_bar(
201          $self_url,
202          count($page['items']),
203          $page['start'],
204          $page['nb_image_page'],
205          false
206          );
207        $template->assign('navbar', $page['navigation_bar']);
208      }
209     
210      include(PHPWG_ROOT_PATH . 'include/category_default.inc.php');
211     
212      $template->concat('TITLE', $conf['level_separator'].$UserCollection->getParam('name'));
213    }
214    catch (Exception $e)
215    {
216      array_push($page['errors'], $e->getMessage());
217    }
218   
219    break;
220  }
221 
222  /* view */
223  case 'view':
224  {
225    if (empty($page['col_id']))
226    {
227      $_SESSION['page_errors'][] = l10n('Invalid collection');
228      redirect(get_home_url());
229    }
230   
231    $self_url = USER_COLLEC_PUBLIC . 'view/'.$page['col_id'];
232   
233    $template->set_filename('index', dirname(__FILE__).'/../template/view.tpl');
234    $template->assign(array(
235      'USER_COLLEC_PATH' => USER_COLLEC_PATH,
236      'U_VIEW' => $self_url,
237      ));
238   
239    try
240    {
241      $UserCollection = new UserCollection($page['col_id']);
242     
243      if ($UserCollection->getParam('user_id') == $user['id'])
244      {
245        $template->assign('U_LIST', USER_COLLEC_PUBLIC);
246      }
247     
248      $template->assign('collection', $UserCollection->getCollectionInfo());
249     
250      $page['start'] = isset($_GET['start']) ? $_GET['start'] : 0;
251      $page['items'] = $UserCollection->getImages();
252     
253      if (count($page['items']) > $page['nb_image_page'])
254      {
255        $page['navigation_bar'] = create_navigation_bar(
256          $self_url,
257          count($page['items']),
258          $page['start'],
259          $page['nb_image_page'],
260          false
261          );
262        $template->assign('navbar', $page['navigation_bar']);
263      }
264     
265      include(PHPWG_ROOT_PATH . 'include/category_default.inc.php');
266     
267      include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
268      $template->concat('TITLE', 
269        $conf['level_separator'].$UserCollection->getParam('name').
270        ' ('.sprintf(l10n('by %s'), get_username($UserCollection->getParam('user_id'))).')'
271        );
272    }
273    catch (Exception $e)
274    {
275      access_denied();
276    }
277   
278    break;
279  }
280}
281
282$template->assign('USER_COLLEC_PATH', USER_COLLEC_PATH);
283
284function user_collection_thumbnails_list_prefilter($content, &$smarty)
285{
286  $search = '<span class="thumbName">';
287 
288  $add = '<a href="{$U_VIEW}&amp;remove={$thumbnail.id}" rel="nofollow">
289<img src="{$USER_COLLEC_PATH}template/image_delete.png" title="{\'Remove from collection\'|@translate}">
290</a>&nbsp;';
291
292  return str_replace($search, $search.$add, $content);
293}
294
295?>
Note: See TracBrowser for help on using the repository browser.