ViewController.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <?php
  2. namespace Admin\Controller;
  3. /**
  4. * 界面管理
  5. * @author Devil
  6. * @blog http://gong.gg/
  7. * @version 0.0.1
  8. * @datetime 2016-12-01T21:51:08+0800
  9. */
  10. class ViewController extends CommonController
  11. {
  12. /**
  13. * [_initialize 前置操作-继承公共前置方法]
  14. * @author Devil
  15. * @blog http://gong.gg/
  16. * @version 0.0.1
  17. * @datetime 2016-12-03T12:39:08+0800
  18. */
  19. public function _initialize()
  20. {
  21. // 调用父类前置方法
  22. parent::_initialize();
  23. // 登录校验
  24. $this->Is_Login();
  25. // 权限校验
  26. $this->Is_Power();
  27. }
  28. /**
  29. * [Index 列表]
  30. * @author Devil
  31. * @blog http://gong.gg/
  32. * @version 0.0.1
  33. * @datetime 2016-12-06T21:31:53+0800
  34. */
  35. public function Index()
  36. {
  37. // 参数
  38. $type = I('type', 'home');
  39. // 布局页面类型
  40. $this->assign('layout_type', $type);
  41. // 布局+模块列表
  42. $this->assign('data', $this->GetLayoutList());
  43. // 友情链接
  44. $this->assign('link', LayoutLink($type));
  45. // 文章分类
  46. $this->assign('article_class_list', M('ArticleClass')->field(array('id', 'name'))->where(array('is_enable'=>1))->select());
  47. // 排序
  48. $this->assign('common_view_sort_list', L('common_view_sort_list'));
  49. // 时间
  50. $this->assign('common_view_time_list', L('common_view_time_list'));
  51. // 标题显示样式
  52. $this->assign('common_view_title_style_list', L('common_view_title_style_list'));
  53. // 打开方式
  54. $this->assign('common_view_link_open_way_list', L('common_view_link_open_way_list'));
  55. // 日期格式
  56. $this->assign('common_view_date_format_list', L('common_view_date_format_list'));
  57. $this->display('Index');
  58. }
  59. /**
  60. * [GetLayoutList 获取布局-模块列表]
  61. * @author Devil
  62. * @blog http://gong.gg/
  63. * @version 0.0.1
  64. * @datetime 2017-02-22T10:15:40+0800
  65. */
  66. private function GetLayoutList()
  67. {
  68. // 布局+模块列表
  69. $data = M('Layout')->field(array('id', 'value', 'is_enable'))->where(array('type'=>I('type', 'home')))->order('sort asc, id desc')->select();
  70. if(!empty($data))
  71. {
  72. // 布局模块处理驱动
  73. $lay = \My\LayoutModule::SetInstance();
  74. // 开始处理布局数据
  75. foreach($data as $k=>$v)
  76. {
  77. // 模块
  78. $item = M('LayoutModule')->where(array('layout_id'=>$v['id']))->select();
  79. if(!empty($item))
  80. {
  81. foreach($item as $ik=>$iv)
  82. {
  83. // 参数条件json
  84. $temp_json = $iv;
  85. if(strlen($temp_json['article_class_id']) > 0)
  86. {
  87. $temp_json['article_class_id'] = explode(',', $temp_json['article_class_id']);
  88. }
  89. $iv['json'] = json_encode($temp_json);
  90. // 获取文章数据
  91. $article = LayoutArticleList($lay->GetLayoutMouleWhere($iv), $iv);
  92. // 模块数据生成
  93. $fun = GetViewTitleStyleFun($iv['title_style']);
  94. $iv['html'] = method_exists($lay, $fun) ? $lay->$fun($article, $iv) : '';
  95. // 重新赋值
  96. $item[$ik] = $iv;
  97. }
  98. }
  99. $data[$k]['item'] = $item;
  100. }
  101. }
  102. return $data;
  103. }
  104. /**
  105. * [GetLayoutModuleData 获取模块数据-及保存模块数据]
  106. * @author Devil
  107. * @blog http://gong.gg/
  108. * @version 0.0.1
  109. * @datetime 2017-02-11T21:18:00+0800
  110. */
  111. public function GetLayoutModuleData()
  112. {
  113. // 是否ajax请求
  114. if(!IS_AJAX)
  115. {
  116. $this->error(L('common_unauthorized_access'));
  117. }
  118. // 布局模块处理驱动
  119. $lay = \My\LayoutModule::SetInstance();
  120. // 模块模型
  121. $m = D('LayoutModule');
  122. // 数据自动校验
  123. if($m->create($_POST, 2))
  124. {
  125. // 额外数据
  126. $m->upd_time = time();
  127. $m->right_title = str_replace(';', ';', I('right_title'));
  128. $m->keyword = str_replace(array(';', '—'), array(';', '-'), I('keyword'));
  129. $m->name = I('name');
  130. $m->right_title = I('right_title');
  131. $m->article_id = I('article_id');
  132. $m->keyword = I('keyword');
  133. // 更新数据库
  134. if($m->where(array('id'=>I('id')))->save())
  135. {
  136. // 获取文章数据
  137. $article = LayoutArticleList($lay->GetLayoutMouleWhere($_POST), $_POST);
  138. // 模块数据生成
  139. $fun = GetViewTitleStyleFun(I('title_style'));
  140. if(method_exists($lay, $fun))
  141. {
  142. $html = $lay->$fun($article, $_POST);
  143. if(strlen($_POST['article_class_id']) > 0)
  144. {
  145. $_POST['article_class_id'] = explode(',', $_POST['article_class_id']);
  146. }
  147. $result = array('html' => $html, 'json' => json_encode($_POST));
  148. $this->ajaxReturn(L('common_operation_edit_success'), 0, $result);
  149. } else {
  150. $this->ajaxReturn(str_replace('{$1}', $fun, L('common_method_exists_error')), -101);
  151. }
  152. } else {
  153. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  154. }
  155. } else {
  156. $this->ajaxReturn($m->getError(), -1);
  157. }
  158. }
  159. /**
  160. * [ModuleAdd 模块添加]
  161. * @author Devil
  162. * @blog http://gong.gg/
  163. * @version 0.0.1
  164. * @datetime 2017-02-17T16:49:58+0800
  165. */
  166. public function ModuleAdd()
  167. {
  168. // 是否ajax请求
  169. if(!IS_AJAX)
  170. {
  171. $this->error(L('common_unauthorized_access'));
  172. }
  173. // 布局类型
  174. if(empty($_POST['id']))
  175. {
  176. $this->ajaxReturn(L('view_module_param_save_tips'), -1);
  177. }
  178. // 数据添加
  179. $data = array(
  180. 'layout_id' => I('id'),
  181. 'add_time' => time(),
  182. 'upd_time' => time(),
  183. );
  184. $id = M('LayoutModule')->add($data);
  185. if($id > 0)
  186. {
  187. $this->ajaxReturn(L('common_operation_add_success'), 0, $id);
  188. } else {
  189. $this->ajaxReturn(L('common_operation_add_error'), -100);
  190. }
  191. }
  192. /**
  193. * [LayoutSave 布局保存]
  194. * @author Devil
  195. * @blog http://gong.gg/
  196. * @version 0.0.1
  197. * @datetime 2017-02-17T16:49:58+0800
  198. */
  199. public function LayoutSave()
  200. {
  201. // 是否ajax请求
  202. if(!IS_AJAX)
  203. {
  204. $this->error(L('common_unauthorized_access'));
  205. }
  206. // 布局类型
  207. if(empty($_POST['type']) || empty($_POST['value']))
  208. {
  209. $this->ajaxReturn(L('view_layout_param_save_tips'), -1);
  210. }
  211. // 布局数据添加
  212. $data = array(
  213. 'type' => I('type'),
  214. 'value' => I('value'),
  215. 'upd_time' => time(),
  216. );
  217. $layout_id = M('Layout')->add($data);
  218. if($layout_id > 0)
  219. {
  220. $result = array('layout_id' => $layout_id);
  221. $module = array('100' => 1, '84' => 2, '48' => 2, '633' => 3, '363' => 3, '336' => 3);
  222. if(array_key_exists($data['value'], $module))
  223. {
  224. $count = $module[$data['value']];
  225. for($i=1; $i<=$count; $i++)
  226. {
  227. // 模块数据添加
  228. $temp_field = 'module'.$i.'_id';
  229. $temp_module = array(
  230. 'layout_id' => $layout_id,
  231. 'add_time' => time(),
  232. 'upd_time' => time(),
  233. );
  234. $result[$temp_field] = M('LayoutModule')->add($temp_module);
  235. }
  236. $module_count = $count;
  237. } else {
  238. $module_count = 0;
  239. }
  240. $result['module_count'] = $module_count;
  241. $this->ajaxReturn(L('common_operation_add_success'), 0, $result);
  242. } else {
  243. $this->ajaxReturn(L('common_operation_add_error'), -100);
  244. }
  245. }
  246. /**
  247. * [StateUpdate 状态更新]
  248. * @author Devil
  249. * @blog http://gong.gg/
  250. * @version 0.0.1
  251. * @datetime 2017-01-12T22:23:06+0800
  252. */
  253. public function StateUpdate()
  254. {
  255. // 是否ajax请求
  256. if(!IS_AJAX)
  257. {
  258. $this->error(L('common_unauthorized_access'));
  259. }
  260. // 参数
  261. if(empty($_POST['id']) || !isset($_POST['state']))
  262. {
  263. $this->ajaxReturn(L('common_param_error'), -1);
  264. }
  265. // 数据更新
  266. if(M('Layout')->where(array('id'=>I('id')))->save(array('is_enable'=>I('state'))))
  267. {
  268. $this->ajaxReturn(L('common_operation_edit_success'));
  269. } else {
  270. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  271. }
  272. }
  273. /**
  274. * [LayoutDelete 布局删除]
  275. * @author Devil
  276. * @blog http://gong.gg/
  277. * @version 0.0.1
  278. * @datetime 2017-02-20T13:30:50+0800
  279. */
  280. public function LayoutDelete()
  281. {
  282. // 是否ajax请求
  283. if(!IS_AJAX)
  284. {
  285. $this->error(L('common_unauthorized_access'));
  286. }
  287. // 参数
  288. if(empty($_POST['id']))
  289. {
  290. $this->ajaxReturn(L('common_param_error'), -1);
  291. }
  292. // 布局模型
  293. $m = M('Layout');
  294. // 开启事务
  295. $m->startTrans();
  296. // 删除数据
  297. if($m->delete(I('id')) !== false && M('LayoutModule')->where(array('layout_id'=>I('id')))->delete() !== false)
  298. {
  299. // 提交事务
  300. $m->commit();
  301. $this->ajaxReturn(L('common_operation_delete_success'));
  302. } else {
  303. // 回滚事务
  304. $m->rollback();
  305. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  306. }
  307. }
  308. /**
  309. * [ModuleDelete 模块删除]
  310. * @author Devil
  311. * @blog http://gong.gg/
  312. * @version 0.0.1
  313. * @datetime 2017-02-20T13:30:50+0800
  314. */
  315. public function ModuleDelete()
  316. {
  317. // 是否ajax请求
  318. if(!IS_AJAX)
  319. {
  320. $this->error(L('common_unauthorized_access'));
  321. }
  322. // 参数
  323. if(empty($_POST['id']))
  324. {
  325. $this->ajaxReturn(L('common_param_error'), -1);
  326. }
  327. if(M('LayoutModule')->delete(I('id')) !== false)
  328. {
  329. $this->ajaxReturn(L('common_operation_delete_success'));
  330. } else {
  331. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  332. }
  333. }
  334. /**
  335. * [LayoutSortSave 布局排序保存]
  336. * @author Devil
  337. * @blog http://gong.gg/
  338. * @version 0.0.1
  339. * @datetime 2017-02-20T16:02:56+0800
  340. */
  341. public function LayoutSortSave()
  342. {
  343. // 是否ajax请求
  344. if(!IS_AJAX)
  345. {
  346. $this->error(L('common_unauthorized_access'));
  347. }
  348. // 参数
  349. if(empty($_POST['data']) || !is_array($_POST['data']))
  350. {
  351. $this->ajaxReturn(L('common_param_error'), -1);
  352. }
  353. $success = 0;
  354. $failure = 0;
  355. $m = M('Layout');
  356. foreach($_POST['data'] as $k=>$v)
  357. {
  358. if($m->where(array('id'=>intval($v)))->save(array('sort'=>$k)))
  359. {
  360. $success++;
  361. } else {
  362. $failure++;
  363. }
  364. }
  365. if($success > 0)
  366. {
  367. $this->ajaxReturn(L('common_operation_success'));
  368. } else {
  369. $this->ajaxReturn(L('common_operation_error'), -100);
  370. }
  371. }
  372. /**
  373. * [LayoutExport 布局数据导出]
  374. * @author Devil
  375. * @blog http://gong.gg/
  376. * @version 0.0.1
  377. * @datetime 2017-05-11T16:24:34+0800
  378. */
  379. public function LayoutExport()
  380. {
  381. $type = I('type', 'home');
  382. $data = M('Layout')->where(array('type'=>$type))->select();
  383. if(!empty($data))
  384. {
  385. $module = M('LayoutModule');
  386. foreach($data as &$v)
  387. {
  388. $v['item'] = $module->where(array('layout_id'=>$v['id']))->select();
  389. }
  390. }
  391. // 输出内容
  392. header('Content-Type: application/octet-stream');
  393. header('Content-Disposition: attachment; filename=layout-'.$type.'.txt');
  394. header('Pragma: no-cache');
  395. header('Expires: 0');
  396. echo serialize($data);
  397. }
  398. /**
  399. * [LayoutImport 布局数据导入]
  400. * @author Devil
  401. * @blog http://gong.gg/
  402. * @version 0.0.1
  403. * @datetime 2017-05-11T16:43:53+0800
  404. */
  405. public function LayoutImport()
  406. {
  407. // 是否ajax请求
  408. if(!IS_AJAX)
  409. {
  410. $this->error(L('common_unauthorized_access'));
  411. }
  412. // 文件上传校验
  413. $error = FileUploadError('file');
  414. if($error !== true)
  415. {
  416. $this->ajaxReturn($error, -1);
  417. }
  418. // 文件格式化校验
  419. $type = array('text/plain');
  420. if(!in_array($_FILES['file']['type'], $type))
  421. {
  422. $this->ajaxReturn(L('theme_upload_error'), -2);
  423. }
  424. // 读取文件内容
  425. $data = unserialize(file_get_contents($_FILES['file']['tmp_name']));
  426. // 初始化变量
  427. $failure = 0;
  428. $del_state = false;
  429. // 数据导入
  430. if(!empty($data) && is_array($data))
  431. {
  432. // 模型
  433. $layout = M('Layout');
  434. $module = M('LayoutModule');
  435. // 开启事务
  436. $layout->startTrans();
  437. // 开始处理数据
  438. foreach($data as $v)
  439. {
  440. // 删除原始数据
  441. if($del_state == false && !empty($v['type']))
  442. {
  443. if(!$this->LayoutImportDelete($layout, $module, $v['type']))
  444. {
  445. $failure++;
  446. }
  447. $del_state = true;
  448. }
  449. // 插入模块数据
  450. if(!empty($v['item']))
  451. {
  452. foreach($v['item'] as $vs)
  453. {
  454. if($module->add($vs) === false)
  455. {
  456. $failure++;
  457. }
  458. }
  459. }
  460. // 插入新的布局
  461. if($layout->add($v) === false)
  462. {
  463. $failure++;
  464. }
  465. }
  466. }
  467. // 状态
  468. if($failure == 0)
  469. {
  470. // 提交事务
  471. $layout->commit();
  472. $this->ajaxReturn(L('common_import_success_name'));
  473. } else {
  474. // 回滚事务
  475. $layout->rollback();
  476. $this->ajaxReturn(L('common_import_error_name'), -100);
  477. }
  478. }
  479. /**
  480. * [LayoutImportDelete 布局导入删除]
  481. * @author Devil
  482. * @blog http://gong.gg/
  483. * @version 0.0.1
  484. * @datetime 2017-05-11T17:23:58+0800
  485. * @param [object] $layout [布局模型]
  486. * @param [object] $module [模块模型]
  487. * @param [string] $type [布局类型(home, channel, detail)]
  488. * @return [boolean] [成功true, 失败false]
  489. */
  490. private function LayoutImportDelete($layout, $module, $type)
  491. {
  492. $failure = 0;
  493. $data = $layout->where(array('type'=>$type))->select();
  494. if(!empty($data))
  495. {
  496. foreach($data as $v)
  497. {
  498. if($layout->delete($v['id']) === false)
  499. {
  500. $failure++;
  501. }
  502. if($module->where(array('layout_id'=>$v['id']))->delete() === false)
  503. {
  504. $failure++;
  505. }
  506. }
  507. }
  508. return ($failure == 0);
  509. }
  510. }
  511. ?>