PHPCMS V9 模塊開發 二次開發實例 留言本


鄙人實現了PHPCMS V9 產品開發權威指南(2011官方最新版).doc中的留言板實例,並加上模塊安裝和卸載功能,

程序可以運行,但只實現基本功能,目的是想讓和我一樣徘徊在PHPCMS門口不知道從哪兒進門的初學者走一下流程,歡迎指正!

對於像我這樣的入門者希望先把上面這個文檔仔細讀一遍再往下看!

聲明:我用的是GBK版本。

二次開發流程

  • 創建數據庫和數據庫表(無數據庫操作可略過)
  • 創建數據模型文件
  • 創建模塊目錄
  • 開發控制器和模板
  • install和uninstall模塊

一、創建數據庫表

具體需求請查看上面的文檔,不再贅述直接上SQL語句:

DROP TABLE IF EXISTS `guestbook`;

CREATE TABLE IF NOT EXISTS `guestbook` (
  `gid` smallint(5) NOT NULL AUTO_INCREMENT,
  `siteid` smallint(5) NOT NULL,
  `title` char(80) NOT NULL,
  `content` text NOT NULL,
  `reply` text NOT NULL,
  `userid` mediumint(8) unsigned NOT NULL DEFAULT '0',
  `username` char(20) NOT NULL,
  `passed` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `reply_status` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `addtime` int(10) unsigned NOT NULL DEFAULT '0',
  `replyer` char(20) NOT NULL,
  `replytime` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`gid`)
)DEFAULT CHARSET=utf8;

 

二、創建數據模型文件

數據庫模型位於:phpcms/model/ 目錄下。數據模型文件的命名規則建議為 '數據表名稱' + '_model.class.php' 。

這個模塊中我們要使表“guestbook”,則數據庫模型文件名稱為'guestbook_model.class.php',程序如下:

<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_sys_class('model', '', 0);
class guestbook_model extends model {
    public function __construct() {
        $this->db_config = pc_base::load_config('database');
        $this->db_setting = 'default';
        
        // 記得換成自己的表名
        $this->table_name = 'guestbook';
        parent::__construct();
    }
}
?>

說明:任何自定義模塊的數據模型類,均繼承於model.class.php 數據模型基類。

在此基類中PHPCMS 系統已經把最常用的數據庫操作方法進行了封裝。 二次開發者不必關於如何操作數據庫,

只需要根據需要用到的,已定義操作方法的要求,傳遞參數即可。系統會自動對數據進行處理,並返回結果。

說白了就是對你自定義的數據表的包裝,更方便操作數據庫。

 

三、創建模塊目錄  

PHPCMS v9框架中的模塊,位於phpcms/modules目錄中,每一個目錄稱之為一個模塊。

如果要創建一個新模塊,只要在 phpcms/modules 目錄下創建文件夾並放入你的控制器類就可以了。

當前我們要開發一個叫做guestbook的留言本模塊,那么首先在 phpcms/modules 目錄下創建文件夾,

並將其命名為guestbook。如下圖所示:

guestbook 模塊的標准結構通常是這樣的,如下圖所示:

classes  為模塊類文件夾

functions 為模塊函數文件夾

templates 為模塊模板文件夾,這里通常放置含有權限控制的控制器模板,也就是后台模板!!!

如果您的模塊有單獨的前台模版,你需要在phpcms/templates/default下,

創建一個您的模塊同名目錄來放置前台模板(並進行配置,后面會說到),“default”為你的風格包名稱,我們默認適用default。

install和uninstall為模塊安裝和卸載模塊

 

四、開發控制器和模板

PHPCMS V9的控制器位於phpcms/modules/模塊/目錄下面,不是classes目錄下。文件名是類名+.php,

例如一個名為guestbook的控制器,那么他的命名為guestbook.php即可。控制器類默認繼承系統的函數庫,可以直接使用。

