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

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

too many changes

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        $template->assign(array(
187          'SUBSCRIBED_ALL_IMAGES' => true,
188          'MANAGE_LINK' => make_stc_url('manage', $user['email']),
189          ));
190        $subscribed = true;
191      }
192    }
193
194    // registered to pictures in this album
195    if ( !$subscribed and !empty($page['category']['id']) )
196    {
197      $query = '
198SELECT id
199  FROM '.SUBSCRIBE_TO_TABLE.'
200  WHERE
201    email = "'.$user['email'].'"
202    AND element_id = '.$page['category']['id'].'
203    AND type = "album-images"
204    AND validated = "true"
205;';
206      $result = pwg_query($query);
207     
208      if (pwg_db_num_rows($result))
209      {
210        list($stc_id) = pwg_db_fetch_row($result);
211        $template->assign(array(
212          'SUBSCRIBED_ALBUM_IMAGES' => true,
213          'UNSUB_LINK' => add_url_params($picture['current']['url'], array('stc_unsubscribe'=>$stc_id)),
214          ));
215        $subscribed = true;
216      }
217    }
218   
219    // registered to this picture
220    if (!$subscribed)
221    {
222      $query = '
223SELECT id
224  FROM '.SUBSCRIBE_TO_TABLE.'
225  WHERE
226    email = "'.$user['email'].'"
227    AND element_id = '.$picture['current']['id'].'
228    AND type = "image"
229    AND validated = "true"
230;';
231      $result = pwg_query($query);
232     
233      if (pwg_db_num_rows($result))
234      {
235        list($stc_id) = pwg_db_fetch_row($result);
236        $template->assign(array(
237          'SUBSCRIBED_IMAGE' => true,
238          'UNSUB_LINK' => add_url_params($picture['current']['url'], array('stc_unsubscribe'=>$stc_id)),
239          ));
240        $subscribed = true;
241      }
242    }
243  }
244  else
245  {
246    $template->assign('STC_ASK_MAIL', true);
247  }
248 
249  $template->assign(array(
250    'STC_ON_PICTURE' => true,
251    'STC_ALLOW_ALBUM_IMAGES' => !empty($page['category']['id']),
252    'STC_ALLOW_GLOBAL' => $conf['Subscribe_to_Comments']['allow_global_subscriptions'] || is_admin(),
253    'SUBSCRIBE_TO_PATH' => SUBSCRIBE_TO_PATH,
254    ));
255 
256  $template->set_prefilter('picture', 'stc_main_prefilter');
257}
258
259
260/**
261 * add field and link on album page
262 */
263function stc_on_album()
264{
265  global $page, $template, $pwg_loaded_plugins, $user, $conf;
266 
267  if (
268      script_basename() != 'index' or !isset($page['section']) or
269      !isset($pwg_loaded_plugins['Comments_on_Albums']) 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    $subscribed = false;
301   
302    // registered to all albums
303    if (!$subscribed)
304    {
305      $query = '
306SELECT id
307  FROM '.SUBSCRIBE_TO_TABLE.'
308  WHERE
309    email = "'.$user['email'].'"
310    AND type = "all-albums"
311    AND validated = "true"
312;';
313      $result = pwg_query($query);
314     
315      if (pwg_db_num_rows($result))
316      {
317        $template->assign(array(
318          'SUBSCRIBED_ALL_ALBUMS' => true,
319          'MANAGE_LINK' => make_stc_url('manage', $user['email']),
320          ));
321        $subscribed = true;
322      }
323    }
324   
325    // registered to this album
326    if (!$subscribed)
327    {
328      $query = '
329SELECT id
330  FROM '.SUBSCRIBE_TO_TABLE.'
331  WHERE
332    email = "'.$user['email'].'"
333    AND element_id = '.$page['category']['id'].'
334    AND type = "album"
335    AND validated = "true"
336;';
337      $result = pwg_query($query);
338     
339      if (pwg_db_num_rows($result))
340      {
341        list($stc_id) = pwg_db_fetch_row($result);
342        $element_url = make_index_url(array(
343          'section' => 'categories',
344          'category' => $page['category'],
345          ));
346       
347        $template->assign(array(
348          'SUBSCRIBED_ALBUM' => true,
349          'UNSUB_LINK' => add_url_params($element_url, array('stc_unsubscribe'=>$stc_id)),
350          ));
351        $subscribed = true;
352      }
353    }
354  }
355  else
356  {
357    $template->assign('STC_ASK_MAIL', true);
358  }
359 
360  $template->assign(array(
361    'STC_ON_ALBUM' => true,
362    'STC_ALLOW_GLOBAL' => $conf['Subscribe_to_Comments']['allow_global_subscriptions'] || is_admin(),
363    'SUBSCRIBE_TO_PATH' => SUBSCRIBE_TO_PATH,
364    ));
365
366  $template->set_prefilter('comments_on_albums', 'stc_main_prefilter');
367}
368
369
370/**
371 * main prefilter
372 */
373function stc_main_prefilter($content, &$smarty)
374{
375  ## subscribe at any moment ##
376  $search = '{if isset($comments)}';
377  $replace = file_get_contents(SUBSCRIBE_TO_PATH.'template/form_standalone.tpl');
378  $content = str_replace($search, $replace.$search, $content);
379 
380  ## subscribe while add a comment ##
381  $search = '{$comment_add.CONTENT}</textarea></p>';
382  $replace = file_get_contents(SUBSCRIBE_TO_PATH.'template/form_comment.tpl');
383  $content = str_replace($search, $search.$replace, $content);
384 
385  return $content;
386}
387
388
389/**
390 * delete subscriptions to deleted images or categories
391 */
392function stc_delete_elements($ids)
393{
394  $query = '
395DELETE FROM '.SUBSCRIBE_TO_TABLE.'
396  WHERE
397    element_id IN ('.implode(',', $ids).')
398    AND type = "image"
399';
400  pwg_query($query);
401}
402function stc_delete_categories($ids)
403{
404  $query = '
405DELETE FROM '.SUBSCRIBE_TO_TABLE.'
406  WHERE
407    element_id IN ('.implode(',', $ids).')
408    AND (type = "album" OR type = "album-images")
409';
410  pwg_query($query);
411}
412
413
414/**
415 * add link to management page for registered users
416 */
417function stc_profile_link()
418{
419  global $template, $user;
420 
421  if (!empty($user['email']))
422  {
423    $template->assign('MANAGE_LINK', make_stc_url('manage', $user['email']) );
424    $template->set_prefilter('profile_content', 'stc_profile_link_prefilter');
425  }
426}
427function stc_profile_link_prefilter($content, &$smarty)
428{
429  $search = '<p class="bottomButtons">';
430  $replace = '<p style="font-size:1.1em;text-decoration:underline;"><a href="{$MANAGE_LINK}" rel="nofollow">{\'Manage my subscriptions\'|@translate}</a></p>';
431 
432  return str_replace($search, $replace.$search, $content);
433}
434
435?>
Note: See TracBrowser for help on using the repository browser.