Hook.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use app\admin\model\HookPlugin;
  13. use app\common\builder\ZBuilder;
  14. use app\admin\model\Hook as HookModel;
  15. use app\admin\model\HookPlugin as HookPluginModel;
  16. /**
  17. * 钩子控制器
  18. * @package app\admin\controller
  19. */
  20. class Hook extends Admin
  21. {
  22. /**
  23. * 钩子管理
  24. * @author 蔡伟明 <314013107@qq.com>
  25. * @return mixed
  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. */
  91. public function edit($id = 0)
  92. {
  93. if ($id === 0) $this->error('参数错误');
  94. // 保存数据
  95. if ($this->request->isPost()) {
  96. $data = $this->request->post();
  97. // 验证
  98. $result = $this->validate($data, 'Hook');
  99. if(true !== $result) $this->error($result);
  100. if ($hook = HookModel::update($data)) {
  101. // 调整插件顺序
  102. if ($data['sort'] != '') {
  103. HookPluginModel::sort($data['name'], $data['sort']);
  104. }
  105. cache('hook_plugins', null);
  106. // 记录行为
  107. action_log('hook_edit', 'admin_hook', $hook['id'], UID, $data['name']);
  108. $this->success('编辑成功', 'index');
  109. } else {
  110. $this->error('编辑失败');
  111. }
  112. }
  113. // 获取数据
  114. $info = HookModel::get($id);
  115. // 该钩子的所有插件
  116. $hooks = HookPluginModel::where('hook', $info['name'])->order('sort')->column('plugin');
  117. $hooks = parse_array($hooks);
  118. // 使用ZBuilder快速创建表单
  119. return ZBuilder::make('form')
  120. ->setPageTitle('编辑')
  121. ->addHidden('id')
  122. ->addText('name', '钩子名称', '由字母和下划线组成,如:<code>page_tips</code>')
  123. ->addText('description', '钩子描述')
  124. ->addSort('sort', '插件排序', '', $hooks)
  125. ->setFormData($info)
  126. ->fetch();
  127. }
  128. /**
  129. * 快速编辑(启用/禁用)
  130. * @param string $status 状态
  131. * @author 蔡伟明 <314013107@qq.com>
  132. * @return mixed
  133. */
  134. public function quickEdit($status = '')
  135. {
  136. $id = $this->request->post('pk');
  137. $status = $this->request->param('value');
  138. $hook_name = HookModel::where('id', $id)->value('name');
  139. if (false === HookPluginModel::where('hook', $hook_name)->setField('status', $status == 'true' ? 1 : 0)) {
  140. $this->error('操作失败,请重试');
  141. }
  142. cache('hook_plugins', null);
  143. $details = $status == 'true' ? '启用钩子' : '禁用钩子';
  144. return parent::quickEdit(['hook_edit', 'admin_hook', $id, UID, $details]);
  145. }
  146. /**
  147. * 启用
  148. * @param array $record 行为日志内容
  149. * @author 蔡伟明 <314013107@qq.com>
  150. * @return mixed
  151. */
  152. public function enable($record = [])
  153. {
  154. return $this->setStatus('enable');
  155. }
  156. /**
  157. * 禁用
  158. * @param array $record 行为日志内容
  159. * @author 蔡伟明 <314013107@qq.com>
  160. * @return mixed
  161. */
  162. public function disable($record = [])
  163. {
  164. return $this->setStatus('disable');
  165. }
  166. /**
  167. * 删除钩子
  168. * @param array $record 行为日志内容
  169. * @author 蔡伟明 <314013107@qq.com>
  170. * @return mixed
  171. */
  172. public function delete($record = [])
  173. {
  174. $ids = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
  175. $map['id'] = ['in', $ids];
  176. $map['system'] = 1;
  177. if (HookModel::where($map)->find()) {
  178. $this->error('禁止删除系统钩子');
  179. }
  180. return $this->setStatus('delete');
  181. }
  182. /**
  183. * 设置状态
  184. * @param string $type 类型
  185. * @param array $record 行为日志内容
  186. * @author 蔡伟明 <314013107@qq.com>
  187. * @return mixed
  188. */
  189. public function setStatus($type = '', $record = [])
  190. {
  191. $ids = $this->request->param('ids/a');
  192. foreach ($ids as $id) {
  193. $hook_name = HookModel::where('id', $id)->value('name');
  194. if (false === HookPluginModel::where('hook', $hook_name)->setField('status', $type == 'enable' ? 1 : 0)) {
  195. $this->error('操作失败,请重试');
  196. }
  197. }
  198. cache('hook_plugins', null);
  199. $hook_delete = is_array($ids) ? '' : $ids;
  200. $hook_names = HookModel::where('id', 'in', $ids)->column('name');
  201. return parent::setStatus($type, ['hook_'.$type, 'admin_hook', $hook_delete, UID, implode('、', $hook_names)]);
  202. }
  203. }