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

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

display thumbnails on mails and subscriptions list
fix misplaced field on comment form
move management link to profile 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  // if registered user we check if already subscribed
178  $subscribed = false;
179  if (!is_a_guest())
180  {
181    $query = '
182SELECT id
183  FROM '.SUBSCRIBE_TO_TABLE.'
184  WHERE
185    email = "'.$user['email'].'"
186    AND image_id = '.$picture['current']['id'].'
187    AND validated = "true"
188;';
189    if (pwg_db_num_rows(pwg_query($query)))
190    {
191      $subscribed = true;
192    }
193  }
194 
195  ## subscribe at any moment ##
196  $search[1] = '{if isset($comment_add)}';
197 
198  $replace[1] = $search[1].'
199<form method="post" action="{$comment_add.F_ACTION}" class="filter" id="stc_standalone">
200  <fieldset>';
201 
202  if ($subscribed)
203  {
204    $replace[1].= '
205    {\'You are currently subscribed to comments of this picture.\'|@translate}
206    <a href="'.add_url_params($picture['current']['url'], array('stc_unsubscribe'=>'1')).'">{\'Unsubscribe\'|@translate}';
207  }
208  else
209  {
210    $replace[1].= '
211    <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>';
212   
213    // form for guests
214    if (is_a_guest())
215    {
216      $replace[1].= '
217      <label style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail_stdl"></label>
218      <label style="display:none;"><input type="submit" id="stc_submit" value="{\'Submit\'|@translate}"></label>
219    {footer_script require="jquery"}{literal}
220    jQuery(document).ready(function() {
221      $("a#stc_check_stdl").click(function() {
222        $("input[name=stc_check_stdl]").prop("checked", true);
223        $("#stc_standalone label").toggle();
224        return false;
225      });
226    });
227    {/literal}{/footer_script}';
228    }
229    // simple link for registered users
230    else
231    {
232      $replace[1].= '
233    {footer_script require="jquery"}{literal}
234    jQuery(document).ready(function() {
235      $("a#stc_check_stdl").click(function() {
236        $("input[name=stc_check_stdl]").prop("checked", true);
237        $(this).parents("form#stc_standalone").submit();
238        return false;
239      });
240    });
241    {/literal}{/footer_script}';
242    }
243  }
244     
245  $replace[1].= '
246  </fieldset>
247</form>';
248
249
250  ## subscribe while add a comment ##
251  $search[0] = '<input type="submit" value="{\'Submit\'|@translate}">';
252  $replace[0] = null;
253 
254  if (!$subscribed)
255  {
256    $replace[0].= '
257<label>{\'Subscribe to new comments\'|@translate} <input type="checkbox" name="stc_check" value="1"></label>';
258  }
259  if (is_a_guest())
260  {
261    $replace[0].= '
262<label id="stc_mail" style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail"></label>
263{footer_script require="jquery"}{literal}
264jQuery(document).ready(function() {
265  $("input[name=stc_check]").change(function() {
266    if ($(this).is(":checked")) $("#stc_mail").css("display", "");
267    else $("#stc_mail").css("display", "none");
268  });
269});
270{/literal}{/footer_script}';
271  }
272  $replace[0].= $search[0];
273 
274  return str_replace($search, $replace, $template);
275}
276
277
278/**
279 * add field and on album page
280 */
281function stc_on_album()
282{
283  global $page, $template, $pwg_loaded_plugins;
284 
285  $infos = $errors = array();
286 
287  if (
288      script_basename() != 'index' or !isset($page['section']) or
289      !isset($pwg_loaded_plugins['Comments_on_Albums']) or 
290      $page['section'] != 'categories' or !isset($page['category'])
291    )
292  {
293    return;
294  }
295 
296  if (isset($_POST['stc_check_stdl']))
297  {
298    $return = subscribe_to_comments($page['category']['id'], @$_POST['stc_mail_stdl'], 'category');
299    if ($return === 'confirm_mail')
300    {
301      array_push($infos, l10n('Please check your email inbox to confirm your subscription.'));
302    }
303    else if ($return === true)
304    {
305      array_push($infos, l10n('You have been added to the list of subscribers for this album.'));
306    }
307    else
308    {
309      array_push($errors, l10n('Invalid email adress, your are not subscribed to comments.'));
310    }
311  }
312  else if (isset($_GET['stc_unsubscribe']))
313  {
314    if (un_subscribe_to_comments($page['category']['id'], null, 'category'))
315    {
316      array_push($infos, l10n('Successfully unsubscribed your email address from receiving notifications.'));
317    }
318  }
319 
320  // messages management
321  if (!empty($errors))
322  {
323    $errors_bak = $template->get_template_vars('errors');
324    if (empty($errors_bak)) $errors_bak = array();
325    $template->assign('errors', array_merge($errors_bak, $errors));
326    $template->set_prefilter('index', 'coa_messages'); // here we use a prefilter existing in COA
327  }
328  if (!empty($infos))
329  {
330    $infos_bak = $template->get_template_vars('infos');
331    if (empty($infos_bak)) $infos_bak = array();
332    $template->assign('infos', array_merge($infos_bak, $infos));
333    $template->set_prefilter('index', 'coa_messages');
334  }
335 
336  $template->set_prefilter('comments_on_albums', 'stc_on_album_prefilter');
337}
338
339function stc_on_album_prefilter($template, &$smarty)
340{
341  global $user, $page;
342 
343  // if registered user we check if already subscribed
344  $subscribed = false;
345  if (!is_a_guest())
346  {
347    $query = '
348SELECT id
349  FROM '.SUBSCRIBE_TO_TABLE.'
350  WHERE
351    email = "'.$user['email'].'"
352    AND category_id = '.$page['category']['id'].'
353    AND validated = "true"
354;';
355    if (pwg_db_num_rows(pwg_query($query)))
356    {
357      $subscribed = true;
358    }
359  }
360 
361  ## subscribe at any moment ##
362  $search[1] = '{if isset($comment_add)}';
363 
364  $replace[1] = $search[1].'
365<form method="post" action="{$comment_add.F_ACTION}" class="filter" id="stc_standalone">
366  <fieldset>';
367 
368  // if registered user we check if already subscribed
369  if ($subscribed)
370  {
371    $url_params['section'] = 'categories';
372    $url_params['category'] = $page['category'];
373   
374    $element_url = make_index_url($url_params);
375 
376    $replace[1].= '
377    {\'You are currently subscribed to comments of this album.\'|@translate}
378    <a href="'.add_url_params($element_url, array('stc_unsubscribe'=>'1')).'">{\'Unsubscribe\'|@translate}';
379  }
380  else
381  {
382    $replace[1].= '
383    <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>';
384   
385    // form for guests
386    if (is_a_guest())
387    {
388      $replace[1].= '
389      <label style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail_stdl"></label>
390      <label style="display:none;"><input type="submit" id="stc_submit" value="{\'Submit\'|@translate}"></label>
391    {footer_script require="jquery"}{literal}
392    jQuery(document).ready(function() {
393      $("a#stc_check_stdl").click(function() {
394        $("input[name=stc_check_stdl]").prop("checked", true);
395        $("#stc_standalone label").toggle();
396        return false;
397      });
398    });
399    {/literal}{/footer_script}';
400    }
401    // simple link for registered users
402    else
403    {
404      $replace[1].= '
405    {footer_script require="jquery"}{literal}
406    jQuery(document).ready(function() {
407      $("a#stc_check_stdl").click(function() {
408        $("input[name=stc_check_stdl]").prop("checked", true);
409        $(this).parents("form#stc_standalone").submit();
410        return false;
411      });
412    });
413    {/literal}{/footer_script}';
414    }
415  }
416     
417  $replace[1].= '
418  </fieldset>
419</form>';
420
421
422  ## subscribe while add a comment ##
423  $search[0] = '<input type="submit" value="{\'Submit\'|@translate}">';
424  $replace[0] = null;
425 
426  if (!$subscribed)
427  {
428    $replace[0].= '
429<label>{\'Subscribe to new comments\'|@translate} <input type="checkbox" name="stc_check" value="1"></label>';
430  }
431  if (is_a_guest())
432  {
433    $replace[0].= '
434<label id="stc_mail" style="display:none;">{\'Email address\'|@translate} <input type="text" name="stc_mail"></label>
435{footer_script require="jquery"}{literal}
436jQuery(document).ready(function() {
437  $("input[name=stc_check]").change(function() {
438    if ($(this).is(":checked")) $("#stc_mail").css("display", "");
439    else $("#stc_mail").css("display", "none");
440  });
441});
442{/literal}{/footer_script}';
443  }
444  $replace[0].= $search[0];
445
446  return str_replace($search, $replace, $template);
447}
448
449
450/**
451 * add link to management page for registered users
452 */
453function stc_profile_link()
454{
455  global $template;
456 
457  $template->set_prefilter('profile_content', 'stc_profile_link_prefilter'); 
458}
459
460function stc_profile_link_prefilter($content, &$smarty)
461{
462  global $user;
463 
464  $search = '<p class="bottomButtons">';
465  $replace = '<a href="'.make_stc_url('manage', $user['email']).'" title="{\'Manage my subscriptions to comments\'|@translate}" rel="nofollow">{\'Manage my subscriptions to comments\'|@translate}</a><br>';
466 
467  return str_replace($search, $search.$replace, $content);
468}
469?>
Note: See TracBrowser for help on using the repository browser.