服务报价 | 域名主机 | 网络营销 | 软件工具| [加入收藏]
 热线电话: #
当前位置: 主页 > php教程 > dedecms教程 >

dedecms织梦AJAX评论实现评论楼层化 引用内嵌美化

时间:2016-10-24 02:40来源:未知 作者:最模板编辑 点击:
AJAX 无刷新评论功能是织梦DeDeCMS 5.5 Final 正式版的一大亮点,也是众多站长们选择升级中一个重要原因。 相比5.3 版本的评论,AJAX评论能达到无刷新即时显示最新留言及其回复功能,同

AJAX 无刷新评论功能是织梦DeDeCMS 5.5 Final 正式版的一大亮点,也是众多站长们选择升级中一个重要原因。
  相比5.3 版本的评论,AJAX评论能达到无刷新即时显示最新留言及其回复功能,同时,支持游客对游客发表的评论进行引用回复,增加了“支持者”“反对者”“中立者”三个不同的人物较色,让文章的互动性更人性化、合理化。
  但是,惊喜之余难免有丝丝遗憾。
AJAX 评论无法实现楼层化,游客引用的评论无法嵌套在新评论,样式不够醒目直观。
最近九亿博客在升级程序时,着重对DeDeCMS 5.5 中新增加的AJAX评论插件进行了二次开发美化。
开发的主要内容包括两方面:评论实现楼层效果;评论回复为内嵌样式。
  评论实现楼层效果的解决办法:
  分析观察plus/feedback_ajax.php 插件文件,源代码如下所示
<?php
require_once(dirname(__FILE__)."/../include/common.inc.php");
if($cfg_feedback_forbid==’Y’) exit(‘系统已经禁止评论功能!‘);
require_once(DEDEINC."/filter.inc.php");
if(!isset($action))
{
$action = ”;
}
//兼容旧的JS代码
if($action == ‘good’ || $action == ‘bad’)
{
if(!empty($aid)) $id = $aid;
require_once(dirname(__FILE__).’/digg_ajax.php’);
exit();
}
 
$cfg_formmember = isset($cfg_formmember) ? true : false;
$ischeck = $cfg_feedbackcheck==’Y’ ? 0 : 1;
$aid = (isset($aid) && is_numeric($aid)) ? $aid : 0;
$fid = (isset($fid) && is_numeric($fid)) ? $fid : 0;
if(empty($aid) && empty($fid))
{
ShowMsg(‘文档id不能为空!’,’-1′);
exit();
}
include_once(DEDEINC."/memberlogin.class.php");
$cfg_ml = new MemberLogin();
 