需要注意的是:控制器類的類名稱與控制器文件名必須相同本留言本模塊有以下二個控制器:

  • 前台index.php控制器開發

  前台控制器主要涉及前台留言顯示、留言的提交處理等功能函數,以下為全部源代碼,代碼如下所示:  

<?php 
defined('IN_PHPCMS') or exit('No permission resources.');
class index {
    function __construct() {
        // 加載留言本的數據模型
        $this->guestbook_db = pc_base::load_model('guestbook_model');
        
        // 取得當前登錄會員的會員名(username)和會員ID(userid)
        $this->_username = param::get_cookie('_username');
        $this->_userid = param::get_cookie('_userid');
        
        //定義站點ID常量,選擇模版使用
        $siteid = isset($_GET['siteid']) ? intval($_GET['siteid']) : get_siteid();
          define("SITEID", $siteid);
          
          //讀取配置
        $setting = new_html_special_chars(getcache('guestbook', 'commons'));
        $this->set = $setting[SITEID];
    }
    
    public function init() {
        //設置分頁條數
        $pagesize = $this->set['pagesize'];
        
        $where = array('passed'=>1,'siteid'=>SITEID);
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        $infos = $this->guestbook_db->listinfo($where, 'gid DESC', $page, $pagesize);
        $infos = new_html_special_chars($infos);
        
        // 加載系統form類,用於前台模塊文件中生成驗證碼
        pc_base::load_sys_class('form', '', 0);
        
        // 加載前台模板
        include template('guestbook', 'index');
    }
    
    /**
     *    在線留言 
     */
    public function ly()  {
        if(isset($_POST['dosubmit'])){ 
            // 標題和內容不能為空
            if (!(isset($_POST['ly']['title']) && trim($_POST['ly']['title']) && 
                isset($_POST['ly']['content']) && trim($_POST['ly']['content']))) {
                showmessage(L('輸入不能為空'), "?m=guestbook&c=index&siteid=".SITEID);
            }
            
            // 驗證碼
            if(isset($_POST['code'])){
                /*V9驗證碼的數值是通過SESSION傳遞,故在這段代碼中,首先加載配置文件,                 
                 取出當前系統配置中SESSION的存儲方式。然后根據SESSION的存儲方式,來加載對應的系統類庫*/
                $session_storage = 'session_'.pc_base::load_config('system','session_storage');
                pc_base::load_sys_class($session_storage);
                if(!isset($_SESSION)) {
                    session_start();
                }
                $code = isset($_POST['code']) && trim($_POST['code']) ? trim($_POST['code']) : showmessage(L('請輸入驗證碼'), HTTP_REFERER);
                if ($_SESSION['code'] != strtolower($code)) {
                    showmessage(L('驗證碼錯誤'), HTTP_REFERER);
                }
            } 
            $set = $this->set;
            $_POST['ly']['addtime'] = SYS_TIME;
            $_POST['ly']['userid'] = $this->_userid;
            $_POST['ly']['username'] = $this->_username;
            $_POST['ly']['siteid'] = SITEID;
            $_POST['ly']['passed'] = $set['check_pass'];
            $this->guestbook_db->insert($_POST['ly']);
            showmessage(L('添加成功'), "?m=guestbook&c=index&siteid=".SITEID);
        }  else  {
            echo  '請通過正常的方式提交留言,謝謝';
        }
    }
}
?>

  前台模板

  第三部分我們已經說了需要在phpcms/templates/default下創建一個'guestbook'目錄,在該目錄下再創建index.html文件,其源碼如下:  

