source: extensions/reply_to/reply_to.inc.php @ 12389

Last change on this file since 12389 was 12389, checked in by mistic100, 13 years ago

activate replyTo button on comments.php

File size: 7.2 KB
Line 
1<?php 
2if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
3
4define('REPLYTO_REGEX', '#\[reply=([0-9]+)\]([^\[\]]*)\[/reply\]#i');
5
6/**
7 * Add anchors and reply button to comments blocks
8 */
9function replyto_add_link()
10{
11  global $pwg_loaded_plugins, $template, $page, $conf, $lang;
12  $template->assign('REPLYTO_PATH', REPLYTO_PATH);
13 
14  // comment form has different id
15  if (
16    (isset($_GET['action']) AND $_GET['action'] == 'edit_comment') OR 
17    (isset($page['body_id']) AND $page['body_id'] == 'theCommentsPage')
18    ) 
19  {
20    $template->assign('replyto_form_name', 'editComment');
21  } 
22  else 
23  {
24    $template->assign('replyto_form_name', 'addComment');
25  }
26 
27  // must check our location
28  if (script_basename() == 'comments') /* COMMENTS page */
29  {
30    if ( !is_a_guest() OR $conf['comments_forall'] )
31    {
32      $template->set_prefilter('comments', 'replyto_add_link_comments_prefilter');
33    }
34  }
35  else if (script_basename() == 'picture') /* PICTURE page */
36  {
37    add_event_handler('user_comment_insertion', 'replyto_parse_picture_mail');
38   
39    if ( !is_a_guest() OR $conf['comments_forall'] )
40    {
41      $template->set_prefilter('picture', 'replyto_add_link_prefilter');
42    }
43  } 
44  else if /* ALBUM page */
45    (
46      script_basename() == 'index' AND isset($page['section']) AND
47      isset($pwg_loaded_plugins['Comments_on_Albums']) AND 
48      $page['section'] == 'categories' AND isset($page['category'])
49    )
50  {
51    add_event_handler('user_comment_insertion', 'replyto_parse_album_mail');
52   
53    if ( !is_a_guest() OR $conf['comments_forall'] )
54    {
55      $template->set_prefilter('comments_on_albums', 'replyto_add_link_prefilter');
56    }
57  }
58 
59  /* we come from comments.php page */
60  if ( !empty($_GET['reply_to']) and !empty($_GET['reply_to_a']) )
61  {
62    $lang['Comment'].= '
63    <script type="text/javascript">
64      $(document).ready(function(){
65        jQuery("#'.$template->get_template_vars('replyto_form_name').' textarea").insertAtCaret("[reply='.$_GET['reply_to'].']'.$_GET['reply_to_a'].'[/reply] ");
66      });
67    </script>';
68  }
69}
70
71/**
72 * links to commentform on comments.php page
73 */
74function replyto_add_link_comments_prefilter($content, &$smarty)
75{
76  global $template;
77  $comments = $template->get_template_vars('comments');
78 
79  foreach ($comments as $tpl_var)
80  {
81    $replyto_links[ $tpl_var['ID'] ] = get_absolute_root_url().$tpl_var['U_PICTURE'].'&reply_to='.$tpl_var['ID'].'&reply_to_a='.$tpl_var['AUTHOR'].'#commentform';
82  }
83 
84  $template->assign('replyto_links', $replyto_links);
85 
86    // style
87  $search[0] = '<ul class="thumbnailCategories">';
88  $replace[0] = '
89{html_head}
90<style type="text/css">
91  .replyTo {ldelim}
92    display:inline-block;
93    width:16px;
94    height:16px;
95    background:url({$REPLYTO_PATH}reply.png) center top no-repeat;
96  }
97  .replyTo:hover {ldelim}
98    background-position:center -16px;
99  }
100</style>
101{/html_head}'
102.$search[0];
103
104  // button
105  $search[1] = '<span class="author">';
106  $replace[1] = '
107{if not isset($comment.IN_EDIT)}
108<div class="actions" style="float:right;">
109  <a href="{$replyto_links[$comment.ID]}" title="{\'reply to this comment\'|@translate}" class="replyTo">&nbsp;</a>
110</div>
111{/if}'
112.$search[1];
113 
114  return str_replace($search, $replace, $content);
115}
116
117/**
118 * links to commentform on picture.php (and index.php) page(s)
119 */
120function replyto_add_link_prefilter($content, &$smarty)
121{
122  // script & style
123  $search[0] = '<ul class="thumbnailCategories">';
124  $replace[0] = '
125{combine_script id=\'insertAtCaret\' require=\'jquery\' path=$REPLYTO_PATH|@cat:\'insertAtCaret.js\'}
126
127{footer_script require=\'insertAtCaret\'}
128function replyTo(commentID, author) {ldelim}
129  jQuery("#{$replyto_form_name} textarea").insertAtCaret("[reply=" + commentID + "]" + author + "[/reply] ");
130}
131{/footer_script}
132
133{html_head}
134<style type="text/css">
135  .replyTo {ldelim}
136    display:inline-block;
137    width:16px;
138    height:16px;
139    background:url({$REPLYTO_PATH}reply.png) center top no-repeat;
140  }
141  .replyTo:hover {ldelim}
142    background-position:center -16px;
143  }
144</style>
145{/html_head}'
146.$search[0];
147
148  // button
149  $search[1] = '<span class="author">';
150  $replace[1] = '
151{if not isset($comment.IN_EDIT)}
152<div class="actions" style="float:right;">
153  <a href="#commentform" title="{\'reply to this comment\'|@translate}" class="replyTo" onclick="replyTo(\'{$comment.ID}\', \'{$comment.AUTHOR}\');">&nbsp;</a>
154</div>
155{/if}'
156.$search[1];
157 
158  // anchor
159  $search[2] = '<div class="thumbnailCategory';
160  $replace[2] = '
161<a name="comment-{$comment.ID}"></a>'
162.$search[2];
163
164  $search[3] = '<legend>{\'Add a comment\'|@translate}</legend>';
165  $replace[3] = $search[3].'
166<a name="commentform"></a>';
167
168  return str_replace($search, $replace, $content);
169}
170
171
172/**
173 * Replace BBcode tag by a link with absolute url
174 */
175function replyto_parse($comment, $in_album = false)
176{
177  if (preg_match(REPLYTO_REGEX, $comment, $matches))
178  { 
179    /* try to parse a ReplyTo tag link for picture page */
180    if (!$in_album)
181    {
182      // picture informations
183      $query = '
184SELECT
185  img.id,
186  img.file,
187  cat.category_id
188FROM ' . IMAGES_TABLE . ' AS img
189INNER JOIN ' . IMAGE_CATEGORY_TABLE . ' AS cat
190  ON cat.image_id = img.id
191INNER JOIN ' . COMMENTS_TABLE . ' AS com
192  ON com.image_id = img.id
193WHERE com.id = ' . $matches[1] . '
194;';
195      $result = pwg_query($query);
196     
197      // make sure the target comment exists
198      if (pwg_db_num_rows($result))
199      {
200        $image = pwg_db_fetch_assoc($result);
201
202        // retrieving category informations
203        $query = '
204SELECT
205  id,
206  name,
207  permalink,
208  uppercats
209FROM ' . CATEGORIES_TABLE . '
210WHERE id = ' . $image['category_id'] . '
211;';
212        $image['cat'] = pwg_db_fetch_assoc(pwg_query($query));
213
214        // link to the full size picture
215        $image['url'] = make_picture_url(array(
216          'category' => $image['cat'],
217          'image_id' => $image['id'],
218          'image_file' => $image['file'],
219        ));               
220       
221        $replace = '@ <a href="'.get_absolute_root_url().$image['url'].'#comment-$1">$2</a> :';
222      }
223      else
224      {
225        $replace = '';
226      }
227    } 
228    /* try to parse a ReplyTo tag link for an album */
229    else if ( $in_album == 'album')
230    {   
231      // retrieving category informations
232      $query = '
233SELECT
234  cat.id,
235  cat.name,
236  cat.permalink,
237  cat.uppercats
238FROM ' . COA_TABLE . ' AS com
239INNER JOIN ' . CATEGORIES_TABLE . ' AS cat
240  ON cat.id = com.category_id
241WHERE com.id = ' . $matches[1] . '
242;';
243      $result = pwg_query($query);
244     
245      // make sure the target comment exists
246      if (pwg_db_num_rows($result))
247      {
248        $category = pwg_db_fetch_assoc($result);
249
250        // link to the album
251        $category['url'] = make_index_url(array(
252          'category' => $category,
253        ));               
254     
255        $replace = '@ <a href="'.get_absolute_root_url().$category['url'].'#comment-$1">$2</a> :';
256      }
257      else
258      {
259        $replace = '';
260      }
261    }
262 
263    return preg_replace(REPLYTO_REGEX, $replace, $comment);
264  }
265  else
266  {
267    return $comment;
268  }
269}
270
271/**
272 * Replace BBcode tag by a link in notification mails
273 */
274function replyto_parse_picture_mail($comment)
275{
276  $comment['content'] = replyto_parse($comment['content']);
277  return $comment;
278}
279
280function replyto_parse_album_mail($comment)
281{
282  $comment['content'] = replyto_parse($comment['content'], 'album');
283  return $comment;
284}
285
286?>
Note: See TracBrowser for help on using the repository browser.