CatalogController.class.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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") : '' ;
  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. if($s_number)$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 ($cat_id > 0 ) {
  143. $ret = D("Catalog")->deleteCat($cat_id);
  144. }
  145. if ($ret) {
  146. $this->sendResult($ret);
  147. }else{
  148. $return['error_code'] = -1 ;
  149. $return['error_message'] = 'request fail' ;
  150. $this->sendResult($return);
  151. }
  152. }
  153. //编辑页面时,自动帮助用户选中目录
  154. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  155. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  156. public function getDefaultCat(){
  157. $login_user = $this->checkLogin();
  158. $page_id = I("page_id/d");
  159. $item_id = I("item_id/d");
  160. $page_history_id = I("page_history_id/d");
  161. $copy_page_id = I("copy_page_id/d");
  162. if ($page_id > 0 ) {
  163. if ($page_history_id) {
  164. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  165. }else{
  166. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  167. }
  168. $default_cat_id = $page['cat_id'];
  169. }
  170. //如果是复制接口
  171. elseif ($copy_page_id) {
  172. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  173. $page['item_id'] = $copy_page['item_id'];
  174. $default_cat_id = $copy_page['cat_id'];
  175. }else{
  176. //查找用户上一次设置的目录
  177. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  178. $default_cat_id = $last_page['cat_id'];
  179. }
  180. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  181. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  182. $this->sendError(10101,L('no_permissions'));
  183. return;
  184. }
  185. $this->sendResult(array("default_cat_id"=>$default_cat_id ));
  186. }
  187. //批量更新
  188. public function batUpdate(){
  189. $cats = I("cats");
  190. $item_id = I("item_id/d");
  191. $login_user = $this->checkLogin();
  192. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  193. $this->sendError(10103);
  194. return ;
  195. }
  196. $ret = '';
  197. $data_array = json_decode(htmlspecialchars_decode($cats) , true) ;
  198. if ($data_array) {
  199. foreach ($data_array as $key => $value) {
  200. if ($value['cat_name']) {
  201. $ret = D("Catalog")->where(" cat_id = '$value[cat_id]' and item_id = '$item_id' ")->save(array(
  202. "cat_name" => $value['cat_name'] ,
  203. "parent_cat_id" => $value['parent_cat_id'] ,
  204. "level" => $value['level'] ,
  205. "s_number" => $value['s_number'] ,
  206. ));
  207. }
  208. }
  209. }
  210. $this->sendResult(array());
  211. }
  212. //获取某个目录下所有页面的标题
  213. public function getPagesBycat(){
  214. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  215. $item_id = I("item_id/d");
  216. $login_user = $this->checkLogin();
  217. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  218. $this->sendError(10103);
  219. return ;
  220. }
  221. $return = D("Page")->where("cat_id = '$cat_id' and item_id = '$item_id' and is_del = 0 ")->field("page_id , page_title,s_number")->order("`s_number` asc , `page_id` asc")->select();
  222. $this->sendResult($return);
  223. }
  224. }