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

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

error if comment list is empty on comments.php

File size: 7.3 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  if (!count($comments)) return $content;
80 
81  foreach ($comments as $tpl_var)
82  {
83    $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';
84  }
85 
86  $template->assign('replyto_links', $replyto_links);
87 
88    // style
89  $search[0] = '<ul class="thumbnailCategories">';
90  $replace[0] = '
91{html_head}
92<style type="text/css">
93  .replyTo {ldelim}
94    display:inline-block;
95    width:16px;
96    height:16px;
97    background:url({$REPLYTO_PATH}reply.png) center top no-repeat;
98  }
99  .replyTo:hover {ldelim}
100    background-position:center -16px;
101  }
102</style>
103{/html_head}'
104.$search[0];
105
106  // button
107  $search[1] = '<span class="author">';
108  $replace[1] = '
109{if not isset($comment.IN_EDIT)}
110<div class="actions" style="float:right;">
111  <a href="{$replyto_links[$comment.ID]}" title="{\'reply to this comment\'|@translate}" class="replyTo">&nbsp;</a>
112</div>
113{/if}'
114.$search[1];
115 
116  return str_replace($search, $replace, $content);
117}
118
119/**
120 * links to commentform on picture.php (and index.php) page(s)
121 */
122function replyto_add_link_prefilter($content, &$smarty)
123{
124  // script & style
125  $search[0] = '<ul class="thumbnailCategories">';
126  $replace[0] = '
127{combine_script id=\'insertAtCaret\' require=\'jquery\' path=$REPLYTO_PATH|@cat:\'insertAtCaret.js\'}
128
129{footer_script require=\'insertAtCaret\'}
130function replyTo(commentID, author) {ldelim}
131  jQuery("#{$replyto_form_name} textarea").insertAtCaret("[reply=" + commentID + "]" + author + "[/reply] ");
132}
133{/footer_script}
134
135{html_head}
136<style type="text/css">
137  .replyTo {ldelim}
138    display:inline-block;
139    width:16px;
140    height:16px;
141    background:url({$REPLYTO_PATH}reply.png) center top no-repeat;
142  }
143  .replyTo:hover {ldelim}
144    background-position:center -16px;
145  }
146</style>
147{/html_head}'
148.$search[0];
149
150  // button
151  $search[1] = '<span class="author">';
152  $replace[1] = '
153{if not isset($comment.IN_EDIT)}
154<div class="actions" style="float:right;">
155  <a href="#commentform" title="{\'reply to this comment\'|@translate}" class="replyTo" onclick="replyTo(\'{$comment.ID}\', \'{$comment.AUTHOR}\');">&nbsp;</a>
156</div>
157{/if}'
158.$search[1];
159 
160  // anchor
161  $search[2] = '<div class="thumbnailCategory';
162  $replace[2] = '
163<a name="comment-{$comment.ID}"></a>'
164.$search[2];
165
166  $search[3] = '<legend>{\'Add a comment\'|@translate}</legend>';
167  $replace[3] = $search[3].'
168<a name="commentform"></a>';
169
170  return str_replace($search, $replace, $content);
171}
172
173
174/**
175 * Replace BBcode tag by a link with absolute url
176 */
177function replyto_parse($comment, $in_album = false)
178{
179  if (preg_match(REPLYTO_REGEX, $comment, $matches))
180  { 
181    /* try to parse a ReplyTo tag link for picture page */
182    if (!$in_album)
183    {
184      // picture informations
185      $query = '
186SELECT
187  img.id,
188  img.file,
189  cat.category_id
190FROM ' . IMAGES_TABLE . ' AS img
191INNER JOIN ' . IMAGE_CATEGORY_TABLE . ' AS cat
192  ON cat.image_id = img.id
193INNER JOIN ' . COMMENTS_TABLE . ' AS com
194  ON com.image_id = img.id
195WHERE com.id = ' . $matches[1] . '
196;';
197      $result = pwg_query($query);
198     
199      // make sure the target comment exists
200      if (pwg_db_num_rows($result))
201      {
202        $image = pwg_db_fetch_assoc($result);
203
204        // retrieving category informations
205        $query = '
206SELECT
207  id,
208  name,
209  permalink,
210  uppercats
211FROM ' . CATEGORIES_TABLE . '
212WHERE id = ' . $image['category_id'] . '
213;';
214        $image['cat'] = pwg_db_fetch_assoc(pwg_query($query));
215
216        // link to the full size picture
217        $image['url'] = make_picture_url(array(
218          'category' => $image['cat'],
219          'image_id' => $image['id'],
220          'image_file' => $image['file'],
221        ));               
222       
223        $replace = '@ <a href="'.get_absolute_root_url().$image['url'].'#comment-$1">$2</a> :';
224      }
225      else
226      {
227        $replace = '';
228      }
229    } 
230    /* try to parse a ReplyTo tag link for an album */
231    else if ( $in_album == 'album')
232    {   
233      // retrieving category informations
234      $query = '
235SELECT
236  cat.id,
237  cat.name,
238  cat.permalink,
239  cat.uppercats
240FROM ' . COA_TABLE . ' AS com
241INNER JOIN ' . CATEGORIES_TABLE . ' AS cat
242  ON cat.id = com.category_id
243WHERE com.id = ' . $matches[1] . '
244;';
245      $result = pwg_query($query);
246     
247      // make sure the target comment exists
248      if (pwg_db_num_rows($result))
249      {
250        $category = pwg_db_fetch_assoc($result);
251
252        // link to the album
253        $category['url'] = make_index_url(array(
254          'category' => $category,
255        ));               
256     
257        $replace = '@ <a href="'.get_absolute_root_url().$category['url'].'#comment-$1">$2</a> :';
258      }
259      else
260      {
261        $replace = '';
262      }
263    }
264 
265    return preg_replace(REPLYTO_REGEX, $replace, $comment);
266  }
267  else
268  {
269    return $comment;
270  }
271}
272
273/**
274 * Replace BBcode tag by a link in notification mails
275 */
276function replyto_parse_picture_mail($comment)
277{
278  $comment['content'] = replyto_parse($comment['content']);
279  return $comment;
280}
281
282function replyto_parse_album_mail($comment)
283{
284  $comment['content'] = replyto_parse($comment['content'], 'album');
285  return $comment;
286}
287
288?>
Note: See TracBrowser for help on using the repository browser.