ItemModel.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. $cat_id = 0 ;
  88. foreach ($catalogs as $key => $value) {
  89. $catalog_data = array(
  90. "cat_name" => $this->_htmlspecialchars($value['cat_name']) ,
  91. "level" => $level ,
  92. "s_number" => $this->_htmlspecialchars($value['s_number']) ,
  93. "item_id" => $item_id,
  94. "parent_cat_id" => $parent_cat_id,
  95. "addtime" =>time(),
  96. );
  97. $cat_id = D("Catalog")->add($catalog_data);
  98. //该目录下的页面们
  99. if ($value['pages']) {
  100. foreach ($value['pages'] as $key2 => &$value2) {
  101. $page_data = array(
  102. "author_uid"=>$userInfo['uid'],
  103. "author_username"=>$userInfo['username'],
  104. "page_title" =>$this->_htmlspecialchars( $value2['page_title']),
  105. "page_content" =>$this->_htmlspecialchars( $value2['page_content']),
  106. "s_number" =>$this->_htmlspecialchars( $value2['s_number']),
  107. "page_comments" =>$this->_htmlspecialchars( $value2['page_comments']),
  108. "item_id" => $item_id,
  109. "cat_id" => $cat_id ,
  110. "addtime" =>time(),
  111. );
  112. D("Page")->add($page_data);
  113. unset($page_data);
  114. unset($value2);
  115. }
  116. }
  117. //该目录的子目录
  118. if ($value['catalogs']) {
  119. $this->_insertCat($item_id , $value['catalogs'] , $userInfo , $cat_id, $level + 1 ) ;
  120. }
  121. }
  122. return $cat_id ;
  123. }
  124. //插入一个目录下的所有页面和子目录
  125. public function insertCat($item_id , $catalogs , $userInfo , $parent_cat_id = 0 , $level = 2 ){
  126. return $this->_insertCat($item_id , $catalogs , $userInfo , $parent_cat_id , $level );
  127. }
  128. public function copy($item_id,$uid,$item_name= '',$item_description= '',$item_password = '',$item_domain=''){
  129. return $this->import($this->export($item_id),$uid,$item_name,$item_description,$item_password,$item_domain);
  130. }
  131. //获取菜单结构
  132. public function getMemu($item_id){
  133. $page_field = "page_id,author_uid,cat_id,page_title,addtime";
  134. $catalog_field = '*';
  135. $data = $this->getContent($item_id , $page_field , $catalog_field) ;
  136. return $data ;
  137. }
  138. public function getContent($item_id , $page_field ="*" , $catalog_field ="*" , $uncompress = 0 ){
  139. //获取该项目下的所有页面
  140. $all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->order(" s_number asc , page_id asc ")->field($page_field)->select();
  141. $pages = array() ;
  142. if ($all_pages) {
  143. foreach ($all_pages as $key => $value) {
  144. if ($value['cat_id']) {
  145. # code...
  146. }else{
  147. $pages[] = $value ;
  148. }
  149. }
  150. }
  151. //获取该项目下的所有目录
  152. $all_catalogs = D("Catalog")->field($catalog_field)->where("item_id = '$item_id' ")->order(" s_number asc , cat_id asc ")->select();
  153. //获取所有二级目录
  154. $catalogs = array() ;
  155. if ($all_catalogs) {
  156. foreach ($all_catalogs as $key => $value) {
  157. if ($value['level'] == 2 ) {
  158. $catalogs[] = $value;
  159. }
  160. }
  161. }
  162. if ($catalogs) {
  163. foreach ($catalogs as $key => &$catalog2) {
  164. $catalog2 = $this->_getCat($catalog2 , $all_pages , $all_catalogs);
  165. }
  166. }
  167. $menu = array(
  168. "pages" =>$pages,
  169. "catalogs" =>$catalogs,
  170. );
  171. unset($pages);
  172. unset($catalogs);
  173. return $menu;
  174. }
  175. //获取某个目录下的页面和子目录
  176. private function _getCat($catalog_data , & $all_pages , & $all_catalogs){
  177. $catalog_data['pages'] = $this->_getPageByCatId($catalog_data['cat_id'],$all_pages);
  178. //该目录下的所有子目录
  179. $sub_catalogs = $this->_getCatByCatId($catalog_data['cat_id'],$all_catalogs);
  180. if ($sub_catalogs) {
  181. foreach ($sub_catalogs as $key => $value) {
  182. $catalog_data['catalogs'][] = $this->_getCat($value , $all_pages , $all_catalogs ) ;
  183. }
  184. }
  185. if(!$catalog_data['catalogs']){
  186. $catalog_data['catalogs'] = array() ;
  187. }
  188. return $catalog_data ;
  189. }
  190. //获取某个目录下的所有页面
  191. private function _getPageByCatId($cat_id ,$all_pages){
  192. $pages = array() ;
  193. if ($all_pages) {
  194. foreach ($all_pages as $key => $value) {
  195. if ($value['cat_id'] == $cat_id) {
  196. $pages[] = $value ;
  197. }
  198. }
  199. }
  200. return $pages;
  201. }
  202. //获取某个目录下的页面和子目录
  203. public function getCat($catalog_data , & $all_pages , & $all_catalogs){
  204. return $this->_getCat($catalog_data , $all_pages , $all_catalogs) ;
  205. }
  206. //获取某个目录下的所有子目录
  207. private function _getCatByCatId($cat_id ,$all_catalogs){
  208. $cats = array() ;
  209. if ($all_catalogs) {
  210. foreach ($all_catalogs as $key => $value) {
  211. if ($value['parent_cat_id'] == $cat_id) {
  212. $cats[] = $value ;
  213. }
  214. }
  215. }
  216. return $cats;
  217. }
  218. //删除项目
  219. public function delete_item($item_id){
  220. D("Page")->where("item_id = '$item_id' ")->delete();
  221. D("Page")->where("item_id = '$item_id' ")->delete();
  222. D("Catalog")->where("item_id = '$item_id' ")->delete();
  223. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  224. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  225. D("TeamItem")->where("item_id = '$item_id' ")->delete();
  226. D("TeamItemMember")->where("item_id = '$item_id' ")->delete();
  227. return D("Item")->where("item_id = '$item_id' ")->delete();
  228. }
  229. //软删除项目
  230. public function soft_delete_item($item_id){
  231. return $this->where("item_id = '$item_id' ")->save(array("is_del"=>1 ,"last_update_time"=>time()));
  232. }
  233. private function _htmlspecialchars($str){
  234. if (!$str) {
  235. return '' ;
  236. }
  237. //之所以先htmlspecialchars_decode是为了防止被htmlspecialchars转义了两次
  238. return htmlspecialchars(htmlspecialchars_decode($str));
  239. }
  240. //根据用户目录权限来过滤项目数据
  241. public function filteMemberItem($uid , $item_id , $menuData){
  242. if(!$menuData || !$menuData['catalogs']){
  243. return $menuData ;
  244. }
  245. $cat_id = 0 ;
  246. //首先看是否被添加为项目成员
  247. $itemMember = D("ItemMember")->where("uid = '$uid' and item_id = '$item_id' ")->find() ;
  248. if($itemMember && $itemMember['cat_id'] > 0 ){
  249. $cat_id = $itemMember['cat_id'] ;
  250. }
  251. //再看是否添加为团队-项目成员
  252. $teamItemMember = D("TeamItemMember")->where("member_uid = '$uid' and item_id = '$item_id' ")->find() ;
  253. if($teamItemMember && $teamItemMember['cat_id'] > 0 ){
  254. $cat_id = $teamItemMember['cat_id'] ;
  255. }
  256. //开始根据cat_id过滤
  257. if($cat_id > 0 ){
  258. foreach ($menuData['catalogs'] as $key => $value) {
  259. if( $value['cat_id'] != $cat_id){
  260. unset($menuData['catalogs'][$key]);
  261. }
  262. }
  263. $menuData['catalogs'] = array_values($menuData['catalogs']);
  264. }
  265. return $menuData ;
  266. }
  267. }