source: extensions/AdminTools/include/events.inc.php @ 27487

Last change on this file since 27487 was 27487, checked in by mistic100, 10 years ago

redirect to previous page keeping pagination when deleting photo

File size: 8.0 KB
Line 
1<?php
2defined('ADMINTOOLS_PATH') or die('Hacking attempt!');
3
4/**
5 * Add main toolbar to current page
6 * @trigger loc_after_page_header
7 */
8function admintools_add_public_controller()
9{
10  global $MultiView, $conf, $template, $page, $user, $picture;
11
12  if (script_basename() == 'picture' and empty($picture['current']))
13  {
14    return;
15  }
16
17  $url_root = get_root_url();
18  $tpl_vars = array();
19
20  if ($MultiView->is_admin())
21  { // full options for admin
22    $tpl_vars['U_SITE_ADMIN'] =     $url_root . 'admin.php?page=';
23    $tpl_vars['MULTIVIEW'] =        $MultiView->get_data();
24    $tpl_vars['USER'] =             $MultiView->get_user();
25    $tpl_vars['CURRENT_USERNAME'] = $user['id']==$conf['guest_id'] ? l10n('guest') : $user['username'];
26    $tpl_vars['DELETE_CACHE'] =     isset($conf['multiview_invalidate_cache']);
27
28    if (($admin_lang = $MultiView->get_user_language()) !== false)
29    {
30      include_once(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
31      switch_lang_to($admin_lang);
32    }
33  }
34  else if ($conf['AdminTools']['public_quick_edit'] and
35      script_basename() == 'picture' and $picture['current']['added_by'] == $user['id']
36    )
37  { // only "edit" button for photo owner
38  }
39  else
40  {
41    return;
42  }
43
44  $tpl_vars['POSITION'] = $conf['AdminTools']['closed_position'];
45  $tpl_vars['DEFAULT_OPEN'] = $conf['AdminTools']['default_open'];
46  $tpl_vars['U_SELF'] = $MultiView->get_clean_url(true);
47
48  // photo page
49  if (script_basename() == 'picture')
50  {
51    $url_self = duplicate_picture_url();
52    $tpl_vars['IS_PICTURE'] = true;
53
54    // admin can add to caddie and set representattive
55    if ($MultiView->is_admin())
56    {
57      $template->clear_assign(array(
58        'U_SET_AS_REPRESENTATIVE',
59        'U_PHOTO_ADMIN',
60        'U_CADDIE',
61        ));
62
63      $template->set_prefilter('picture', 'admintools_remove_privacy');
64
65      $tpl_vars['U_CADDIE'] = add_url_params(
66        $url_self,
67        array('action'=>'add_to_caddie')
68        );
69
70      $query = '
71SELECT element_id FROM ' . CADDIE_TABLE . '
72  WHERE element_id = ' . $page['image_id'] .'
73;';
74      $tpl_vars['IS_IN_CADDIE'] = pwg_db_num_rows(pwg_query($query)) > 0;
75
76      if (isset($page['category']))
77      {
78        $tpl_vars['CATEGORY_ID'] = $page['category']['id'];
79
80        $tpl_vars['U_SET_REPRESENTATIVE'] = add_url_params(
81          $url_self,
82          array('action'=>'set_as_representative')
83          );
84
85        $tpl_vars['IS_REPRESENTATIVE'] = $page['category']['representative_picture_id'] == $page['image_id'];
86      }
87
88      $tpl_vars['U_ADMIN_EDIT'] = $url_root . 'admin.php?page=photo-' . $page['image_id']
89        .(isset($page['category']) ? '&amp;cat_id=' . $page['category']['id'] : '');
90    }
91
92    $tpl_vars['U_DELETE'] = add_url_params(
93      $url_self, array(
94        'delete'=>'',
95        'pwg_token'=>get_pwg_token()
96        )
97      );
98
99    // gets tags (full available list is loaded in ajax)
100    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
101
102    $query = '
103SELECT id, name
104  FROM '.IMAGE_TAG_TABLE.' AS it
105    JOIN '.TAGS_TABLE.' AS t ON t.id = it.tag_id
106  WHERE image_id = '.$page['image_id'].'
107;';
108    $tag_selection = get_taglist($query);
109
110    $tpl_vars['QUICK_EDIT'] = array(
111      'img' =>                $picture['current']['derivatives']['square']->get_url(),
112      'name' =>               $picture['current']['name'],
113      'comment' =>            $picture['current']['comment'],
114      'author' =>             $picture['current']['author'],
115      'level' =>              $picture['current']['level'],
116      'date_creation' =>      substr($picture['current']['date_creation'], 0, 10),
117      'date_creation_time' => substr($picture['current']['date_creation'], 11, 5),
118      'tag_selection' =>      $tag_selection,
119      );
120  }
121  // album page (admin only)
122  else if ($MultiView->is_admin() and @$page['section'] == 'categories' and isset($page['category']))
123  {
124    $url_self = duplicate_index_url();
125
126    $tpl_vars['IS_CATEGORY'] = true;
127    $tpl_vars['CATEGORY_ID'] = $page['category']['id'];
128
129    $template->clear_assign(array(
130      'U_EDIT',
131      'U_CADDIE',
132      ));
133
134    $tpl_vars['U_ADMIN_EDIT'] = $url_root . 'admin.php?page=album-' . $page['category']['id'];
135
136    if (!empty($page['items']))
137    {
138      $tpl_vars['U_CADDIE'] = add_url_params(
139        $url_self,
140        array('caddie'=>1)
141        );
142    }
143
144    $tpl_vars['QUICK_EDIT'] = array(
145      'img' =>      null,
146      'name' =>     $page['category']['name'],
147      'comment' =>  $page['category']['comment'],
148      );
149
150    if (!empty($page['category']['representative_picture_id']))
151    {
152      $query = '
153SELECT * FROM '.IMAGES_TABLE.'
154  WHERE id = '. $page['category']['representative_picture_id'] .'
155;';
156      $image_infos = pwg_db_fetch_assoc(pwg_query($query));
157
158      $tpl_vars['QUICK_EDIT']['img'] = DerivativeImage::get_one(IMG_SQUARE, $image_infos)->get_url();
159    }
160  }
161
162
163  $template->assign(array(
164    'ADMINTOOLS_PATH' => './plugins/' . ADMINTOOLS_ID .'/',
165    'ato' => $tpl_vars,
166  ));
167
168  $template->set_filename('ato_public_controller', realpath(ADMINTOOLS_PATH . 'template/public_controller.tpl'));
169  $template->parse('ato_public_controller');
170
171  if ($MultiView->is_admin() && @$admin_lang !== false)
172  {
173    switch_lang_back();
174  }
175}
176
177/**
178 * Disable privacy level switchbox
179 */
180function admintools_remove_privacy($content)
181{
182  $search = '{if $display_info.privacy_level and isset($available_permission_levels)}';
183  $replace = '{if false}';
184  return str_replace($search, $replace, $content);
185}
186
187/**
188 * Save picture form
189 * @trigger loc_begin_picture
190 */
191function admintools_save_picture()
192{
193  global $page, $conf, $MultiView, $user, $picture;
194
195  if (!isset($_GET['delete']) and !isset($_POST['action']) and @$_POST['action'] != 'quick_edit')
196  {
197    return;
198  }
199
200  $query = 'SELECT added_by FROM '. IMAGES_TABLE .' WHERE id = '. $page['image_id'] .';';
201  list($added_by) = pwg_db_fetch_row(pwg_query($query));
202
203  if (!$MultiView->is_admin() and $user['id'] != $added_by)
204  {
205    return;
206  }
207
208  if (isset($_GET['delete']) and get_pwg_token()==@$_GET['pwg_token'])
209  {
210    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
211
212    delete_elements(array($page['image_id']), true);
213    invalidate_user_cache();
214
215    if (isset($page['rank_of'][ $page['image_id'] ]))
216    {
217      redirect(
218        duplicate_index_url(
219          array(
220            'start' =>
221              floor($page['rank_of'][ $page['image_id'] ] / $page['nb_image_page'])
222              * $page['nb_image_page']
223            )
224          )
225        );
226    }
227    else
228    {
229      redirect(make_index_url());
230    }
231  }
232
233  if ($_POST['action'] == 'quick_edit')
234  {
235    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');
236
237    $data = array(
238      'name' =>   $_POST['name'],
239      'author' => $_POST['author'],
240      );
241
242    if ($MultiView->is_admin())
243    {
244      $data['level'] = $_POST['level'];
245    }
246
247    if ($conf['allow_html_descriptions'])
248    {
249      $data['comment'] = @$_POST['comment'];
250    }
251    else
252    {
253      $data['comment'] = strip_tags(@$_POST['comment']);
254    }
255
256    if (!empty($_POST['date_creation']) and strtotime($_POST['date_creation']) !== false)
257    {
258      $data['date_creation'] = $_POST['date_creation'] .' '. $_POST['date_creation_time'];
259    }
260
261    single_update(
262      IMAGES_TABLE,
263      $data,
264      array('id' => $page['image_id'])
265      );
266
267    $tag_ids = array();
268    if (!empty($_POST['tags']))
269    {
270      $tag_ids = get_tag_ids($_POST['tags']);
271    }
272    set_tags($tag_ids, $page['image_id']);
273  }
274}
275
276/**
277 * Save category form
278 * @trigger loc_begin_index
279 */
280function admintools_save_category()
281{
282  global $page, $conf, $MultiView;
283
284  if (!$MultiView->is_admin())
285  {
286    return;
287  }
288
289  if (@$_POST['action'] == 'quick_edit')
290  {
291    $data = array(
292      'name' => $_POST['name'],
293      );
294
295    if ($conf['allow_html_descriptions'])
296    {
297      $data['comment'] = @$_POST['comment'];
298    }
299    else
300    {
301      $data['comment'] = strip_tags(@$_POST['comment']);
302    }
303
304    single_update(
305      CATEGORIES_TABLE,
306      $data,
307      array('id' => $page['category']['id'])
308      );
309
310    redirect(duplicate_index_url());
311  }
312}
Note: See TracBrowser for help on using the repository browser.