NavHeaderController.class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 NavHeaderController 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. $this->assign('list', $this->GetNavList());
  39. // 一级分类
  40. $this->assign('nav_header_pid_list', M('Navigation')->field(array('id', 'name'))->where(array('is_show'=>1, 'pid'=>0))->select());
  41. // 文章分类
  42. $this->assign('article_class_list', M('ArticleClass')->field(array('id', 'name'))->where(array('is_enable'=>1))->select());
  43. // 自定义页面
  44. $this->assign('customview_list', M('CustomView')->field(array('id', 'title'))->where(array('is_enable'=>1))->select());
  45. // 是否新窗口打开
  46. $this->assign('common_is_new_window_open_list', L('common_is_new_window_open_list'));
  47. // 是否显示
  48. $this->assign('common_is_show_list', L('common_is_show_list'));
  49. $this->display('Index');
  50. }
  51. /**
  52. * [GetNavList 获取数据列表]
  53. * @author Devil
  54. * @blog http://gong.gg/
  55. * @version 0.0.1
  56. * @datetime 2016-12-10T22:16:29+0800
  57. */
  58. private function GetNavList()
  59. {
  60. $m = M('Navigation');
  61. $field = array('id', 'pid', 'name', 'url', 'value', 'data_type', 'sort', 'is_show', 'is_new_window_open');
  62. $data = NavDataDealWith($m->field($field)->where(array('nav_type'=>'header', 'pid'=>0))->order('sort')->select());
  63. if(!empty($data))
  64. {
  65. foreach($data as $k=>$v)
  66. {
  67. $data[$k]['item'] = NavDataDealWith($m->field($field)->where(array('nav_type'=>'header', 'pid'=>$v['id']))->order('sort')->select());
  68. }
  69. }
  70. return $data;
  71. }
  72. /**
  73. * [Save 添加/编辑]
  74. * @author Devil
  75. * @blog http://gong.gg/
  76. * @version 0.0.1
  77. * @datetime 2016-12-07T21:58:19+0800
  78. */
  79. public function Save()
  80. {
  81. // 是否ajax请求
  82. if(!IS_AJAX)
  83. {
  84. $this->error(L('common_unauthorized_access'));
  85. }
  86. // 请求类型
  87. switch(I('data_type'))
  88. {
  89. // 自定义导航
  90. case 'custom':
  91. $this->DataSave(5);
  92. break;
  93. // 文章分类导航
  94. case 'article_class':
  95. $this->DataSave(6);
  96. break;
  97. // 自读页面导航
  98. case 'customview':
  99. $this->DataSave(7);
  100. break;
  101. }
  102. $this->ajaxReturn(L('common_param_error'), -1);
  103. }
  104. /**
  105. * [DataSave 导航数据保存]
  106. * @author Devil
  107. * @blog http://gong.gg/
  108. * @version 0.0.1
  109. * @datetime 2017-02-05T20:12:30+0800
  110. * @param [int] $check_type [校验类型]
  111. */
  112. private function DataSave($check_type)
  113. {
  114. $m = D('Navigation');
  115. // 数据校验
  116. if($m->create($_POST, $check_type))
  117. {
  118. // 非自定义导航数据处理
  119. if(empty($_POST['name']))
  120. {
  121. switch(I('data_type'))
  122. {
  123. // 文章分类导航
  124. case 'article_class':
  125. $temp_name = M('ArticleClass')->where(array('id'=>I('value')))->getField('name');
  126. break;
  127. // 自读页面导航
  128. case 'customview':
  129. $temp_name = M('CustomView')->where(array('id'=>I('value')))->getField('title');
  130. break;
  131. }
  132. // 只截取16个字符
  133. $m->name = mb_substr($temp_name, 0, 16, C('DEFAULT_CHARSET'));
  134. } else {
  135. $m->name = I('name');
  136. }
  137. // 清除缓存
  138. S(C('cache_common_home_nav_header_key'), null);
  139. // id为空则表示是新增
  140. if(empty($_POST['id']))
  141. {
  142. // 额外数据处理
  143. $m->add_time = time();
  144. $m->nav_type = 'header';
  145. // 写入数据库
  146. if($m->add())
  147. {
  148. $this->ajaxReturn(L('common_operation_add_success'));
  149. } else {
  150. $this->ajaxReturn(L('common_operation_add_error'), -100);
  151. }
  152. } else {
  153. // 数据编辑
  154. if($m->where(array('id'=>I('id')))->save())
  155. {
  156. $this->ajaxReturn(L('common_operation_edit_success'));
  157. } else {
  158. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  159. }
  160. }
  161. } else {
  162. $this->ajaxReturn($m->getError(), -1);
  163. }
  164. }
  165. /**
  166. * [Delete 删除]
  167. * @author Devil
  168. * @blog http://gong.gg/
  169. * @version 0.0.1
  170. * @datetime 2016-12-09T21:13:47+0800
  171. */
  172. public function Delete()
  173. {
  174. if(!IS_AJAX)
  175. {
  176. $this->error(L('common_unauthorized_access'));
  177. }
  178. $m = D('Navigation');
  179. if($m->create($_POST, 4))
  180. {
  181. if($m->delete($id))
  182. {
  183. // 清除缓存
  184. S(C('cache_common_home_nav_header_key'), null);
  185. $this->ajaxReturn(L('common_operation_delete_success'));
  186. } else {
  187. $this->ajaxReturn(L('common_operation_delete_error'), -100);
  188. }
  189. } else {
  190. $this->ajaxReturn($m->getError(), -1);
  191. }
  192. }
  193. /**
  194. * [StateUpdate 状态更新]
  195. * @author Devil
  196. * @blog http://gong.gg/
  197. * @version 0.0.1
  198. * @datetime 2017-01-12T22:23:06+0800
  199. */
  200. public function StateUpdate()
  201. {
  202. // 参数
  203. if(empty($_POST['id']) || !isset($_POST['state']))
  204. {
  205. $this->ajaxReturn(L('common_param_error'), -1);
  206. }
  207. // 数据更新
  208. if(M('Navigation')->where(array('id'=>I('id')))->save(array('is_show'=>I('state'))))
  209. {
  210. // 清除缓存
  211. S(C('cache_common_home_nav_header_key'), null);
  212. $this->ajaxReturn(L('common_operation_edit_success'));
  213. } else {
  214. $this->ajaxReturn(L('common_operation_edit_error'), -100);
  215. }
  216. }
  217. }
  218. ?>