ItemModel.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. //获取所有父目录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. //获取菜单结构
  176. public function getMemu($item_id){
  177. $page_field = "page_id,author_uid,cat_id,page_title,addtime";
  178. $catalog_field = '*';
  179. $data = $this->getContent($item_id , $page_field , $catalog_field) ;
  180. return $data ;
  181. }
  182. public function getContent($item_id , $page_field ="*" , $catalog_field ="*"){
  183. //获取所有父目录id为0的页面
  184. $all_pages = D("Page")->where("item_id = '$item_id' and is_del = 0 ")->order(" `s_number` asc ")->field($page_field)->select();
  185. $pages = array() ;
  186. if ($all_pages) {
  187. foreach ($all_pages as $key => $value) {
  188. if ($value['cat_id']) {
  189. # code...
  190. }else{
  191. $pages[] = $value ;
  192. }
  193. }
  194. }
  195. //获取该项目下的所有目录
  196. $all_catalogs = D("Catalog")->field($catalog_field)->where("item_id = '$item_id' ")->order(" `s_number` asc ")->select();
  197. //获取所有二级目录
  198. $catalogs = array() ;
  199. if ($all_catalogs) {
  200. foreach ($all_catalogs as $key => $value) {
  201. if ($value['level'] == 2 ) {
  202. $catalogs[] = $value;
  203. }
  204. }
  205. }
  206. if ($catalogs) {
  207. foreach ($catalogs as $key => &$catalog2) {
  208. //该二级目录下的所有子页面
  209. $catalog2['pages'] = $this->_getPageByCatId($catalog2['cat_id'],$all_pages);
  210. //该二级目录下的所有子目录
  211. $catalog2['catalogs'] = $this->_getCatByCatId($catalog2['cat_id'],$all_catalogs);
  212. if($catalog2['catalogs']){
  213. //获取所有三级目录的子页面
  214. foreach ($catalog2['catalogs'] as $key3 => &$catalog3) {
  215. //该三级目录下的所有子页面
  216. $catalog3['pages'] = $this->_getPageByCatId($catalog3['cat_id'],$all_pages);
  217. //该三级目录下的所有子目录
  218. $catalog3['catalogs'] = $this->_getCatByCatId($catalog3['cat_id'],$all_catalogs);
  219. if($catalog3['catalogs']){
  220. //获取所有三级目录的子页面
  221. foreach ($catalog3['catalogs'] as $key4 => &$catalog4) {
  222. //该三级目录下的所有子页面
  223. $catalog4['pages'] = $this->_getPageByCatId($catalog4['cat_id'],$all_pages);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. $menu = array(
  231. "pages" =>$pages,
  232. "catalogs" =>$catalogs,
  233. );
  234. unset($pages);
  235. unset($catalogs);
  236. return $menu;
  237. }
  238. //获取某个目录下的所有页面
  239. private function _getPageByCatId($cat_id ,$all_pages){
  240. $pages = array() ;
  241. if ($all_pages) {
  242. foreach ($all_pages as $key => $value) {
  243. if ($value['cat_id'] == $cat_id) {
  244. $pages[] = $value ;
  245. }
  246. }
  247. }
  248. return $pages;
  249. }
  250. //获取某个目录下的所有子目录
  251. private function _getCatByCatId($cat_id ,$all_catalogs){
  252. $cats = array() ;
  253. if ($all_catalogs) {
  254. foreach ($all_catalogs as $key => $value) {
  255. if ($value['parent_cat_id'] == $cat_id) {
  256. $cats[] = $value ;
  257. }
  258. }
  259. }
  260. return $cats;
  261. }
  262. //删除项目
  263. public function delete_item($item_id){
  264. D("Page")->where("item_id = '$item_id' ")->delete();
  265. D("Page")->where("item_id = '$item_id' ")->delete();
  266. D("Catalog")->where("item_id = '$item_id' ")->delete();
  267. D("PageHistory")->where("item_id = '$item_id' ")->delete();
  268. return D("Item")->where("item_id = '$item_id' ")->delete();
  269. }
  270. //软删除项目
  271. public function soft_delete_item($item_id){
  272. return $this->where("item_id = '$item_id' ")->save(array("is_del"=>1 ,"last_update_time"=>time()));
  273. }
  274. }