// +---------------------------------------------------------------------- namespace app\controller\admin; use app\model\Addons as AddonsM; use app\model\Hooks; /** * @title 插件管理 * @description 插件管理 */ class Addons extends Base { /** * @title 插件列表 */ public function index($refresh = 0) { $map = []; if ($refresh) { AddonsM::refreshAddons($this->app->getRootPath() . 'addons' . DIRECTORY_SEPARATOR); } $list = AddonsM::where($map)->order('id desc')->paginate($this->request->pageConfig); $this->data = array( 'list' => $list, 'page' => $list->render(), ); return $this->fetch(); } /** * @title 删除插件 */ public function del() { } /** * @title 安装插件 */ public function install() { $addon_name = input('addon_name', '', 'trim,ucfirst'); $class = get_addon_class($addon_name); if (class_exists($class)) { $addons = new $class; $info = $addons->info; if (!$info || !$addons->checkInfo()) { //检测信息的正确性 return $this->error('插件信息缺失'); } session('addons_install_error', null); $install_flag = $addons->install(); if (!$install_flag) { return $this->error('执行插件预安装操作失败' . session('addons_install_error')); } $result = $this->addons->install($info); if ($result) { cache('hooks', null); return $this->success('安装成功'); } else { return $this->error($this->addons->getError()); } } else { return $this->error('插件不存在'); } } /** * @title 卸载插件 */ public function uninstall($id) { $result = $this->addons->uninstall($id); if ($result === false) { return $this->error($this->addons->getError(), ''); } else { return $this->success('卸载成功!'); } } /** * @title 启用插件 */ public function enable() { $id = input('id'); cache('hooks', null); $model = model('Addons'); $result = $model::where(array('id' => $id))->update(array('status' => 1)); if ($result) { return $this->success('启用成功'); } else { return $this->error("启用失败!"); } } /** * @title 禁用插件 */ public function disable() { $id = input('id'); cache('hooks', null); $model = model('Addons'); $result = $model::where(array('id' => $id))->update(array('status' => 0)); if ($result) { return $this->success('禁用成功'); } else { return $this->error("禁用失败!"); } } /** * @title 设置插件页面 */ public function config() { if ($this->request->isPost()) { # code... } else { $id = input('id', '', 'trim,intval'); if (!$id) { return $this->error("非法操作!"); } $info = $this->addons->find($id); if (!empty($info)) { $class = get_addon_class($info['name']); $keyList = array(); $data = array( 'keyList' => $keyList, ); $this->assign($data); $this->setMeta($info['title'] . " - 设置"); return $this->fetch('public/edit'); } else { return $this->error("未安装此插件!"); } } } /** * @title 检测插件 * 获取插件所需的钩子是否存在,没有则新增 * @param string $str 钩子名称 * @param string $addons 插件名称 * @param string $addons 插件简介 */ public function existHook($str, $addons, $msg = '') { $hook_mod = db('Hooks'); $where['name'] = $str; $gethook = $hook_mod->where($where)->find(); if (!$gethook || empty($gethook) || !is_array($gethook)) { $data['name'] = $str; $data['description'] = $msg; $data['type'] = 1; $data['update_time'] = time(); $data['addons'] = $addons; if (false !== $hook_mod->create($data)) { $hook_mod->add(); } } } /** * @title 删除钩子 * @param string $hook 钩子名称 */ public function deleteHook($hook) { $model = db('hooks'); $condition = array( 'name' => $hook, ); $model->where($condition)->delete(); S('hooks', null); } /** * @title 钩子列表 */ public function hooks() { $map = []; $order = "id desc"; $list = Hooks::where($map)->order($order)->paginate($this->request->pageConfig); $this->data = array( 'list' => $list, 'page' => $list->render(), ); return $this->fetch(); } /** * @title 添加钩子 */ public function addhook() { if ($this->request->isPost()) { $result = $hooks->change(); if ($result !== false) { return $this->success("修改成功"); } else { return $this->error($hooks->getError()); } } else { $this->data = array( 'keyList' => Hooks::$keylist, ); return $this->fetch('admin/public/edit'); } } /** * @title 编辑钩子 */ public function edithook($id) { $hooks = model('Hooks'); if ($this->request->isPost()) { $result = $hooks->change(); if ($result !== false) { return $this->success("修改成功"); } else { return $this->error($hooks->getError()); } } else { $info = db('Hooks')->find($id); $keylist = $hooks->getaddons($info['addons']); $data = array( 'info' => $info, 'keyList' => $keylist, ); $this->assign($data); $this->setMeta('编辑钩子'); return $this->fetch('public/edit'); } } /** * @title 删除钩子 */ public function delhook() { $id = $this->getArrayParam('id'); $map['id'] = array('IN', $id); $result = $this->hooks->where($map)->delete(); if ($result !== false) { return $this->success('删除成功'); } else { return $this->error('删除失败'); } } /** * @title 更新钩子 */ public function updateHook() { $hookModel = D('Hooks'); $data = $hookModel->create(); if ($data) { if ($data['id']) { $flag = $hookModel->save($data); if ($flag !== false) { S('hooks', null); $this->success('更新成功', Cookie('__forward__')); } else { $this->error('更新失败'); } } else { $flag = $hookModel->add($data); if ($flag) { S('hooks', null); $this->success('新增成功', Cookie('__forward__')); } else { $this->error('新增失败'); } } } else { $this->error($hookModel->getError()); } } }