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

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

update for Piwigo 2.4
fix a bug in links refresh on comments.php

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