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

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

forgot debug code

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