ModelConfigEditorController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. namespace Addons\ModelConfigEditor\Controller;
  3. use Admin\Controller\AddonController;
  4. use Util\Tree;
  5. class ModelConfigEditorController extends AddonController
  6. {
  7. // 模块菜单列表
  8. public function menus()
  9. {
  10. $module_id = I('request.module_id');
  11. $this->assign('module_id', $module_id);
  12. $module_object = M('admin_module');
  13. $module = $module_object->find($module_id);
  14. if (empty($module)) {
  15. $this->error('插件不存在,请重新安装');
  16. } else {
  17. $menus = $this->getMenus($module_id);
  18. // 转换成树状列表
  19. $tree = new Tree();
  20. $data_list = $tree->array2tree($menus);
  21. // 使用Builder快速建立列表页面。
  22. $builder = new \Common\Builder\ListBuilder();
  23. $builder->setMetaTitle('添加菜单') // 设置页面标题
  24. ->addTopButton('addnew', ['href' => addons_url('ModelConfigEditor://ModelConfigEditor/add', ['module_id' => $module_id]),
  25. ]) // 添加新增按钮
  26. // ->addTopButton('delete') // 添加删除按钮
  27. ->addTableColumn('id', 'ID')
  28. ->addTableColumn('icon', '图标', 'icon')
  29. ->addTableColumn('title_show', '标题')
  30. ->addTableColumn('right_button', '操作', 'btn')
  31. ->setTableDataList($data_list) // 数据列表
  32. ->addRightButton('self', [
  33. 'href' => addons_url('ModelConfigEditor://ModelConfigEditor/add', ['pid' => '__data_id__', 'module_id' => $module_id]),
  34. 'title' => '添加子菜单',
  35. 'class' => 'label label-primary',
  36. ]
  37. ) // 添加编辑按钮
  38. ->addRightButton('edit', [
  39. 'href' => addons_url('ModelConfigEditor://ModelConfigEditor/edit', ['id' => '__data_id__', 'module_id' => $module_id]),
  40. ]
  41. ) // 添加编辑按钮
  42. ->addRightButton('delete', [
  43. 'href' => addons_url('ModelConfigEditor://ModelConfigEditor/delete', ['id' => '__data_id__', 'module_id' => $module_id]),
  44. ]
  45. ) // 添加删除按钮
  46. ->display();
  47. }
  48. }
  49. public function add()
  50. {
  51. $module_id = I('request.module_id');
  52. $data = I('post.');
  53. if (isset($data['icon']) && stripos($data['icon'], 'fa ') === false) {
  54. $data['icon'] = 'fa ' . $data['icon'];
  55. }
  56. $menus = $this->getMenus($module_id);
  57. if (IS_POST) {
  58. $ids = i_array_column($menus, 'id');
  59. asort($ids, SORT_NUMERIC);
  60. $next_id = end($ids) + 1;
  61. $menus[$next_id] = $data;
  62. if (1 == $data['pid']) {
  63. $data['icon'] = 'fa fa-folder-open-o';
  64. }
  65. $this->saveModule($module_id, $menus);
  66. } else {
  67. // 使用FormBuilder快速建立表单页面。
  68. $builder = new \Common\Builder\FormBuilder();
  69. $builder->setMetaTitle('新增链接') // 设置页面标题
  70. ->setPostUrl(addons_url('ModelConfigEditor://ModelConfigEditor/add', ['module_id' => $module_id])) // 设置表单提交地址
  71. ->addFormItem('pid', 'select', '上级链接', '上级链接', $this->list_as_tree($menus, '顶级链接'))
  72. ->addFormItem('title', 'text', '链接标题', '链接前台显示标题')
  73. ->addFormItem('url', 'text', '请填写外链URL地址', '支持http://格式或者TP的U函数解析格式')
  74. ->addFormItem('icon', 'icon', '图标', '链接图标')
  75. ->setFormData(['pid' => I('pid', 0)])
  76. ->display();
  77. }
  78. }
  79. // 编辑菜单
  80. public function edit()
  81. {
  82. $id = I('id');
  83. $module_id = I('get.module_id');
  84. if (IS_POST) {
  85. $menus = $this->getMenus($module_id);
  86. $menus[$id] = I('post.');
  87. if (stripos($menus[$id]['icon'], 'fa ') === false) {
  88. $menus[$id]['icon'] = 'fa ' . $menus[$id]['icon'];
  89. }
  90. //第一层节点 的图标固定为目录展开图标
  91. if (1 == $menus[$id]['pid']) {
  92. $menus[$id]['icon'] = 'fa fa-folder-open-o';
  93. }
  94. $this->saveModule($module_id, $menus);
  95. } else {
  96. $menus = $this->getMenus($module_id);
  97. $info = $menus[$id] ?: null;
  98. // 使用FormBuilder快速建立表单页面。
  99. $builder = new \Common\Builder\FormBuilder();
  100. $builder->setMetaTitle('编辑链接') // 设置页面标题
  101. ->setPostUrl(addons_url('ModelConfigEditor://ModelConfigEditor/edit', ['id' => $id, 'module_id' => $module_id])) // 设置表单提交地址
  102. ->addFormItem('id', 'hidden', 'ID', 'ID')
  103. ->addFormItem('pid', 'select', '上级链接', '上级链接', $this->list_as_tree($menus, '顶级链接'))
  104. ->addFormItem('title', 'text', '链接标题', '链接前台显示标题')
  105. ->addFormItem('url', 'text', '请填写外链URL地址', '支持http://格式或者TP的U函数解析格式')
  106. ->addFormItem('icon', 'icon', '图标', '链接图标')
  107. ->setFormData($info)
  108. ->display();
  109. }
  110. }
  111. //删除菜单
  112. public function delete()
  113. {
  114. $id = I('request.id');
  115. if (1 == $id) {
  116. $this->error('根节点菜单不能删除');
  117. } else {
  118. $module_id = I('request.module_id');
  119. $menus = $this->getMenus($module_id);
  120. unset($menus[$id]);
  121. $this->saveModule($module_id, $menus);
  122. }
  123. }
  124. // 保存菜单配置
  125. public function saveModule($id, $data)
  126. {
  127. if ($module = $this->getModule($id)) {
  128. // dump($module);
  129. $config_file = APP_PATH . $module['name'] . '/opencmf.php';
  130. if (is_writeable($config_file)) {
  131. $old_config = include $config_file;
  132. $new_config = array_merge($old_config, ['admin_menu' => $data]);
  133. if (!$new_config) {
  134. $this->error('合并配置失败');
  135. } else {
  136. $config_source = var_export($new_config, true);
  137. $config_file_str = <<<PHP
  138. <?php
  139. // 模块信息配置
  140. return {$config_source}
  141. ;
  142. PHP;
  143. $addon_index = addons_url('ModelConfigEditor://ModelConfigEditor/menus', ['id' => $id, 'module_id' => $module['id']]);
  144. if (file_put_contents($config_file, $config_file_str)) {
  145. $ret = $this->updateinfo($id);
  146. if (true === $ret) {
  147. // 清空所有缓存
  148. $cache = \Think\Cache::getInstance();
  149. $cache->clear();
  150. $this->success('更新配置成功', $addon_index);
  151. } else {
  152. $this->error('更新失败:' . $ret);
  153. }
  154. } else {
  155. $this->error('更新配置失败');
  156. }
  157. }
  158. } else {
  159. $this->error("{$module['name']}下的opencmf.php 配置文件不可写");
  160. }
  161. } else {
  162. $this->error('保存失败,模型数据获取不到');
  163. }
  164. }
  165. // 获取模块名称
  166. public function getModuleName($id)
  167. {
  168. if (!$module = $this->getModule($id)) {
  169. return false;
  170. } else {
  171. return $module['name'];
  172. }
  173. }
  174. // 获取模块路径
  175. public function getModulePath($id)
  176. {
  177. if ($name = $this->getModuleName($id)) {
  178. return APP_PATH . $module['name'];
  179. } else {
  180. return false;
  181. }
  182. }
  183. // 获取全部模块
  184. public function getMenus($module_id)
  185. {
  186. $module = $this->getModule($module_id);
  187. $module_path = APP_PATH . $module['name'];
  188. $config = include $module_path . '/opencmf.php';
  189. $menus = $config['admin_menu'];
  190. foreach ($menus as $key => &$value) {
  191. $value['id'] = $key;
  192. }
  193. return $menus;
  194. }
  195. // 获取模块
  196. public function getModule($module_id)
  197. {
  198. $module_object = M('admin_module');
  199. $module = $module_object->find($module_id);
  200. return $module;
  201. }
  202. // 更新数据库模块菜单缓存
  203. public function updateInfo($id)
  204. {
  205. $module_object = M('admin_module');
  206. $record = $module_object->find($id);
  207. $name = $record['name'];
  208. $config_file = realpath(APP_PATH . $name) . '/'
  209. . D('Module')->install_file();
  210. if (!$config_file) {
  211. $this->error('不存在安装文件');
  212. }
  213. $config_info = include $config_file;
  214. $data = $config_info['info'];
  215. // 读取数据库已有配置
  216. $db_moduel_config = $record['config'];
  217. $db_moduel_config = json_decode($db_moduel_config, true);
  218. // 处理模块配置
  219. if (isset($config_info['config'])) {
  220. $temp_arr = $config_info['config'];
  221. foreach ($temp_arr as $key => $value) {
  222. if ($value['type'] == 'group') {
  223. foreach ($value['options'] as $gkey => $gvalue) {
  224. foreach ($gvalue['options'] as $ikey => $ivalue) {
  225. $config[$ikey] = $ivalue['value'];
  226. }
  227. }
  228. } else {
  229. if (isset($db_moduel_config[$key])) {
  230. $config[$key] = $db_moduel_config[$key];
  231. } else {
  232. $config[$key] = isset($temp_arr[$key]['value']) ? $temp_arr[$key]['value'] : '';
  233. }
  234. }
  235. }
  236. $data['config'] = json_encode($config);
  237. } else {
  238. $data['config'] = '';
  239. }
  240. // 获取后台菜单
  241. if ($config_info['admin_menu']) {
  242. // 将key值赋给id
  243. foreach ($config_info['admin_menu'] as $key => &$val) {
  244. $val['id'] = (string) $key;
  245. }
  246. $data['admin_menu'] = json_encode($config_info['admin_menu']);
  247. }
  248. // 获取用户中心导航
  249. if (isset($config_info['user_nav'])) {
  250. $data['user_nav'] = json_encode($config_info['user_nav']);
  251. } else {
  252. $data['user_nav'] = '';
  253. }
  254. $data['id'] = $id;
  255. $data = $module_object->create($data);
  256. if ($data) {
  257. $id = $module_object->save($data);
  258. if (false !== $id) {
  259. return true;
  260. } else {
  261. return '更新失败';
  262. }
  263. } else {
  264. return $module_object->getError();
  265. }
  266. }
  267. // 将树形列表转换为树
  268. public function list_as_tree($list, $extra = null, $key = 'id', $title_field = 'title')
  269. {
  270. //转换成树状列表(非严格模式)
  271. $tree = new \Util\Tree();
  272. $list = $tree->array2tree($list, $title_field, 'id', 'pid', 0, false);
  273. if ($extra) {
  274. $result[0] = $extra;
  275. }
  276. //转换成一维数组
  277. foreach ($list as $val) {
  278. $result[$val[$key]] = $val['title_show'];
  279. }
  280. return $result;
  281. }
  282. }