ModuleController.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. namespace Admin\Controller;
  3. use Util\Sql;
  4. /**
  5. * 功能模块控制器
  6. *
  7. */
  8. class ModuleController extends AdminController
  9. {
  10. /**
  11. * 默认方法
  12. *
  13. */
  14. public function index()
  15. {
  16. $module_object = D('Module');
  17. $data_list = $module_object->getAll();
  18. // 使用Builder快速建立列表页面。
  19. $builder = new \Common\Builder\ListBuilder();
  20. $builder->setMetaTitle('模块列表') // 设置页面标题
  21. ->addTopButton('resume') // 添加启用按钮
  22. ->addTopButton('forbid') // 添加禁用按钮
  23. ->setSearch('请输入ID/标题', U('index'))
  24. ->addTableColumn('name', '名称')
  25. ->addTableColumn('title', '标题')
  26. ->addTableColumn('description', '描述')
  27. ->addTableColumn('developer', '开发者')
  28. ->addTableColumn('version', '版本')
  29. ->addTableColumn('create_time', '创建时间', 'time')
  30. ->addTableColumn('status_icon', '状态', 'text')
  31. ->addTableColumn('right_button', '操作', 'btn')
  32. ->setTableDataList($data_list) // 数据列表
  33. ->display();
  34. }
  35. /**
  36. * 检查模块依赖
  37. *
  38. */
  39. public function checkDependence($dependences)
  40. {
  41. if (is_array($dependences)) {
  42. foreach ($dependences as $key => $val) {
  43. $con['name'] = $key;
  44. $module_info = D('Module')->where($con)->find();
  45. if (!$module_info) {
  46. $this->error('该模块依赖' . $key . '模块');
  47. }
  48. if (version_compare($module_info['version'], $val) >= 0) {
  49. continue;
  50. } else {
  51. $this->error($module_info['title'] . '模块版本不得低于v' . $val);
  52. return false;
  53. }
  54. }
  55. return true;
  56. }
  57. }
  58. /**
  59. * 安装模块之前
  60. *
  61. */
  62. public function install_before($name)
  63. {
  64. // 使用FormBuilder快速建立表单页面。
  65. $builder = new \Common\Builder\FormBuilder();
  66. $builder->setMetaTitle('准备安装模块') // 设置页面标题
  67. ->setPostUrl(U('install')) // 设置表单提交地址
  68. ->addFormItem('name', 'hidden', 'name', 'name')
  69. ->addFormItem('clear', 'radio', '是否清除历史数据', '是否清除历史数据', array('1' => '是', '0' => '否'))
  70. ->setFormData(array('name' => $name))
  71. ->display();
  72. }
  73. /**
  74. * 安装模块
  75. *
  76. */
  77. public function install($name, $clear = true)
  78. {
  79. // 获取当前模块信息
  80. $config_file = realpath(APP_PATH . $name) . '/'
  81. . D('Module')->install_file();
  82. if (!$config_file) {
  83. $this->error('安装失败');
  84. }
  85. $config_info = include $config_file;
  86. $data = $config_info['info'];
  87. // 处理模块配置
  88. if ($config_info['config']) {
  89. $temp_arr = $config_info['config'];
  90. foreach ($temp_arr as $key => $value) {
  91. if ($value['type'] == 'group') {
  92. foreach ($value['options'] as $gkey => $gvalue) {
  93. foreach ($gvalue['options'] as $ikey => $ivalue) {
  94. $config[$ikey] = $ivalue['value'];
  95. }
  96. }
  97. } else {
  98. $config[$key] = $temp_arr[$key]['value'];
  99. }
  100. }
  101. $data['config'] = json_encode($config);
  102. } else {
  103. $data['config'] = '';
  104. }
  105. // 检查依赖
  106. if ($data['dependences']) {
  107. $result = $this->checkDependence($data['dependences']);
  108. if (!$result) {
  109. return false;
  110. }
  111. }
  112. // 获取后台菜单
  113. if ($config_info['admin_menu']) {
  114. // 将key值赋给id
  115. foreach ($config_info['admin_menu'] as $key => &$val) {
  116. $val['id'] = (string) $key;
  117. }
  118. $data['admin_menu'] = json_encode($config_info['admin_menu']);
  119. }
  120. // 获取用户中心导航
  121. if ($config_info['user_nav']) {
  122. $data['user_nav'] = json_encode($config_info['user_nav']);
  123. } else {
  124. $data['user_nav'] = '';
  125. }
  126. // 安装数据库
  127. $sql_object = new Sql();
  128. $uninstall_sql_status = true;
  129. // 清除旧数据
  130. if ($clear) {
  131. $sql_file = realpath(APP_PATH . $name) . '/Sql/uninstall.sql';
  132. $uninstall_sql_status = $sql_object->execute_sql_from_file($sql_file);
  133. }
  134. // 安装新数据表
  135. if (!$uninstall_sql_status) {
  136. $this->error('安装失败');
  137. }
  138. $sql_file = realpath(APP_PATH . $name) . '/Sql/install.sql';
  139. $sql_status = $sql_object->execute_sql_from_file($sql_file);
  140. if ($sql_status) {
  141. // 写入数据库记录
  142. $module_object = D('Module');
  143. $data = $module_object->create($data);
  144. if ($data) {
  145. $id = $module_object->add($data);
  146. if ($id) {
  147. // 安装成功后自动在前台新增导航
  148. $nav_data['group'] = 'top';
  149. $nav_data['title'] = $data['title'];
  150. $nav_data['type'] = 'module';
  151. $nav_data['value'] = $data['name'];
  152. $nav_data['icon'] = $data['icon'] ?: '';
  153. $nav_object = D('Nav');
  154. $nav_data_created = $nav_object->create($nav_data);
  155. if ($nav_data_created) {
  156. $nav_add_result = $nav_object->add($nav_data_created);
  157. }
  158. $this->success('安装成功', U('index'));
  159. } else {
  160. $this->error('安装失败');
  161. }
  162. } else {
  163. $this->error($module_object->getError());
  164. }
  165. } else {
  166. $sql_file = realpath(APP_PATH . $name) . '/Sql/uninstall.sql';
  167. $sql_status = $sql_object->execute_sql_from_file($sql_file);
  168. $this->error('安装失败');
  169. }
  170. }
  171. /**
  172. * 卸载模块之前
  173. *
  174. */
  175. public function uninstall_before($id)
  176. {
  177. // 使用FormBuilder快速建立表单页面。
  178. $builder = new \Common\Builder\FormBuilder();
  179. $builder->setMetaTitle('准备卸载模块') // 设置页面标题
  180. ->setPostUrl(U('uninstall')) // 设置表单提交地址
  181. ->addFormItem('id', 'hidden', 'ID', 'ID')
  182. ->addFormItem('clear', 'radio', '是否清除数据', '是否清除数据', array('1' => '是', '0' => '否'))
  183. ->setFormData(array('id' => $id))
  184. ->display();
  185. }
  186. /**
  187. * 卸载模块
  188. *
  189. */
  190. public function uninstall($id, $clear = false)
  191. {
  192. $module_object = D('Module');
  193. $module_info = $module_object->find($id);
  194. if ($module_info['is_system'] === '1') {
  195. $this->error('系统模块不允许卸载!');
  196. }
  197. $result = $module_object->delete($id);
  198. if ($result) {
  199. if ($clear) {
  200. $sql_object = new Sql();
  201. $sql_file = realpath(APP_PATH . $module_info['name']) . '/Sql/uninstall.sql';
  202. $sql_status = $sql_object->execute_sql_from_file($sql_file);
  203. if ($sql_status) {
  204. $this->success('卸载成功,相关数据彻底删除!', U('index'));
  205. }
  206. } else {
  207. $this->success('卸载成功,相关数据未卸载!', U('index'));
  208. }
  209. } else {
  210. $this->error('卸载失败', U('index'));
  211. }
  212. }
  213. /**
  214. * 更新模块信息
  215. *
  216. */
  217. public function updateInfo($id)
  218. {
  219. $module_object = D('Module');
  220. $name = $module_object->getFieldById($id, 'name');
  221. $config_file = realpath(APP_PATH . $name) . '/' . D('Module')->install_file();
  222. if (!$config_file) {
  223. $this->error('不存在安装文件');
  224. }
  225. $config_info = include $config_file;
  226. $data = $config_info['info'];
  227. // 读取数据库已有配置
  228. $db_moduel_config = D('Module')->getFieldByName($name, 'config');
  229. $db_moduel_config = json_decode($db_moduel_config, true);
  230. // 处理模块配置
  231. if ($config_info['config']) {
  232. $temp_arr = $config_info['config'];
  233. foreach ($temp_arr as $key => $value) {
  234. if ($value['type'] == 'group') {
  235. foreach ($value['options'] as $gkey => $gvalue) {
  236. foreach ($gvalue['options'] as $ikey => $ivalue) {
  237. $config[$ikey] = $ivalue['value'];
  238. }
  239. }
  240. } else {
  241. if (isset($db_moduel_config[$key])) {
  242. $config[$key] = $db_moduel_config[$key];
  243. } else {
  244. $config[$key] = $temp_arr[$key]['value'];
  245. }
  246. }
  247. }
  248. $data['config'] = json_encode($config);
  249. } else {
  250. $data['config'] = '';
  251. }
  252. // 获取后台菜单
  253. if ($config_info['admin_menu']) {
  254. // 将key值赋给id
  255. foreach ($config_info['admin_menu'] as $key => &$val) {
  256. $val['id'] = (string) $key;
  257. }
  258. $data['admin_menu'] = json_encode($config_info['admin_menu']);
  259. }
  260. // 获取用户中心导航
  261. if ($config_info['user_nav']) {
  262. $data['user_nav'] = json_encode($config_info['user_nav']);
  263. } else {
  264. $data['user_nav'] = '';
  265. }
  266. $data['id'] = $id;
  267. $data = $module_object->create($data);
  268. if ($data) {
  269. $id = $module_object->save($data);
  270. if ($id) {
  271. $this->success('更新成功', U('index'));
  272. } else {
  273. $this->error('更新失败');
  274. }
  275. } else {
  276. $this->error($module_object->getError());
  277. }
  278. }
  279. /**
  280. * 设置一条或者多条数据的状态
  281. *
  282. */
  283. public function setStatus($model = CONTROLLER_NAME, $script = false)
  284. {
  285. $ids = I('request.ids');
  286. if (is_array($ids)) {
  287. foreach ($ids as $id) {
  288. $is_system = D($model)->getFieldById($id, 'is_system');
  289. if ($is_system) {
  290. $this->error('系统模块不允许操作');
  291. }
  292. }
  293. } else {
  294. $is_system = D($model)->getFieldById($ids, 'is_system');
  295. if ($is_system) {
  296. $this->error('系统模块不允许操作');
  297. }
  298. }
  299. parent::setStatus($model);
  300. }
  301. }