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

Last change on this file since 26677 was 26677, checked in by mistic100, 10 years ago

compatibility with Comments on Albums for 2.6

File size: 7.0 KB
Line 
1<?php 
2defined('REPLYTO_PATH') or 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  /* COMMENTS page */
28  if (script_basename() == 'comments') 
29  {
30    if (!is_a_guest() or $conf['comments_forall'])
31    {
32      $comments = $template->get_template_vars('comments');
33      if (!count($comments))
34      {
35        return;
36      }
37     
38      // generates urls to picture or albums with necessary url params
39      foreach ($comments as $tpl_var)
40      {
41        $replyto_links[ $tpl_var['ID'] ] = add_url_params(
42          $tpl_var['U_PICTURE'],
43          array(
44            'rt' => $tpl_var['ID'],
45            'rta' =>$tpl_var['AUTHOR'],
46            )
47          ).'#commentform';
48      }
49     
50      var_dump($replyto_links);
51     
52      $template->assign('replyto_links', $replyto_links);
53      $template->set_prefilter('comments', 'replyto_add_link_comments_prefilter');
54    }
55  }
56  /* PICTURE page */
57  else if (script_basename() == 'picture') 
58  {
59    add_event_handler('user_comment_insertion', 'replyto_parse_picture_mail');
60   
61    if (!is_a_guest() or $conf['comments_forall'])
62    {
63      $template->set_prefilter('picture', 'replyto_add_link_prefilter');
64    }
65  } 
66  /* ALBUM page */
67  else if (
68      script_basename() == 'index' and isset($page['section']) and
69      isset($pwg_loaded_plugins['Comments_on_Albums']) and 
70      $page['section'] == 'categories' and isset($page['category'])
71    )
72  {
73    add_event_handler('user_comment_insertion', 'replyto_parse_album_mail');
74   
75    if (!is_a_guest() or $conf['comments_forall'])
76    {
77      $template->set_prefilter('comments_on_albums', 'replyto_add_link_prefilter');
78    }
79  }
80 
81
82  /* we come from comments.php page */
83  if (!empty($_GET['rt']) and !empty($_GET['rta']))
84  {
85    $template->func_combine_script(array('id'=>'insertAtCaret', 'path'=>REPLYTO_PATH.'insertAtCaret.js'));
86    $template->block_footer_script(array('require'=>'insertAtCaret'), 'replyTo("'.$_GET['rt'].'", "'.$_GET['rta'].'");');
87  }
88}
89
90/**
91 * reply buttons on comments.php page
92 */
93function replyto_add_link_comments_prefilter($content)
94{ 
95  // style
96  $content.= '
97{html_style}
98.replyTo {
99  display:none;
100  background:url({$ROOT_URL}{$REPLYTO_PATH}reply.png) left top no-repeat;
101  height:16px;
102  margin-left:20px;
103  padding-left:20px;
104}
105li.commentElement:hover .replyTo {
106  display:inline;
107}
108.replyTo:hover {
109  background-position:left -16px;
110}
111{/html_style}';
112
113  // button
114  $search = '<span class="commentDate">{$comment.DATE}</span>';
115  $replace = $search.'<a href="{$replyto_links[$comment.ID]}" class="replyTo">{\'Reply\'|@translate}</a>';
116 
117  return str_replace($search, $replace, $content);
118}
119
120/**
121 * reply buttons on picture.php and index.php pages
122 */
123function replyto_add_link_prefilter($content, &$smarty)
124{
125  // script & style
126  $content.= '
127{combine_script id=\'insertAtCaret\' require=\'jquery\' path=$REPLYTO_PATH|cat:\'insertAtCaret.js\'}
128
129{footer_script require=\'insertAtCaret\'}
130function replyTo(commentID, author) {
131  jQuery("#{$replyto_form_name} textarea").insertAtCaret("[reply=" + commentID + "]" + author + "[/reply] ").focus();
132}
133{/footer_script}
134
135{html_style}
136.replyTo {
137  display:none;
138  background:url({$ROOT_URL}{$REPLYTO_PATH}reply.png) left top no-repeat;
139  height:16px;
140  margin-left:20px;
141  padding-left:20px;
142}
143li.commentElement:hover .replyTo {
144  display:inline;
145}
146.replyTo:hover {
147  background-position:left -16px;
148}
149{/html_style}';
150
151  // button
152  $search[1] = '<span class="commentDate">{$comment.DATE}</span>';
153  $replace[1] = $search[1].'<a href="#commentform" class="replyTo" onclick="replyTo(\'{$comment.ID}\', \'{$comment.AUTHOR}\');">{\'Reply\'|@translate}</a>';
154 
155  // anchors
156  $search[2] = '<li class="commentElement';
157  $replace[2] = '<a name="comment-{$comment.ID}"></a>'.$search[2];
158
159  $search[3] = '<div id="commentAdd">';
160  $replace[3] = $search[3].'<a name="commentform"></a>';
161
162  return str_replace($search, $replace, $content);
163}
164
165
166/**
167 * Replace reply tag by a link with absolute url
168 */
169function replyto_parse($comment, $in_album = false)
170{
171  if (preg_match(REPLYTO_REGEX, $comment, $matches))
172  { 
173    /* try to parse a ReplyTo tag link for picture page */
174    if (!$in_album)
175    {
176      // picture informations
177      $query = '
178SELECT
179    img.id,
180    img.file,
181    cat.category_id
182  FROM ' . IMAGES_TABLE . ' AS img
183    INNER JOIN ' . IMAGE_CATEGORY_TABLE . ' AS cat
184      ON cat.image_id = img.id
185    INNER JOIN ' . COMMENTS_TABLE . ' AS com
186      ON com.image_id = img.id
187  WHERE com.id = ' . $matches[1] . '
188;';
189      $result = pwg_query($query);
190     
191      // make sure the target comment exists
192      if (pwg_db_num_rows($result))
193      {
194        $image = pwg_db_fetch_assoc($result);
195
196        // retrieving category informations
197        $query = '
198SELECT
199    id,
200    name,
201    permalink,
202    uppercats
203  FROM ' . CATEGORIES_TABLE . '
204  WHERE id = ' . $image['category_id'] . '
205;';
206        $image['cat'] = pwg_db_fetch_assoc(pwg_query($query));
207
208        // link to the full size picture
209        $image['url'] = make_picture_url(array(
210          'category' => $image['cat'],
211          'image_id' => $image['id'],
212          'image_file' => $image['file'],
213        ));
214       
215        $replace = '@ <a href="'.$image['url'].'#comment-$1">$2</a> :';
216      }
217      else
218      {
219        $replace = '';
220      }
221    } 
222    /* try to parse a ReplyTo tag link for an album */
223    else if ( $in_album == 'album')
224    {   
225      // retrieving category informations
226      $query = '
227SELECT
228    cat.id,
229    cat.name,
230    cat.permalink,
231    cat.uppercats
232  FROM ' . COA_TABLE . ' AS com
233    INNER JOIN ' . CATEGORIES_TABLE . ' AS cat
234      ON cat.id = com.category_id
235  WHERE com.id = ' . $matches[1] . '
236;';
237      $result = pwg_query($query);
238     
239      // make sure the target comment exists
240      if (pwg_db_num_rows($result))
241      {
242        $category = pwg_db_fetch_assoc($result);
243
244        // link to the album
245        $category['url'] = make_index_url(array(
246          'category' => $category,
247        ));
248     
249        $replace = '@ <a href="'.$category['url'].'#comment-$1">$2</a> :';
250      }
251      else
252      {
253        $replace = '';
254      }
255    }
256 
257    return preg_replace(REPLYTO_REGEX, $replace, $comment);
258  }
259  else
260  {
261    return $comment;
262  }
263}
264
265/**
266 * Replace BBcode tag by a link in notification mails
267 */
268function replyto_parse_picture_mail($comment)
269{
270  $comment['content'] = replyto_parse($comment['content']);
271  return $comment;
272}
273
274function replyto_parse_album_mail($comment)
275{
276  $comment['content'] = replyto_parse($comment['content'], 'album');
277  return $comment;
278}
Note: See TracBrowser for help on using the repository browser.