ItemModel.class.php 12 KB

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