GoodsCategoryController.class.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. * ==========================================================================
  6. * @link http://www.liofis.com/
  7. * @copyright Copyright (c) 2015 liofis.com.
  8. * @license http://www.liofis.com/license.html License
  9. * ==========================================================================
  10. *
  11. * @author fish
  12. *
  13. */
  14. namespace Admin\Controller;
  15. class GoodsCategoryController extends CommonController{
  16. protected function _initialize(){
  17. parent::_initialize();
  18. $this->breadcrumb1='商品';
  19. $this->breadcrumb2='商品分类';
  20. }
  21. public function index(){
  22. $sql='SELECT id,pid,name,logo FROM '
  23. .C('DB_PREFIX').'goods_category';
  24. $cate = M()->query($sql);
  25. foreach($cate as $key => $val)
  26. {
  27. $val['name'] = $val['name'].'( id = '.$val['id'].' )';
  28. $cate[$key] = $val;
  29. }
  30. $list =list_to_tree($cate);
  31. $this->list=json_encode($list);
  32. $this->display();
  33. }
  34. function add(){
  35. if(IS_POST){
  36. $d = array();
  37. $d['name']=I('name');
  38. $d['pid']=I('id');
  39. $d['sort_order']=I('sort_order');
  40. $d['c_sort_order']=I('c_sort_order');
  41. $d['is_search'] = I('is_search');
  42. $d['is_hot'] = I('is_hot');
  43. $d['is_haitao'] = I('is_haitao');
  44. $d['logo'] = I('image');
  45. $id=M('goods_category')->add($d);
  46. if($id){
  47. $data['name'] =$d['name'];
  48. $data['id']=$id;
  49. $this->ajaxReturn($data);
  50. die();
  51. }else{
  52. die();
  53. }
  54. }
  55. }
  56. function edit(){
  57. if(IS_POST){
  58. $d['id']=I('id');
  59. $d['name']=I('name');
  60. $d['sort_order']=I('sort_order');
  61. $d['c_sort_order']=I('c_sort_order');
  62. $d['logo'] = I('image');
  63. $d['is_search'] = I('is_search');
  64. $d['is_hot'] = I('is_hot');
  65. $d['is_haitao'] = I('is_haitao');
  66. $category=M('goods_category')->find($d['id']);
  67. $r=M('goods_category')->save($d);
  68. if($r){
  69. $data['success']='修改成功';
  70. $data['name']=$d['name'];
  71. $this->ajaxReturn($data);
  72. die();
  73. }else{
  74. $data['err']='修改失败';
  75. $this->ajaxReturn($data);
  76. die();
  77. }
  78. }
  79. }
  80. function get_info(){
  81. if(IS_POST){
  82. $id=I('id');
  83. $d=M('goods_category')->find($id);
  84. $data['name']=$d['name'];
  85. $data['logo']=$d['logo'];
  86. $data['is_search'] = $d['is_search'];
  87. $data['is_hot']=$d['is_hot'];
  88. $data['is_haitao']=$d['is_haitao'];
  89. $data['c_sort_order']=$d['c_sort_order'];
  90. if(!empty($d['logo']))
  91. {
  92. $data['thumb_image'] = resize($d['logo'], 100, 100);
  93. }else {
  94. $data['thumb_image'] = '';
  95. }
  96. $data['sort_order']=$d['sort_order'];
  97. $this->ajaxReturn($data);
  98. }
  99. }
  100. function del(){
  101. if(IS_POST){
  102. $id=I('post.id');
  103. if(M('goods_category')->where('pid='.$id)->find()){
  104. $data['err']='请先删除子节点!!';
  105. $this->ajaxReturn($data);
  106. die;
  107. }
  108. $res1 = M('goods_to_category')->where(array('class_id1'=>$id))->find();
  109. $res2 = M('goods_to_category')->where(array('class_id2'=>$id))->find();
  110. $res3 = M('goods_to_category')->where(array('class_id3'=>$id))->find();
  111. if( !empty($res1) || !empty($res2) || !empty($res3)){
  112. $data['err']='请先删除该分类下商品!!';
  113. $this->ajaxReturn($data);
  114. die;
  115. }
  116. if(M('goods_category')->where('id='.$id)->delete()){
  117. $data['success']='删除成功';
  118. $this->ajaxReturn($data);
  119. die();
  120. }
  121. }
  122. }
  123. function autocomplete(){
  124. $json = array();
  125. $filter_name=I('filter_name');
  126. if (isset($filter_name)) {
  127. $sql='SELECT id,name FROM '.c('DB_PREFIX')."goods_category where name LIKE'%".$filter_name."%' LIMIT 0,20";
  128. }else{
  129. $sql='SELECT id,name FROM '.c('DB_PREFIX')."goods_category LIMIT 0,20";
  130. }
  131. $results = M('goods_category')->query($sql);
  132. foreach ($results as $result) {
  133. $json[] = array(
  134. 'category_id' => $result['id'],
  135. 'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8'))
  136. );
  137. }
  138. $this->ajaxReturn($json);
  139. }
  140. }
  141. ?>