ItemModel.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Home\Model;
  3. use Home\Model\BaseModel;
  4. class ItemModel extends BaseModel {
  5. public function export($item_id){
  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. //获取所有父目录id为0的页面
  9. $pages = D("Page")->where("cat_id = '0' and item_id = '$item_id' ")->field(" page_title ,page_content,s_number,page_comments ")->order(" s_number asc ")->select();
  10. //获取所有二级目录
  11. $catalogs = D("Catalog")->where("item_id = '$item_id' and level = 2 ")->field("cat_id, cat_name ,level,s_number ")->order(" s_number asc ")->select();
  12. if ($catalogs) {
  13. foreach ($catalogs as $key => &$catalog) {
  14. //该二级目录下的所有子页面
  15. $temp = D("Page")->where("cat_id = '$catalog[cat_id]' ")->field(" page_title ,page_content,s_number,page_comments ")->order(" s_number asc ")->select();
  16. $catalog['pages'] = $temp ? $temp: array();
  17. //该二级目录下的所有子目录
  18. $temp = D("catalog")->where("parent_cat_id = '$catalog[cat_id]' ")->field(" cat_id,cat_name ,level,s_number ")->order(" s_number asc ")->select();
  19. $catalog['catalogs'] = $temp ? $temp: array();
  20. if($catalog['catalogs']){
  21. //获取所有三级目录的子页面
  22. foreach ($catalog['catalogs'] as $key3 => &$catalog3) {
  23. //该二级目录下的所有子页面
  24. $temp = D("Page")->where("cat_id = '$catalog3[cat_id]' ")->field(" page_title ,page_content,s_number,page_comments ")->order(" s_number asc ")->select();
  25. $catalog3['pages'] = $temp ? $temp: array();
  26. unset($catalog3['cat_id']);
  27. }
  28. }
  29. unset($catalog['cat_id']);
  30. }
  31. }
  32. $item['pages'] = array(
  33. "pages" =>$pages,
  34. "catalogs" =>$catalogs,
  35. );
  36. unset($pages);
  37. unset($catalogs);
  38. $item['members'] = D("ItemMember")->where("item_id = '$item_id' ")->field(" member_group_id ,uid,username ")->select();
  39. return json_encode($item);
  40. }
  41. public function import($json,$uid,$item_name= '',$item_description= '',$item_password = '',$item_domain = ''){
  42. $userInfo = D("User")->userInfo($uid);
  43. $item = json_decode($json ,1 );
  44. unset($json);
  45. if ($item) {
  46. if ($item['item_domain']) {
  47. $item2 = D("Item")->where("item_domain = '%s' ".array($item['item_domain']))->find();
  48. if ($item2) {
  49. //个性域名已经存在
  50. return false;
  51. }
  52. if(!ctype_alnum($item_domain) || is_numeric($item_domain) ){
  53. //echo '个性域名只能是字母或数字的组合';exit;
  54. return false;
  55. }
  56. }else{
  57. $item['item_domain'] = '';
  58. }
  59. $item_data = array(
  60. "item_name"=>$item_name ? $item_name :$item['item_name'],
  61. "item_domain"=>$item_domain ? $item_domain :$item['item_domain'],
  62. "item_type"=>$item['item_type'],
  63. "item_description"=>$item_description ? $item_description :$item['item_description'],
  64. "password"=>$item_password ? $item_password :$item['password'],
  65. "uid"=>$userInfo['uid'],
  66. "username"=>$userInfo['username'],
  67. "addtime"=>time(),
  68. );
  69. $item_id = D("Item")->add($item_data);
  70. }
  71. if ($item['pages']) {
  72. //父页面们(一级目录)
  73. if ($item['pages']['pages']) {
  74. foreach ($item['pages']['pages'] as $key => &$value) {
  75. $page_data = array(
  76. "author_uid"=>$userInfo['uid'],
  77. "author_username"=>$userInfo['username'],
  78. "page_title" =>$value['page_title'],
  79. "page_content" =>$value['page_content'],
  80. "s_number" =>$value['s_number'],
  81. "page_comments" =>$value['page_comments'],
  82. "item_id" => $item_id,
  83. "cat_id" => 0 ,
  84. "addtime" =>time(),
  85. );
  86. D("Page")->add($page_data);
  87. unset($page_data);
  88. }
  89. unset($item['pages']['pages']);
  90. }
  91. //二级目录
  92. if ($item['pages']['catalogs']) {
  93. foreach ($item['pages']['catalogs'] as $key => &$value) {
  94. $catalog_data = array(
  95. "cat_name" => $value['cat_name'],
  96. "level" => $value['level'],
  97. "s_number" => $value['s_number'],
  98. "item_id" => $item_id,
  99. "addtime" =>time(),
  100. );
  101. $cat_id = D("Catalog")->add($catalog_data);
  102. //二级目录的页面们
  103. if ($value['pages']) {
  104. foreach ($value['pages'] as $key2 => &$value2) {
  105. $page_data = array(
  106. "author_uid"=>$userInfo['uid'],
  107. "author_username"=>$userInfo['username'],
  108. "page_title" =>$value2['page_title'],
  109. "page_content" =>$value2['page_content'],
  110. "s_number" =>$value2['s_number'],
  111. "page_comments" =>$value2['page_comments'],
  112. "item_id" => $item_id,
  113. "cat_id" => $cat_id ,
  114. "addtime" =>time(),
  115. );
  116. D("Page")->add($page_data);
  117. unset($page_data);
  118. unset($value2);
  119. }
  120. }
  121. //判断是否存在三级目录
  122. if ($value['catalogs']) {
  123. foreach ($value['catalogs'] as $key3 => &$value3) {
  124. $catalog_data = array(
  125. "cat_name" => $value3['cat_name'],
  126. "level" => $value3['level'],
  127. "s_number" => $value3['s_number'],
  128. "parent_cat_id" => $cat_id,
  129. "item_id" => $item_id,
  130. "addtime" =>time(),
  131. );
  132. $cat_id2 = D("Catalog")->add($catalog_data);
  133. //三级目录的页面们
  134. if ($value3['pages']) {
  135. foreach ($value3['pages'] as $key4 => &$value4) {
  136. $page_data = array(
  137. "author_uid"=>$userInfo['uid'],
  138. "author_username"=>$userInfo['username'],
  139. "page_title" =>$value4['page_title'],
  140. "page_content" =>$value4['page_content'],
  141. "s_number" =>$value4['s_number'],
  142. "page_comments" =>$value4['page_comments'],
  143. "item_id" => $item_id,
  144. "cat_id" => $cat_id2 ,
  145. "addtime" =>time(),
  146. );
  147. D("Page")->add($page_data);
  148. unset($page_data);
  149. unset($value4);
  150. }
  151. }
  152. unset($value3);
  153. }
  154. }
  155. unset($value);
  156. }
  157. }
  158. }
  159. if ($item['members']) {
  160. foreach ($item['members'] as $key => $value) {
  161. $member_data = array(
  162. "member_group_id"=>$value['member_group_id'],
  163. "uid"=>$value['uid'],
  164. "username"=>$value['username'],
  165. "item_id"=>$item_id,
  166. "addtime"=>time(),
  167. );
  168. D("ItemMember")->add($member_data);
  169. }
  170. }
  171. return $item_id;
  172. }
  173. public function copy($item_id,$uid,$item_name= '',$item_description= '',$item_password = '',$item_domain){
  174. return $this->import($this->export($item_id),$uid,$item_name,$item_description,$item_password,$item_domain);
  175. }
  176. }