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

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

-add complete breadcrumb
-AJAX for add/remove photos (more faster)

File size: 8.7 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_SAVE'] = USER_COLLEC_PUBLIC.'&amp;action=save&amp;col_id='.$col['id'];
138        $template->append('temp_col', $col);
139      }
140      else
141      {
142        $template->append('collections', $col);
143      }
144    }
145   
146    $template->assign('U_CREATE', USER_COLLEC_PUBLIC.'&amp;action=new&amp;col_id=0');
147    break;
148  }
149 
150  /* edit */
151  case 'edit':
152  {
153    if (empty($page['col_id']))
154    {
155      $_SESSION['page_errors'][] = l10n('Invalid collection');
156      redirect(USER_COLLEC_PUBLIC);
157    }
158   
159    $self_url = USER_COLLEC_PUBLIC . 'edit/'.$page['col_id'];
160   
161    $template->set_filename('index', dirname(__FILE__).'/../template/edit.tpl');
162    $template->assign(array(
163      'USER_COLLEC_PATH' => USER_COLLEC_PATH,
164      'U_VIEW' => $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      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      // remove an element
185      if ( isset($_GET['remove']) and preg_match('#^[0-9]+$#', $_GET['remove']) )
186      {
187        $UserCollection->removeImages(array($_GET['remove']));
188      }
189     
190      $template->assign('collection', $UserCollection->getCollectionInfo());
191     
192      $template->set_prefilter('index_thumbnails', 'user_collections_thumbnails_list_special_prefilter');
193     
194      $page['start'] = isset($_GET['start']) ? $_GET['start'] : 0;
195      $page['items'] = $UserCollection->getImages();
196     
197      if (count($page['items']) > $page['nb_image_page'])
198      {
199        $page['navigation_bar'] = create_navigation_bar(
200          $self_url,
201          count($page['items']),
202          $page['start'],
203          $page['nb_image_page'],
204          false
205          );
206        $template->assign('navbar', $page['navigation_bar']);
207      }
208     
209      include(PHPWG_ROOT_PATH . 'include/category_default.inc.php');
210     
211      $template->concat('TITLE', $conf['level_separator'].$UserCollection->getParam('name'));
212    }
213    catch (Exception $e)
214    {
215      array_push($page['errors'], $e->getMessage());
216    }
217   
218    break;
219  }
220 
221  /* view */
222  case 'view':
223  {
224    if (empty($page['col_id']))
225    {
226      $_SESSION['page_errors'][] = l10n('Invalid collection');
227      redirect(get_home_url());
228    }
229   
230    $self_url = USER_COLLEC_PUBLIC . 'view/'.$page['col_id'];
231   
232    $template->set_filename('index', dirname(__FILE__).'/../template/view.tpl');
233    $template->assign(array(
234      'USER_COLLEC_PATH' => USER_COLLEC_PATH,
235      'U_VIEW' => $self_url,
236      ));
237   
238    try
239    {
240      $UserCollection = new UserCollection($page['col_id']);
241     
242      if ($UserCollection->getParam('user_id') == $user['id'])
243      {
244        $template->assign('U_LIST', USER_COLLEC_PUBLIC);
245      }
246     
247      $template->assign('collection', $UserCollection->getCollectionInfo());
248     
249      $page['start'] = isset($_GET['start']) ? $_GET['start'] : 0;
250      $page['items'] = $UserCollection->getImages();
251     
252      if (count($page['items']) > $page['nb_image_page'])
253      {
254        $page['navigation_bar'] = create_navigation_bar(
255          $self_url,
256          count($page['items']),
257          $page['start'],
258          $page['nb_image_page'],
259          false
260          );
261        $template->assign('navbar', $page['navigation_bar']);
262      }
263     
264      include(PHPWG_ROOT_PATH . 'include/category_default.inc.php');
265     
266      include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
267      $template->concat('TITLE', 
268        $conf['level_separator'].$UserCollection->getParam('name').
269        ' ('.sprintf(l10n('by %s'), get_username($UserCollection->getParam('user_id'))).')'
270        );
271    }
272    catch (Exception $e)
273    {
274      access_denied();
275    }
276   
277    break;
278  }
279}
280
281$template->assign('USER_COLLEC_PATH', USER_COLLEC_PATH);
282
283
284function user_collections_thumbnails_list_special_prefilter($content, &$smarty)
285{
286  // custom style
287  $search[0] = '{/html_style}';
288  $replace[0] = '.thumbnails  .wrap1 {ldelim} position:relative; }
289.addCollection {ldelim} width:100%;height:16px;display:none;position:absolute;top:0;background:rgba(0,0,0,0.8);padding:2px;border-radius:2px;font-size:0.8em; }
290.wrap1:hover .addCollection {ldelim} display:block; }'
291.$search[0];
292
293  // links
294  $search[1] = '<span class="wrap1">';
295  $replace[1] = $search[1].'
296{strip}<a class="addCollection" href="{$U_VIEW}&amp;remove={$thumbnail.id}" data-id="{$thumbnail.id}" rel="nofollow">
297{\'Remove from collection\'|@translate}&nbsp;<img src="{$USER_COLLEC_PATH}template/image_delete.png" title="{\'Remove from collection\'|@translate}">
298</a>{/strip}';
299
300  // AJAX request
301  $search[2] = '{/html_style}';
302  $replace[2] = $search[2].'
303{footer_script require=\'jquery\'}
304jQuery(".addCollection").click(function() {ldelim}
305  var toggle_id = jQuery(this).data("id");
306  var $trigger = jQuery(this);
307 
308  jQuery.ajax({ldelim}
309    type: "POST",
310    url: "{$USER_COLLEC_PATH}toggle_image.php",
311    data: {ldelim} "col_id": "{$COL_ID}", "toggle_id": toggle_id }
312  }).done(function(msg) {ldelim}
313    if (msg == "false") {ldelim}
314      $trigger.parent(".wrap1").hide("fast", function() {ldelim} $trigger.remove() });
315    } else {ldelim}
316      $trigger.html(\'{\'Un unknown error occured\'|@translate}\');
317    }
318  });
319 
320  return false;
321});
322{/footer_script}';
323
324  return str_replace($search, $replace, $content);
325}
326
327?>
Note: See TracBrowser for help on using the repository browser.