ItemModel.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. class ItemModel extends BaseModel {
  5. public function export($item_id){
  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 );
  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 ? $item_name :$item['item_name'],
  33. "item_domain"=>$item_domain ? $item_domain :$item['item_domain'],
  34. "item_type"=>$item['item_type'],
  35. "item_description"=>$item_description ? $item_description :$item['item_description'],
  36. "password"=>$item_password ? $item_password :$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" =>$value['page_title'],
  51. "page_content" =>$value['page_content'],
  52. "s_number" =>$value['s_number'],
  53. "page_comments" =>$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. foreach ($item['pages']['catalogs'] as $key => &$value) {
  66. $catalog_data = array(
  67. "cat_name" => $value['cat_name'],
  68. "level" => $value['level'],
  69. "s_number" => $value['s_number'],
  70. "item_id" => $item_id,
  71. "addtime" =>time(),
  72. );
  73. $cat_id = D("Catalog")->add($catalog_data);
  74. //二级目录的页面们
  75. if ($value['pages']) {
  76. foreach ($value['pages'] as $key2 => &$value2) {
  77. $page_data = array(
  78. "author_uid"=>$userInfo['uid'],
  79. "author_username"=>$userInfo['username'],
  80. "page_title" =>$value2['page_title'],
  81. "page_content" =>$value2['page_content'],
  82. "s_number" =>$value2['s_number'],
  83. "page_comments" =>$value2['page_comments'],
  84. "item_id" => $item_id,
  85. "cat_id" => $cat_id ,
  86. "addtime" =>time(),
  87. );
  88. D("Page")->add($page_data);
  89. unset($page_data);
  90. unset($value2);
  91. }
  92. }
  93. //判断是否存在三级目录
  94. if ($value['catalogs']) {
  95. foreach ($value['catalogs'] as $key3 => &$value3) {
  96. $catalog_data = array(
  97. "cat_name" => $value3['cat_name'],
  98. "level" => $value3['level'],
  99. "s_number" => $value3['s_number'],
  100. "parent_cat_id" => $cat_id,
  101. "item_id" => $item_id,
  102. "addtime" =>time(),
  103. );
  104. $cat_id2 = D("Catalog")->add($catalog_data);
  105. //三级目录的页面们
  106. if ($value3['pages']) {
  107. foreach ($value3['pages'] as $key4 => &$value4) {
  108. $page_data = array(
  109. "author_uid"=>$userInfo['uid'],
  110. "author_username"=>$userInfo['username'],
  111. "page_title" =>$value4['page_title'],
  112. "page_content" =>$value4['page_content'],
  113. "s_number" =>$value4['s_number'],
  114. "page_comments" =>$value4['page_comments'],
  115. "item_id" => $item_id,
  116. "cat_id" => $cat_id2 ,
  117. "addtime" =>time(),
  118. );
  119. D("Page")->add($page_data);
  120. unset($page_data);
  121. unset($value4);
  122. }
  123. }
  124. //判断是否存在四级目录
  125. if ($value3['catalogs']) {
  126. foreach ($value3['catalogs'] as $key5 => &$value5) {
  127. $catalog_data = array(
  128. "cat_name" => $value5['cat_name'],
  129. "level" => $value5['level'],
  130. "s_number" => $value5['s_number'],
  131. "parent_cat_id" => $cat_id2,
  132. "item_id" => $item_id,
  133. "addtime" =>time(),
  134. );
  135. $cat_id3 = D("Catalog")->add($catalog_data);
  136. //四级目录的页面们
  137. if ($value5['pages']) {
  138. foreach ($value5['pages'] as $key6 => &$value6) {
  139. $page_data = array(
  140. "author_uid"=>$userInfo['uid'],
  141. "author_username"=>$userInfo['username'],
  142. "page_title" =>$value6['page_title'],
  143. "page_content" =>$value6['page_content'],
  144. "s_number" =>$value6['s_number'],
  145. "page_comments" =>$value6['page_comments'],
  146. "item_id" => $item_id,
  147. "cat_id" => $cat_id3 ,
  148. "addtime" =>time(),
  149. );
  150. D("Page")->add($page_data);
  151. unset($page_data);
  152. unset($value6);
  153. }
  154. }
  155. unset($value3);
  156. }
  157. }
  158. unset($value3);
  159. }
  160. }
  161. unset($value);
  162. }
  163. }
  164. }
  165. if ($item['members']) {
  166. foreach ($item['members'] as $key => $value) {
  167. $member_data = array(
  168. "member_group_id"=>$value['member_group_id'],
  169. "uid"=>$value['uid'],
  170. "username"=>$value['username'],
  171. "item_id"=>$item_id,
  172. "addtime"=>time(),
  173. );
  174. D("ItemMember")->add($member_data);
  175. }
  176. }
  177. return $item_id;
  178. }
  179. public function copy($item_id,$uid,$item_name= '',$item_description= '',$item_password = '',$item_domain){
  180. return $this->import($this->export($item_id),$uid,$item_name,$item_description,$item_password,$item_domain);
  181. }
  182. //获取菜单结构
  183. public function getMemu($item_id){
  184. $page_field = "page_id,author_uid,cat_id,page_title,addtime";
  185. $catalog_field = '*';
  186. $data = $this->getContent($item_id , $page_field , $catalog_field) ;
  187. return $data ;
  188. }
  189. public function getContent($item_id , $page_field ="*" , $catalog_field ="*" , $uncompress = 0 ){
  190. //获取所有父目录id为0的页面
  191. $all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->order(" `s_number` asc ")->field($page_field)->select();
  192. $pages = array() ;
  193. if ($all_pages) {
  194. foreach ($all_pages as $key => $value) {
  195. if ($value['cat_id']) {
  196. # code...
  197. }else{
  198. $pages[] = $value ;
  199. }
  200. }
  201. }
  202. //获取该项目下的所有目录
  203. $all_catalogs = D("Catalog")->field($catalog_field)->where("item_id = '$item_id' ")->order(" `s_number` asc ")->select();
  204. //获取所有二级目录
  205. $catalogs = array() ;
  206. if ($all_catalogs) {
  207. foreach ($all_catalogs as $key => $value) {
  208. if ($value['level'] == 2 ) {
  209. $catalogs[] = $value;
  210. }
  211. }
  212. }
  213. if ($catalogs) {
  214. foreach ($catalogs as $key => &$catalog2) {
  215. //该二级目录下的所有子页面
  216. $catalog2['pages'] = $this->_getPageByCatId($catalog2['cat_id'],$all_pages);
  217. //该二级目录下的所有子目录
  218. $catalog2['catalogs'] = $this->_getCatByCatId($catalog2['cat_id'],$all_catalogs);
  219. if($catalog2['catalogs']){
  220. //获取所有三级目录的子页面
  221. foreach ($catalog2['catalogs'] as $key3 => &$catalog3) {
  222. //该三级目录下的所有子页面
  223. $catalog3['pages'] = $this->_getPageByCatId($catalog3['cat_id'],$all_pages);
  224. //该三级目录下的所有子目录
  225. $catalog3['catalogs'] = $this->_getCatByCatId($catalog3['cat_id'],$all_catalogs);
  226. if($catalog3['catalogs']){
  227. //获取所有三级目录的子页面
  228. foreach ($catalog3['catalogs'] as $key4 => &$catalog4) {
  229. //该三级目录下的所有子页面
  230. $catalog4['pages'] = $this->_getPageByCatId($catalog4['cat_id'],$all_pages);
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. $menu = array(
  238. "pages" =>$pages,
  239. "catalogs" =>$catalogs,
  240. );
  241. unset($pages);
  242. unset($catalogs);
  243. return $menu;
  244. }
  245. //获取某个目录下的所有页面
  246. private function _getPageByCatId($cat_id ,$all_pages){
  247. $pages = array() ;
  248. if ($all_pages) {
  249. foreach ($all_pages as $key => $value) {
  250. if ($value['cat_id'] == $cat_id) {
  251. $pages[] = $value ;
  252. }
  253. }
  254. }
  255. return $pages;
  256. }
  257. //获取某个目录下的所有子目录
  258. private function _getCatByCatId($cat_id ,$all_catalogs){
  259. $cats = array() ;
  260. if ($all_catalogs) {
  261. foreach ($all_catalogs as $key => $value) {
  262. if ($value['parent_cat_id'] == $cat_id) {
  263. $cats[] = $value ;
  264. }
  265. }
  266. }
  267. return $cats;
  268. }
  269. //删除项目
  270. public function delete_item($item_id){
  271. D("Page")->where("item_id = '$item_id' ")->delete();
  272. D("Page")->where("item_id = '$item_id' ")->delete();
  273. D("Catalog")->where("item_id = '$item_id' ")->delete();
  274. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  275. D("ItemMember")->where("item_id = '$item_id' ")->delete();
  276. D("TeamItem")->where("item_id = '$item_id' ")->delete();
  277. D("TeamItemMember")->where("item_id = '$item_id' ")->delete();
  278. return D("Item")->where("item_id = '$item_id' ")->delete();
  279. }
  280. //软删除项目
  281. public function soft_delete_item($item_id){
  282. return $this->where("item_id = '$item_id' ")->save(array("is_del"=>1 ,"last_update_time"=>time()));
  283. }
  284. }