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
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      unset($_POST['stc_mode']);
82    }
83  }
84}
85
86
87/**
88 * send notifications on comment validation
89 * @param: array|int comment_id
90 * @param: string type (image|category)
91 */
92function stc_comment_validation($comm_ids, $type='image')
93{
94  if (!is_array($comm_ids))
95  {
96    $comm_ids = array($comm_ids);
97  }
98
99  if ($type == 'image')
100  {
101    $query = '
102SELECT
103    id,
104    image_id,
105    author,
106    content
107  FROM '.COMMENTS_TABLE.'
108  WHERE id IN('.implode(',', $comm_ids).')
109;';
110  }
111  else if ($type == 'album' && defined('COA_TABLE'))
112  {
113    $query = '
114SELECT
115    id,
116    category_id,
117    author,
118    content
119  FROM '.COA_TABLE.'
120  WHERE id IN('.implode(',', $comm_ids).')
121;';
122  }
123
124  if (isset($query))
125  {
126    $comms = query2array($query);
127    foreach ($comms as $comm)
128    {
129      send_comment_to_subscribers($comm);
130    }
131  }
132}
133
134
135/**
136 * add field and link on picture page
137 */
138function stc_on_picture()
139{
140  global $template, $picture, $page, $user, $conf;
141
142  // standalone subscription
143  if (isset($_POST['stc_submit']))
144  {
145    switch ($_POST['stc_mode'])
146    {
147      case 'all-images':
148        subscribe_to_comments(@$_POST['stc_mail'], 'all-images');
149        break;
150      case 'album-images':
151        if (!empty($page['category']['id']))
152        {
153          subscribe_to_comments(@$_POST['stc_mail'], 'album-images', $page['category']['id']);
154        }
155        break;
156      case 'image':
157        subscribe_to_comments(@$_POST['stc_mail'], 'image', $picture['current']['id']);
158        break;
159    }
160    unset($_POST['stc_mode']);
161  }
162  else if (isset($_GET['stc_unsubscribe']))
163  {
164    if (un_subscribe_to_comments(null, $_GET['stc_unsubscribe']))
165    {
166      $page['infos'][] = l10n('Successfully unsubscribed your email address from receiving notifications.');
167    }
168  }
169
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
182  // if registered user with mail we check if already subscribed
183  if (!is_a_guest() and !empty($user['email']))
184  {
185    $subscribed = false;
186
187    $base_query = '
188SELECT id
189  FROM '.SUBSCRIBE_TO_TABLE.'
190  WHERE
191    email = "'.$user['email'].'"
192    AND validated = "true"
193';
194
195    // registered to all pictures
196    if (!$subscribed)
197    {
198      $result = pwg_query($base_query . 'AND type = "all-images";');
199
200      if (pwg_db_num_rows($result))
201      {
202        list($stc_id) = pwg_db_fetch_row($result);
203        $subscribed = 'all-images';
204      }
205    }
206
207    // registered to pictures in this album
208    if (!$subscribed and !empty($page['category']['id']))
209    {
210      $result = pwg_query($base_query . 'AND type = "album-images" AND element_id = '.$page['category']['id'].';');
211
212      if (pwg_db_num_rows($result))
213      {
214        list($stc_id) = pwg_db_fetch_row($result);
215        $subscribed = 'album-images';
216      }
217    }
218
219    // registered to this picture
220    if (!$subscribed)
221    {
222      $result = pwg_query($base_query . 'AND type = "image" AND element_id = '.$picture['current']['id'].';');
223
224      if (pwg_db_num_rows($result))
225      {
226        list($stc_id) = pwg_db_fetch_row($result);
227        $subscribed = 'image';
228      }
229    }
230
231    if ($subscribed)
232    {
233      $tpl_vars['SUBSCRIBED'] = $subscribed;
234      $tpl_vars['U_UNSUB'] = add_url_params($picture['current']['url'], array('stc_unsubscribe'=>$stc_id));
235    }
236  }
237
238  $template->assign(array(
239    'SUBSCRIBE_TO_PATH' => SUBSCRIBE_TO_PATH,
240    'STC' => $tpl_vars,
241    ));
242
243  $template->set_prefilter('picture', 'stc_main_prefilter');
244}
245
246
247/**
248 * add field and link on album page
249 */
250function stc_on_album()
251{
252  global $page, $template, $user, $conf;
253
254  // standalone subscription
255  if (isset($_POST['stc_submit']))
256  {
257    switch ($_POST['stc_mode'])
258    {
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;
265    }
266    unset($_POST['stc_mode']);
267  }
268  else if (isset($_GET['stc_unsubscribe']))
269  {
270    if (un_subscribe_to_comments(null, $_GET['stc_unsubscribe']))
271    {
272      $page['infos'][] = l10n('Successfully unsubscribed your email address from receiving notifications.');
273    }
274  }
275
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
287  // if registered user we check if already subscribed
288  if (!is_a_guest() and !empty($user['email']))
289  {
290    $subscribed = false;
291
292    $base_query = '
293SELECT id
294  FROM '.SUBSCRIBE_TO_TABLE.'
295  WHERE
296    email = "'.$user['email'].'"
297    AND validated = "true"
298';
299
300    // registered to all albums
301    if (!$subscribed)
302    {
303      $result = pwg_query($base_query . 'AND type = "all-albums"');
304
305      if (pwg_db_num_rows($result))
306      {
307        list($stc_id) = pwg_db_fetch_row($result);
308        $subscribed = 'all-albums';
309      }
310    }
311
312    // registered to this album
313    if (!$subscribed)
314    {
315      $result = pwg_query($base_query . 'AND type = "album" AND element_id = '.$page['category']['id'].';');
316
317      if (pwg_db_num_rows($result))
318      {
319        list($stc_id) = pwg_db_fetch_row($result);
320        $subscribed = 'album';
321      }
322    }
323
324    if ($subscribed)
325    {
326      $element_url = make_index_url(array(
327        'section' => 'categories',
328        'category' => $page['category'],
329        ));
330
331      $tpl_vars['SUBSCRIBED'] = $subscribed;
332      $tpl_vars['U_UNSUB'] = add_url_params($element_url, array('stc_unsubscribe'=>$stc_id));
333    }
334  }
335
336  $template->assign(array(
337    'STC' => $tpl_vars,
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)
349{
350  ## subscribe at any moment ##
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);
354
355  ## subscribe while add a comment ##
356  $search = '{$comment_add.CONTENT}</textarea></p>';
357  $add = file_get_contents(SUBSCRIBE_TO_PATH.'template/form_inside.tpl');
358  $content = str_replace($search, $search.$add, $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.