«

emlog插件开发中的代码模板

nciaer 发布于 阅读:21 插件


common文件

这个默认情况下只定义了一个nciaer_form方法,用来生成表单的

<?php
defined('EMLOG_ROOT') || exit('access denied!');

function nciaer_form($options) {

    global $storage;

    $html = "";

    foreach($options as $option) {
        if($option['type'] == 'int') {
            $value = (int)$storage->getValue($option['name']);
        } elseif($option['type'] == 'string') {
            $value = (string)$storage->getValue($option['name']);
        }

        $html .= "<div class = 'form-group'>";
        if($option['form'] == 'alert') {
            $html .= "<div class = 'alert alert-info'>{$option['tip']}</div>";

        } elseif($option['form'] == 'text') {
            $html .= "<label class = 'font-weight-bolder'>{$option['title']}</label><input class = 'form-control' type = 'text' name = '{$option['name']}' value = '{$value}' />";
        } elseif($option['form'] == 'textarea') {
            $html .= "<label class = 'font-weight-bolder'>{$option['title']}</label><textarea class = 'form-control' name = '{$option['name']}' rows='4'>{$value}</textarea>";
        } elseif($option['form'] == 'radio') {
            $checked1 = $checked0 = '';
            if($value == 1) $checked1 = 'checked=true';
            if($value == 0) $checked0 = 'checked=true';
            $html .= "<label class = 'font-weight-bolder'>{$option['title']}</label><div><div class = 'form-check form-check-inline'><input name = '{$option['name']}' class = 'form-check-input' type = 'radio' value = '1' id = '{$option['name']}1' {$checked1} /><label for = '{$option['name']}1' class = 'form-check-label' >是</label></div><div class = 'form-check form-check-inline'><input name = '{$option['name']}' class = 'form-check-input' type = 'radio' value = '0' id = '{$option['name']}0' {$checked0} /><label for = '{$option['name']}0' class = 'form-check-label'>否</label></div></div>";
        } elseif($option['form'] == 'color') {
            $html .= "<label class = 'font-weight-bolder'>{$option['title']}</label><input class = 'form-control' type = 'color' name = '{$option['name']}' value = '{$value}' />";
        } elseif($option['form'] == 'select') {
            $html .= "<label class = 'font-weight-bolder'>{$option['title']}</label>";
            foreach($option['values'] as $k=>$v) {
                $checked = $value == $k ? 'checked':'';
                $html .= "<div class = 'form-check mb-1'><input type = 'radio'name = '{$option['name']}' value = '{$k}' class = 'form-check-input' id = 'select{$k}' {$checked}/> <label for = 'select{$k}' class = 'form-check-label'>{$v}</label></div>";
            }
        }
        if(!empty($option['tip']) && $option['form'] != 'alert') {
            $html .= "<div class = 'text-secondary mt-1'>{$option['tip']}</div>";
        }
        $html .= "</div>";
    }

    return $html;
}

callback文件

插件开启,升级,删除都会执行pluginName_callback.php,下面是一个模板,开发新插件时尽量都用变量,减少不必要的修改;

/**
 * 插件激活回调
 */
function callback_init() {
    global $plugin;

    $r = explode('/', $plugin, 2);
    $plugin = $r[0];
    $storage = Storage::getInstance($plugin);

    Nciaer_db::instance()->run(__FUNCTION__);

}

/**
 * 插件更新回调
 */
function callback_up() {

    Nciaer_db::instance()->run(__FUNCTION__);

}

/**
 * 插件删除回调
 */
function callback_rm() {

    global $plugin;
    $r = explode('/', $plugin, 2);
    $plugin = $r[0];

    $storage = Storage::getInstance($plugin);
    $storage->deleteAllName('YES');

    Nciaer_db::instance()->run(__FUNCTION__);
}

// 订单表
class Nciaer_db {