{if $this->set['guestbook_status']}
<div class="box boxsbg cboxs">
    <h5>我要留言  </h5>
    <div class="tag_a">
    <form action="{APP_PATH}index.php?m=guestbook&c=index&a=ly&siteid={SITEID}" method="post" name="myform" id="myform">
    <table cellspacing="1" cellpadding="0" class="table_form">
    <tbody>  
        <tr> 
        <th>標題</th>
        <td><input type="text" value="" id="title" name="ly[title]" class="input-text"></td>
        </tr>
        <tr> 
        <th>內容</th>
        <td> 
        <textarea name="ly[content]" id="content" cols="5" rows="8" value="" style="width:400px;"></textarea>
        </td>
        </tr>
        {if $this->set['enablecheckcode']==1}
        <tr> 
        <th>驗證碼:</th>
        <td> 
        <input name="code" type="text" id="code"/>{form::checkcode('code_img','4','14',110,30)}
        </td>
        </tr>
        {/if}
        <tr> 
        <th></th>
        <td>
        <input type="submit" value="提交" name="dosubmit" class="button">
        <input type="reset" value=" 取消" name="reset" class="button"> </td>
        </tr> 
    </tbody>
    </table> 
    </form>
    </div> 
</div>
<div style="height:5px;"></div>
{/if}

 
<?php
if(is_array($infos)){
foreach($infos as $info){
?>
<div class="box boxsbg cboxs">
    <h5>{$info['title']}</h5>
    <div class="tag_a">
         {$info['content']}
         <br><br>
         {if $info['reply']}<font color=red>回復內容:</font>
         <font style="color: #09F;">{$info['reply']}</font>
         {/if}
    </div> 
</div>
<div style="height:5px;"></div>
<?php 
}}
?>
 

  打開phpcms/templates/default/config.php , 進行以下兩處修改:

  

  • 后台guestbook.php控制器開發

  后台管理控制器含權限控制,只有特定管理員才有權限訪問,所以這個控制器需要加載admin 模塊下的admin類,並繼承該類。代碼如下:  

<?php
defined('IN_PHPCMS') or exit('No permission resources. - guestbook.php');
pc_base::load_app_class('admin', 'admin', 0);

class guestbook extends admin {
    public function __construct() {
        parent::__construct();//繼承父類構造函數
        $setting = new_html_special_chars(getcache('guestbook', 'commons'));//讀取留言本配置緩存文件
        $this->set = $setting[$this->get_siteid()];
        $this->guestbook_db = pc_base::load_model('guestbook_model');//加載留言本數據模型
    }
    
    public function init() {
        $where = array('siteid'=>$this->get_siteid());
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        $infos = $this->guestbook_db->listinfo($where, 'gid DESC', $page, '15');
        
        /* 加載后台管理模版 guestbook_list.tpl.php。 
         * 此文件位於phpcms/modules/模塊/templates/  
         * 由此即可明白,其它后台管理模版亦位於此目錄*/
         include $this->admin_tpl('guestbook_list');
    }
    
    /* 未回復列表 */
    public function unreplylist() {
        $where = array('reply'=>'','siteid'=>$this->get_siteid());        
        $page = isset($_GET['page']) && intval($_GET['page']) ? intval($_GET['page']) : 1;
        $infos = $this->guestbook_db->listinfo($where, 'gid DESC', $page, '15');
        include $this->admin_tpl('guestbook_list');
    }
    
    /**
     * 回復留言
     */
    public function reply() {
        if(isset($_POST['dosubmit'])){
             $gid = intval($_GET['gid']);
            if($gid < 1) return false;  
             $_POST['reply']['replytime'] = SYS_TIME;
            $_POST['reply']['reply_status'] = '1';
            $this->guestbook_db->update($_POST['reply'], array('gid'=>$gid)); 
            showmessage(L('回復成功'),'?m=guestbook&c=guestbook&a=init');
         } else {
             $gid = intval($_GET['gid']);
            if($gid < 1) return false; 
             $show_validator = $show_scroll = $show_header = true;
             $info = $this->guestbook_db->get_one(array('gid'=>$_GET['gid']));
            if(!$info) showmessage(L('guestbook_exit'),'?m=guestbook&c=guestbook&a=init');
            extract($info); 
            // 加載后台管理模版 guestbook_reply.tpl.php
             include $this->admin_tpl('guestbook_reply');
        }
    }
    
