CatalogModel.class.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. $cat_id = intval($cat_id) ;
  95. //如果有子目录的话,递归把子目录清了
  96. $cats = $this->where(" parent_cat_id = '$cat_id' ")->select();
  97. if ($cats) {
  98. foreach ($cats as $key => $value) {
  99. $this->deleteCat($value['cat_id']);
  100. }
  101. }
  102. //获取当前目录信息
  103. $cat = $this->where(" cat_id = '$cat_id' ")->find();
  104. $item_id = $cat['item_id'];
  105. $all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->field("page_id,cat_id")->select();
  106. $pages = array() ;
  107. if ($all_pages) {
  108. foreach ($all_pages as $key => $value) {
  109. if ($value['cat_id'] == $cat_id) {
  110. $pages[] = $value ;
  111. }
  112. }
  113. }
  114. if ($pages) {
  115. foreach ($pages as $key => $value) {
  116. D("Page")->softDeletePage($value['page_id']);
  117. }
  118. }
  119. $this->where(" cat_id = '$cat_id' ")->delete();
  120. return true ;
  121. }
  122. //根据用户目录权限来过滤目录数据
  123. public function filteMemberCat($uid , $catData){
  124. if(!$catData || !$catData[0]['item_id']){
  125. return $catData ;
  126. }
  127. $item_id = $catData[0]['item_id'] ;
  128. $cat_id = 0 ;
  129. //首先看是否被添加为项目成员
  130. $itemMember = D("ItemMember")->where("uid = '%d' and item_id = '%d' ", array($uid ,$item_id ))->find() ;
  131. if($itemMember && $itemMember['cat_id'] > 0 ){
  132. $cat_id = $itemMember['cat_id'] ;
  133. }
  134. //再看是否添加为团队-项目成员
  135. $teamItemMember = D("TeamItemMember")->where("member_uid = '%d' and item_id = '%d' ",array($uid ,$item_id ))->find() ;
  136. if($teamItemMember && $teamItemMember['cat_id'] > 0 ){
  137. $cat_id = $teamItemMember['cat_id'] ;
  138. }
  139. //开始根据cat_id过滤
  140. if($cat_id > 0 ){
  141. foreach ($catData as $key => $value) {
  142. if( $value['cat_id'] != $cat_id){
  143. unset($catData[$key]);
  144. }
  145. }
  146. $catData = array_values($catData);
  147. }
  148. return $catData ;
  149. }
  150. //复制目录
  151. // old_cat_id 原目录id
  152. // new_p_cat_id 复制完目录后,挂在哪个父目录下。这里是父目录id。可为0。默认使用old_cat_id的父目录id
  153. // $to_item_id 要复制到的项目id。可以是同一个项目,可以是跨项目。默认是同一个项目
  154. public function copy($uid , $old_cat_id , $new_p_cat_id = 0 , $to_item_id = 0 ){
  155. $userInfo = D("User")->userInfo($uid);
  156. $old_cat_ary = $this->where("cat_id = '%d' ",array($old_cat_id) )->find() ;
  157. $to_item_id = $to_item_id ? $to_item_id : $cat_ary['item_id'] ;
  158. //这里需要读取目录下的页面以及子目录信息
  159. $old_cat_data = $this->getCat($old_cat_id) ;
  160. $catalogs[] =$old_cat_data ;
  161. //获取$level.先初始化$level = 2 ;
  162. $level = 2 ;
  163. if($new_p_cat_id){
  164. $p_cat_ary = $this->where("cat_id = '%d' " ,array($new_p_cat_id) )->find() ;
  165. $level = $p_cat_ary['level'] + 1 ;
  166. }
  167. //插入
  168. $res = $this->insertCat($to_item_id , $catalogs , $userInfo , $new_p_cat_id , $level ) ;
  169. return $res ;
  170. }
  171. //获取某个目录下的页面和子目录
  172. public function getCat($cat_id){
  173. $cat_id = intval($cat_id) ;
  174. $cat_ary = $this->where("cat_id = '$cat_id' ")->find() ;
  175. $item_id = $cat_ary['item_id'] ;
  176. //获取项目下所有页面信息
  177. $all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->order(" s_number asc , page_id asc ")->field($page_field)->select();
  178. //获取项目下所有目录信息
  179. $all_catalogs = $this->where(" item_id = '%d' ",array($item_id) )->order(" s_number, cat_id asc ")->select();
  180. return D("Item")->getCat($cat_ary , $all_pages , $all_catalogs) ;
  181. }
  182. //插入一个目录下的所有页面和子目录
  183. public function insertCat($item_id , $catalogs , $userInfo , $parent_cat_id = 0 , $level = 2 ){
  184. return D("Item")->insertCat($item_id , $catalogs , $userInfo , $parent_cat_id , $level );
  185. }
  186. }