ArticleController.class.php 8.6 KB

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