CatalogController.class.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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("你无权限");
  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("你无权限");
  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. }
  43. if ($cat_id > 0 ) {
  44. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  45. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  46. }else{
  47. $data['addtime'] = time();
  48. $cat_id = D("Catalog")->add($data);
  49. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  50. }
  51. if (!$return) {
  52. $return['error_code'] = 10103 ;
  53. $return['error_message'] = 'request fail' ;
  54. }
  55. $this->sendResult($return);
  56. }
  57. //获取目录列表
  58. public function catList(){
  59. $item_id = I("item_id/d");
  60. if ($item_id > 0 ) {
  61. $ret = D("Catalog")->where(" item_id = '$item_id' ")->order(" 's_number', addtime asc ")->select();
  62. }
  63. if ($ret) {
  64. $this->sendResult($ret);
  65. }else{
  66. $return['error_code'] = 10103 ;
  67. $return['error_message'] = 'request fail' ;
  68. $this->sendResult($return);
  69. }
  70. }
  71. //获取二级目录列表
  72. public function secondCatList(){
  73. $item_id = I("item_id/d");
  74. if ($item_id > 0 ) {
  75. $ret = D("Catalog")->where(" item_id = '$item_id' and level =2 ")->order(" 's_number', addtime asc ")->select();
  76. }
  77. if ($ret) {
  78. $this->sendResult($ret);
  79. }else{
  80. $return['error_code'] = 10103 ;
  81. $return['error_message'] = 'request fail' ;
  82. $this->sendResult($return);
  83. }
  84. }
  85. //获取一个目录的子目录列表(如果存在的话)
  86. public function childCatList(){
  87. $cat_id = I("cat_id/d");
  88. if ($cat_id > 0 ) {
  89. $ret = D("Catalog")->where(" parent_cat_id = '$cat_id' ")->order(" 's_number', addtime asc ")->select();
  90. }
  91. if ($ret) {
  92. $this->sendResult($ret);
  93. }else{
  94. $return['error_code'] = 10103 ;
  95. $return['error_message'] = 'request fail' ;
  96. $this->sendResult($return);
  97. }
  98. }
  99. //删除目录
  100. public function delete(){
  101. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  102. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  103. $item_id = $cat['item_id'];
  104. $login_user = $this->checkLogin();
  105. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  106. $return['error_code'] = -1 ;
  107. $return['error_message'] = '你无权限' ;
  108. $this->sendResult($return);
  109. return;
  110. }
  111. if (D("Page")->where(" cat_id = '$cat_id' ")->find()) {
  112. $return['error_code'] = -1 ;
  113. $return['error_message'] = '为了安全,不允许直接删除非空目录。请先删除或转移该目录下的所有页面' ;
  114. $this->sendResult($return);
  115. return;
  116. }
  117. if ($cat_id > 0 ) {
  118. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->limit(1)->delete();
  119. }
  120. if ($ret) {
  121. $this->sendResult($ret);
  122. }else{
  123. $return['error_code'] = -1 ;
  124. $return['error_message'] = 'request fail' ;
  125. $this->sendResult($return);
  126. }
  127. }
  128. }