source: extensions/Subscribe_to_comments/include/subscribe_to_comments.inc.php @ 15907

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

HUGE update, main features : global subscriptions (all images in an album, all images, all albums), beautyful (!) mails

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