source: extensions/Subscribe_to_comments/include/events.inc.php @ 26144

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

change layout of form (use colorbox for standalone) + cleaning of subscriptions page

File size: 9.4 KB
RevLine 
[12560]1<?php
[26139]2defined('SUBSCRIBE_TO_PATH') or die('Hacking attempt!');
[12560]3
4/**
[15641]5 * detect 'subscriptions' section and load the page
[12560]6 */
[12708]7function stc_detect_section()
8{
[21340]9  global $tokens, $page, $conf;
[26139]10
[12560]11  if ($tokens[0] == 'subscriptions')
12  {
13    $page['section'] = 'subscriptions';
[26139]14    $page['body_id'] = 'theSubscriptionsPage';
15    $page['is_homepage'] = false;
16    $page['is_external'] = true;
17
[15641]18    $page['title'] = l10n('Comments notifications');
[12560]19  }
20}
21
[12708]22function stc_load_section()
23{
[12560]24  global $page;
25
26  if (isset($page['section']) and $page['section'] == 'subscriptions')
27  {
[26139]28    include(SUBSCRIBE_TO_PATH.'include/subscriptions_page.inc.php');
[12560]29  }
30}
31
32
33/**
[15641]34 * send notifications and/or add subscriber on comment insertion
35 * @param: array comment
[12560]36 */
37function stc_comment_insertion($comm)
38{
[12600]39  global $page, $template;
[26139]40
[12560]41  if ($comm['action'] == 'validate')
42  {
43    send_comment_to_subscribers($comm);
44  }
[26139]45
46  if (!empty($_POST['stc_mode']) and $_POST['stc_mode'] != -1)
[12560]47  {
[15641]48    if ($comm['action'] != 'reject')
[12560]49    {
[15641]50      if (isset($comm['image_id']))
[12600]51      {
[15641]52        switch ($_POST['stc_mode'])
53        {
54          case 'all-images':
[21340]55            subscribe_to_comments(@$_POST['email'], 'all-images');
[15641]56            break;
57          case 'album-images':
[26139]58            if (!empty($page['category']['id']))
59            {
60              subscribe_to_comments(@$_POST['email'], 'album-images', $page['category']['id']);
61            }
[15641]62            break;
63          case 'image':
[21340]64            subscribe_to_comments(@$_POST['email'], 'image', $comm['image_id']);
[15641]65            break;
66        }
[12600]67      }
[15641]68      else if (isset($comm['category_id']))
[12600]69      {
[15641]70        switch ($_POST['stc_mode'])
71        {
72          case 'all-albums':
[21340]73            subscribe_to_comments(@$_POST['email'], 'all-albums');
[15641]74            break;
75          case 'album':
[21340]76            subscribe_to_comments(@$_POST['email'], 'album', $comm['category_id']);
[15641]77            break;
78        }
[12600]79      }
[26144]80
81      unset($_POST['stc_mode']);
[12600]82    }
[12560]83  }
84}
85
[15641]86
87/**
88 * send notifications on comment validation
89 * @param: array|int comment_id
90 * @param: string type (image|category)
91 */
[12600]92function stc_comment_validation($comm_ids, $type='image')
93{
[26139]94  if (!is_array($comm_ids))
95  {
96    $comm_ids = array($comm_ids);
97  }
98
[15641]99  if ($type == 'image')
[12560]100  {
[15641]101    $query = '
[12560]102SELECT
103    id,
104    image_id,
105    author,
106    content
107  FROM '.COMMENTS_TABLE.'
[15641]108  WHERE id IN('.implode(',', $comm_ids).')
[12560]109;';
[15641]110  }
[26139]111  else if ($type == 'album' && defined('COA_TABLE'))
[15641]112  {
113    $query = '
[12560]114SELECT
115    id,
116    category_id,
117    author,
118    content
119  FROM '.COA_TABLE.'
[15641]120  WHERE id IN('.implode(',', $comm_ids).')
[12560]121;';
[15641]122  }
[26139]123
124  if (isset($query))
[15641]125  {
[26139]126    $comms = query2array($query);
127    foreach ($comms as $comm)
128    {
129      send_comment_to_subscribers($comm);
130    }
[12560]131  }
132}
133
134
135/**
[12561]136 * add field and link on picture page
[12560]137 */
138function stc_on_picture()
139{
[15641]140  global $template, $picture, $page, $user, $conf;
[26139]141
[15641]142  // standalone subscription
[12609]143  if (isset($_POST['stc_submit']))
[12561]144  {
[15641]145    switch ($_POST['stc_mode'])
[12600]146    {
[15641]147      case 'all-images':
148        subscribe_to_comments(@$_POST['stc_mail'], 'all-images');
149        break;
150      case 'album-images':
[26139]151        if (!empty($page['category']['id']))
152        {
153          subscribe_to_comments(@$_POST['stc_mail'], 'album-images', $page['category']['id']);
154        }
[15641]155        break;
156      case 'image':
157        subscribe_to_comments(@$_POST['stc_mail'], 'image', $picture['current']['id']);
158        break;
[12600]159    }
[26144]160    unset($_POST['stc_mode']);
[12561]161  }
[12600]162  else if (isset($_GET['stc_unsubscribe']))
[26139]163  {
[15641]164    if (un_subscribe_to_comments(null, $_GET['stc_unsubscribe']))
[12600]165    {
[26139]166      $page['infos'][] = l10n('Successfully unsubscribed your email address from receiving notifications.');
[12600]167    }
168  }
[26139]169
[26144]170  $tpl_vars = array(
171    'ASK_MAIL' => is_a_guest() or empty($user['email']),
172    'ON_PICTURE' => true,
173    'ALLOW_ALBUM_IMAGES' => !empty($page['category']['id']),
174    'ALLOW_GLOBAL' => $conf['Subscribe_to_Comments']['allow_global_subscriptions'] || is_admin(),
175    );
176
177  if (!empty($_POST['stc_mode']))
178  {
179    $tpl_vars['MODE'] = $_POST['stc_mode'];
180  }
181
[12609]182  // if registered user with mail we check if already subscribed
[26139]183  if (!is_a_guest() and !empty($user['email']))
[12561]184  {
[15641]185    $subscribed = false;
[26139]186
187    $base_query = '
[12561]188SELECT id
189  FROM '.SUBSCRIBE_TO_TABLE.'
190  WHERE
191    email = "'.$user['email'].'"
[12600]192    AND validated = "true"
[26139]193';
194
195    // registered to all pictures
196    if (!$subscribed)
197    {
198      $result = pwg_query($base_query . 'AND type = "all-images";');
199
[15641]200      if (pwg_db_num_rows($result))
201      {
[21439]202        list($stc_id) = pwg_db_fetch_row($result);
[26139]203        $subscribed = 'all-images';
[15641]204      }
205    }
206
207    // registered to pictures in this album
[26139]208    if (!$subscribed and !empty($page['category']['id']))
[12561]209    {
[26139]210      $result = pwg_query($base_query . 'AND type = "album-images" AND element_id = '.$page['category']['id'].';');
211
[15641]212      if (pwg_db_num_rows($result))
213      {
214        list($stc_id) = pwg_db_fetch_row($result);
[26139]215        $subscribed = 'album-images';
[15641]216      }
[12561]217    }
[26139]218
[15641]219    // registered to this picture
220    if (!$subscribed)
221    {
[26139]222      $result = pwg_query($base_query . 'AND type = "image" AND element_id = '.$picture['current']['id'].';');
223
[15641]224      if (pwg_db_num_rows($result))
225      {
226        list($stc_id) = pwg_db_fetch_row($result);
[26139]227        $subscribed = 'image';
[15641]228      }
229    }
[26139]230
231    if ($subscribed)
232    {
[26144]233      $tpl_vars['SUBSCRIBED'] = $subscribed;
234      $tpl_vars['U_UNSUB'] = add_url_params($picture['current']['url'], array('stc_unsubscribe'=>$stc_id));
[26139]235    }
[12561]236  }
[26139]237
[21340]238  $template->assign(array(
239    'SUBSCRIBE_TO_PATH' => SUBSCRIBE_TO_PATH,
[26144]240    'STC' => $tpl_vars,
[21340]241    ));
[26139]242
[15641]243  $template->set_prefilter('picture', 'stc_main_prefilter');
[12560]244}
245
[15641]246
[12561]247/**
[15641]248 * add field and link on album page
[12561]249 */
250function stc_on_album()
251{
[21441]252  global $page, $template, $user, $conf;
[26139]253
[26144]254  // standalone subscription
[12609]255  if (isset($_POST['stc_submit']))
[12561]256  {
[15641]257    switch ($_POST['stc_mode'])
[12600]258    {
[15641]259      case 'all-albums':
260        subscribe_to_comments(@$_POST['stc_mail'], 'all-albums');
261        break;
262      case 'album':
263        subscribe_to_comments(@$_POST['stc_mail'], 'album', $page['category']['id']);
264        break;
[12600]265    }
[26144]266    unset($_POST['stc_mode']);
[12561]267  }
[12600]268  else if (isset($_GET['stc_unsubscribe']))
269  {
[15641]270    if (un_subscribe_to_comments(null, $_GET['stc_unsubscribe']))
[12600]271    {
[26139]272      $page['infos'][] = l10n('Successfully unsubscribed your email address from receiving notifications.');
[12600]273    }
274  }
[26139]275
[26144]276  $tpl_vars = array(
277    'ASK_MAIL' => is_a_guest() or empty($user['email']),
278    'ON_ALBUM' => true,
279    'ALLOW_GLOBAL' => $conf['Subscribe_to_Comments']['allow_global_subscriptions'] || is_admin(),
280    );
281
282  if (!empty($_POST['stc_mode']))
283  {
284    $tpl_vars['MODE'] = $_POST['stc_mode'];
285  }
286
[12561]287  // if registered user we check if already subscribed
[26144]288  if (!is_a_guest() and !empty($user['email']))
[12561]289  {
[15641]290    $subscribed = false;
[26139]291
292    $base_query = '
[12561]293SELECT id
294  FROM '.SUBSCRIBE_TO_TABLE.'
295  WHERE
296    email = "'.$user['email'].'"
[12600]297    AND validated = "true"
[26139]298';
299
300    // registered to all albums
301    if (!$subscribed)
302    {
303      $result = pwg_query($base_query . 'AND type = "all-albums"');
304
[15641]305      if (pwg_db_num_rows($result))
306      {
[21439]307        list($stc_id) = pwg_db_fetch_row($result);
[26139]308        $subscribed = 'all-albums';
[15641]309      }
310    }
[26139]311
[15641]312    // registered to this album
313    if (!$subscribed)
[12561]314    {
[26139]315      $result = pwg_query($base_query . 'AND type = "album" AND element_id = '.$page['category']['id'].';');
316
[15641]317      if (pwg_db_num_rows($result))
318      {
319        list($stc_id) = pwg_db_fetch_row($result);
[26139]320        $subscribed = 'album';
[15641]321      }
[12607]322    }
[26139]323
324    if ($subscribed)
325    {
[26144]326      $element_url = make_index_url(array(
327        'section' => 'categories',
328        'category' => $page['category'],
[26139]329        ));
[26144]330
331      $tpl_vars['SUBSCRIBED'] = $subscribed;
332      $tpl_vars['U_UNSUB'] = add_url_params($element_url, array('stc_unsubscribe'=>$stc_id));
[26139]333    }
[12607]334  }
[26139]335
[21340]336  $template->assign(array(
[26144]337    'STC' => $tpl_vars,
[21340]338    'SUBSCRIBE_TO_PATH' => SUBSCRIBE_TO_PATH,
339    ));
[15641]340
341  $template->set_prefilter('comments_on_albums', 'stc_main_prefilter');
[12708]342}
343
344
345/**
[15641]346 * main prefilter
[12708]347 */
[26144]348function stc_main_prefilter($content)
[15641]349{
[12607]350  ## subscribe at any moment ##
[26144]351  $search = '{if isset($comment_add)}';
352  $add = file_get_contents(SUBSCRIBE_TO_PATH.'template/form_outside.tpl');
353  $content = str_replace($search, $search.$add, $content);
[26139]354
[12708]355  ## subscribe while add a comment ##
[17350]356  $search = '{$comment_add.CONTENT}</textarea></p>';
[26144]357  $add = file_get_contents(SUBSCRIBE_TO_PATH.'template/form_inside.tpl');
358  $content = str_replace($search, $search.$add, $content);
[26139]359
[12708]360  return $content;
361}
362
[12561]363
[12600]364/**
[16105]365 * delete subscriptions to deleted images or categories
366 */
367function stc_delete_elements($ids)
368{
369  $query = '
370DELETE FROM '.SUBSCRIBE_TO_TABLE.'
[26139]371  WHERE
[16106]372    element_id IN ('.implode(',', $ids).')
373    AND type = "image"
[16105]374';
375  pwg_query($query);
376}
[16106]377function stc_delete_categories($ids)
378{
379  $query = '
380DELETE FROM '.SUBSCRIBE_TO_TABLE.'
[26139]381  WHERE
[16106]382    element_id IN ('.implode(',', $ids).')
383    AND (type = "album" OR type = "album-images")
384';
385  pwg_query($query);
386}
[16105]387
388
389/**
[12600]390 * add link to management page for registered users
391 */
[12607]392function stc_profile_link()
[12600]393{
[12609]394  global $template, $user;
[26139]395
[12609]396  if (!empty($user['email']))
397  {
[15641]398    $template->assign('MANAGE_LINK', make_stc_url('manage', $user['email']) );
[12609]399    $template->set_prefilter('profile_content', 'stc_profile_link_prefilter');
400  }
[12600]401}
[12607]402function stc_profile_link_prefilter($content, &$smarty)
[12600]403{
[12607]404  $search = '<p class="bottomButtons">';
[16377]405  $replace = '<p style="font-size:1.1em;text-decoration:underline;"><a href="{$MANAGE_LINK}" rel="nofollow">{\'Manage my subscriptions\'|@translate}</a></p>';
[26139]406
[15641]407  return str_replace($search, $replace.$search, $content);
[12600]408}
Note: See TracBrowser for help on using the repository browser.