aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/AdminTools/include/events.inc.php
blob: 5bdfa1baaa14a255b0b20be18db3ae063760cc6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
<?php
defined('ADMINTOOLS_PATH') or die('Hacking attempt!');

/**
 * Add main toolbar to current page
 * @trigger loc_after_page_header
 */
function admintools_add_public_controller()
{
  global $MultiView, $conf, $template, $page, $user, $picture;

  if (script_basename() == 'picture' and empty($picture['current']))
  {
    return;
  }

  $url_root = get_root_url();
  $tpl_vars = array();

  if ($MultiView->is_admin())
  { // full options for admin
    $tpl_vars['U_SITE_ADMIN'] =     $url_root . 'admin.php?page=';
    $tpl_vars['MULTIVIEW'] =        $MultiView->get_data();
    $tpl_vars['USER'] =             $MultiView->get_user();
    $tpl_vars['CURRENT_USERNAME'] = $user['id']==$conf['guest_id'] ? l10n('guest') : $user['username'];
    $tpl_vars['DELETE_CACHE'] =     isset($conf['multiview_invalidate_cache']);

    if (($admin_lang = $MultiView->get_user_language()) !== false)
    {
      include_once(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
      switch_lang_to($admin_lang);
    }
  }
  else if ($conf['AdminTools']['public_quick_edit'] and
      script_basename() == 'picture' and $picture['current']['added_by'] == $user['id']
    )
  { // only "edit" button for photo owner
  }
  else
  {
    return;
  }

  $tpl_vars['POSITION'] = $conf['AdminTools']['closed_position'];
  $tpl_vars['DEFAULT_OPEN'] = $conf['AdminTools']['default_open'];
  $tpl_vars['U_SELF'] = $MultiView->get_clean_url(true);

  // photo page
  if (script_basename() == 'picture')
  {
    $url_self = duplicate_picture_url();
    $tpl_vars['IS_PICTURE'] = true;

    // admin can add to caddie and set representattive
    if ($MultiView->is_admin())
    {
      $template->clear_assign(array(
        'U_SET_AS_REPRESENTATIVE',
        'U_PHOTO_ADMIN',
        'U_CADDIE',
        ));

      $template->set_prefilter('picture', 'admintools_remove_privacy');

      $tpl_vars['U_CADDIE'] = add_url_params(
        $url_self,
        array('action'=>'add_to_caddie')
        );

      $query = '
SELECT element_id FROM ' . CADDIE_TABLE . '
  WHERE element_id = ' . $page['image_id'] .'
;';
      $tpl_vars['IS_IN_CADDIE'] = pwg_db_num_rows(pwg_query($query)) > 0;

      if (isset($page['category']))
      {
        $tpl_vars['CATEGORY_ID'] = $page['category']['id'];

        $tpl_vars['U_SET_REPRESENTATIVE'] = add_url_params(
          $url_self,
          array('action'=>'set_as_representative')
          );

        $tpl_vars['IS_REPRESENTATIVE'] = $page['category']['representative_picture_id'] == $page['image_id'];
      }

      $tpl_vars['U_ADMIN_EDIT'] = $url_root . 'admin.php?page=photo-' . $page['image_id']
        .(isset($page['category']) ? '&amp;cat_id=' . $page['category']['id'] : '');
    }

    $tpl_vars['U_DELETE'] = add_url_params(
      $url_self, array(
        'delete'=>'',
        'pwg_token'=>get_pwg_token()
        )
      );

    // gets tags (full available list is loaded in ajax)
    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');

    $query = '
SELECT id, name
  FROM '.IMAGE_TAG_TABLE.' AS it
    JOIN '.TAGS_TABLE.' AS t ON t.id = it.tag_id
  WHERE image_id = '.$page['image_id'].'
;';
    $tag_selection = get_taglist($query);

    $tpl_vars['QUICK_EDIT'] = array(
      'img' =>                $picture['current']['derivatives']['square']->get_url(),
      'name' =>               $picture['current']['name'],
      'comment' =>            $picture['current']['comment'],
      'author' =>             $picture['current']['author'],
      'level' =>              $picture['current']['level'],
      'date_creation' =>      substr($picture['current']['date_creation'], 0, 10),
      'date_creation_time' => substr($picture['current']['date_creation'], 11, 5),
      'tag_selection' =>      $tag_selection,
      );
  }
  // album page (admin only)
  else if ($MultiView->is_admin() and @$page['section'] == 'categories' and isset($page['category']))
  {
    $url_self = duplicate_index_url();

    $tpl_vars['IS_CATEGORY'] = true;
    $tpl_vars['CATEGORY_ID'] = $page['category']['id'];

    $template->clear_assign(array(
      'U_EDIT',
      'U_CADDIE',
      ));

    $tpl_vars['U_ADMIN_EDIT'] = $url_root . 'admin.php?page=album-' . $page['category']['id'];

    if (!empty($page['items']))
    {
      $tpl_vars['U_CADDIE'] = add_url_params(
        $url_self,
        array('caddie'=>1)
        );
    }

    $tpl_vars['QUICK_EDIT'] = array(
      'img' =>      null,
      'name' =>     $page['category']['name'],
      'comment' =>  $page['category']['comment'],
      );

    if (!empty($page['category']['representative_picture_id']))
    {
      $query = '
SELECT * FROM '.IMAGES_TABLE.'
  WHERE id = '. $page['category']['representative_picture_id'] .'
;';
      $image_infos = pwg_db_fetch_assoc(pwg_query($query));

      $tpl_vars['QUICK_EDIT']['img'] = DerivativeImage::get_one(IMG_SQUARE, $image_infos)->get_url();
    }
  }


  $template->assign(array(
    'ADMINTOOLS_PATH' => './plugins/' . ADMINTOOLS_ID .'/',
    'ato' => $tpl_vars,
  ));

  $template->set_filename('ato_public_controller', realpath(ADMINTOOLS_PATH . 'template/public_controller.tpl'));
  $template->parse('ato_public_controller');

  if ($MultiView->is_admin() && @$admin_lang !== false)
  {
    switch_lang_back();
  }
}

/**
 * Add main toolbar to current page
 * @trigger loc_after_page_header
 */
function admintools_add_admin_controller()
{
  global $MultiView, $conf, $template, $page, $user;

  $url_root = get_root_url();
  $tpl_vars = array();

  $tpl_vars['MULTIVIEW'] =     $MultiView->get_data();
  $tpl_vars['DELETE_CACHE'] =  isset($conf['multiview_invalidate_cache']);
  $tpl_vars['U_SELF'] =        $MultiView->get_clean_admin_url(true);
  
  if (($admin_lang = $MultiView->get_user_language()) !== false)
  {
    include_once(PHPWG_ROOT_PATH . 'include/functions_mail.inc.php');
    switch_lang_to($admin_lang);
  }

  $template->assign(array(
    'ADMINTOOLS_PATH' => './plugins/' . ADMINTOOLS_ID .'/',
    'ato' => $tpl_vars,
  ));

  $template->set_filename('ato_admin_controller', realpath(ADMINTOOLS_PATH . 'template/admin_controller.tpl'));
  $template->parse('ato_admin_controller');

  if ($MultiView->is_admin() && @$admin_lang !== false)
  {
    switch_lang_back();
  }
  
  $template->set_prefilter('header', 'admintools_admin_prefilter');
}

function admintools_admin_prefilter($content)
{
  $search = '<a class="icon-brush tiptip" href="{$U_CHANGE_THEME}" title="{\'Switch to clear or dark colors for administration\'|translate}">{\'Change Admin Colors\'|translate}</a>';
  $replace = '<span id="ato_container"><a class="icon-cog-alt" href="#">{\'Tools\'|translate}</a></span>';
  return str_replace($search, $replace, $content);
}

/**
 * Disable privacy level switchbox
 */
function admintools_remove_privacy($content)
{
  $search = '{if $display_info.privacy_level and isset($available_permission_levels)}';
  $replace = '{if false}';
  return str_replace($search, $replace, $content);
}

/**
 * Save picture form
 * @trigger loc_begin_picture
 */
function admintools_save_picture()
{
  global $page, $conf, $MultiView, $user, $picture;

  if (!isset($_GET['delete']) and !isset($_POST['action']) and @$_POST['action'] != 'quick_edit')
  {
    return;
  }

  $query = 'SELECT added_by FROM '. IMAGES_TABLE .' WHERE id = '. $page['image_id'] .';';
  list($added_by) = pwg_db_fetch_row(pwg_query($query));

  if (!$MultiView->is_admin() and $user['id'] != $added_by)
  {
    return;
  }

  if (isset($_GET['delete']) and get_pwg_token()==@$_GET['pwg_token'])
  {
    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');

    delete_elements(array($page['image_id']), true);
    invalidate_user_cache();

    if (isset($page['rank_of'][ $page['image_id'] ]))
    {
      redirect(
        duplicate_index_url(
          array(
            'start' =>
              floor($page['rank_of'][ $page['image_id'] ] / $page['nb_image_page'])
              * $page['nb_image_page']
            )
          )
        );
    }
    else
    {
      redirect(make_index_url());
    }
  }

  if ($_POST['action'] == 'quick_edit')
  {
    include_once(PHPWG_ROOT_PATH . 'admin/include/functions.php');

    $data = array(
      'name' =>   $_POST['name'],
      'author' => $_POST['author'],
      );

    if ($MultiView->is_admin())
    {
      $data['level'] = $_POST['level'];
    }

    if ($conf['allow_html_descriptions'])
    {
      $data['comment'] = @$_POST['comment'];
    }
    else
    {
      $data['comment'] = strip_tags(@$_POST['comment']);
    }

    if (!empty($_POST['date_creation']) and strtotime($_POST['date_creation']) !== false)
    {
      $data['date_creation'] = $_POST['date_creation'] .' '. $_POST['date_creation_time'];
    }

    single_update(
      IMAGES_TABLE,
      $data,
      array('id' => $page['image_id'])
      );

    $tag_ids = array();
    if (!empty($_POST['tags']))
    {
      $tag_ids = get_tag_ids($_POST['tags']);
    }
    set_tags($tag_ids, $page['image_id']);
  }
}

/**
 * Save category form
 * @trigger loc_begin_index
 */
function admintools_save_category()
{
  global $page, $conf, $MultiView;

  if (!$MultiView->is_admin())
  {
    return;
  }

  if (@$_POST['action'] == 'quick_edit')
  {
    $data = array(
      'name' => $_POST['name'],
      );

    if ($conf['allow_html_descriptions'])
    {
      $data['comment'] = @$_POST['comment'];
    }
    else
    {
      $data['comment'] = strip_tags(@$_POST['comment']);
    }

    single_update(
      CATEGORIES_TABLE,
      $data,
      array('id' => $page['category']['id'])
      );

    redirect(duplicate_index_url());
  }
}