CatalogController.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. //禁止空目录的生成
  37. if (!$cat_name) {
  38. return;
  39. }
  40. $data['cat_name'] = $cat_name ;
  41. $data['s_number'] = $s_number ;
  42. $data['item_id'] = $item_id ;
  43. $data['parent_cat_id'] = $parent_cat_id ;
  44. if ($parent_cat_id > 0 ) {
  45. $data['level'] = 3;
  46. }else{
  47. $data['level'] = 2;
  48. }
  49. if ($cat_id > 0 ) {
  50. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  51. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  52. }else{
  53. $data['addtime'] = time();
  54. $cat_id = D("Catalog")->add($data);
  55. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  56. }
  57. if (!$return) {
  58. $return['error_code'] = 10103 ;
  59. $return['error_message'] = 'request fail' ;
  60. }
  61. $this->sendResult($return);
  62. }
  63. //获取目录列表
  64. public function catList(){
  65. $item_id = I("item_id/d");
  66. if ($item_id > 0 ) {
  67. $ret = D("Catalog")->where(" item_id = '$item_id' ")->order(" 's_number', addtime asc ")->select();
  68. }
  69. if ($ret) {
  70. $this->sendResult($ret);
  71. }else{
  72. $return['error_code'] = 10103 ;
  73. $return['error_message'] = 'request fail' ;
  74. $this->sendResult($return);
  75. }
  76. }
  77. //获取二级目录列表
  78. public function secondCatList(){
  79. $item_id = I("item_id/d");
  80. if ($item_id > 0 ) {
  81. $ret = D("Catalog")->where(" item_id = '$item_id' and level =2 ")->order(" 's_number', addtime asc ")->select();
  82. }
  83. if ($ret) {
  84. $this->sendResult($ret);
  85. }else{
  86. $return['error_code'] = 10103 ;
  87. $return['error_message'] = 'request fail' ;
  88. $this->sendResult($return);
  89. }
  90. }
  91. //获取一个目录的子目录列表(如果存在的话)
  92. public function childCatList(){
  93. $cat_id = I("cat_id/d");
  94. if ($cat_id > 0 ) {
  95. $ret = D("Catalog")->where(" parent_cat_id = '$cat_id' ")->order(" 's_number', addtime asc ")->select();
  96. }
  97. if ($ret) {
  98. $this->sendResult($ret);
  99. }else{
  100. $return['error_code'] = 10103 ;
  101. $return['error_message'] = 'request fail' ;
  102. $this->sendResult($return);
  103. }
  104. }
  105. //删除目录
  106. public function delete(){
  107. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  108. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  109. $item_id = $cat['item_id'];
  110. $login_user = $this->checkLogin();
  111. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  112. $return['error_code'] = -1 ;
  113. $return['error_message'] = L('no_permissions');
  114. $this->sendResult($return);
  115. return;
  116. }
  117. if (D("Page")->where(" cat_id = '$cat_id' ")->find() || D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find()) {
  118. $return['error_code'] = -1 ;
  119. $return['error_message'] = L('no_delete_empty_catalog') ;
  120. $this->sendResult($return);
  121. return;
  122. }
  123. if ($cat_id > 0 ) {
  124. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->delete();
  125. }
  126. if ($ret) {
  127. $this->sendResult($ret);
  128. }else{
  129. $return['error_code'] = -1 ;
  130. $return['error_message'] = 'request fail' ;
  131. $this->sendResult($return);
  132. }
  133. }
  134. }