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

Last change on this file since 21441 was 21441, checked in by mistic100, 11 years ago

some code corrections

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