>[danger]上節課我們講了如何開發插件,這一節課我們用一個實例來講解如何開發一個評論插件
有些做過其他系統的插件開發的同學,再來學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>
~~~
評論列表頁:

評論詳情頁面:

## 三、前臺功能
找到site\article,加入以下代碼,用于顯示評論
~~~
<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控件的前端功能,一旦前臺界面有交互,還是用非插件的模式實現比較方便點,每個工具都有優缺點,能實現功能的就是好工具
>[warning]如有不明白的地方,留言或是加入我們?“三眾技術QQ交流群”一起討論
## 關于我們
>[danger][三眾科技](http://www.sunzoon.com)資訊平臺——大道至簡,悅你所閱!
>本教程由[三眾簡悅](http://it.sunzoon.com)原創,轉載請注明出處,作者:bobball,由bobo整理成看云書籍
三眾技術交流群:**543102562**
歡迎大家加入我們,共同討論IT,互聯網技術。同時可以掃描下面的二維碼關注我們,謝謝!