    /**
    * 刪除留言 
    * @param    intval    $gid    留言ID,遞歸刪除
    */
    public function delete() {
        if((!isset($_GET['gid']) || empty($_GET['gid'])) && (!isset($_POST['gid']) || empty($_POST['gid']))) {
            showmessage(L('未選中'), HTTP_REFERER);
        }
        if(is_array($_POST['gid'])){
            foreach($_POST['gid'] as $gid_arr) {
                $gid_arr = intval($gid_arr);
                $this->guestbook_db->delete(array('gid'=>$gid_arr)); 
            }
            showmessage(L('刪除成功'),'?m=guestbook&c=guestbook');
        }else{
            $gid = intval($_GET['gid']);
            if($gid < 1) return false;
            $result = $this->guestbook_db->delete(array('gid'=>$gid));
            if($result){
                showmessage(L('刪除成功'),'?m=guestbook&c=guestbook');
            }else {
                showmessage(L("刪除失敗"),'?m=guestbook&c=guestbook');
            }
        }
    }
    
    /**
     * 留言本模塊配置
     */
    public function setting() {
        //更新模型數據庫,重設setting 數據. 
        $m_db = pc_base::load_model('module_model');
        $set = $m_db->get_one(array('module'=>'guestbook'));
        $setting = string2array($set['setting']);
        $now_setting = $setting[$this->get_siteid()];//當前站點的配置
        if(isset($_POST['dosubmit'])) {
            $setting[$this->get_siteid()] = $_POST['setting'];
            setcache('guestbook', $setting, 'commons'); 
            $set = array2string($setting);
            $m_db->update(array('setting'=>$set), array('module'=>ROUTE_M));
            showmessage('配置更新成功', '?m=guestbook&c=guestbook&a=init');
        } else {
            extract($now_setting);
            // 加載后台管理模版 setting.tpl.php
            include $this->admin_tpl('setting');
        }
    }
}
?>

  上面涉及的后台模板代碼如下:

  guestbook_list.tpl.php  

<?php
defined('IN_ADMIN') or exit('No permission resources. - guestbook_list.tpl.php');
$show_dialog = 1;
include $this->admin_tpl('header', 'admin');
?>

<form action="?m=guestbook&c=guestbook&a=delete" method="post" name="myform" id="myform">
<table border="0" width="100%">
    <tr>
        <th><input type="checkbox" /></th><th>標題</th><th>內容</th><th>姓名</th><th>發表時間</th><th>是否回復</th><th>管理操作</th>
    </tr>
    <?php
    if(is_array($infos)){
    foreach($infos as $info){
    ?>
    <tr>
        <td align="center" width="35"><input type="checkbox" name="gid[]" value="<?php echo $info['gid']?>"></td><!-- 多選按鈕 -->
        <td align="center"><?php echo $info['title']?></td><!-- 標題 -->
        <td align="center" width="30%"><?php echo $info['content']?></td><!-- 內容 -->
        <td align="center" width="100"><?php echo $info['username'];?></td><!-- 姓名 -->
        <td align="center" width="120"><?php echo date('Y-m-d H-i-s',$info['addtime']);?></td><!-- 發表時間 -->
        <td align="center" width="10%"><!-- 是否回復 -->
        <?php if($info['reply']==''){echo '<font color=red>未回復</font>';}else{echo '已回復';}?>
        </td>
        <td align="center" width="12%"><!-- 管理操作 -->
        <a href="?m=guestbook&c=guestbook&a=reply&gid=<?php echo $info['gid']; ?>" title="回復留言">回復</a> |
        <a href='?m=guestbook&c=guestbook&a=delete&gid=<?php echo $info['gid']?>'
         onClick="return confirm('<?php echo L('confirm', array('message' => new_addslashes($info['title'])))?>')">
         <?php echo L('刪除')?>
        </a>
        </td>
    </tr>
    <?php } } ?>
</table>
<br />&nbsp;&nbsp;
<input type="submit" name="dosubmit" id="dosubmit" value="<?php echo L('刪除留言')?>" />
</form>

  guestbook_reply.tpl.php  

