CatalogController.class.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  107. $item_id = $cat['item_id'];
  108. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  109. $this->sendError(10103);
  110. return;
  111. }
  112. //如果一个目录已经是别的目录的父目录,那么它将无法再转为level4目录
  113. if (D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find() && $data['level'] == 4 ) {
  114. $this->sendError(10101,"该目录含有子目录,不允许转为底层目录。");
  115. return;
  116. }
  117. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  118. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  119. }else{
  120. $data['addtime'] = time();
  121. $cat_id = D("Catalog")->add($data);
  122. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  123. }
  124. if (!$return) {
  125. $return['error_code'] = 10103 ;
  126. $return['error_message'] = 'request fail' ;
  127. }
  128. $this->sendResult($return);
  129. }
  130. //删除目录
  131. public function delete(){
  132. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  133. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  134. $item_id = $cat['item_id'];
  135. $login_user = $this->checkLogin();
  136. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  137. $return['error_code'] = -1 ;
  138. $return['error_message'] = L('no_permissions');
  139. $this->sendResult($return);
  140. return;
  141. }
  142. if (D("Page")->where(" cat_id = '$cat_id' and is_del = 0")->find() || D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find()) {
  143. $return['error_code'] = -1 ;
  144. $return['error_message'] = L('no_delete_empty_catalog') ;
  145. $this->sendResult($return);
  146. return;
  147. }
  148. if ($cat_id > 0 ) {
  149. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->delete();
  150. }
  151. if ($ret) {
  152. $this->sendResult($ret);
  153. }else{
  154. $return['error_code'] = -1 ;
  155. $return['error_message'] = 'request fail' ;
  156. $this->sendResult($return);
  157. }
  158. }
  159. //编辑页面时,自动帮助用户选中目录
  160. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  161. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  162. public function getDefaultCat(){
  163. $login_user = $this->checkLogin();
  164. $page_id = I("page_id/d");
  165. $item_id = I("item_id/d");
  166. $page_history_id = I("page_history_id/d");
  167. $copy_page_id = I("copy_page_id/d");
  168. if ($page_id > 0 ) {
  169. if ($page_history_id) {
  170. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  171. }else{
  172. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  173. }
  174. $default_cat_id = $page['cat_id'];
  175. }
  176. //如果是复制接口
  177. elseif ($copy_page_id) {
  178. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  179. $page['item_id'] = $copy_page['item_id'];
  180. $default_cat_id = $copy_page['cat_id'];
  181. }else{
  182. //查找用户上一次设置的目录
  183. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  184. $default_cat_id = $last_page['cat_id'];
  185. }
  186. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  187. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  188. $this->sendError(10101,L('no_permissions'));
  189. return;
  190. }
  191. $this->sendResult(array("default_cat_id"=>$default_cat_id ));
  192. }
  193. }