Changeset 11416


Ignore:
Timestamp:
06/17/11 19:14:05 (8 years ago)
Author:
mistic100
Message:

change parsing method, parse link on PWG Stuffs comments bloc

Location:
extensions/reply_to
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/reply_to/main.inc.php

    r11325 r11416  
    44Version: auto 
    55Description: This plugin allows you to add Twitter-like reply links to comments. 
    6 Plugin URI: http://piwigo.org/ext/extension_view.php?eid= 
     6Plugin URI: http://piwigo.org/ext/extension_view.php?eid=556 
    77Author: Mistic 
    88Author URI: http://www.strangeplanet.fr 
     
    1919load_language('plugin.lang', REPLYTO_PATH); 
    2020 
    21 // add link and parse on picture page 
    22 if (script_basename() == 'picture') 
    23 { 
    24   add_event_handler('loc_begin_picture', 'replyto_add_link'); 
    25 } 
    26 // parse on comments page (picture section) 
    27 else if (script_basename() == 'comments' AND !isset($_GET['display_mode'])) 
    28 { 
    29   add_event_handler('render_comment_content', 'replyto_parse_picture', 60); 
    30 } 
    31 // parse comment admin side (picture section) 
    32 else if (script_basename() == 'admin' AND isset($_GET['page']) AND $_GET['page'] == 'comments' AND ( !isset($_GET['section']) OR $_GET['section'] == 'pictures' ) ) 
    33 { 
    34   add_event_handler('render_comment_content', 'replyto_parse_picture', 60); 
    35 } 
    36 // add link and parse on album page (compatibility with Comment on Albums) 
    37 else if (script_basename() == 'index') 
    38 { 
    39   add_event_handler('loc_begin_index', 'replyto_add_link'); 
    40 } 
    41 // parse on comments page (album section) 
    42 else if (script_basename() == 'comments' AND $_GET['display_mode'] == 'albums') 
    43 { 
    44   add_event_handler('render_comment_content', 'replyto_parse_album', 60); 
    45 } 
    46 // parse comment admin side (album section) 
    47 else if (script_basename() == 'admin' AND isset($_GET['page']) AND $_GET['page'] == 'comments' AND $_GET['section'] == 'albums') 
    48 { 
    49   add_event_handler('render_comment_content', 'replyto_parse_album', 60); 
    50 } 
     21add_event_handler('render_comment_content', 'replyto_parse', 60, 2); 
     22// add_event_handler('init', 'replyto_add_link');                  // for comments page (section_init.inc is not called on this page) 
     23add_event_handler('loc_end_section_init', 'replyto_add_link');  // for all the rest 
    5124     
    5225?> 
  • extensions/reply_to/reply_to.inc.php

    r11326 r11416  
    11<?php  
    22if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); 
     3 
     4define('REPLYTO_REGEX', '#\[reply=([0-9]+)\]([^\[\]]*)\[/reply\]#i'); 
    35 
    46/** 
     
    2224    $template->assign('replyto_form_name', 'addComment'); 
    2325  } 
     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'); 
    2439     
    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'); 
    3040    if ( !is_a_guest() OR $conf['comments_forall'] ) 
    3141    { 
     
    3545  else if  
    3646    ( 
    37       script_basename() == 'index' AND  
     47      script_basename() == 'index' AND isset($page['section']) AND 
    3848      isset($pwg_loaded_plugins['Comments_on_Albums']) AND  
    3949      $page['section'] == 'categories' AND isset($page['category']) 
    4050    ) 
    4151  { 
    42     add_event_handler('render_comment_content', 'replyto_parse_album', 60); 
    4352    add_event_handler('user_comment_insertion', 'replyto_parse_album_mail'); 
     53     
    4454    if ( !is_a_guest() OR $conf['comments_forall'] ) 
    4555    { 
     
    5767 
    5868{footer_script require=\'insertAtCaret\'} 
    59 function replyto(commentID, author) {ldelim} 
     69function replyTo(commentID, author) {ldelim} 
    6070  jQuery("#{$replyto_form_name} textarea").insertAtCaret("[reply=" + commentID + "]" + author + "[/reply] "); 
    6171} 
     
    8292{if not isset($comment.IN_EDIT)} 
    8393<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> 
     94  <a href="#commentform" title="{\'reply to this comment\'|@translate}" class="replyTo" onclick="replyTo(\'{$comment.ID}\', \'{$comment.AUTHOR}\');">&nbsp;</a> 
    8595</div> 
    8696{/if}' 
     
    104114 * Replace BBcode tag by a link with absolute url 
    105115 */ 
    106 function replyto_parse_picture($comment) 
    107 { 
    108   if (preg_match('#\[reply=([0-9]+)\]([^\[\]]*)\[/reply\]#i', $comment, $matches)) 
    109   { 
    110     // picture informations 
    111     $query = ' 
     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 = ' 
    112125SELECT 
    113126  img.id, 
     
    121134WHERE com.id = ' . $matches[1] . ' 
    122135;'; 
    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 = ' 
     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 = ' 
    132145SELECT  
    133146  id,  
     
    138151WHERE id = ' . $image['category_id'] . ' 
    139152;'; 
    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     } 
     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 */ 
    152170    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  
    166 function 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 = ' 
    175 SELECT id 
    176 FROM ' . COA_TABLE . ' 
    177 WHERE content = "' . $comment .'" 
    178 ;'; 
    179     if (!pwg_db_num_rows(pwg_query($query))) 
    180     { 
    181       return $comment; 
    182     } 
    183      
    184     // retrieving category informations 
    185     $query = ' 
     171    {     
     172      // retrieving category informations 
     173      $query = ' 
    186174SELECT 
    187175  cat.id,  
     
    194182WHERE com.id = ' . $matches[1] . ' 
    195183;'; 
    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> :'; 
     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      } 
    210202    } 
    211     else 
    212     { 
    213       $search = "#\[reply=([0-9]+)\]([^\[\]]*)\[/reply\]#i"; 
    214       $replace = ''; 
    215     } 
    216      
    217     return preg_replace($search, $replace, $comment); 
     203   
     204    return preg_replace(REPLYTO_REGEX, $replace, $comment); 
    218205  } 
    219206  else 
     
    228215function replyto_parse_picture_mail($comment) 
    229216{ 
    230   $comment['content'] = replyto_parse_picture($comment['content']); 
     217  $comment['content'] = replyto_parse($comment['content']); 
    231218  return $comment; 
    232219} 
     
    234221function replyto_parse_album_mail($comment) 
    235222{ 
    236   $comment['content'] = replyto_parse_album($comment['content']); 
     223  $comment['content'] = replyto_parse($comment['content'], 'album'); 
    237224  return $comment; 
    238225} 
Note: See TracChangeset for help on using the changeset viewer.