CatalogController.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $cat_id = I("cat_id/d");
  59. if ($cat_id > 0 ) {
  60. $row = D("Catalog")->where(" cat_id = '$cat_id' ")->find() ;
  61. $item_id = $row['item_id'] ;
  62. $ret = D("Catalog")->getChlid($item_id , $cat_id);
  63. }
  64. if ($ret) {
  65. $this->sendResult($ret);
  66. }else{
  67. $this->sendResult(array());
  68. }
  69. }
  70. //保存目录
  71. public function save(){
  72. $cat_name = I("cat_name");
  73. $s_number = I("s_number/d") ? I("s_number/d") : 99 ;
  74. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  75. $parent_cat_id = I("parent_cat_id/d")? I("parent_cat_id/d") : 0;
  76. $item_id = I("item_id/d");
  77. $login_user = $this->checkLogin();
  78. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  79. $this->sendError(10103);
  80. return;
  81. }
  82. //禁止空目录的生成
  83. if (!$cat_name) {
  84. return;
  85. }
  86. if ($parent_cat_id && $parent_cat_id == $cat_id) {
  87. $this->sendError(10101,"上级目录不能选择自身");
  88. return;
  89. }
  90. $data['cat_name'] = $cat_name ;
  91. $data['s_number'] = $s_number ;
  92. $data['item_id'] = $item_id ;
  93. $data['parent_cat_id'] = $parent_cat_id ;
  94. if ($parent_cat_id > 0 ) {
  95. $data['level'] = 3;
  96. }else{
  97. $data['level'] = 2;
  98. }
  99. if ($cat_id > 0 ) {
  100. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  101. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  102. }else{
  103. $data['addtime'] = time();
  104. $cat_id = D("Catalog")->add($data);
  105. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  106. }
  107. if (!$return) {
  108. $return['error_code'] = 10103 ;
  109. $return['error_message'] = 'request fail' ;
  110. }
  111. $this->sendResult($return);
  112. }
  113. //删除目录
  114. public function delete(){
  115. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  116. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  117. $item_id = $cat['item_id'];
  118. $login_user = $this->checkLogin();
  119. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  120. $return['error_code'] = -1 ;
  121. $return['error_message'] = L('no_permissions');
  122. $this->sendResult($return);
  123. return;
  124. }
  125. if (D("Page")->where(" cat_id = '$cat_id' and is_del = 0")->find() || D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find()) {
  126. $return['error_code'] = -1 ;
  127. $return['error_message'] = L('no_delete_empty_catalog') ;
  128. $this->sendResult($return);
  129. return;
  130. }
  131. if ($cat_id > 0 ) {
  132. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->delete();
  133. }
  134. if ($ret) {
  135. $this->sendResult($ret);
  136. }else{
  137. $return['error_code'] = -1 ;
  138. $return['error_message'] = 'request fail' ;
  139. $this->sendResult($return);
  140. }
  141. }
  142. //编辑页面时,自动帮助用户选中目录
  143. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  144. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  145. public function getDefaultCat(){
  146. $login_user = $this->checkLogin();
  147. $page_id = I("page_id/d");
  148. $item_id = I("item_id/d");
  149. $page_history_id = I("page_history_id/d");
  150. $copy_page_id = I("copy_page_id/d");
  151. if ($page_id > 0 ) {
  152. if ($page_history_id) {
  153. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  154. }else{
  155. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  156. }
  157. $default_cat_id = $page['cat_id'];
  158. }
  159. //如果是复制接口
  160. elseif ($copy_page_id) {
  161. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  162. $page['item_id'] = $copy_page['item_id'];
  163. $default_cat_id = $copy_page['cat_id'];
  164. }else{
  165. //查找用户上一次设置的目录
  166. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  167. $default_cat_id = $last_page['cat_id'];
  168. }
  169. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  170. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  171. $this->sendError(10101,L('no_permissions'));
  172. return;
  173. }
  174. $Catalog = D("Catalog")->where(" cat_id = '$default_cat_id' ")->find();
  175. if ($Catalog['parent_cat_id']) {
  176. $default_cat_id2 = $Catalog['parent_cat_id'];
  177. $default_cat_id3 = $default_cat_id;
  178. }else{
  179. $default_cat_id2 = $default_cat_id;
  180. }
  181. $this->sendResult(array("default_cat_id2"=>$default_cat_id2 , "default_cat_id3"=>$default_cat_id3));
  182. }
  183. }