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

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

fix many bugs, and management page

File size: 12.7 KB
Line 
1<?php
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4/**
5 * detect 'subscriptions' section and load page
6 */
7function stc_detect_section() {
8  global $tokens, $page;
9 
10  if ($tokens[0] == 'subscriptions')
11  {
12    $page['section'] = 'subscriptions';
13  }
14}
15
16function stc_load_section() {
17  global $page;
18
19  if (isset($page['section']) and $page['section'] == 'subscriptions')
20  {
21    include(SUBSCRIBE_TO_PATH.'include/subscribtions_page.inc.php');
22  }
23}
24
25
26/**
27 * send notifications and/or add subscriber
28 */
29function stc_comment_insertion($comm)
30{
31  global $page, $template;
32 
33  $infos = $errors = array();
34 
35  if ($comm['action'] == 'validate')
36  {
37    send_comment_to_subscribers($comm);
38  }
39 
40  if ( !empty($_POST['stc_check']) and ( $comm['action'] == 'validate' or $comm['action'] == 'moderate' ) )
41  {
42    if (isset($comm['image_id']))
43    {
44      $return = subscribe_to_comments($comm['image_id'], @$_POST['stc_mail'], 'image');
45    }
46    else if (isset($comm['category_id']))
47    {
48      $return = subscribe_to_comments($comm['category_id'], @$_POST['stc_mail'], 'category');
49     
50    }
51   
52    if (isset($return))
53    {
54      if ($return === 'confirm_mail')
55      {
56        array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
57      }
58      else if ($return === true)
59      {
60        array_push($infos, l10n('You have been added to the list of subscribers for this '.(isset($comm['image_id'])?'picture':'album').'.'));
61      }
62      else
63      {
64        array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
65      }
66     
67       // messages management
68      if (!empty($errors))
69      {
70        $errors_bak = $template->get_template_vars('errors');
71        if (empty($errors_bak)) $errors_bak = array();
72        $template->assign('errors', array_merge($errors_bak, $errors));
73        $template->set_prefilter('index', 'coa_messages'); // here we use a prefilter existing in COA
74      }
75      if (!empty($infos))
76      {
77        $infos_bak = $template->get_template_vars('infos');
78        if (empty($infos_bak)) $infos_bak = array();
79        $template->assign('infos', array_merge($infos_bak, $infos));
80        $template->set_prefilter('index', 'coa_messages');
81      }
82    }
83  }
84}
85
86function stc_comment_validation($comm_ids, $type='image')
87{
88  if (!is_array($comm_ids)) $comm_ids = array($comm_ids);
89 
90  foreach($comm_ids as $comm_id)
91  {
92    if ($type == 'image')
93    {
94      $query = '
95SELECT
96    id,
97    image_id,
98    author,
99    content
100  FROM '.COMMENTS_TABLE.'
101  WHERE id = '.$comm_id.'
102;';
103    }
104    else if ($type == 'category')
105    {
106      $query = '
107SELECT
108    id,
109    category_id,
110    author,
111    content
112  FROM '.COA_TABLE.'
113  WHERE id = '.$comm_id.'
114;';
115    }
116   
117    $comm = pwg_db_fetch_assoc(pwg_query($query));
118    send_comment_to_subscribers($comm);
119  }
120}
121
122
123/**
124 * add field and link on picture page
125 */
126function stc_on_picture()
127{
128  global $template, $picture, $page;
129 
130  $infos = $array = array();
131 
132  if (isset($_POST['stc_check_stdl']))
133  {
134    $return = subscribe_to_comments($picture['current']['id'], @$_POST['stc_mail_stdl'], 'image');
135    if ($return === 'confirm_mail')
136    {
137      array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
138    }
139    else if ($return === true)
140    {
141      array_push($infos, l10n('You have been added to the list of subscribers for this picture.'));
142    }
143    else
144    {
145      array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
146    }
147  }
148  else if (isset($_GET['stc_unsubscribe']))
149  {
150    if (un_subscribe_to_comments($picture['current']['id'], null, 'image'))
151    {
152      array_push($infos, l10n('Successfully unsubscribed your email address from receiving notifications.'));
153    }
154  }
155 
156  // messages management
157  if (!empty($errors))
158  {
159    $errors_bak = $template->get_template_vars('errors');
160    if (empty($errors_bak)) $errors_bak = array();
161    $template->assign('errors', array_merge($errors_bak, $errors));
162  }
163  if (!empty($infos))
164  {
165    $infos_bak = $template->get_template_vars('infos');
166    if (empty($infos_bak)) $infos_bak = array();
167    $template->assign('infos', array_merge($infos_bak, $infos));
168  }
169 
170  $template->set_prefilter('picture', 'stc_on_picture_prefilter');
171}
172
173function stc_on_picture_prefilter($template, &$smarty)
174{
175  global $user, $picture; 
176 
177  ## subscribe at any moment ##
178  $search[1] = '{if isset($comment_add)}';
179 
180  $replace[1] = $search[1].'
181<form method="post" action="{$comment_add.F_ACTION}" class="filter" id="stc_standalone">
182  <fieldset>';
183 
184  // if registered user we check if already subscribed
185  if (!is_a_guest())
186  {
187    $query = '
188SELECT id
189  FROM '.SUBSCRIBE_TO_TABLE.'
190  WHERE
191    email = "'.$user['email'].'"
192    AND image_id = '.$picture['current']['id'].'
193    AND validated = "true"
194;';
195    if (pwg_db_num_rows(pwg_query($query)))
196    {
197      $replace[1].= '
198    {\'You are currently subscribed to comments of this picture.\'|@translate}
199    <a href="'.add_url_params($picture['current']['url'], array('stc_unsubscribe'=>'1')).'">{\'Unsubscribe\'|@translate}';
200      $no_form = true;
201    }
202  }
203 
204  if (!isset($no_form))
205  {
206    $replace[1].= '
207    <label><a href="#" id="stc_check_stdl">{\'Subscribe to new comments\'|@translate}</a> <input type="checkbox" name="stc_check_stdl" value="1" style="display:none;"></label>';
208   
209    // form for guests
210    if (is_a_guest())
211    {
212      $replace[1].= '
213      <label style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail_stdl"></label>
214      <label style="display:none;"><input type="submit" value="{\'Submit\'|@translate}"></label>
215    {footer_script require="jquery"}{literal}
216    jQuery(document).ready(function() {
217      $("a#stc_check_stdl").click(function() {
218        $("input[name=stc_check_stdl]").prop("checked", true);
219        $("#stc_standalone label").toggle();
220        return false;
221      });
222    });
223    {/literal}{/footer_script}';
224    }
225    // simple link for registered users
226    else
227    {
228      $replace[1].= '
229    {footer_script require="jquery"}{literal}
230    jQuery(document).ready(function() {
231      $("a#stc_check_stdl").click(function() {
232        $("input[name=stc_check_stdl]").prop("checked", true);
233        $(this).parents("form#stc_standalone").submit();
234        return false;
235      });
236    });
237    {/literal}{/footer_script}';
238    }
239  }
240     
241  $replace[1].= '
242  </fieldset>
243</form>';
244
245
246  ## subscribe while add a comment ##
247  $search[0] = '<input type="submit" value="{\'Submit\'|@translate}">';
248  $replace[0] = null;
249 
250  if (!isset($no_form))
251  {
252    $replace[0].= '
253<label>{\'Subscribe to new comments\'|@translate} <input type="checkbox" name="stc_check" value="1"></label>';
254  }
255  if (is_a_guest())
256  {
257    $replace[0].= '
258<label id="stc_mail" style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail"></label>
259{footer_script require="jquery"}{literal}
260jQuery(document).ready(function() {
261  $("input[name=stc_check]").change(function() {
262    if ($(this).is(":checked")) $("#stc_mail").css("display", "");
263    else $("#stc_mail").css("display", "none");
264  });
265});
266{/literal}{/footer_script}';
267  }
268  $replace[0].= $search[0];
269 
270  return str_replace($search, $replace, $template);
271}
272
273
274/**
275 * add field and on album page
276 */
277function stc_on_album()
278{
279  global $page, $template, $pwg_loaded_plugins;
280 
281  $infos = $errors = array();
282 
283  if (
284      script_basename() != 'index' or !isset($page['section']) or
285      !isset($pwg_loaded_plugins['Comments_on_Albums']) or 
286      $page['section'] != 'categories' or !isset($page['category'])
287    )
288  {
289    return;
290  }
291 
292  if (isset($_POST['stc_check_stdl']))
293  {
294    $return = subscribe_to_comments($page['category']['id'], @$_POST['stc_mail_stdl'], 'category');
295    if ($return === 'confirm_mail')
296    {
297      array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
298    }
299    else if ($return === true)
300    {
301      array_push($infos, l10n('You have been added to the list of subscribers for this album.'));
302    }
303    else
304    {
305      array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
306    }
307  }
308  else if (isset($_GET['stc_unsubscribe']))
309  {
310    if (un_subscribe_to_comments($page['category']['id'], null, 'category'))
311    {
312      array_push($infos, l10n('Successfully unsubscribed your email address from receiving notifications.'));
313    }
314  }
315 
316  // messages management
317  if (!empty($errors))
318  {
319    $errors_bak = $template->get_template_vars('errors');
320    if (empty($errors_bak)) $errors_bak = array();
321    $template->assign('errors', array_merge($errors_bak, $errors));
322    $template->set_prefilter('index', 'coa_messages'); // here we use a prefilter existing in COA
323  }
324  if (!empty($infos))
325  {
326    $infos_bak = $template->get_template_vars('infos');
327    if (empty($infos_bak)) $infos_bak = array();
328    $template->assign('infos', array_merge($infos_bak, $infos));
329    $template->set_prefilter('index', 'coa_messages');
330  }
331 
332  $template->set_prefilter('comments_on_albums', 'stc_on_album_prefilter');
333}
334
335function stc_on_album_prefilter($template, &$smarty)
336{
337  global $user, $page; 
338 
339  ## subscribe at any moment ##
340  $search[1] = '{if isset($comment_add)}';
341 
342  $replace[1] = $search[1].'
343<form method="post" action="{$comment_add.F_ACTION}" class="filter" id="stc_standalone">
344  <fieldset>';
345 
346  // if registered user we check if already subscribed
347  if (!is_a_guest())
348  {
349    $query = '
350SELECT id
351  FROM '.SUBSCRIBE_TO_TABLE.'
352  WHERE
353    email = "'.$user['email'].'"
354    AND category_id = '.$page['category']['id'].'
355    AND validated = "true"
356;';
357    if (pwg_db_num_rows(pwg_query($query)))
358    {
359      $url_params['section'] = 'categories';
360      $url_params['category'] = $page['category'];
361     
362      $element_url = make_index_url($url_params);
363   
364      $replace[1].= '
365    {\'You are currently subscribed to comments of this album.\'|@translate}
366    <a href="'.add_url_params($element_url, array('stc_unsubscribe'=>'1')).'">{\'Unsubscribe\'|@translate}';
367      $no_form = true;
368    }
369  }
370 
371  if (!isset($no_form))
372  {
373    $replace[1].= '
374    <label><a href="#" id="stc_check_stdl">{\'Subscribe to new comments\'|@translate}</a> <input type="checkbox" name="stc_check_stdl" value="1" style="display:none;"></label>';
375   
376    // form for guests
377    if (is_a_guest())
378    {
379      $replace[1].= '
380      <label style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail_stdl"></label>
381      <label style="display:none;"><input type="submit" value="{\'Submit\'|@translate}"></label>
382    {footer_script require="jquery"}{literal}
383    jQuery(document).ready(function() {
384      $("a#stc_check_stdl").click(function() {
385        $("input[name=stc_check_stdl]").prop("checked", true);
386        $("#stc_standalone label").toggle();
387        return false;
388      });
389    });
390    {/literal}{/footer_script}';
391    }
392    // simple link for registered users
393    else
394    {
395      $replace[1].= '
396    {footer_script require="jquery"}{literal}
397    jQuery(document).ready(function() {
398      $("a#stc_check_stdl").click(function() {
399        $("input[name=stc_check_stdl]").prop("checked", true);
400        $(this).parents("form#stc_standalone").submit();
401        return false;
402      });
403    });
404    {/literal}{/footer_script}';
405    }
406  }
407     
408  $replace[1].= '
409  </fieldset>
410</form>';
411
412
413  ## subscribe while add a comment ##
414  $search[0] = '<input type="submit" value="{\'Submit\'|@translate}">';
415  $replace[0] = null;
416 
417  if (!isset($no_form))
418  {
419    $replace[0].= '
420<label>{\'Subscribe to new comments\'|@translate} <input type="checkbox" name="stc_check" value="1"></label>';
421  }
422  if (is_a_guest())
423  {
424    $replace[0].= '
425<label id="stc_mail" style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail"></label>
426{footer_script require="jquery"}{literal}
427jQuery(document).ready(function() {
428  $("input[name=stc_check]").change(function() {
429    if ($(this).is(":checked")) $("#stc_mail").css("display", "");
430    else $("#stc_mail").css("display", "none");
431  });
432});
433{/literal}{/footer_script}';
434  }
435  $replace[0].= $search[0];
436
437  return str_replace($search, $replace, $template);
438}
439
440
441/**
442 * add link to management page for registered users
443 */
444function stc_menubar_apply($menu_ref_arr)
445{
446  global $template;
447  $menu = &$menu_ref_arr[0];
448 
449  if ( !is_a_guest() and ($block = $menu->get_block('mbIdentification')) != null )
450  {
451    $template->set_prefilter('menubar', 'stc_menubar_apply_prefilter'); 
452  }
453}
454
455function stc_menubar_apply_prefilter($content, &$smarty)
456{
457  global $user;
458 
459  $search = '{if isset($U_REGISTER)}';
460  $replace = '<li><a href="'.make_stc_url('manage', $user['email']).'" title="{\'Manage my subscriptions\'|@translate}" rel="nofollow">{\'Manage my subscriptions\'|@translate}</a></li>';
461  return str_replace($search, $replace.$search, $content);
462}
463?>
Note: See TracBrowser for help on using the repository browser.