CatalogController.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4. class CatalogController extends BaseController {
  5. //编辑页面
  6. public function edit(){
  7. $cat_id = I("cat_id/d");
  8. $Catalog = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  9. if ($Catalog) {
  10. $this->assign("Catalog" , $Catalog);
  11. }
  12. if ($Catalog['parent_cat_id']) {
  13. $this->assign("default_parent_cat_id" , $Catalog['parent_cat_id']);
  14. }
  15. $item_id = $Catalog['item_id'] ? $Catalog['item_id'] : I("item_id");
  16. $login_user = $this->checkLogin();
  17. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  18. $this->message(L('no_permissions'));
  19. return;
  20. }
  21. $this->assign("item_id" , $item_id);
  22. $this->display();
  23. }
  24. //保存目录
  25. public function save(){
  26. $cat_name = I("cat_name");
  27. $s_number = I("s_number/d") ? I("s_number/d") : 99 ;
  28. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  29. $parent_cat_id = I("parent_cat_id/d")? I("parent_cat_id/d") : 0;
  30. $item_id = I("item_id/d");
  31. $login_user = $this->checkLogin();
  32. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  33. $this->message(L('no_permissions'));
  34. return;
  35. }
  36. $data['cat_name'] = $cat_name ;
  37. $data['s_number'] = $s_number ;
  38. $data['item_id'] = $item_id ;
  39. $data['parent_cat_id'] = $parent_cat_id ;
  40. if ($parent_cat_id > 0 ) {
  41. $data['level'] = 3;
  42. }else{
  43. $data['level'] = 2;
  44. }
  45. if ($cat_id > 0 ) {
  46. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  47. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  48. }else{
  49. $data['addtime'] = time();
  50. $cat_id = D("Catalog")->add($data);
  51. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  52. }
  53. if (!$return) {
  54. $return['error_code'] = 10103 ;
  55. $return['error_message'] = 'request fail' ;
  56. }
  57. $this->sendResult($return);
  58. }
  59. //获取目录列表
  60. public function catList(){
  61. $item_id = I("item_id/d");
  62. if ($item_id > 0 ) {
  63. $ret = D("Catalog")->where(" item_id = '$item_id' ")->order(" 's_number', addtime asc ")->select();
  64. }
  65. if ($ret) {
  66. $this->sendResult($ret);
  67. }else{
  68. $return['error_code'] = 10103 ;
  69. $return['error_message'] = 'request fail' ;
  70. $this->sendResult($return);
  71. }
  72. }
  73. //获取二级目录列表
  74. public function secondCatList(){
  75. $item_id = I("item_id/d");
  76. if ($item_id > 0 ) {
  77. $ret = D("Catalog")->where(" item_id = '$item_id' and level =2 ")->order(" 's_number', addtime asc ")->select();
  78. }
  79. if ($ret) {
  80. $this->sendResult($ret);
  81. }else{
  82. $return['error_code'] = 10103 ;
  83. $return['error_message'] = 'request fail' ;
  84. $this->sendResult($return);
  85. }
  86. }
  87. //获取一个目录的子目录列表(如果存在的话)
  88. public function childCatList(){
  89. $cat_id = I("cat_id/d");
  90. if ($cat_id > 0 ) {
  91. $ret = D("Catalog")->where(" parent_cat_id = '$cat_id' ")->order(" 's_number', addtime asc ")->select();
  92. }
  93. if ($ret) {
  94. $this->sendResult($ret);
  95. }else{
  96. $return['error_code'] = 10103 ;
  97. $return['error_message'] = 'request fail' ;
  98. $this->sendResult($return);
  99. }
  100. }
  101. //删除目录
  102. public function delete(){
  103. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  104. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  105. $item_id = $cat['item_id'];
  106. $login_user = $this->checkLogin();
  107. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  108. $return['error_code'] = -1 ;
  109. $return['error_message'] = L('no_permissions');
  110. $this->sendResult($return);
  111. return;
  112. }
  113. if (D("Page")->where(" cat_id = '$cat_id' ")->find()) {
  114. $return['error_code'] = -1 ;
  115. $return['error_message'] = L('no_delete_empty_catalog') ;
  116. $this->sendResult($return);
  117. return;
  118. }
  119. if ($cat_id > 0 ) {
  120. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->delete();
  121. }
  122. if ($ret) {
  123. $this->sendResult($ret);
  124. }else{
  125. $return['error_code'] = -1 ;
  126. $return['error_message'] = 'request fail' ;
  127. $this->sendResult($return);
  128. }
  129. }
  130. }