if($action==’goodfb’)
{
AjaxHead();
$fid = intval($fid);
$dsql->ExecuteNoneQuery("Update `dede_feedback` set good = good+1 where id=’$fid’ ");
$row = $dsql->GetOne("Select good From `dede_feedback` where id=’$fid’ ");
echo "<a onclick="postBadGood(‘goodfb’,{$aid})">支持</a>[{$row['good']}]";
exit();
}
else if($action==’badfb’)
{
AjaxHead();
$fid = intval($fid);
$dsql->ExecuteNoneQuery("Update `dede_feedback` set bad = bad+1 where id=’$fid’ ");
$row = $dsql->GetOne("Select bad From `dede_feedback` where id=’$fid’ ");
echo "<a onclick="postBadGood(‘badfb’,{$aid})">反对</a>[{$row['bad']}]";
exit();
}
//查看评论
/*
function __ViewFeedback(){ }
*/
//———————————–
else if($action==” || $action==’show’)
{
//读取文档信息
$arcRow = GetOneArchive($aid);
if(empty($arcRow['aid']))
{
ShowMsg(‘无法查看未知文档的评论!’,’-1′);
exit();
}
extract($arcRow, EXTR_SKIP);
include_once(DEDEINC.’/datalistcp.class.php’);
$dlist = new DataListCP();
$dlist->pageSize = 20;
if(empty($ftype) || ($ftype!=’good’ && $ftype!=’bad’ && $ftype!=’feedback’))
{
$ftype = ”;
}
$wquery = $ftype!=” ? " And ftype like ‘$ftype’ " : ”;
//评论内容列表
$querystring = "select fb.*,mb.userid,mb.face as mface,mb.spacesta,mb.scores from `dede_feedback` fb
left join `dede_member` mb on mb.mid = fb.mid
where fb.aid=’$aid’ and fb.ischeck=’1′ $wquery order by fb.id desc";
$dlist->SetParameter(‘aid’,$aid);
$dlist->SetParameter(‘action’,’show’);
$dlist->SetTemplate(DEDETEMPLATE.’/plus/feedback_templet.htm’);
$dlist->SetSource($querystring);
$dlist->Display();
exit();
}
//引用评论
//————————————
/*
function __Quote(){ }
*/
else if($action==’quote’)
{
$row = $dsql->GetOne("Select * from `dede_feedback` where id =’$fid’");
require_once(DEDEINC.’/dedetemplate.class.php’);
$dtp = new DedeTemplate();
$dtp->LoadTemplate(DEDETEMPLATE.’/plus/feedback_quote.htm’);
$dtp->Display();
exit();
}
//发表评论
//————————————
/*
function __SendFeedback(){ }
*/
else if($action==’send’)
{
//读取文档信息
$arcRow = GetOneArchive($aid);
if((empty($arcRow['aid']) || $arcRow['notpost']==’1′)&&empty($fid))
{
ShowMsg(‘无法对该文档发表评论!’,’-1′);
exit();
}
//是否加验证码重确认
if(empty($isconfirm))
{
$isconfirm = ”;
}
if($isconfirm!=’yes’ && $cfg_feedback_ck==’Y’)
{
extract($arcRow, EXTR_SKIP);
require_once(DEDEINC.’/dedetemplate.class.php’);
$dtp = new DedeTemplate();
$dtp->LoadTemplate(DEDETEMPLATE.’/plus/feedback_confirm.htm’);
$dtp->Display();
exit();
}
//检查验证码
if($cfg_feedback_ck==’Y’)
{
$validate = isset($validate) ? strtolower(trim($validate)) : ”;
$svali = strtolower(trim(GetCkVdValue()));
if($validate != $svali || $svali==”)
{
ResetVdValue();
ShowMsg(‘验证码错误!‘,’-1′);
exit();
}
}
//检查用户登录
if(empty($notuser))
{
$notuser=0;
}
//匿名发表评论
if($notuser==1)
{
$username = $cfg_ml->M_ID > 0 ? ‘匿名‘ : ‘游客‘;
}
//已登录的用户
else if($cfg_ml->M_ID > 0)
{
$username = $cfg_ml->M_UserName;
}
//用户身份验证
else
{
if($username!=” && $pwd!=”)
{
$rs = $cfg_ml->CheckUser($username,$pwd);
if($rs==1)
{
$dsql->ExecuteNoneQuery("Update `dede_member` set logintime=’".time()."’,loginip=’".GetIP()."’ where mid=’{$cfg_ml->M_ID}’; ");
}
else
{
$username = ‘游客‘;
}
}
else
{
$username = ‘游客‘;
}
}
$ip = GetIP();
$dtime = time();
//检查评论间隔时间;
if(!empty($cfg_feedback_time))
{
//检查最后发表评论时间,如果未登陆判断当前IP最后评论时间
if($cfg_ml->M_ID > 0)
{
$where = "WHERE `mid` = ‘$cfg_ml->M_ID’";
}
else
{
$where = "WHERE `ip` = ‘$ip’";
}
$row = $dsql->GetOne("SELECT dtime FROM `dede_feedback` $where ORDER BY `id` DESC ");
if(is_array($row) && $dtime – $row['dtime'] < $cfg_feedback_time)
{
ResetVdValue();
ShowMsg(‘管理员设置了评论间隔时间,请稍等休息一下!‘,’-1′);
exit();
}
}
if(empty($face))
{
$face = 0;
}
$face = intval($face);
extract($arcRow, EXTR_SKIP);
$msg = cn_substrR(TrimMsg($msg),1000);
$username = cn_substrR(HtmlReplace($username,2),20);
if(empty($feedbacktype) || ($feedbacktype!=’good’ && $feedbacktype!=’bad’))
{
$feedbacktype = ‘feedback’;
}
//保存评论内容
if($comtype == ‘comments’)
{
$arctitle = addslashes($title);
if($msg!=”)
{
$inquery = "INSERT INTO `dede_feedback`(`aid`,`typeid`,`username`,`arctitle`,`ip`,`ischeck`,`dtime`, `mid`,`bad`,`good`,`ftype`,`face`,`msg`)
VALUES (‘$aid’,’$typeid’,’$username’,’$arctitle’,’$ip’,’$ischeck’,’$dtime’, ‘{$cfg_ml->M_ID}’,’0′,’0′,’$feedbacktype’,’$face’,’$msg’); ";
$rs = $dsql->ExecuteNoneQuery($inquery);
if(!$rs)
{
ShowMsg(‘ 发表评论错误! ‘, ‘-1′);
//echo $dsql->GetError();
exit();
}
}
}
//引用回复
elseif ($comtype == ‘reply’)
{
$row = $dsql->GetOne("Select * from `dede_feedback` where id =’$fid’");
$arctitle = $row['arctitle'];
$aid =$row['aid'];
$msg = $quotemsg.$msg;
$msg = HtmlReplace($msg,2);
$inquery = "INSERT INTO `dede_feedback`(`aid`,`typeid`,`username`,`arctitle`,`ip`,`ischeck`,`dtime`,`mid`,`bad`,`good`,`ftype`,`face`,`msg`)
VALUES (‘$aid’,’$typeid’,’$username’,’$arctitle’,’$ip’,’$ischeck’,’$dtime’,’{$cfg_ml->M_ID}’,’0′,’0′,’$feedbacktype’,’$face’,’$msg’)";
$dsql->ExecuteNoneQuery($inquery);
}
if($feedbacktype==’bad’)
{
$dsql->ExecuteNoneQuery("Update `dede_archives` set scores=scores-{cfg_feedback_sub},badpost=badpost+1,lastpost=’$dtime’ where id=’$aid’ ");
}
else if($feedbacktype==’good’)
{
$dsql->ExecuteNoneQuery("Update `dede_archives` set scores=scores+{$cfg_feedback_add},goodpost=goodpost+1,lastpost=’$dtime’ where id=’$aid’ ");
}
else
{
$dsql->ExecuteNoneQuery("Update `dede_archives` set scores=scores+1,lastpost=’$dtime’ where id=’$aid’ ");
}
if($cfg_ml->M_ID > 0)
{
$dsql->ExecuteNoneQuery("Update `dede_member` set scores=scores+{$cfg_sendfb_scores} where mid=’{$cfg_ml->M_ID}’ ");
}
//统计用户发出的评论
if($cfg_ml->M_ID > 0)
{
#api{{
if(defined(‘UC_API’) && @include_once DEDEROOT.’/api/uc.func.php’)
{
//同步积分
uc_credit_note($cfg_ml->M_LoginID, $cfg_sendfb_scores);
//推送事件
$arcRow = GetOneArchive($aid);
$feed['icon'] = ‘thread’;
$feed['title_template'] = ‘<b>{username} 在网站发表了评论</b>’;
$feed['title_data'] = array(‘username’ => $cfg_ml->M_UserName);
$feed['body_template'] = ‘<b>{subject}</b><br>{message}’;
$url = !strstr($arcRow['arcurl'],’http://’) ? ($cfg_basehost.$arcRow['arcurl']) : $arcRow['arcurl']; 
$feed['body_data'] = array(‘subject’ => "<a href="".$url."">$arcRow[arctitle]</a>", ‘message’ => cn_substr(strip_tags(preg_replace("/[.+?]/is", ”, $msg)), 150));
$feed['images'][] = array(‘url’ => $cfg_basehost.’/images/scores.gif’, ‘link’=> $cfg_basehost);
uc_feed_note($cfg_ml->M_LoginID,$feed); unset($arcRow);
}
#/aip}}
$row = $dsql->GetOne("SELECT COUNT(*) AS nums FROM `dede_feedback` WHERE `mid`=’".$cfg_ml->M_ID."’");
$dsql->ExecuteNoneQuery("UPDATE `dede_member_tj` SET `feedback`=’$row[nums]‘ WHERE `mid`=’".$cfg_ml->M_ID."’");
}
$_SESSION['sedtime'] = time();
if(empty($uid) && isset($cmtuser)) $uid = $cmtuser;
$backurl = $cfg_formmember ? "index.php?uid={$uid}&action=viewarchives&aid={$aid}" : "feedback.php?aid=$aid";
if($ischeck==0)
{
ShowMsg(‘成功发表评论,但需审核后才会显示你的评论!’, $backurl);
}
else
{
ShowMsg(‘成功发表评论,现在转到评论页面!’, $backurl);
}
exit();
}
?> 
  分析这个文件,有不少循环语句。
哪个是与评论列表有关的呢?首先,你必须明白一个技术常识。
实现楼层化,要靠那种CSS+DIV 技术效果来表现?最简单的方法,就是用<ul><li></li></ul>这个方式了。
然后对<li style="list-style-type: decimal;list-style-position: inside;"></li>设置列表的类型为数字,位置为内。
这样一来,<ul>下循环语句用<li>来控制,就很轻松实现评论楼层的效果了。
  再来讲一讲,如何将引用过评论嵌套在新评论的上方。
首先,你要明白文章内容页调用的是ajaxfeedback.htm这个模板文件,而这个文件又由feedback_ajax.php 程序来控制。
也就是,修改引用样式就修改feedback_ajax.php 文件,而不是feedback_quote.htm 这个文件。
  找到了程序文件,接着查找与引用有关的函数值。
下面这段代码中就包含我所要找的{quote}参数:
 //保存评论内容
 if(!empty($fid))
 {
  $row = $dsql->GetOne("Select username,msg from `dede_feedback` where id =’$fid’ ");
  $qmsg = ‘{quote}{title}’.$row['username'].’ 的原帖:{/title}{content}’.$row['msg'].’{/content}{/quote}’;
  $msg = addslashes($qmsg).$msg;
 }
 $ischeck = ($cfg_feedbackcheck==’Y’ ? 0 : 1);
 $arctitle = addslashes($title);
 $inquery = "INSERT INTO `dede_feedback`(`aid`,`typeid`,`username`,`arctitle`,`ip`,`ischeck`,`dtime`, `mid`,`bad`,`good`,`ftype`,`face`,`msg`)
                VALUES (‘$aid’,’$typeid’,’$username’,’$arctitle’,’$ip’,’$ischeck’,’$dtime’, ‘{$cfg_ml->M_ID}’,’0′,’0′,’$feedbacktype’,’$face’,’$msg’); ";
 $rs = $dsql->ExecuteNoneQuery($inquery);
 if( !$rs )
 {
   echo "<font color=’red’>发表评论出错了!</font>";
   //echo $dslq->GetError();
   exit();
 }
 $newid = $dsql->GetLastID(); 
  也许,不能找出是哪个CSS控制了这个引用样式。
官方提供的帮助文档,也没有提供该参数的具体信息。
看来,只要靠自己分析了。
既然知道引用的函数值是{quote} 字样。
那就是用DW的查找替换功能,在全站范围内展开搜索,终于找到了,在include/channelunit.func.php 这个程序文件中发现了{quote} 影子。
  快速打开这个文件,查找到第506行的位置,代码如下:
//引用回复标记处理
function Quote_replace($quote)
{
 $quote = str_replace(‘{quote}’,’<div>’,$quote);
 $quote = str_replace(‘{title}’,’<div class="reply1">’,$quote);
 $quote = str_replace(‘{/title}’,’</div>’,$quote);
 $quote = str_replace(‘&lt;br/&gt;’,’<br>’,$quote);
 $quote = str_replace(‘{content}’,’<div class="reply2">’,$quote);
 $quote = str_replace(‘{/content}’,’</div>’,$quote);
 $quote = str_replace(‘{/quote}’,’</div>’,$quote);
 return $quote;
  将其修改为以下内容:
//引用回复标记处理
function Quote_replace($quote)
{
$quote = str_replace(‘{quote}’,’<div class="decmt-box">’,$quote);
$quote = str_replace(‘{title}’,’<div class="decmt-title"><span class="username">’,$quote);
$quote = str_replace(‘{/title}’,’</span></div>’,$quote);
$quote = str_replace(‘&lt;br/&gt;’,’<br>’,$quote);
$quote = str_replace(‘{content}’,’<div class="decmt-content">’,$quote);
$quote = str_replace(‘{/content}’,’</div>’,$quote);
$quote = str_replace(‘{/quote}’,’</div>’,$quote);
return $quote;
  层<div class="reply1"></div> 主要用于显示引用的标题字样,如“引用XXX的原帖”,层<div class="reply2"></div> 主要用于控制引用评论的内容,如“楼上评论的内容”,明白了这些,再加以合理的CSS样式美化,就可以实现完美的评论内嵌效果了。
(责任编辑:最模板)
顶一下
(1)
100%
踩一下
(0)
0%
------分隔线----------------------------
栏目列表
热点内容