Hook.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2019 广东卓锐软件有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\controller;
  10. use app\admin\model\HookPlugin;
  11. use app\common\builder\ZBuilder;
  12. use app\admin\model\Hook as HookModel;
  13. use app\admin\model\HookPlugin as HookPluginModel;
  14. /**
  15. * 钩子控制器
  16. * @package app\admin\controller
  17. */
  18. class Hook extends Admin
  19. {
  20. /**
  21. * 钩子管理
  22. * @author 蔡伟明 <314013107@qq.com>
  23. * @return mixed
  24. * @throws \think\Exception
  25. * @throws \think\exception\DbException
  26. */
  27. public function index()
  28. {
  29. $map = $this->getMap();
  30. $order = $this->getOrder();
  31. // 数据列表
  32. $data_list = HookModel::where($map)->order($order)->paginate();
  33. // 分页数据
  34. $page = $data_list->render();
  35. // 使用ZBuilder快速创建数据表格
  36. return ZBuilder::make('table')
  37. ->setPageTitle('钩子管理') // 设置页面标题
  38. ->setSearch(['name' => '钩子名称']) // 设置搜索框
  39. ->addColumns([ // 批量添加数据列
  40. ['name', '名称'],
  41. ['description', '描述'],
  42. ['plugin', '所属插件', 'callback', function($plugin){
  43. return $plugin == '' ? '系统' : $plugin;
  44. }],
  45. ['system', '系统钩子', 'yesno'],
  46. ['status', '状态', 'switch'],
  47. ['right_button', '操作', 'btn']
  48. ])
  49. ->addOrder('name,status')
  50. ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
  51. ->addRightButtons('edit,delete') // 批量添加右侧按钮
  52. ->setRowList($data_list) // 设置表格数据
  53. ->setPages($page) // 设置分页数据
  54. ->fetch(); // 渲染模板
  55. }
  56. /**
  57. * 新增
  58. * @author 蔡伟明 <314013107@qq.com>
  59. */
  60. public function add()
  61. {
  62. // 保存数据
  63. if ($this->request->isPost()) {
  64. // 表单数据
  65. $data = $this->request->post();
  66. $data['system'] = 1;
  67. // 验证
  68. $result = $this->validate($data, 'Hook');
  69. if(true !== $result) $this->error($result);
  70. if ($hook = HookModel::create($data)) {
  71. cache('hook_plugins', null);
  72. // 记录行为
  73. action_log('hook_add', 'admin_hook', $hook['id'], UID, $data['name']);
  74. $this->success('新增成功', 'index');
  75. } else {
  76. $this->error('新增失败');
  77. }
  78. }
  79. // 使用ZBuilder快速创建表单
  80. return ZBuilder::make('form')
  81. ->setPageTitle('新增')
  82. ->addText('name', '钩子名称', '由字母和下划线组成,如:<code>page_tips</code>')
  83. ->addText('description', '钩子描述')
  84. ->fetch();
  85. }
  86. /**
  87. * 编辑
  88. * @param int $id 钩子id
  89. * @author 蔡伟明 <314013107@qq.com>
  90. * @return mixed
  91. * @throws \think\Exception
  92. */
  93. public function edit($id = 0)
  94. {
  95. if ($id === 0) $this->error('参数错误');
  96. // 保存数据
  97. if ($this->request->isPost()) {
  98. $data = $this->request->post();
  99. // 验证
  100. $result = $this->validate($data, 'Hook');
  101. if(true !== $result) $this->error($result);
  102. if ($hook = HookModel::update($data)) {
  103. // 调整插件顺序
  104. if ($data['sort'] != '') {
  105. HookPluginModel::sort($data['name'], $data['sort']);
  106. }
  107. cache('hook_plugins', null);
  108. // 记录行为
  109. action_log('hook_edit', 'admin_hook', $hook['id'], UID, $data['name']);
  110. $this->success('编辑成功', 'index');
  111. } else {
  112. $this->error('编辑失败');
  113. }
  114. }
  115. // 获取数据
  116. $info = HookModel::get($id);
  117. // 该钩子的所有插件
  118. $hooks = HookPluginModel::where('hook', $info['name'])->order('sort')->column('plugin');
  119. $hooks = parse_array($hooks);
  120. // 使用ZBuilder快速创建表单
  121. return ZBuilder::make('form')
  122. ->setPageTitle('编辑')
  123. ->addHidden('id')
  124. ->addText('name', '钩子名称', '由字母和下划线组成,如:<code>page_tips</code>')
  125. ->addText('description', '钩子描述')
  126. ->addSort('sort', '插件排序', '', $hooks)
  127. ->setFormData($info)
  128. ->fetch();
  129. }
  130. /**
  131. * 快速编辑(启用/禁用)
  132. * @param string $status 状态
  133. * @author 蔡伟明 <314013107@qq.com>
  134. * @return mixed
  135. */
  136. public function quickEdit($status = '')
  137. {
  138. $id = $this->request->post('pk');
  139. $status = $this->request->param('value');
  140. $hook_name = HookModel::where('id', $id)->value('name');
  141. if (false === HookPluginModel::where('hook', $hook_name)->setField('status', $status == 'true' ? 1 : 0)) {
  142. $this->error('操作失败,请重试');
  143. }
  144. cache('hook_plugins', null);
  145. $details = $status == 'true' ? '启用钩子' : '禁用钩子';
  146. return parent::quickEdit(['hook_edit', 'admin_hook', $id, UID, $details]);
  147. }
  148. /**
  149. * 启用
  150. * @param array $record 行为日志内容
  151. * @author 蔡伟明 <314013107@qq.com>
  152. * @throws \think\Exception
  153. * @throws \think\exception\PDOException
  154. */
  155. public function enable($record = [])
  156. {
  157. return $this->setStatus('enable');
  158. }
  159. /**
  160. * 禁用
  161. * @param array $record 行为日志内容
  162. * @author 蔡伟明 <314013107@qq.com>
  163. * @return mixed
  164. */
  165. /**
  166. * 禁用
  167. * @param array $record 行为日志内容
  168. * @author 蔡伟明 <314013107@qq.com>
  169. * @throws \think\Exception
  170. * @throws \think\exception\PDOException
  171. */
  172. public function disable($record = [])
  173. {
  174. return $this->setStatus('disable');
  175. }
  176. /**
  177. * 删除钩子
  178. * @param array $record 行为日志内容
  179. * @author 蔡伟明 <314013107@qq.com>
  180. * @throws \think\Exception
  181. * @throws \think\db\exception\DataNotFoundException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. * @throws \think\exception\DbException
  184. * @throws \think\exception\PDOException
  185. */
  186. public function delete($record = [])
  187. {
  188. $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
  189. $map = [
  190. ['id', 'in', $ids],
  191. ['system', '=', 1],
  192. ];
  193. if (HookModel::where($map)->find()) {
  194. $this->error('禁止删除系统钩子');
  195. }
  196. return $this->setStatus('delete');
  197. }
  198. /**
  199. * 设置状态
  200. * @param string $type 类型
  201. * @param array $record 行为日志内容
  202. * @author 蔡伟明 <314013107@qq.com>
  203. * @throws \think\Exception
  204. * @throws \think\exception\PDOException
  205. */
  206. public function setStatus($type = '', $record = [])
  207. {
  208. $ids = $this->request->param('ids/a');
  209. foreach ($ids as $id) {
  210. $hook_name = HookModel::where('id', $id)->value('name');
  211. if (false === HookPluginModel::where('hook', $hook_name)->setField('status', $type == 'enable' ? 1 : 0)) {
  212. $this->error('操作失败,请重试');
  213. }
  214. }
  215. cache('hook_plugins', null);
  216. $hook_delete = is_array($ids) ? '' : $ids;
  217. $hook_names = HookModel::where('id', 'in', $ids)->column('name');
  218. return parent::setStatus($type, ['hook_'.$type, 'admin_hook', $hook_delete, UID, implode('、', $hook_names)]);
  219. }
  220. }