From 8e3a9181829b3b790ad695477ef20c065ec23a90 Mon Sep 17 00:00:00 2001 From: molong Date: Sun, 18 Sep 2016 23:30:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=8F=92=E4=BB=B6=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=AE=8C=E5=96=84=20=E6=B5=8B=E8=AF=95=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E7=BC=96=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- addons/test/Test.php | 44 ++++++++++ application/admin/controller/Addons.php | 2 +- application/common/model/Addons.php | 4 +- application/common/model/Hooks.php | 107 +++++++++++++----------- 4 files changed, 102 insertions(+), 55 deletions(-) create mode 100644 addons/test/Test.php diff --git a/addons/test/Test.php b/addons/test/Test.php new file mode 100644 index 00000000..a3c9639f --- /dev/null +++ b/addons/test/Test.php @@ -0,0 +1,44 @@ + +// +---------------------------------------------------------------------- + +namespace addons\test; +use app\common\controller\Addons; + +/** + * 系统环境信息插件 + * @author thinkphp + */ + +class Test extends Addons { + + public $info = array( + 'name' => 'Test', + 'title' => '测试', + 'description' => '用于显示一些服务器的信息', + 'status' => 1, + 'author' => 'molong', + 'version' => '0.1', + ); + + public function AdminIndex(){ + } + + public function test(){ + + } + + public function install(){ + return true; + } + + + public function uninstall(){ + return true; + } +} \ No newline at end of file diff --git a/application/admin/controller/Addons.php b/application/admin/controller/Addons.php index d15627b0..85b73b67 100644 --- a/application/admin/controller/Addons.php +++ b/application/admin/controller/Addons.php @@ -275,7 +275,7 @@ class Addons extends Admin { //超级管理员删除钩子 public function delhook() { - $id = $this->getArrayParam('id'); + $id = $this->getArrayParam('id'); $map['id'] = array('IN', $id); $result = $this->hooks->where($map)->delete(); if ($result !== false) { diff --git a/application/common/model/Addons.php b/application/common/model/Addons.php index b52b8d51..9af1e737 100644 --- a/application/common/model/Addons.php +++ b/application/common/model/Addons.php @@ -87,9 +87,7 @@ class Addons extends \app\common\model\Base { if ($info) { $result = $this->save(array('isinstall'=>1, 'status'=>1), array('id'=>$info)); }else{ - $data['isinstall'] = 1; - $data['status'] = 1; - $result = $this->save($data); + $result = false; } if (false !== $result) { return model('Hooks')->addHooks($data['name']); diff --git a/application/common/model/Hooks.php b/application/common/model/Hooks.php index c8034e0c..aacd0c00 100644 --- a/application/common/model/Hooks.php +++ b/application/common/model/Hooks.php @@ -16,13 +16,13 @@ namespace app\common\model; class Hooks extends Base { public $keyList = array( - array('name'=>'name','title'=>'钩子名称','type'=>'text','help'=>'需要在程序中先添加钩子,否则无效'), - array('name'=>'description','title'=>'钩子描述','type'=>'text','help'=>'钩子的描述信息'), - array('name'=>'type_text','title'=>'钩子类型','type'=>'select','help'=>'钩子的描述信息'), - array('name'=>'addons','title'=>'插件排序','type'=>'kanban') + array('name' => 'name', 'title' => '钩子名称', 'type' => 'text', 'help' => '需要在程序中先添加钩子,否则无效'), + array('name' => 'description', 'title' => '钩子描述', 'type' => 'text', 'help' => '钩子的描述信息'), + array('name' => 'type_text', 'title' => '钩子类型', 'type' => 'select', 'help' => '钩子的描述信息'), + array('name' => 'addons', 'title' => '插件排序', 'type' => 'kanban'), ); - public function initialize(){ + public function initialize() { parent::initialize(); foreach ($this->keyList as $key => $value) { if ($value['name'] == 'type_text') { @@ -32,30 +32,30 @@ class Hooks extends Base { } } - protected function setAddonsAttr($value){ - if ($value[1]) { - $string = implode(",", $value[1]); + protected function setAddonsAttr($value) { + if ($value) { + $string = implode(",", $value); return $string; } } - protected function getTypeTextAttr($value, $data){ + protected function getTypeTextAttr($value, $data) { $hooks_type = config('hooks_type'); return $hooks_type[$data['type']]; } /** - * 处理钩子挂载插件排序 - */ - public function getaddons($addons = ''){ + * 处理钩子挂载插件排序 + */ + public function getaddons($addons = '') { if ($addons) { - $hook_list = explode(',',$addons); + $hook_list = explode(',', $addons); foreach ($hook_list as $key => $value) { - $field_list[] = array('id'=>$value,'title'=>$value,'name'=>$value,'is_show'=>1); + $field_list[] = array('id' => $value, 'title' => $value, 'name' => $value, 'is_show' => 1); } - $option[1] = array('name'=>'钩子挂载排序','list'=>$field_list); - }else{ - $option[] = array('name'=>'钩子挂载排序','list'=>array()); + $option[1] = array('name' => '钩子挂载排序', 'list' => $field_list); + } else { + $option[] = array('name' => '钩子挂载排序', 'list' => array()); } foreach ($this->keyList as $key => $value) { if ($value['name'] == 'addons') { @@ -66,53 +66,58 @@ class Hooks extends Base { return $keylist; } - public function addHooks($addons_name){ - $addons_class = get_addon_class($addons_name);//获取插件名 - if(!class_exists($addons_class)){ + public function addHooks($addons_name) { + $addons_class = get_addon_class($addons_name); //获取插件名 + if (!class_exists($addons_class)) { $this->error = "未实现{$addons_name}插件的入口文件"; return false; } - $methods = get_class_methods($addons_class); + $methods = array_diff(get_class_methods($addons_class), get_class_methods('\app\common\controller\Addons')); + $methods = array_diff($methods, array('install', 'uninstall')); foreach ($methods as $item) { - if ('Addon' === substr($item, -5, 5)) { - $info = $this->where('name', substr($item, 0, -5))->find(); - if (null == $info) { - $save = array( - 'name' => $addons_name, - 'description' => '', - 'type' => 1, - 'addons' => array($addons_name), - 'update_time' => time(), - 'status' => 1 - ); - self::create($save); - }else{ - if ($info['addons']) { - $addons = explode(',', $info['addons']); - array_push($addons, substr($item, 0, -5)); - }else{ - $addons = array(substr($item, 0, -5)); - } - $this->where('name', $addons_name)->setField('addons', $addons); + $info = $this->where('name', $item)->find(); + if (null == $info) { + $save = array( + 'name' => $item, + 'description' => '', + 'type' => 1, + 'addons' => array($addons_name), + 'update_time' => time(), + 'status' => 1, + ); + self::create($save); + } else { + if ($info['addons']) { + $addons = explode(',', $info['addons']); + array_push($addons, $addons_name); + } else { + $addons = array($addons_name); } + $this->where('name', $item)->setField('addons', implode(',', $addons)); } } return true; } - public function removeHooks($addons_name){ - $addons_class = get_addon_class($addons_name);//获取插件名 - if(!class_exists($addons_class)){ + public function removeHooks($addons_name) { + $addons_class = get_addon_class($addons_name); //获取插件名 + if (!class_exists($addons_class)) { $this->error = "未实现{$addons_name}插件的入口文件"; return false; } - $row = $this->where(array('addons'=>array('like',$addons_name)))->select(); - foreach ($row as $key => $value) { - $value['addons'] = explode(',', $info['addons']); - $key = array_search($addons_name, $value['addons']); - unset($value['addons'][$key]); - $addons = $value['addons']; - $this->where('id', $value['id'])->setField('addons', $addons); + $row = $this->where(array('addons' => array('like', '%' . $addons_name . '%')))->select(); + foreach ($row as $value) { + if ($addons_name === $value['addons']) { + $this->where('id', $value['id'])->delete(); + } else { + $addons = explode(',', $value['addons']); + $key = array_search($addons_name, $addons); + if ($key) { + unset($value['addons'][$key]); + $addons = implode(',', $addons); + $this->where('id', $value['id'])->setField('addons', $addons); + } + } } return true; }