您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

181 行
9.0 KiB

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