<?php
defined('IN_ADMIN') or exit('No permission resources. - guestbook_reply.tpl.php');
$show_dialog = 1;
include $this->admin_tpl('header', 'admin');
?>

<form action="?m=guestbook&c=guestbook&a=reply&gid=<?php echo $gid; ?>" method="post" name="myform" id="myform">
<table cellpadding="2" cellspacing="1" class="table_form" width="100%">
    <tr><th width="100"><?php echo L('username')?>:</th><td><?php echo $username;?></td></tr>
    <tr><th width="100">標題:</th><td><?php echo $title;?></td></tr>
    <tr><th width="100">內容:</th><td><?php echo $content;?></td></tr>
    <tr><th width="100">留言時間:</th><td><?php echo date('Y-m-d H:i:s', $addtime);?></td></tr>
    <tr><th>回復內容:</th><td><textarea name="reply[reply]" id="reply" cols="50" rows="6"><?php echo $reply;?></textarea></td></tr>
    <tr><th>回復時間:</th><td><?php echo date("Y-m-d H:i:s", time());?></td></tr>
    <tr>
    <th>是否通過審核:</th>
    <td>
    <input name="reply[passed]" type="radio" value="1" <?php if($passed==1){echo "checked";}?>>&nbsp;<?php echo L('yes')?>&nbsp;&nbsp;
    <input name="reply[passed]" type="radio" value="0" <?php if($passed==0){echo "checked";}?>>&nbsp;<?php echo L('no')?>
    </td>
    </tr>
    <tr><th></th><td><input type="submit" name="dosubmit" id="dosubmit" value=" <?php echo L('submit')?> "></td></tr>
</table>
</form>

  setting.tpl.php

<?php
defined('IN_ADMIN') or exit('No permission resources.');
$show_dialog = 1;
include $this->admin_tpl('header', 'admin');
?>
<form action="?m=guestbook&c=guestbook&a=setting" method="post" name="myform" id="myform">
<table cellpadding="2" cellspacing="1" class="table_form" width="100%">

<tr>
<td align="right">是否允許留言:</td>
<td align="left">
<input name="setting[guestbook_status]" type="radio" value="1" <?php if($guestbook_status==1){echo "checked";}?> />&nbsp;<?php echo L('yes')?>&nbsp;&nbsp;
<input name="setting[guestbook_status]" type="radio" value="0" <?php if($guestbook_status==0){echo "checked";}?> />&nbsp;<?php echo L('no')?>
</td>
</tr>

<tr>
<td align="right">是否允許游客留言:</td>
<td align="left">
<input name="setting[allow_guest]" type="radio" value="1" <?php if($allow_guest==1){echo "checked";}?> />&nbsp;<?php echo L('yes')?>&nbsp;&nbsp;
<input name="setting[allow_guest]" type="radio" value="0" <?php if($allow_guest==0){echo "checked";}?> />&nbsp;<?php echo L('no')?>
</td>
</tr>

<tr>
<td align="right">是否需要審核:</td>
<td align="left">
<input name="setting[check_pass]" type="radio" value="1" <?php if($check_pass==1){echo "checked";}?> />&nbsp;<?php echo L('yes')?>&nbsp;&nbsp;
<input name="setting[check_pass]" type="radio" value="0" <?php if($check_pass==0){echo "checked";}?> />&nbsp;<?php echo L('no')?>
</td>
</tr>

<tr>
<td align="right">每頁條數:</td>
<td align="left">
<input name="setting[pagesize]" type="text" size="20" value="<?php echo $pagesize;?>" />
</td>
</tr>

<tr>
<td align="right">是否開啟驗證碼:</td>
<td align="left">
<input name="setting[enablecheckcode]" type="radio" value="1" <?php if($enablecheckcode==1){echo "checked";}?> />&nbsp;<?php echo L('yes')?>&nbsp;&nbsp;
<input name="setting[enablecheckcode]" type="radio" value="0" <?php if($enablecheckcode==0){echo "checked";}?> />&nbsp;<?php echo L('no')?>
</td>
</tr>