    private static $instance;
    private $db;
    private $option = [
        "tableName" => DB_PREFIX . 'nciaer_ssl',
        "checkDeleteTable" => true,
        "fieldData" => [
            'id' => "`id` int NOT NULL AUTO_INCREMENT PRIMARY KEY",
            'domain' => "`domain` varchar(100) NOT NULL default '' comment '域名'",
            'starttime' => "`starttime` int(10) NOT NULL DEFAULT 0 comment '颁发日期'",
            'endtime' => "`endtime` int(10) NOT NULL DEFAULT 0 comment '截至日期'",
            'checktime' => "`checktime` int(10) NOT NULL DEFAULT 0 comment '上次检测时间'",
            'result' => "`result` varchar(100) NOT NULL DEFAULT '' comment '检测结果'",
            'notify' => "`notify` tinyint NOT NULL DEFAULT 0 comment '开启通知'",
            'email' => "`email` varchar(100) NOT NULL DEFAULT '' comment '通知邮箱'",
            'dateline' => "`dateline` int(10) NOT NULL DEFAULT 0",
        ]
    ];

    public function run($func) {

        if($func == 'callback_init') {
            $this->pluginInit();
        }elseif($func == 'callback_up') {
            $this->pluginUp();
        } elseif($func == 'callback_rm') {
            $this->pluginRm();
        }

    }
    /**
     * 私有构造函数,保证单例
     */
    private function __construct()
    {
        //数据库实例赋值
        $this->db = Database::getInstance();
    }

    /**
     * 单例入口
     */
    public static function instance()
    {
        if (self::$instance === null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 插件启用执行函数
     */
    public function pluginInit()
    {
        if ($this->checkDataTable()) {
            $this->addDataTableField();
        } else {
            $this->addDataTable();
        }
    }

    /**
     * 检测数据表是否存在
     */
    public function checkDataTable()
    {
        if (isset($this->option['tableName'])) {
            $query = $this->db->query("SHOW TABLES LIKE '{$this->option['tableName']}'");
            if ($this->db->num_rows($query) > 0) {
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 检测数据表字段是否存在,不存在则创建字段
     */
    private function addDataTableField()
    {
        if (!empty($this->option) && is_array($this->option) && isset($this->option['fieldData']) && is_array($this->option['fieldData'])) {
            $preForeachData = '';
            foreach ($this->option['fieldData'] as $field => $fieldSql) {
                if (!$this->checkDataField($field)) {
                    $after = !empty($preForeachData) ? " AFTER {$preForeachData}" : '';
                    $this->db->query("ALTER TABLE {$this->option['tableName']} ADD COLUMN {$fieldSql}{$after}");
                }
                $preForeachData = $field;
            }
        }
    }

    /**
     * 检测数据表中字段是否存在 - 指定字段名
     */
    public function checkDataField($fieldName = '')
    {
        if (!empty($fieldName) && $this->checkDataTable()) {
            $query = $this->db->query("SHOW COLUMNS FROM {$this->option['tableName']} LIKE '{$fieldName}'");
            if ($this->db->num_rows($query) > 0) {
                return true;
            }
            return false;
        }
        return false;
    }

    /**
     * 数据表创建函数
     */
    private function addDataTable()
    {
        if (!empty($this->option) && is_array($this->option) && isset($this->option['fieldData']) && is_array($this->option['fieldData'])) {
            $sql = "CREATE TABLE IF NOT EXISTS {$this->option['tableName']} (";
            foreach ($this->option['fieldData'] as $field => $fieldSql) {
                $sql .= $fieldSql . ',';
            }
            $sql .= "index(`domain`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
            $this->db->query($sql);
        }
    }

    /**
     * 插件更新执行函数
     */
    public function pluginUp()
    {
        $this->addDataTableField();
    }

    /**
     * 插件卸载执行函数
     */
    public function pluginRm()
    {
        if (isset($this->option['checkDeleteTable']) && $this->option['checkDeleteTable'] === true) {
            $this->db->query("DROP TABLE IF EXISTS {$this->option['tableName']}");
        }
    }
}

插件