CatalogController.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Api\Controller;
  3. use Think\Controller;
  4. class CatalogController extends BaseController {
  5. //获取目录列表
  6. public function catList(){
  7. $login_user = $this->checkLogin();
  8. $item_id = I("item_id/d");
  9. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  10. $this->sendError(10103);
  11. return ;
  12. }
  13. if ($item_id > 0 ) {
  14. $ret = D("Catalog")->getList($item_id);
  15. }
  16. if ($ret) {
  17. $this->sendResult($ret);
  18. }else{
  19. $this->sendResult(array());
  20. }
  21. }
  22. //获取目录列表
  23. public function catListGroup(){
  24. $login_user = $this->checkLogin();
  25. $item_id = I("item_id/d");
  26. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  27. $this->sendError(10103);
  28. return ;
  29. }
  30. if ($item_id > 0 ) {
  31. $ret = D("Catalog")->getList($item_id,true);
  32. }
  33. if ($ret) {
  34. $this->sendResult($ret);
  35. }else{
  36. $this->sendResult(array());
  37. }
  38. }
  39. //获取二级目录列表
  40. public function secondCatList(){
  41. $login_user = $this->checkLogin();
  42. $item_id = I("item_id/d");
  43. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  44. $this->sendError(10103);
  45. return ;
  46. }
  47. if ($item_id > 0 ) {
  48. $ret = D("Catalog")->getListByLevel($item_id , 2);
  49. }
  50. if ($ret) {
  51. $this->sendResult($ret);
  52. }else{
  53. $this->sendResult(array());
  54. }
  55. }
  56. //获取二级目录的子目录列表,即三级目录列表(如果存在的话)
  57. public function childCatList(){
  58. $login_user = $this->checkLogin();
  59. $cat_id = I("cat_id/d");
  60. if ($cat_id > 0 ) {
  61. $row = D("Catalog")->where(" cat_id = '$cat_id' ")->find() ;
  62. $item_id = $row['item_id'] ;
  63. if (!$this->checkItemVisit($login_user['uid'] , $item_id)) {
  64. $this->sendError(10103);
  65. return ;
  66. }
  67. $ret = D("Catalog")->getChlid($item_id , $cat_id);
  68. }
  69. if ($ret) {
  70. $this->sendResult($ret);
  71. }else{
  72. $this->sendResult(array());
  73. }
  74. }
  75. //保存目录
  76. public function save(){
  77. $cat_name = I("cat_name");
  78. $s_number = I("s_number/d") ? I("s_number/d") : 99 ;
  79. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  80. $parent_cat_id = I("parent_cat_id/d")? I("parent_cat_id/d") : 0;
  81. $item_id = I("item_id/d");
  82. $login_user = $this->checkLogin();
  83. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  84. $this->sendError(10103);
  85. return;
  86. }
  87. //禁止空目录的生成
  88. if (!$cat_name) {
  89. return;
  90. }
  91. if ($parent_cat_id && $parent_cat_id == $cat_id) {
  92. $this->sendError(10101,"上级目录不能选择自身");
  93. return;
  94. }
  95. $data['cat_name'] = $cat_name ;
  96. $data['s_number'] = $s_number ;
  97. $data['item_id'] = $item_id ;
  98. $data['parent_cat_id'] = $parent_cat_id ;
  99. if ($parent_cat_id > 0 ) {
  100. $row = D("Catalog")->where(" cat_id = '$parent_cat_id' ")->find() ;
  101. $data['level'] = $row['level'] +1 ;
  102. }else{
  103. $data['level'] = 2;
  104. }
  105. if ($cat_id > 0 ) {
  106. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  107. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  108. }else{
  109. $data['addtime'] = time();
  110. $cat_id = D("Catalog")->add($data);
  111. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  112. }
  113. if (!$return) {
  114. $return['error_code'] = 10103 ;
  115. $return['error_message'] = 'request fail' ;
  116. }
  117. $this->sendResult($return);
  118. }
  119. //删除目录
  120. public function delete(){
  121. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  122. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  123. $item_id = $cat['item_id'];
  124. $login_user = $this->checkLogin();
  125. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  126. $return['error_code'] = -1 ;
  127. $return['error_message'] = L('no_permissions');
  128. $this->sendResult($return);
  129. return;
  130. }
  131. if (D("Page")->where(" cat_id = '$cat_id' and is_del = 0")->find() || D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find()) {
  132. $return['error_code'] = -1 ;
  133. $return['error_message'] = L('no_delete_empty_catalog') ;
  134. $this->sendResult($return);
  135. return;
  136. }
  137. if ($cat_id > 0 ) {
  138. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->delete();
  139. }
  140. if ($ret) {
  141. $this->sendResult($ret);
  142. }else{
  143. $return['error_code'] = -1 ;
  144. $return['error_message'] = 'request fail' ;
  145. $this->sendResult($return);
  146. }
  147. }
  148. //编辑页面时,自动帮助用户选中目录
  149. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  150. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  151. public function getDefaultCat(){
  152. $login_user = $this->checkLogin();
  153. $page_id = I("page_id/d");
  154. $item_id = I("item_id/d");
  155. $page_history_id = I("page_history_id/d");
  156. $copy_page_id = I("copy_page_id/d");
  157. if ($page_id > 0 ) {
  158. if ($page_history_id) {
  159. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  160. }else{
  161. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  162. }
  163. $default_cat_id = $page['cat_id'];
  164. }
  165. //如果是复制接口
  166. elseif ($copy_page_id) {
  167. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  168. $page['item_id'] = $copy_page['item_id'];
  169. $default_cat_id = $copy_page['cat_id'];
  170. }else{
  171. //查找用户上一次设置的目录
  172. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  173. $default_cat_id = $last_page['cat_id'];
  174. }
  175. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  176. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  177. $this->sendError(10101,L('no_permissions'));
  178. return;
  179. }
  180. $this->sendResult(array("default_cat_id"=>$default_cat_id ));
  181. }
  182. }