ArticleClassController.class.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 ArticleClassController 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. $this->assign('common_is_enable_list', L('common_is_enable_list'));
  38. $this->display('Index');
  39. }
  40. /**
  41. * [GetNodeSon 获取节点子列表]
  42. * @author Devil
  43. * @blog http://gong.gg/
  44. * @version 0.0.1
  45. * @datetime 2016-12-25T15:19:45+0800
  46. */
  47. public function GetNodeSon()
  48. {
  49. // 是否ajax请求
  50. if(!IS_AJAX)
  51. {
  52. $this->error(L('common_unauthorized_access'));
  53. }
  54. // 获取数据
  55. $field = array('id', 'name', 'sort', 'is_enable');
  56. $data = M('ArticleClass')->field($field)->where(array('pid'=>intval(I('id', 0))))->select();
  57. if(!empty($data))
  58. {
  59. foreach($data as $k=>$v)
  60. {
  61. $data[$k]['is_son'] = $this->IsExistSon($v['id']);
  62. $data[$k]['ajax_url'] = U('Admin/ArticleClass/GetNodeSon', array('id'=>$v['id']));
  63. $data[$k]['delete_url'] = U('Admin/ArticleClass/Delete');
  64. $data[$k]['json'] = json_encode($v);
  65. }
  66. }
  67. $msg = empty($data) ? L('common_not_data_tips') : L('common_operation_success');
  68. $this->ajaxReturn($msg, 0, $data);
  69. }
  70. /**
  71. * [IsExistSon 节点是否存在子数据]
  72. * @author Devil
  73. * @blog http://gong.gg/
  74. * @version 0.0.1
  75. * @datetime 2016-12-25T15:22:47+0800
  76. * @param [int] $id [节点id]
  77. * @return [string] [有数据ok, 则no]
  78. */
  79. private function IsExistSon($id)
  80. {
  81. if(!empty($id))
  82. {
  83. return (M('ArticleClass')->where(array('pid'=>$id))->count() > 0) ? 'ok' : 'no';
  84. }
  85. return 'no';
  86. }
  87. /**
  88. * [Save 文章分类保存]
  89. * @author Devil
  90. * @blog http://gong.gg/
  91. * @version 0.0.1
  92. * @datetime 2016-12-25T22:36:12+0800
  93. */
  94. public function Save()
  95. {
  96. // 是否ajax请求
  97. if(!IS_AJAX)
  98. {
  99. $this->error(L('common_unauthorized_access'));
  100. }
  101. // id为空则表示是新增
  102. $m = D('ArticleClass');
  103. // 公共额外数据处理
  104. $m->sort = intval(I('sort'));
  105. // 添加
  106. if(empty($_POST['id']))
  107. {
  108. if($m->create($_POST, 1))
  109. {
  110. // 额外数据处理
  111. $m->add_time = time();
  112. $m->name = I('name');
  113. // 写入数据库
  114. if($m->add())
  115. {
  116. $this->ajaxReturn(L('common_operation_add_success'));
  117. } else {
  118. $this->ajaxReturn(L('common_operation_add_error'), -100);
  119. }
  120. }
  121. } else {
  122. // 编辑
  123. if($m->create($_POST, 2))
  124. {
  125. // 额外数据处理
  126. $m->name = I('name');
  127. // 移除 id
  128. unset($m->id);
  129. // 更新数据库
  130. if($m->where(array('id'=>I('id')))->save())
  131. {
  132. $this->ajaxReturn(L('common_operation_edit_success'));
  133. } else {
  134. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  135. }
  136. }
  137. }
  138. $this->ajaxReturn($m->getError(), -1);
  139. }
  140. /**
  141. * [Delete 文章分类删除]
  142. * @author Devil
  143. * @blog http://gong.gg/
  144. * @version 0.0.1
  145. * @datetime 2016-12-25T22:36:12+0800
  146. */
  147. public function Delete()
  148. {
  149. if(!IS_AJAX)
  150. {
  151. $this->error(L('common_unauthorized_access'));
  152. }
  153. $m = D('ArticleClass');
  154. if($m->create($_POST, 5))
  155. {
  156. if($m->delete(I('id')))
  157. {
  158. $this->ajaxReturn(L('common_operation_delete_success'));
  159. } else {
  160. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  161. }
  162. } else {
  163. $this->ajaxReturn($m->getError(), -1);
  164. }
  165. }
  166. }
  167. ?>