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

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

update for 2.6

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