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

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

change parsing method, parse link on PWG Stuffs comments bloc

File size: 5.6 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;
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 + some additional tests
28  // if (script_basename() == 'comments')
29  // {
30    // if ( !is_a_guest() OR $conf['comments_forall'] )
31    // {
32      // $template->set_prefilter('comments', 'replyto_add_link_prefilter');
33    // }
34  // }
35  // else
36  if (script_basename() == 'picture')
37  {
38    add_event_handler('user_comment_insertion', 'replyto_parse_picture_mail');
39   
40    if ( !is_a_guest() OR $conf['comments_forall'] )
41    {
42      $template->set_prefilter('picture', 'replyto_add_link_prefilter');
43    }
44  } 
45  else if 
46    (
47      script_basename() == 'index' AND isset($page['section']) AND
48      isset($pwg_loaded_plugins['Comments_on_Albums']) AND 
49      $page['section'] == 'categories' AND isset($page['category'])
50    )
51  {
52    add_event_handler('user_comment_insertion', 'replyto_parse_album_mail');
53   
54    if ( !is_a_guest() OR $conf['comments_forall'] )
55    {
56      $template->set_prefilter('comments_on_albums', 'replyto_add_link_prefilter');
57    }
58  }
59}
60
61function replyto_add_link_prefilter($content, &$smarty)
62{
63  // script
64  $search[0] = '<ul class="thumbnailCategories">';
65  $replace[0] = '
66{combine_script id=\'insertAtCaret\' require=\'jquery\' path=$REPLYTO_PATH|@cat:\'insertAtCaret.js\'}
67
68{footer_script require=\'insertAtCaret\'}
69function replyTo(commentID, author) {ldelim}
70  jQuery("#{$replyto_form_name} textarea").insertAtCaret("[reply=" + commentID + "]" + author + "[/reply] ");
71}
72{/footer_script}
73
74{html_head}
75<style type="text/css">
76  .replyTo {ldelim}
77    display:inline-block;
78    width:16px;
79    height:16px;
80    background:url({$REPLYTO_PATH}reply.png) center top no-repeat;
81  }
82  .replyTo:hover {ldelim}
83    background-position:center -16px;
84  }
85</style>
86{/html_head}'
87.$search[0];
88
89  // button
90  $search[1] = '<span class="author">';
91  $replace[1] = '
92{if not isset($comment.IN_EDIT)}
93<div class="actions" style="float:right;">
94  <a href="#commentform" title="{\'reply to this comment\'|@translate}" class="replyTo" onclick="replyTo(\'{$comment.ID}\', \'{$comment.AUTHOR}\');">&nbsp;</a>
95</div>
96{/if}'
97.$search[1];
98 
99  // anchor
100  $search[2] = '<div class="thumbnailCategory';
101  $replace[2] = '
102<a name="comment-{$comment.ID}"></a>'
103.$search[2];
104
105  $search[3] = '<legend>{\'Add a comment\'|@translate}</legend>';
106  $replace[3] = $search[3].'
107<a name="commentform"></a>';
108
109  return str_replace($search, $replace, $content);
110}
111
112
113/**
114 * Replace BBcode tag by a link with absolute url
115 */
116function replyto_parse($comment, $in_album = false)
117{
118  if (preg_match(REPLYTO_REGEX, $comment, $matches))
119  { 
120    /* try to parse a ReplyTo tag link for picture page */
121    if (!$in_album)
122    {
123      // picture informations
124      $query = '
125SELECT
126  img.id,
127  img.file,
128  cat.category_id
129FROM ' . IMAGES_TABLE . ' AS img
130INNER JOIN ' . IMAGE_CATEGORY_TABLE . ' AS cat
131  ON cat.image_id = img.id
132INNER JOIN ' . COMMENTS_TABLE . ' AS com
133  ON com.image_id = img.id
134WHERE com.id = ' . $matches[1] . '
135;';
136      $result = pwg_query($query);
137     
138      // make sure the target comment exists
139      if (pwg_db_num_rows($result))
140      {
141        $image = pwg_db_fetch_assoc($result);
142
143        // retrieving category informations
144        $query = '
145SELECT
146  id,
147  name,
148  permalink,
149  uppercats
150FROM ' . CATEGORIES_TABLE . '
151WHERE id = ' . $image['category_id'] . '
152;';
153        $image['cat'] = pwg_db_fetch_assoc(pwg_query($query));
154
155        // link to the full size picture
156        $image['url'] = make_picture_url(array(
157          'category' => $image['cat'],
158          'image_id' => $image['id'],
159          'image_file' => $image['file'],
160        ));               
161       
162        $replace = '@ <a href="'.get_absolute_root_url().$image['url'].'#comment-$1">$2</a> :';
163      }
164      else
165      {
166        $replace = '';
167      }
168    } 
169    /* try to parse a ReplyTo tag link for an album */
170    else
171    {   
172      // retrieving category informations
173      $query = '
174SELECT
175  cat.id,
176  cat.name,
177  cat.permalink,
178  cat.uppercats
179FROM ' . COA_TABLE . ' AS com
180INNER JOIN ' . CATEGORIES_TABLE . ' AS cat
181  ON cat.id = com.category_id
182WHERE com.id = ' . $matches[1] . '
183;';
184      $result = pwg_query($query);
185     
186      // make sure the target comment exists
187      if (pwg_db_num_rows($result))
188      {
189        $category = pwg_db_fetch_assoc($result);
190
191        // link to the album
192        $category['url'] = make_index_url(array(
193          'category' => $category,
194        ));               
195     
196        $replace = '@ <a href="'.get_absolute_root_url().$category['url'].'#comment-$1">$2</a> :';
197      }
198      else
199      {
200        $replace = '';
201      }
202    }
203 
204    return preg_replace(REPLYTO_REGEX, $replace, $comment);
205  }
206  else
207  {
208    return $comment;
209  }
210}
211
212/**
213 * Replace BBcode tag by a link in notification mails
214 */
215function replyto_parse_picture_mail($comment)
216{
217  $comment['content'] = replyto_parse($comment['content']);
218  return $comment;
219}
220
221function replyto_parse_album_mail($comment)
222{
223  $comment['content'] = replyto_parse($comment['content'], 'album');
224  return $comment;
225}
226
227?>
Note: See TracBrowser for help on using the repository browser.