<tr>
<td align="right"><input type="submit" name="dosubmit" id="dosubmit" value=" <?php echo L('submit')?> "></td>
</tr>

</table>
</form>

 

五、安裝模塊和卸載模塊

首先確保你的安裝和卸載目錄如下:

具體配置可以參考這篇帖子phpcms v9添加新模塊我們這里:

guestbook.lang.php -- 內容為空就行了

config.inc.php -- 模塊信息和作者信息

<?php 
defined('IN_PHPCMS') or exit('Access Denied');
defined('INSTALL') or exit('Access Denied');

$module = 'guestbook';
$modulename = '留言板';
$introduce = '留言板獨立模塊';
$author = '你的名字';
$authorsite = 'http://www.nidewangzhi.cn';
$authoremail = 'nideyouxiang@163.com';
?>

extention.inc.php -- 后台管理菜單

<?php
defined('IN_PHPCMS') or exit('Access Denied');
defined('INSTALL') or exit('Access Denied');

$parentid = $menu_db->insert(array('name'=>'guestbook', 'parentid'=>29, 'm'=>'guestbook', 'c'=>'guestbook', 'a'=>'init', 'data'=>'', 'listorder'=>0, 'display'=>'1'), true);
$menu_db->insert(array('name'=>'setting', 'parentid'=>$parentid, 'm'=>'guestbook', 'c'=>'guestbook', 'a'=>'setting', 'data'=>'', 'listorder'=>0, 'display'=>'1'));
$menu_db->insert(array('name'=>'unreply', 'parentid'=>$parentid, 'm'=>'guestbook', 'c'=>'guestbook', 'a'=>'unreplylist', 'data'=>'', 'listorder'=>0, 'display'=>'1'));

$language = array('guestbook'=>'留言本', 'setting'=>'留言配置', 'unreply'=>'未回復留言');
?>

guestbook.sql -- 安裝時數據庫表的添加

DROP TABLE IF EXISTS `guestbook`;

CREATE TABLE IF NOT EXISTS `guestbook` (
  `gid` smallint(5) NOT NULL AUTO_INCREMENT,
  `siteid` smallint(5) NOT NULL,
  `title` char(80) NOT NULL,
  `content` text NOT NULL,
  `reply` text NOT NULL,
  `userid` mediumint(8) unsigned NOT NULL DEFAULT '0',
  `username` char(20) NOT NULL,
  `passed` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `reply_status` tinyint(1) unsigned NOT NULL DEFAULT '0',
  `addtime` int(10) unsigned NOT NULL DEFAULT '0',
  `replyer` char(20) NOT NULL,
  `replytime` int(10) unsigned NOT NULL DEFAULT '0',
  PRIMARY KEY (`gid`)
)DEFAULT CHARSET=utf8;

model.php -- 你所使用到的數據表

<?php 
defined('IN_PHPCMS') or exit('Access Denied');
defined('INSTALL') or exit('Access Denied');

return array('guestbook');
?>

module.sql --

INSERT INTO `module` (`module`, `name`, `url`, `iscore`, `version`, `description`, `setting`, `listorder`, `disabled`, `installdate`, `updatedate`) 
VALUES ('guestbook', '留言本', 'guestbook/', 0, '1.0', '留言本', 'array (
  "1" => 
  array (
    "guestbook_status" => "1",
    "allow_guest" => "1",
    "check_pass" => "1",
    "pagesize" => "3",
    "enablecheckcode" => "0",
), "2" => array ( "guestbook_status" => "1", "allow_guest" => "1", "check_pass" => "1", "pagesize" => "5", "enablecheckcode" => "0", ), )', 0, 0, '2014-7-18', '2014-7-18');

 卸載模塊里面更是照葫蘆畫瓢了,不在贅述。

六、后續

  在后台安裝這個模塊,本人水平有限,帖子難免有許多紕漏,肯定會有很多細節沒有講清楚,大家可以先仔細閱讀開篇的那個文檔,再和帖子結合理解。界面如下:

  

謝謝圍觀!

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM