CatalogController.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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")->where(" item_id = '$item_id' ")->order(" 's_number', addtime asc ")->select();
  15. }
  16. if ($ret) {
  17. $this->sendResult($ret);
  18. }else{
  19. $this->sendResult(array());
  20. }
  21. }
  22. //获取二级目录列表
  23. public function secondCatList(){
  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")->where(" item_id = '$item_id' and level =2 ")->order(" 's_number', addtime asc ")->select();
  32. }
  33. if ($ret) {
  34. $this->sendResult($ret);
  35. }else{
  36. $this->sendResult(array());
  37. }
  38. }
  39. //获取目录列表
  40. public function catListGroup(){
  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")->where(" item_id = '$item_id' and level = 2 ")->order(" s_number, addtime asc ")->select();
  49. if (!empty($ret)) {
  50. foreach ($ret as $key => &$value) {
  51. $value['addtime'] = date("Y-m-d H:i:s",$value['addtime']) ;
  52. $ret2 = D("Catalog")->where(" parent_cat_id = '$value[cat_id]' ")->order(" s_number, addtime asc ")->select();
  53. if (empty($ret2)) {
  54. $value['sub'] = array() ;
  55. }else{
  56. foreach ($ret2 as $key2 => $value2) {
  57. $ret2[$key2]['addtime'] = date("Y-m-d H:i:s",$value2['addtime']) ;
  58. }
  59. $value['sub'] = $ret2 ;
  60. }
  61. }
  62. }
  63. }
  64. if ($ret) {
  65. $this->sendResult($ret);
  66. }else{
  67. $this->sendResult(array());
  68. }
  69. }
  70. //获取二级目录的子目录列表,即三级目录列表(如果存在的话)
  71. public function childCatList(){
  72. $cat_id = I("cat_id/d");
  73. if ($cat_id > 0 ) {
  74. $ret = D("Catalog")->where(" parent_cat_id = '$cat_id' ")->order(" 's_number', addtime asc ")->select();
  75. }
  76. if ($ret) {
  77. $this->sendResult($ret);
  78. }else{
  79. $this->sendResult(array());
  80. }
  81. }
  82. //保存目录
  83. public function save(){
  84. $cat_name = I("cat_name");
  85. $s_number = I("s_number/d") ? I("s_number/d") : 99 ;
  86. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  87. $parent_cat_id = I("parent_cat_id/d")? I("parent_cat_id/d") : 0;
  88. $item_id = I("item_id/d");
  89. $login_user = $this->checkLogin();
  90. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  91. $this->sendError(10103);
  92. return;
  93. }
  94. //禁止空目录的生成
  95. if (!$cat_name) {
  96. return;
  97. }
  98. if ($parent_cat_id && $parent_cat_id == $cat_id) {
  99. $this->sendError(10101,"上级目录不能选择自身");
  100. return;
  101. }
  102. $data['cat_name'] = $cat_name ;
  103. $data['s_number'] = $s_number ;
  104. $data['item_id'] = $item_id ;
  105. $data['parent_cat_id'] = $parent_cat_id ;
  106. if ($parent_cat_id > 0 ) {
  107. $data['level'] = 3;
  108. }else{
  109. $data['level'] = 2;
  110. }
  111. if ($cat_id > 0 ) {
  112. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->save($data);
  113. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  114. }else{
  115. $data['addtime'] = time();
  116. $cat_id = D("Catalog")->add($data);
  117. $return = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  118. }
  119. if (!$return) {
  120. $return['error_code'] = 10103 ;
  121. $return['error_message'] = 'request fail' ;
  122. }
  123. $this->sendResult($return);
  124. }
  125. //删除目录
  126. public function delete(){
  127. $cat_id = I("cat_id/d")? I("cat_id/d") : 0;
  128. $cat = D("Catalog")->where(" cat_id = '$cat_id' ")->find();
  129. $item_id = $cat['item_id'];
  130. $login_user = $this->checkLogin();
  131. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  132. $return['error_code'] = -1 ;
  133. $return['error_message'] = L('no_permissions');
  134. $this->sendResult($return);
  135. return;
  136. }
  137. if (D("Page")->where(" cat_id = '$cat_id' ")->find() || D("Catalog")->where(" parent_cat_id = '$cat_id' ")->find()) {
  138. $return['error_code'] = -1 ;
  139. $return['error_message'] = L('no_delete_empty_catalog') ;
  140. $this->sendResult($return);
  141. return;
  142. }
  143. if ($cat_id > 0 ) {
  144. $ret = D("Catalog")->where(" cat_id = '$cat_id' ")->delete();
  145. }
  146. if ($ret) {
  147. $this->sendResult($ret);
  148. }else{
  149. $return['error_code'] = -1 ;
  150. $return['error_message'] = 'request fail' ;
  151. $this->sendResult($return);
  152. }
  153. }
  154. //编辑页面时,自动帮助用户选中目录
  155. //选中的规则是:编辑页面则选中该页面目录,复制页面则选中目标页面目录;
  156. // 如果是恢复历史页面则使用历史页面的目录,如果都没有则选中用户上次使用的目录
  157. public function getDefaultCat(){
  158. $login_user = $this->checkLogin();
  159. $page_id = I("page_id/d");
  160. $item_id = I("item_id/d");
  161. $page_history_id = I("page_history_id/d");
  162. $copy_page_id = I("copy_page_id/d");
  163. if ($page_id > 0 ) {
  164. if ($page_history_id) {
  165. $page = D("PageHistory")->where(" page_history_id = '$page_history_id' ")->find();
  166. }else{
  167. $page = M("Page")->where(" page_id = '$page_id' ")->find();
  168. }
  169. $default_cat_id = $page['cat_id'];
  170. }
  171. //如果是复制接口
  172. elseif ($copy_page_id) {
  173. $copy_page = M("Page")->where(" page_id = '$copy_page_id' ")->find();
  174. $page['item_id'] = $copy_page['item_id'];
  175. $default_cat_id = $copy_page['cat_id'];
  176. }else{
  177. //查找用户上一次设置的目录
  178. $last_page = D("Page")->where(" author_uid ='$login_user[uid]' and item_id = '$item_id' ")->order(" addtime desc ")->limit(1)->find();
  179. $default_cat_id = $last_page['cat_id'];
  180. }
  181. $item_id = $page['item_id'] ?$page['item_id'] :$item_id;
  182. if (!$this->checkItemPermn($login_user['uid'] , $item_id)) {
  183. $this->message(L('no_permissions'));
  184. return;
  185. }
  186. $Catalog = D("Catalog")->where(" cat_id = '$default_cat_id' ")->find();
  187. if ($Catalog['parent_cat_id']) {
  188. $default_cat_id2 = $Catalog['parent_cat_id'];
  189. $default_cat_id3 = $default_cat_id;
  190. }else{
  191. $default_cat_id2 = $default_cat_id;
  192. }
  193. $this->sendResult(array("default_cat_id2"=>$default_cat_id2 , "default_cat_id3"=>$default_cat_id3));
  194. }
  195. }