CustomViewController.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 CustomViewController 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. $param = array_merge($_POST, $_GET);
  39. // 模型对象
  40. $m = M('CustomView');
  41. // 条件
  42. $where = $this->GetIndexWhere();
  43. // 分页
  44. $number = MyC('admin_page_number');
  45. $page_param = array(
  46. 'number' => $number,
  47. 'total' => $m->where($where)->count(),
  48. 'where' => $param,
  49. 'url' => U('Admin/CustomView/Index'),
  50. );
  51. $page = new \My\Page($page_param);
  52. // 获取列表
  53. $list = $this->SetDataHandle($m->where($where)->limit($page->GetPageStarNumber(), $number)->order('id desc')->select());
  54. // 是否启用
  55. $this->assign('common_is_enable_list', L('common_is_enable_list'));
  56. // 是否包含头部
  57. $this->assign('common_is_header_list', L('common_is_header_list'));
  58. // 是否包含尾部
  59. $this->assign('common_is_footer_list', L('common_is_footer_list'));
  60. // 是否满屏
  61. $this->assign('common_is_full_screen_list', L('common_is_full_screen_list'));
  62. // 参数
  63. $this->assign('param', $param);
  64. // 分页
  65. $this->assign('page_html', $page->GetPageHtml());
  66. // 数据列表
  67. $this->assign('list', $list);
  68. $this->display('Index');
  69. }
  70. /**
  71. * [SetDataHandle 数据处理]
  72. * @author Devil
  73. * @blog http://gong.gg/
  74. * @version 0.0.1
  75. * @datetime 2016-12-29T21:27:15+0800
  76. * @param [array] $data [文章数据]
  77. * @return [array] [处理好的数据]
  78. */
  79. private function SetDataHandle($data)
  80. {
  81. if(!empty($data))
  82. {
  83. foreach($data as $k=>$v)
  84. {
  85. // 时间
  86. $data[$k]['add_time'] = date('Y-m-d H:i:s', $v['add_time']);
  87. $data[$k]['upd_time'] = date('Y-m-d H:i:s', $v['upd_time']);
  88. // 是否启用
  89. $data[$k]['is_enable_text'] = L('common_is_enable_list')[$v['is_enable']]['name'];
  90. }
  91. }
  92. return $data;
  93. }
  94. /**
  95. * [GetIndexWhere 文章列表条件]
  96. * @author Devil
  97. * @blog http://gong.gg/
  98. * @version 0.0.1
  99. * @datetime 2016-12-10T22:16:29+0800
  100. */
  101. private function GetIndexWhere()
  102. {
  103. $where = array();
  104. // 模糊
  105. if(!empty($_REQUEST['keyword']))
  106. {
  107. $where[] = array(
  108. 'title' => array('like', '%'.I('keyword').'%'),
  109. );
  110. }
  111. // 是否更多条件
  112. if(I('is_more', 0) == 1)
  113. {
  114. // 等值
  115. if(I('is_enable', -1) > -1)
  116. {
  117. $where['is_enable'] = intval(I('is_enable', 1));
  118. }
  119. if(I('is_header', -1) > -1)
  120. {
  121. $where['is_header'] = intval(I('is_header'));
  122. }
  123. if(I('is_footer', -1) > -1)
  124. {
  125. $where['is_footer'] = intval(I('is_footer'));
  126. }
  127. // 表达式
  128. if(!empty($_REQUEST['time_start']))
  129. {
  130. $where['add_time'][] = array('gt', strtotime(I('time_start')));
  131. }
  132. if(!empty($_REQUEST['time_end']))
  133. {
  134. $where['add_time'][] = array('lt', strtotime(I('time_end')));
  135. }
  136. }
  137. return $where;
  138. }
  139. /**
  140. * [SaveInfo 添加/编辑页面]
  141. * @author Devil
  142. * @blog http://gong.gg/
  143. * @version 0.0.1
  144. * @datetime 2016-12-14T21:37:02+0800
  145. */
  146. public function SaveInfo()
  147. {
  148. // 数据
  149. if(empty($_REQUEST['id']))
  150. {
  151. $data = array();
  152. } else {
  153. $data = M('CustomView')->find(I('id'));
  154. if(!empty($data['content']))
  155. {
  156. // 静态资源地址处理
  157. $data['content'] = ContentStaticReplace($data['content'], 'get');
  158. }
  159. }
  160. $this->assign('data', $data);
  161. // 是否启用
  162. $this->assign('common_is_enable_list', L('common_is_enable_list'));
  163. // 是否包含头部
  164. $this->assign('common_is_header_list', L('common_is_header_list'));
  165. // 是否包含尾部
  166. $this->assign('common_is_footer_list', L('common_is_footer_list'));
  167. // 是否满屏
  168. $this->assign('common_is_full_screen_list', L('common_is_full_screen_list'));
  169. $this->display('SaveInfo');
  170. }
  171. /**
  172. * [Save 添加/编辑]
  173. * @author Devil
  174. * @blog http://gong.gg/
  175. * @version 0.0.1
  176. * @datetime 2016-12-14T21:37:02+0800
  177. */
  178. public function Save()
  179. {
  180. // 是否ajax请求
  181. if(!IS_AJAX)
  182. {
  183. $this->error(L('common_unauthorized_access'));
  184. }
  185. // 添加
  186. if(empty($_POST['id']))
  187. {
  188. $this->Add();
  189. // 编辑
  190. } else {
  191. $this->Edit();
  192. }
  193. }
  194. /**
  195. * [Add 添加]
  196. * @author Devil
  197. * @blog http://gong.gg/
  198. * @version 0.0.1
  199. * @datetime 2016-12-18T16:20:59+0800
  200. */
  201. private function Add()
  202. {
  203. // 模型
  204. $m = D('CustomView');
  205. // 数据自动校验
  206. if($m->create($_POST, 1))
  207. {
  208. // 额外数据处理
  209. $m->add_time = time();
  210. $m->upd_time = time();
  211. $m->title = I('title');
  212. // 静态资源地址处理
  213. $m->content = ContentStaticReplace($m->content, 'add');
  214. // 正则匹配文章图片
  215. $temp_image = $this->MatchContentImage($m->content);
  216. $m->image = serialize($temp_image);
  217. $m->image_count = count($temp_image);
  218. // 数据添加
  219. if($m->add())
  220. {
  221. $this->ajaxReturn(L('common_operation_add_success'));
  222. } else {
  223. $this->ajaxReturn(L('common_operation_add_error'), -100);
  224. }
  225. } else {
  226. $this->ajaxReturn($m->getError(), -1);
  227. }
  228. }
  229. /**
  230. * [Edit 编辑]
  231. * @author Devil
  232. * @blog http://gong.gg/
  233. * @version 0.0.1
  234. * @datetime 2016-12-17T22:13:40+0800
  235. */
  236. private function Edit()
  237. {
  238. // 模型
  239. $m = D('CustomView');
  240. // 数据自动校验
  241. if($m->create($_POST, 2))
  242. {
  243. // 静态资源地址处理
  244. $m->content = ContentStaticReplace($m->content, 'add');
  245. // 正则匹配文章图片
  246. $temp_image = $this->MatchContentImage($m->content);
  247. $m->image = serialize($temp_image);
  248. $m->image_count = count($temp_image);
  249. $m->upd_time = time();
  250. $m->title = I('title');
  251. // 数据更新
  252. if($m->where(array('id'=>I('id')))->save())
  253. {
  254. $this->ajaxReturn(L('common_operation_edit_success'));
  255. } else {
  256. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  257. }
  258. } else {
  259. $this->ajaxReturn($m->getError(), -1);
  260. }
  261. }
  262. /**
  263. * [MatchContentImage 正则匹配文章图片]
  264. * @author Devil
  265. * @blog http://gong.gg/
  266. * @version 0.0.1
  267. * @datetime 2017-01-22T18:06:53+0800
  268. * @param [string] $content [文章内容]
  269. * @return [array] [文章图片数组(一维)]
  270. */
  271. private function MatchContentImage($content)
  272. {
  273. if(!empty($content))
  274. {
  275. $pattern = '/<img.*?src=[\'|\"](\/Public\/Upload\/Article\/image\/.*?[\.gif|\.jpg|\.jpeg|\.png|\.bmp])[\'|\"].*?[\/]?>/';
  276. preg_match_all($pattern, $content, $match);
  277. return empty($match[1]) ? array() : $match[1];
  278. }
  279. return array();
  280. }
  281. /**
  282. * [Delete 删除]
  283. * @author Devil
  284. * @blog http://gong.gg/
  285. * @version 0.0.1
  286. * @datetime 2016-12-15T11:03:30+0800
  287. */
  288. public function Delete()
  289. {
  290. // 是否ajax请求
  291. if(!IS_AJAX)
  292. {
  293. $this->error(L('common_unauthorized_access'));
  294. }
  295. // 删除数据
  296. if(!empty($_POST['id']))
  297. {
  298. // 更新
  299. if(M('CustomView')->delete(I('id')))
  300. {
  301. $this->ajaxReturn(L('common_operation_delete_success'));
  302. } else {
  303. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  304. }
  305. } else {
  306. $this->ajaxReturn(L('common_param_error'), -1);
  307. }
  308. }
  309. /**
  310. * [StateUpdate 状态更新]
  311. * @author Devil
  312. * @blog http://gong.gg/
  313. * @version 0.0.1
  314. * @datetime 2017-01-12T22:23:06+0800
  315. */
  316. public function StateUpdate()
  317. {
  318. // 参数
  319. if(empty($_POST['id']) || !isset($_POST['state']))
  320. {
  321. $this->ajaxReturn(L('common_param_error'), -1);
  322. }
  323. // 数据更新
  324. if(M('CustomView')->where(array('id'=>I('id')))->save(array('is_enable'=>I('state'))))
  325. {
  326. $this->ajaxReturn(L('common_operation_edit_success'));
  327. } else {
  328. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  329. }
  330. }
  331. }
  332. ?>