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

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

fix major issue with prefilters, works better with Simple and Stripped themes

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