cms_index.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. /**
  3. * cms首页管理
  4. *
  5. *
  6. *
  7. ***/
  8. defined('InShopNC') or exit('Access Invalid!');
  9. class cms_indexControl extends SystemControl{
  10. public function __construct(){
  11. parent::__construct();
  12. Language::read('cms');
  13. }
  14. public function indexOp() {
  15. $this->cms_indexOp();
  16. }
  17. /**
  18. * cms首页管理
  19. */
  20. public function cms_indexOp() {
  21. $model_index_module = Model('cms_index_module');
  22. $module_list = $model_index_module->getList(TRUE, null, 'module_state desc, module_sort asc');
  23. Tpl::output('list', $module_list);
  24. $model_module = Model('cms_module');
  25. $module_list = $model_module->getList(TRUE, null);
  26. Tpl::output('module_list', $module_list);
  27. $this->show_menu('cms_index');
  28. Tpl::showpage('cms_index');
  29. }
  30. /**
  31. * 获取
  32. */
  33. public function get_module_frame_listOp() {
  34. $data = array();
  35. $model_module_frame = Model('cms_module_frame');
  36. $module_frame_list = $model_module_frame->getList(TRUE, null);
  37. $data['frame_list'] = $module_frame_list;
  38. $model_module_assembly = Model('cms_module_assembly');
  39. $module_assembly_list = $model_module_assembly->getList(TRUE, null);
  40. $data['assembly_list'] = $module_assembly_list;
  41. echo json_encode($data);
  42. }
  43. /**
  44. * 模块添加
  45. */
  46. public function add_page_moduleOp() {
  47. $data = array();
  48. $data['result'] = TRUE;
  49. if(empty($_POST['module_name'])) {
  50. $data['result'] = FALSE;
  51. $data['message'] = '参数错误';
  52. } else {
  53. $model_index_module = Model('cms_index_module');
  54. $new_module = array();
  55. $new_module['module_title'] = $_POST['module_title'];
  56. $new_module['module_name'] = $_POST['module_name'];
  57. $new_module['module_type'] = $_POST['module_type'];
  58. $new_module['module_style'] = 'style1';
  59. $new_module['module_view'] = 1;
  60. $result = $model_index_module->save($new_module);
  61. if($result) {
  62. $data['module_id'] = $result;
  63. $data['module_style'] = $new_module['module_style'];
  64. $data['module_view'] = $new_module['module_view'];
  65. } else {
  66. $data['result'] = FALSE;
  67. $data['message'] = '添加失败';
  68. }
  69. }
  70. echo json_encode($data);
  71. }
  72. /**
  73. * 模块删除
  74. */
  75. public function drop_page_moduleOp() {
  76. $data = array();
  77. $data['result'] = TRUE;
  78. $module_id = intval($_POST['module_id']);
  79. if(empty($module_id)) {
  80. $data['result'] = FALSE;
  81. $data['message'] = '参数错误';
  82. } else {
  83. $model_index_module = Model('cms_index_module');
  84. $result = $model_index_module->drop(array('module_id'=>$module_id));
  85. if(!$result) {
  86. $data['result'] = FALSE;
  87. $data['message'] = '删除失败';
  88. }
  89. }
  90. echo json_encode($data);
  91. }
  92. /**
  93. * 启用页面模块
  94. */
  95. public function update_page_module_showOp() {
  96. $update = array('module_state'=>1);
  97. $result = $this->update_page_module($update, $_POST['module_id']);
  98. echo $result;
  99. }
  100. /**
  101. * 关闭页面模块
  102. */
  103. public function update_page_module_hideOp() {
  104. $update = array('module_state'=>0);
  105. $result = $this->update_page_module($update, $_POST['module_id']);
  106. echo $result;
  107. }
  108. /**
  109. * 更新模块标题
  110. */
  111. public function update_page_module_titleOp() {
  112. $new_title = $_POST['value'];
  113. $obj_validate = new Validate();
  114. $obj_validate->validateparam = array(
  115. array('input'=>$new_title,'require'=>'true',"validator"=>"Length","min"=>"1","max"=>"20",'message'=>Language::get('class_name_error')),
  116. );
  117. $error = $obj_validate->validate();
  118. if ($error != ''){
  119. echo json_encode(array('result'=>FALSE,'message'=>'标题名称不能为空且必须小于10个字'));
  120. die;
  121. } else {
  122. $update = array('module_title'=>$new_title);
  123. $result = $this->update_page_module($update, $_POST['id']);
  124. echo $result;
  125. }
  126. }
  127. /**
  128. * 更新模块主题
  129. */
  130. public function update_page_module_styleOp() {
  131. $update = array('module_style'=>$_POST['module_style']);
  132. $result = $this->update_page_module($update, $_POST['module_id']);
  133. echo $result;
  134. }
  135. /**
  136. * 更新模块显示样式
  137. */
  138. public function update_page_module_viewOp() {
  139. $update = array('module_view'=>$_POST['module_view']);
  140. $result = $this->update_page_module($update, $_POST['module_id']);
  141. echo $result;
  142. }
  143. /**
  144. * 更新页面模块
  145. */
  146. private function update_page_module($update, $module_id) {
  147. $data = array();
  148. $data['result'] = TRUE;
  149. $module_id = intval($module_id);
  150. if(empty($module_id)) {
  151. $data['result'] = FALSE;
  152. $data['message'] = '参数错误';
  153. } else {
  154. $model_index_module = Model('cms_index_module');
  155. $result = $model_index_module->modify($update, array('module_id'=>$module_id));
  156. if(!$result) {
  157. $data['result'] = FALSE;
  158. $data['message'] = '操作失败';
  159. }
  160. }
  161. return json_encode($data);
  162. }
  163. /**
  164. * 更新页面模块排序
  165. */
  166. public function update_page_module_indexOp() {
  167. $data = array();
  168. $data['result'] = TRUE;
  169. $page_module_id_string = $_POST['page_module_id_string'];
  170. if(!empty($page_module_id_string)) {
  171. $model_index_module = Model('cms_index_module');
  172. $page_module_id_array = explode(',', $page_module_id_string);
  173. $index = 0;
  174. foreach ($page_module_id_array as $module_id) {
  175. $result = $model_index_module->modify(array('module_sort'=>$index), array('module_id'=>$module_id));
  176. $index++;
  177. }
  178. }
  179. echo json_encode($data);
  180. }
  181. public function cms_index_previewOp() {
  182. $model_index_module = Model('cms_index_module');
  183. $module_list = $model_index_module->getList(array('module_state'=>1), null,'module_sort asc');
  184. for($i=0, $j=count($module_list); $i < $j; $i++) {
  185. $module_list[$i]['module_template'] = $this->get_module_template_path($module_list[$i]);
  186. }
  187. Tpl::output('module_list', $module_list);
  188. Tpl::showpage('cms_index.template', 'null_layout');
  189. }
  190. /**
  191. * 首页静态文件生成
  192. */
  193. public function cms_index_buildOp() {
  194. $data = array();
  195. $data['result'] = TRUE;
  196. $html_path = BASE_UPLOAD_PATH.DS.ATTACH_CMS.DS.'index_html'.DS;
  197. if(!is_dir($html_path)){
  198. if (!@mkdir($html_path, 0755)){
  199. $data = array();
  200. $data['result'] = FALSE;
  201. $data['message'] = Language::get('cms_index_build_fail');
  202. echo json_encode($data);die;
  203. }
  204. }
  205. $model_index_module = Model('cms_index_module');
  206. $module_list = $model_index_module->getList(array('module_state'=>1), null,'module_sort asc');
  207. for($i=0, $j=count($module_list); $i < $j; $i++) {
  208. $module_list[$i]['module_template'] = $this->get_module_template_path($module_list[$i]);
  209. }
  210. Tpl::output('module_list', $module_list);
  211. ob_start();
  212. Tpl::showpage('cms_index.template', 'null_layout');
  213. $result = file_put_contents($html_path.'index.html', ob_get_clean());
  214. if($result) {
  215. $this->log(Language::get('cms_log_index_build'), 1);
  216. $data['message'] = Language::get('cms_index_build_success');
  217. } else {
  218. $this->log(Language::get('cms_log_index_build'), 0);
  219. $data['message'] = Language::get('cms_index_build_fail');
  220. }
  221. echo json_encode($data);die;
  222. }
  223. /**
  224. * 保存自定义模块
  225. */
  226. public function save_moduleOp() {
  227. $data = array();
  228. $data['result'] = TRUE;
  229. if(empty($_POST['frame_name'])) {
  230. $data['result'] = FALSE;
  231. $data['message'] = '参数错误';
  232. echo json_encode($data);die;
  233. }
  234. //插入数据库
  235. $new_module = array();
  236. $new_module['module_title'] = empty($_POST['module_title'])?'自定义模块':$_POST['module_title'];
  237. $new_module['module_name'] = 'model'.strval(time());
  238. $new_module['module_type'] = $_POST['frame_name'];
  239. $new_module['module_class'] = 2;
  240. $model_module = Model('cms_module');
  241. $new_module_id = $model_module->save($new_module);
  242. //生成模板
  243. $model_module_frame = Model('cms_module_frame');
  244. $module_frame = $model_module_frame->getOne(array('frame_name'=>$_POST['frame_name']));
  245. Tpl::output('frame_structure', json_decode($module_frame['frame_structure'], TRUE));
  246. Tpl::output('frame_block', $_POST['frame_block']);
  247. if($_POST['module_display_title_state'] !== 'disable') {
  248. Tpl::output('module_display_title', TRUE);
  249. } else {
  250. Tpl::output('module_display_title', FALSE);
  251. }
  252. $html_path = BASE_UPLOAD_PATH.DS.ATTACH_CMS.DS.'module_template'.DS;
  253. if(!is_dir($html_path)){
  254. if (!@mkdir($html_path, 0755)){
  255. $data = array();
  256. $data['result'] = FALSE;
  257. $data['message'] = Language::get('cms_index_build_fail');
  258. echo json_encode($data);die;
  259. }
  260. }
  261. ob_start();
  262. Tpl::showpage('cms_module.template', 'null_layout');
  263. $result = file_put_contents($html_path.'cms_module.'.$new_module['module_name'].'.php', ob_get_clean());
  264. //返回数据
  265. if($result) {
  266. $new_module['module_id'] = $new_module_id;
  267. $data['module_item'] = $new_module;
  268. echo json_encode($data);die;
  269. } else {
  270. $data['result'] = FALSE;
  271. $data['message'] = '模块添加失败';
  272. echo json_encode($data);die;
  273. }
  274. }
  275. /**
  276. * 删除自定义模块
  277. */
  278. public function drop_moduleOp() {
  279. $data = array();
  280. $data['result'] = TRUE;
  281. $module_id = intval($_POST['module_id']);
  282. if($module_id <= 0) {
  283. $this->return_error('参数错误');
  284. }
  285. $model_cms_module = Model('cms_module');
  286. $module_detail = $model_cms_module->getOne(array('module_id'=>$module_id));
  287. //模块不存在或者是系统自带模块
  288. if(empty($module_detail) || intval($module_detail['module_class']) === 1) {
  289. $this->return_error('参数错误');
  290. }
  291. //删除模块数据库记录
  292. $model_cms_module->drop(array('module_id'=>$module_id));
  293. $model_cms_index_module = Model('cms_index_module');
  294. $model_cms_index_module->drop(array('module_name'=>$module_detail['module_name']));
  295. //删除模板文件
  296. $module_template = BASE_UPLOAD_PATH.DS.ATTACH_CMS.DS.'module_template'.DS.'cms_module.'.$module_detail['module_name'].'.php';
  297. if(is_file($module_template)) {
  298. @unlink($module_template);
  299. }
  300. $data['module_name'] = $module_detail['module_name'];
  301. echo json_encode($data);die;
  302. }
  303. /**
  304. * cms首页模块编辑
  305. */
  306. public function module_editOp() {
  307. $module_id = intval($_GET['module_id']);
  308. if(empty($module_id)) {
  309. showMessage(Language::get('param_error'),'','','error');
  310. }
  311. $model_index_module = Model('cms_index_module');
  312. $module_detail = $model_index_module->getOne(array('module_id'=>$module_id));
  313. Tpl::output('module_detail', $module_detail);
  314. //标签
  315. $model_tag = Model('cms_tag');
  316. $tag_list = $model_tag->getList(TRUE, null, 'tag_sort asc');
  317. Tpl::output('tag_list', $tag_list);
  318. //获取表单组件
  319. Tpl::output('module_template', $this->get_module_template_path($module_detail));
  320. //编辑标志,用于显示模板中的编辑功能
  321. Tpl::output('edit_flag', TRUE);
  322. $this->show_menu('module_edit');
  323. Tpl::showpage('cms_index_module.edit');
  324. }
  325. /**
  326. * cms首页模块保存
  327. */
  328. public function save_page_moduleOp() {
  329. $module_content = array();
  330. foreach($_POST as $key=>$value) {
  331. if($key !== 'module_id' && $key !== 'module_drop_image') {
  332. $module_content[$key] = stripslashes($value);
  333. }
  334. }
  335. if(!empty($_POST['module_drop_image']) && is_array($_POST['module_drop_image'])) {
  336. foreach ($_POST['module_drop_image'] as $value) {
  337. $this->image_drop($value);
  338. }
  339. }
  340. $model_cms_index_module = Model('cms_index_module');
  341. $result = $model_cms_index_module->modify(array('module_content'=>base64_encode(serialize($module_content))), array('module_id'=>$_POST['module_id']));
  342. if($result) {
  343. $this->log(Language::get('cms_log_index_edit').$_POST['module_id'], 1);
  344. showMessage(Language::get('nc_common_save_succ'), 'index.php?act=cms_index&op=cms_index');
  345. } else {
  346. $this->log(Language::get('cms_log_index_edit').$_POST['module_id'], 0);
  347. showMessage(Language::get('nc_common_save_fail'), '', '','error');
  348. }
  349. }
  350. /**
  351. * 首页图片上传
  352. */
  353. public function image_uploadOp() {
  354. $data = array();
  355. $data['status'] = 'success';
  356. if(!empty($_FILES['image_upload']['name'])) {
  357. $upload = new UploadFile();
  358. $upload->set('default_dir',ATTACH_CMS.DS.'index');
  359. $result = $upload->upfile('image_upload');
  360. if(!$result) {
  361. $data['status'] = 'fail';
  362. $data['error'] = $upload->error;
  363. }
  364. $data['file_name'] = $upload->file_name;
  365. $data['file_url'] = getCMSIndexImageUrl($upload->file_name);
  366. }
  367. if (strtoupper(CHARSET) == 'GBK'){
  368. $data = Language::getUTF8($data);//网站GBK使用编码时,转换为UTF-8,防止json输出汉字问题
  369. }
  370. echo json_encode($data);
  371. }
  372. /**
  373. * 首页图片删除
  374. */
  375. private function image_drop($image_name) {
  376. $file = getCMSIndexImagePath($image_name);
  377. if(is_file($file)) {
  378. unlink($file);
  379. }
  380. }
  381. /**
  382. * 获取模块模板路径
  383. */
  384. private function get_module_template_path($module_detail) {
  385. if($module_detail['module_type'] === $module_detail['module_name']) {
  386. return 'cms_index_widget.'.$module_detail['module_type'].'.php';
  387. } else {
  388. return BASE_UPLOAD_PATH.DS.ATTACH_CMS.DS.'module_template'.DS.'cms_module.'.$module_detail['module_name'].'.php';
  389. }
  390. }
  391. /**
  392. * 返回错误信息
  393. */
  394. private function return_error($message) {
  395. $data['result'] = FALSE;
  396. $data['message'] = $message;
  397. echo json_encode($data);die;
  398. }
  399. private function show_menu($menu_key) {
  400. $menu_array = array(
  401. 'cms_index'=>array('menu_type'=>'link','menu_name'=>Language::get('nc_manage'),'menu_url'=>'index.php?act=cms_index&op=cms_index'),
  402. );
  403. if($menu_key == 'module_edit') {
  404. $menu_array['module_edit'] = array('menu_type'=>'link','menu_name'=>Language::get('nc_edit'),'menu_url'=>'###');
  405. }
  406. $menu_array[$menu_key]['menu_type'] = 'text';
  407. Tpl::output('menu',$menu_array);
  408. }
  409. }