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

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

correct regex, parse on notification email and admin page

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