第十三课 插件开发–增加文章评论
上节课我们讲了如何开发插件,这一节课我们用一个实例来讲解如何开发一个评论插件
有些做过其他系统的插件开发的同学,再来学iWebShop的插件开发,很容易就上手,有些也可能发现iWebShop插件开发时,对前端页面的控制目前支持得还不是很灵活。因此,我做这个评论插件,我就采用以下方法开发:
后端维护的功能,用插件实现,前端评论,使用我们之前学的模板开发的方式来做
一、建立插件
在插件目录下增加“comment”目录,创建如下三个文件:
comment.php中加入以下代码:
class comment extends pluginBase{
//【必填】插件中文名称
public static function name()
{
return "三众科技评论插件";
}
//【必填】插件的功能简介
public static function description()
{
return "文章评论插件";
}
//安装插件代码
public static function install()
{
$commentDB = new IModel("article_comment");//表名article_comment
//判断表是否存在
if($commentDB->exists())
{
return true;
}
$data = array(
"comment" => self::name(),
"column" => array(
"id" => array("type" => "int(11) unsigned","auto_increment" => 1),
"content" => array("type" => "text","comment" => "评论内容"),
"create_time" => array("type" => "int(11) unsigned","default" => "0","comment" => "评论时间"),
"recontents" => array("type" => "text","comment" => "回复内容"),
"recomment_time" => array("type" => "int(11) unsigned","default" => "0","comment" => "回复时间"),
"aid" => array("type" => "int(11) unsigned","default" => "0","comment" => "文章id"),
"uid" => array("type" => "int(11) unsigned","default" => "0","comment" => "用户id")
),
"index" => array("primary" => "id","key" => "aid,uid"),
);
$commentDB->setData($data);
//开始创建表
return $commentDB->createTable();
}
//卸载插件代码
public static function uninstall()
{
$commentDB = new IModel("article_comment");
//删除表
return $commentDB->dropTable();
}
//插件参数配置
public static function configName()
{
//在本拿了中无参数配置
}
}
之后在后台中就可以安装插件了
以上为每个插件必须的,如不需要操作数据库的,可以不用写数据库代码。
二、管理后台功能维护
功能注册,comment.php中加入以下代码:
public function reg()
{
//后台插件管理中增加该插件链接
plugin::reg("onSystemMenuCreate",function(){
$link = "/plugins/system_comment_list";
Menu::$menu["插件"]["插件管理"][$link] = $this->name();
});
//后台评论列表的链接
plugin::reg("onBeforeCreateAction@plugins@system_comment_list",function(){
self::controller()->system_comment_list = function(){$this->comment_list();};
});
//后台评论详情的链接
plugin::reg("onBeforeCreateAction@plugins@system_comment_edit",function(){
self::controller()->system_comment_edit = function(){$this->comment_edit();};
});
//后台评论回复的链接
plugin::reg("onBeforeCreateAction@plugins@system_comment_update",function(){
self::controller()->system_comment_update = function(){$this->comment_update();};
});
//后台评论删除的链接
plugin::reg("onBeforeCreateAction@plugins@system_comment_del",function(){
self::controller()->system_comment_del = function(){$this->comment_del();};
});
}
功能实现,comment.php中加入以下代码:
public function comment_list()
{
$this->redirect("system_comment_list");
}
public function comment_edit()
{
$id = IFilter::act(IReq::get("id"));
$commentDB = new IQuery("article_comment as c");
$commentDB->join=" left join article as b on c.aid = b.id left join user as a on c.uid = a.id ";
$commentDB->where=" c.id=$id ";
$commentDB->fields ="c.id,FROM_UNIXTIME(c.create_time) as create_time,a.username,b.title,c.recomment_time,c.recontents,c.content";
$items = $commentDB->find();
$this->redirect("system_comment_edit",array("comment" => current($items)));
}
//回复评论
public function comment_update()
{
$id = IFilter::act(IReq::get("id"));
$recontents = IFilter::act(IReq::get("recontents"));
$data = array();
$data["recontents"]=$recontents;
$data["recomment_time"]=time();
//保存数据库
$commentDB = new IModel("article_comment");
$commentDB->setData($data);
$commentDB->update("id = ".$id);
$this->redirect("system_comment_list");
}
//回复删除
public function comment_del()
{
$commentDB = new IModel("article_comment");
$id = IFilter::act(IReq::get("id"));
$commentDB->del("id = ".$id);
$this->redirect("system_comment_list");
}
后台管理页面,评论列表:system_comment_list.html
<div class="headbar">
<div class="position"><span>文章</span><span>></span><span>评论管理</span></div>
</div>
<form action="{url:/plugins/system_comment_del}" method="post" name="comment_list" onsubmit="return checkboxCheck("check[]","尚未选中任何记录!")">
<div class="content">
<table class="list_table">
<colgroup>
<col width="10px" />
<col width="100px" />
<col width="220px" />
<col width="75px" />
<col width="95px" />
</colgroup>
<thead>
<tr>
<th></th>
<th>评论人</th>
<th>评论文章</th>
<th>评论时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
{set:$page= (isset($_GET["page"])&&(intval($_GET["page"])>0))?intval($_GET["page"]):1;}
{query: name=article_comment as c join=left join article as b on c.aid eq b.id left join user as a on c.uid eq a.id fields=c.id,FROM_UNIXTIME(c.create_time) as create_time,a.username,b.title,c.recomment_time page=$page order = c.id desc}
<tr>
<td></td>
<td>{$item["username"]}</td>
<td>{$item["title"]}</td>
<td>{$item["create_time"]}</td>
<td>
<a href="{url:/plugins/system_comment_edit/id/$item[id]}"><img class="operator" src="{skin:images/admin/icon_check.gif}" alt="查看" /></a>
<a href="javascript:void(0)" onclick="delModel({link:"{url:/plugins/system_comment_del/id/$item[id]}"})"><img class="operator" src="{skin:images/admin/icon_del.gif}" alt="删除" /></a>
</td>
</tr>
{/query}
</tbody>
</table>
</div>
{$query->getPageBar()}
</form>
后台管理页面,评论详情:system_comment_edit.html
<div class="headbar">
<div class="position"><span>文章</span><span>></span><span>查看评论</span></div>
<div class="operating">
<a href="{url:/plugins/system_comment_list}"><button class="operating_btn" type="button"><span class="return">返回</span></button></a>
</div>
</div>
<div class="content_box">
<div class="content form_content">
<form action="{url:/plugins/system_comment_update}" method="post" name="comment_edit">
<input type="hidden" value="{$comment["id"]}" name="id" />
<table class="form_table">
<colgroup>
<col width="150px" />
<col />
</colgroup>
<tr>
<th>评论人:</th>
<td>{$comment["username"]}</td>
</tr>
<tr>
<th>评论时间:</th><td>{$comment["create_time"]}</td>
</tr>
<tr>
<th>评论文章:</th><td>{$comment["title"]}</td>
</tr>
<tr>
<th>评论内容:</th><td>{$comment["content"]}</td>
</tr>
<tr>
<th>回复评论:</th>
<td><textarea class="normal" name="recontents">{$comment["recontents"]}</textarea></td>
</tr>
<tr>
<th></th>
<td>
<button type="submit" class="submit"><span>确定</span></button>
</td>
</tr>
</table>
</form>
</div>
</div>
评论列表页:
评论详情页面:
三、前台功能
找到sitearticle,加入以下代码,用于显示评论
<div class="comment_list box">
{set:$where="c.aid=".$this->articleRow["id"];}
{set:$page= (isset($_GET["page"])&&(intval($_GET["page"])>0))?intval($_GET["page"]):1;}
{query: name=article_comment as c join=left join user as a on c.uid eq a.id fields=c.id,c.content,FROM_UNIXTIME(c.create_time) as create_time,a.username,c.recomment_time,FROM_UNIXTIME(c.recomment_time) as reply_time,c.recontents page=$page pagesize=3 where=$where order = c.id desc}
<div class="item">
<div class="user">
<div class="ico"><img src="{webroot:<%=head_ico%>}" width="70px" height="70px" onerror="this.src="{skin:images/front/user_ico.gif}"" /></div>
<span class="blue">{$item["username"]}</span>
</div>
<dl class="desc gray">
<p>
<img src="{skin:images/front/comm.gif}" width="16px" height="17px" />
<b>评论内容:</b><span class="f_r">{$item["create_time"]}</span>
</p>
<p class="indent">{$item["content"]}</p>
{if:$item["recomment_time"]>0}
<hr />
<p class="bg_gray"><img src="{skin:images/front/answer.gif}" width="16px" height="17px" />
<b class="orange">回复:</b>
<span class="f_r">{$item["reply_time"]}</span></p>
<p class="indent bg_gray">{$item["recontents"]}</p>
{/if}
</dl>
</div>
{/query}
{$query->getPageBar()}
</div>
<div class="wrap_box">
<form method="post" action="{url:/site/article_comment_add}">
<input type="hidden" name="aid" value="{$this->articleRow["id"]}" />
<table class="form_table f_l">
<tr>
<th>评论内容:</th><td valign="top"><textarea name="content" id="content"></textarea></td>
</tr>
<tr><td></td><td><label class="btn"><input type="submit" value="评论" /></label></td></tr>
</table>
</form>
</div>
打开site.php,加入以下代码
public function article_comment_add()
{
if(!isset($this->user["user_id"]) || !$this->user["user_id"])
{
IError::show(403,"未登录用户不能评论");
}
if(!IReq::get("aid"))
{
IError::show(403,"传递的参数不完整");
}
if(trim(IReq::get("content"))=="")
{
IError::show(403,"评论必须输入");
}
$aid = IFilter::act(IReq::get("aid"),"int");
$data = array(
"content" => IFilter::act(IReq::get("content")),
"create_time" => time(),
"aid"=>$aid,
"uid"=>$this->user["user_id"],
);
$tb_comment = new IModel("article_comment");
$tb_comment->setData($data);
$re = $tb_comment->add();
if($re)
{
$this->redirect("/site/article_detail/id/".$aid);
}
else
{
IError::show(403,"评论失败");
}
}
以下代码用于实现评论提交功能。
后记
以上只是举了一个例子说明如何做一个插件,还有前台的视图和控制器的代码实现,但就目前而言,iwebShop的插件还是比较适合做那些没界面的功能,或是在线留言等这些js控件的前端功能,一旦前台界面有交互,还是用非插件的模式实现比较方便点,每个工具都有优缺点,能实现功能的就是好工具
如有不明白的地方,留言或是加入我们 “三众技术QQ交流群”一起讨论
关于我们
三众科技资讯平台——大道至简,悦你所阅!
>本教程由三众简悦原创,转载请注明出处,作者:bobball,由bobo整理成牛骨文书籍
三众技术交流群:543102562
欢迎大家加入我们,共同讨论IT,互联网技术。同时可以扫描下面的二维码关注我们,谢谢!