You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

170 lines
4.6 KiB

3 years ago
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class CatalogModel extends BaseModel {
  9. //获取目录列表。如果isGroup参数为true,则按分组返回
  10. public function getList($item_id,$isGroup = false ){
  11. if ($item_id > 0 ) {
  12. $ret = $this->where(" item_id = '%d' ",array($item_id))->order(" s_number, cat_id asc ")->select();
  13. }
  14. if ($ret) {
  15. foreach ($ret as $key => $value) {
  16. $ret[$key]['addtime'] = date("Y-m-d H:i:s",$value['addtime']) ;
  17. }
  18. if ($isGroup) {
  19. $ret2 = array() ;
  20. foreach ($ret as $key => $value) {
  21. if ($value['parent_cat_id']) {
  22. //跳过
  23. //
  24. }else{
  25. $value['sub'] = $this->_getChlid($value['cat_id'],$ret);
  26. $ret2[] = $value ;
  27. }
  28. }
  29. $ret = $ret2 ;
  30. }
  31. return $ret ;
  32. }else{
  33. return array();
  34. }
  35. }
  36. //获取某个目录的子 (如果存在的话) 此private方法只给本类内调用
  37. private function _getChlid($cat_id,$item_data){
  38. $return = array() ;
  39. if ($item_data && $cat_id) {
  40. foreach ($item_data as $key => $value) {
  41. if ($value['parent_cat_id'] == $cat_id ) {
  42. $value['sub'] = $this->_getChlid($value['cat_id'],$item_data);
  43. $return[] = $value ;
  44. }
  45. }
  46. }
  47. return $return;
  48. }
  49. //获取某id下的子目录列表(此public方法暴露出去给其他地方调用)
  50. public function getChlid($item_id,$cat_id){
  51. $return = array() ;
  52. $ret = $this->getList($item_id , true) ;
  53. if ($ret) {
  54. foreach ($ret as $key => $value) {
  55. if ($value['cat_id'] == $cat_id) {
  56. $return = $value['sub'] ;
  57. }
  58. if ($value['sub']) {
  59. foreach ($value['sub'] as $key2 => $value2) {
  60. if ($value2['cat_id'] == $cat_id) {
  61. $return = $value2['sub'] ;
  62. }
  63. if ($value2['sub']) {
  64. foreach ($value2['sub'] as $key3 => $value3) {
  65. if ($value3['cat_id'] == $cat_id) {
  66. $return = $value3['sub'] ;
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. return $return ;
  75. }
  76. //获取某个层级的目录列表。例如 获取二级目录列表
  77. public function getListByLevel($item_id , $level = 2){
  78. $return = array() ;
  79. $ret = $this->getList($item_id) ;
  80. if ($ret) {
  81. foreach ($ret as $key => $value) {
  82. if ($value['level'] == $level) {
  83. $return[] = $value ;
  84. }
  85. }
  86. }
  87. return $return ;
  88. }
  89. //删除目录以及下面的所有页面/子目录
  90. public function deleteCat($cat_id){
  91. if (!$cat_id) {
  92. return false;
  93. }
  94. //如果有子目录的话,递归把子目录清了
  95. $cats = $this->where(" parent_cat_id = '$cat_id' ")->select();
  96. if ($cats) {
  97. foreach ($cats as $key => $value) {
  98. $this->deleteCat($value['cat_id']);
  99. }
  100. }
  101. //获取当前目录信息
  102. $cat = $this->where(" cat_id = '$cat_id' ")->find();
  103. $item_id = $cat['item_id'];
  104. $all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->field("page_id,cat_id")->select();
  105. $pages = array() ;
  106. if ($all_pages) {
  107. foreach ($all_pages as $key => $value) {
  108. if ($value['cat_id'] == $cat_id) {
  109. $pages[] = $value ;
  110. }
  111. }
  112. }
  113. if ($pages) {
  114. foreach ($pages as $key => $value) {
  115. D("Page")->softDeletePage($value['page_id']);
  116. }
  117. }
  118. $this->where(" cat_id = '$cat_id' ")->delete();
  119. return true ;
  120. }
  121. //根据用户目录权限来过滤目录数据
  122. public function filteMemberCat($uid , $catData){
  123. if(!$catData || !$catData[0]['item_id']){
  124. return $catData ;
  125. }
  126. $item_id = $catData[0]['item_id'] ;
  127. $cat_id = 0 ;
  128. //首先看是否被添加为项目成员
  129. $itemMember = D("ItemMember")->where("uid = '$uid' and item_id = '$item_id' ")->find() ;
  130. if($itemMember && $itemMember['cat_id'] > 0 ){
  131. $cat_id = $itemMember['cat_id'] ;
  132. }
  133. //再看是否添加为团队-项目成员
  134. $teamItemMember = D("TeamItemMember")->where("member_uid = '$uid' and item_id = '$item_id' ")->find() ;
  135. if($teamItemMember && $teamItemMember['cat_id'] > 0 ){
  136. $cat_id = $teamItemMember['cat_id'] ;
  137. }
  138. //开始根据cat_id过滤
  139. if($cat_id > 0 ){
  140. foreach ($catData as $key => $value) {
  141. if( $value['cat_id'] != $cat_id){
  142. unset($catData[$key]);
  143. }
  144. }
  145. $catData = array_values($catData);
  146. }
  147. return $catData ;
  148. }
  149. }