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.

286 lines
12 KiB

3 years ago
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. class ItemModel extends BaseModel {
  5. public function export($item_id , $uncompress = 0){
  6. $item = D("Item")->where("item_id = '$item_id' ")->field(" item_type, item_name ,item_description,password ")->find();
  7. $page_field = "page_title ,cat_id,page_content,s_number,page_comments";
  8. $catalog_field = "cat_id,cat_name ,parent_cat_id,level,s_number";
  9. $item['pages'] = $this->getContent($item_id , $page_field , $catalog_field , $uncompress);
  10. $item['members'] = D("ItemMember")->where("item_id = '$item_id' ")->field(" member_group_id ,uid,username ")->select();
  11. return json_encode($item);
  12. }
  13. public function import($json,$uid,$item_name= '',$item_description= '',$item_password = '',$item_domain = ''){
  14. $userInfo = D("User")->userInfo($uid);
  15. $item = json_decode($json ,1 );
  16. unset($json);
  17. if ($item) {
  18. if ($item['item_domain']) {
  19. $item2 = D("Item")->where("item_domain = '%s' ".array($item['item_domain']))->find();
  20. if ($item2) {
  21. //个性域名已经存在
  22. return false;
  23. }
  24. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  25. //echo '个性域名只能是字母或数字的组合';exit;
  26. return false;
  27. }
  28. }else{
  29. $item['item_domain'] = '';
  30. }
  31. $item_data = array(
  32. "item_name"=>$item_name ? $this->_htmlspecialchars($item_name) : $this->_htmlspecialchars($item['item_name']) ,
  33. "item_domain"=>$item_domain ? $this->_htmlspecialchars($item_domain) : $this->_htmlspecialchars($item['item_domain']) ,
  34. "item_type"=>$this->_htmlspecialchars($item['item_type']),
  35. "item_description"=>$item_description ? $this->_htmlspecialchars($item_description) : $this->_htmlspecialchars($item['item_description']),
  36. "password"=>$item_password ? $this->_htmlspecialchars($item_password) : $this->_htmlspecialchars($item['password']),
  37. "uid"=>$userInfo['uid'],
  38. "username"=>$userInfo['username'],
  39. "addtime"=>time(),
  40. );
  41. $item_id = D("Item")->add($item_data);
  42. }
  43. if ($item['pages']) {
  44. //父页面们(一级目录)
  45. if ($item['pages']['pages']) {
  46. foreach ($item['pages']['pages'] as $key => &$value) {
  47. $page_data = array(
  48. "author_uid"=>$userInfo['uid'],
  49. "author_username"=>$userInfo['username'],
  50. "page_title" => $this->_htmlspecialchars($value['page_title']) ,
  51. "page_content" => $this->_htmlspecialchars($value['page_content']) ,
  52. "s_number" =>$this->_htmlspecialchars($value['s_number']) ,
  53. "page_comments" =>$this->_htmlspecialchars($value['page_comments']),
  54. "item_id" => $item_id,
  55. "cat_id" => 0 ,
  56. "addtime" =>time(),
  57. );
  58. D("Page")->add($page_data);
  59. unset($page_data);
  60. }
  61. unset($item['pages']['pages']);
  62. }
  63. //二级目录
  64. if ($item['pages']['catalogs']) {
  65. $this->_insertCat($item_id , $item['pages']['catalogs'] , $userInfo , 0 , 2 ) ;
  66. }
  67. }
  68. if ($item['members']) {
  69. foreach ($item['members'] as $key => $value) {
  70. $member_data = array(
  71. "member_group_id"=>$value['member_group_id'],
  72. "uid"=>$value['uid'],
  73. "username"=>$value['username'],
  74. "item_id"=>$item_id,
  75. "addtime"=>time(),
  76. );
  77. // 不再导入成员数据 D("ItemMember")->add($member_data);
  78. }
  79. }
  80. return $item_id;
  81. }
  82. //插入一个目录下的所有页面和子目录
  83. private function _insertCat($item_id , $catalogs , $userInfo , $parent_cat_id = 0 , $level = 2 ){
  84. if (!$catalogs) {
  85. return ;
  86. }
  87. foreach ($catalogs as $key => $value) {
  88. $catalog_data = array(
  89. "cat_name" => $this->_htmlspecialchars($value['cat_name']) ,
  90. "level" => $level ,
  91. "s_number" => $this->_htmlspecialchars($value['s_number']) ,
  92. "item_id" => $item_id,
  93. "parent_cat_id" => $parent_cat_id,
  94. "addtime" =>time(),
  95. );
  96. $cat_id = D("Catalog")->add($catalog_data);
  97. //该目录下的页面们
  98. if ($value['pages']) {
  99. foreach ($value['pages'] as $key2 => &$value2) {
  100. $page_data = array(
  101. "author_uid"=>$userInfo['uid'],
  102. "author_username"=>$userInfo['username'],
  103. "page_title" =>$this->_htmlspecialchars( $value2['page_title']),
  104. "page_content" =>$this->_htmlspecialchars( $value2['page_content']),
  105. "s_number" =>$this->_htmlspecialchars( $value2['s_number']),
  106. "page_comments" =>$this->_htmlspecialchars( $value2['page_comments']),
  107. "item_id" => $item_id,
  108. "cat_id" => $cat_id ,
  109. "addtime" =>time(),
  110. );
  111. D("Page")->add($page_data);
  112. unset($page_data);
  113. unset($value2);
  114. }
  115. }
  116. //该目录的子目录
  117. if ($value['catalogs']) {
  118. $this->_insertCat($item_id , $value['catalogs'] , $userInfo , $cat_id, $level + 1 ) ;
  119. }
  120. }
  121. }
  122. public function copy($item_id,$uid,$item_name= '',$item_description= '',$item_password = '',$item_domain=''){
  123. return $this->import($this->export($item_id),$uid,$item_name,$item_description,$item_password,$item_domain);
  124. }
  125. //获取菜单结构
  126. public function getMemu($item_id){
  127. $page_field = "page_id,author_uid,cat_id,page_title,addtime";
  128. $catalog_field = '*';
  129. $data = $this->getContent($item_id , $page_field , $catalog_field) ;
  130. return $data ;
  131. }
  132. public function getContent($item_id , $page_field ="*" , $catalog_field ="*" , $uncompress = 0 ){
  133. //获取所有父目录id为0的页面
  134. $all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->order(" s_number asc , page_id asc ")->field($page_field)->select();
  135. $pages = array() ;
  136. if ($all_pages) {
  137. foreach ($all_pages as $key => $value) {
  138. if ($value['cat_id']) {
  139. # code...
  140. }else{
  141. $pages[] = $value ;
  142. }
  143. }
  144. }
  145. //获取该项目下的所有目录
  146. $all_catalogs = D("Catalog")->field($catalog_field)->where("item_id = '$item_id' ")->order(" s_number asc , cat_id asc ")->select();
  147. //获取所有二级目录
  148. $catalogs = array() ;
  149. if ($all_catalogs) {
  150. foreach ($all_catalogs as $key => $value) {
  151. if ($value['level'] == 2 ) {
  152. $catalogs[] = $value;
  153. }
  154. }
  155. }
  156. if ($catalogs) {
  157. foreach ($catalogs as $key => &$catalog2) {
  158. $catalog2 = $this->_getCat($catalog2 , $all_pages , $all_catalogs);
  159. }
  160. }
  161. $menu = array(
  162. "pages" =>$pages,
  163. "catalogs" =>$catalogs,
  164. );
  165. unset($pages);
  166. unset($catalogs);
  167. return $menu;
  168. }
  169. //获取某个目录下的页面和子目录
  170. private function _getCat($catalog_data , & $all_pages , & $all_catalogs){
  171. $catalog_data['pages'] = $this->_getPageByCatId($catalog_data['cat_id'],$all_pages);
  172. //该目录下的所有子目录
  173. $sub_catalogs = $this->_getCatByCatId($catalog_data['cat_id'],$all_catalogs);
  174. if ($sub_catalogs) {
  175. foreach ($sub_catalogs as $key => $value) {
  176. $catalog_data['catalogs'][] = $this->_getCat($value , $all_pages , $all_catalogs ) ;
  177. }
  178. }
  179. if(!$catalog_data['catalogs']){
  180. $catalog_data['catalogs'] = array() ;
  181. }
  182. return $catalog_data ;
  183. }
  184. //获取某个目录下的所有页面
  185. private function _getPageByCatId($cat_id ,$all_pages){
  186. $pages = array() ;
  187. if ($all_pages) {
  188. foreach ($all_pages as $key => $value) {
  189. if ($value['cat_id'] == $cat_id) {
  190. $pages[] = $value ;
  191. }
  192. }
  193. }
  194. return $pages;
  195. }
  196. //获取某个目录下的所有子目录
  197. private function _getCatByCatId($cat_id ,$all_catalogs){
  198. $cats = array() ;
  199. if ($all_catalogs) {
  200. foreach ($all_catalogs as $key => $value) {
  201. if ($value['parent_cat_id'] == $cat_id) {
  202. $cats[] = $value ;
  203. }
  204. }
  205. }
  206. return $cats;
  207. }
  208. //删除项目
  209. public function delete_item($item_id){
  210. D("Page")->where("item_id = '$item_id' ")->delete();
  211. D("Page")->where("item_id = '$item_id' ")->delete();
  212. D("Catalog")->where("item_id = '$item_id' ")->delete();
  213. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  214. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  215. D("TeamItem")->where("item_id = '$item_id' ")->delete();
  216. D("TeamItemMember")->where("item_id = '$item_id' ")->delete();
  217. return D("Item")->where("item_id = '$item_id' ")->delete();
  218. }
  219. //软删除项目
  220. public function soft_delete_item($item_id){
  221. return $this->where("item_id = '$item_id' ")->save(array("is_del"=>1 ,"last_update_time"=>time()));
  222. }
  223. private function _htmlspecialchars($str){
  224. if (!$str) {
  225. return '' ;
  226. }
  227. //之所以先htmlspecialchars_decode是为了防止被htmlspecialchars转义了两次
  228. return htmlspecialchars(htmlspecialchars_decode($str));
  229. }
  230. //根据用户目录权限来过滤项目数据
  231. public function filteMemberItem($uid , $item_id , $menuData){
  232. if(!$menuData || !$menuData['catalogs']){
  233. return $menuData ;
  234. }
  235. $cat_id = 0 ;
  236. //首先看是否被添加为项目成员
  237. $itemMember = D("ItemMember")->where("uid = '$uid' and item_id = '$item_id' ")->find() ;
  238. if($itemMember && $itemMember['cat_id'] > 0 ){
  239. $cat_id = $itemMember['cat_id'] ;
  240. }
  241. //再看是否添加为团队-项目成员
  242. $teamItemMember = D("TeamItemMember")->where("member_uid = '$uid' and item_id = '$item_id' ")->find() ;
  243. if($teamItemMember && $teamItemMember['cat_id'] > 0 ){
  244. $cat_id = $teamItemMember['cat_id'] ;
  245. }
  246. //开始根据cat_id过滤
  247. if($cat_id > 0 ){
  248. foreach ($menuData['catalogs'] as $key => $value) {
  249. if( $value['cat_id'] != $cat_id){
  250. unset($menuData['catalogs'][$key]);
  251. }
  252. }
  253. $menuData['catalogs'] = array_values($menuData['catalogs']);
  254. }
  255. return $menuData ;
  256. }
  257. }