BlogCategoryController.class.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * 小梦科技资源nanodreamtech.com
  4. *
  5. * ==========================================================================
  6. * @link https://www.nanodreamtech.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license https://www.nanodreamtech.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Admin\Controller;
  15. use Think\Controller;
  16. class BlogCategoryController extends CommonController {
  17. protected function _initialize(){
  18. parent::_initialize();
  19. $this->breadcrumb1='博客';
  20. $this->breadcrumb2='博客分类';
  21. }
  22. function index(){
  23. $cate = M()->query('SELECT id,pid,title AS name FROM '.C('DB_PREFIX').'blog_category ORDER BY sort_order ASC');
  24. $list =list_to_tree($cate);
  25. $this->list=json_encode($list);
  26. $this->display();
  27. }
  28. function add(){
  29. if(IS_POST){
  30. $d['title']=I('title');
  31. $d['sort_order']=I('sort_order');
  32. $d['pid']=I('id');
  33. if(M('blog_category')->where(array('title'=>$d['title'],'pid'=>$d['pid']))->find()){
  34. $data['err']='该分类名称已经存在';
  35. $this->ajaxReturn($data);
  36. die();
  37. }
  38. $id=M('blog_category')->add($d);
  39. if($id){
  40. $data['name'] =$d['title'];
  41. $data['id']=$id;
  42. $this->ajaxReturn($data);
  43. die();
  44. }else{
  45. die();
  46. }
  47. }
  48. }
  49. function edit(){
  50. if(IS_POST){
  51. $d['id']=I('id');
  52. $d['title']=I('title');
  53. $d['sort_order']=I('sort_order');
  54. $category=M('blog_category')->find($d['id']);
  55. if(M('blog_category')->where(array('title'=>$d['title'],'pid'=>$category['pid']))->find()){
  56. $data['err']='该分类名称已经存在';
  57. $this->ajaxReturn($data);
  58. die();
  59. }
  60. $r=M('blog_category')->save($d);
  61. if($r){
  62. $data['success']='修改成功';
  63. $data['name']=$d['title'];
  64. $this->ajaxReturn($data);
  65. die();
  66. }else{
  67. $data['err']='修改失败';
  68. $this->ajaxReturn($data);
  69. die();
  70. }
  71. }
  72. }
  73. function del(){
  74. if(IS_POST){
  75. $id=I('id');
  76. if(M('blog_category')->where('pid='.$id)->find()){
  77. $data['err']='请先删除子节点!!';
  78. $this->ajaxReturn($data);
  79. die;
  80. }
  81. if(M('blog')->where(array('category_id'=>$id))->find()){
  82. $data['err']='请先删除该分类下博客!!';
  83. $this->ajaxReturn($data);
  84. die;
  85. }
  86. if(M('blog_category')->where('id='.$id)->delete()){
  87. $this->ajaxReturn('删除成功');
  88. die();
  89. }
  90. }
  91. }
  92. function get_info(){
  93. if(IS_POST){
  94. $id=I('id');
  95. $d=M('blog_category')->find($id);
  96. $data['title']=$d['title'];
  97. $data['sort_order']=$d['sort_order'];
  98. $this->ajaxReturn($data);
  99. }
  100. }
  101. function autocomplete(){
  102. $json = array();
  103. $filter_name=I('filter_name');
  104. if (isset($filter_name)) {
  105. $sql='SELECT id,title FROM '.c('DB_PREFIX')."blog_category where title LIKE'%".$filter_name."%' LIMIT 0,20";
  106. }else{
  107. $sql='SELECT id,title FROM '.c('DB_PREFIX')."blog_category LIMIT 0,20";
  108. }
  109. $results = M('blog_category')->query($sql);
  110. foreach ($results as $result) {
  111. $json[] = array(
  112. 'category_id' => $result['id'],
  113. 'name' => strip_tags(html_entity_decode($result['title'], ENT_QUOTES, 'UTF-8'))
  114. );
  115. }
  116. $this->ajaxReturn($json);
  117. }
  118. }