Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

153 rader
6.0 KiB

3 år sedan
  1. <?php
  2. namespace Api\Model;
  3. use Api\Model\BaseModel;
  4. /**
  5. *
  6. * @author star7th
  7. */
  8. class PageModel extends BaseModel {
  9. protected $cat_name_id = array();
  10. //根据内容更新页面
  11. //其中cat_name参数特别说明下,传递各格式如 '二级目录/三级目录/四级目录'
  12. public function update_by_content($item_id,$page_title,$page_content,$cat_name='',$s_number = 99){
  13. $item_id = intval($item_id);
  14. if (!$item_id) {
  15. return false;
  16. }
  17. if ($this->cat_name_id && isset($this->cat_name_id[$cat_name])) {
  18. $cat_id = $this->cat_name_id[$cat_name] ;
  19. $cat_name = '' ; //如果已经有缓存了则设置为空
  20. }else{
  21. $cat_id = 0 ;
  22. }
  23. $catalog_array = explode('/', $cat_name);
  24. $cat_name = $catalog_array[0] ;
  25. $cat_name_sub = !empty($catalog_array[1])? $catalog_array[1] : '';
  26. $cat_name_sub_sub = !empty($catalog_array[2])? $catalog_array[2] : '';
  27. //如果传送了二级目录
  28. if ($cat_name) {
  29. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  30. //如果不存在则新建
  31. if (!$cat_name_array) {
  32. $add_data = array(
  33. "cat_name" => $cat_name,
  34. "item_id" => $item_id,
  35. "addtime" => time(),
  36. "level" => 2
  37. );
  38. D("Catalog")->add($add_data);
  39. $cat_name_array = D("Catalog")->where(" item_id = '$item_id' and level = 2 and cat_name = '%s' ",array($cat_name))->find();
  40. }
  41. }
  42. //如果传送了三级目录
  43. if ($cat_name_sub) {
  44. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub,$cat_name_array['cat_id']))->find();
  45. //如果不存在则新建
  46. if (!$cat_name_sub_array) {
  47. $add_data = array(
  48. "cat_name" => $cat_name_sub,
  49. "item_id" => $item_id,
  50. "parent_cat_id" => $cat_name_array['cat_id'],
  51. "addtime" => time(),
  52. "level" => 3
  53. );
  54. D("Catalog")->add($add_data);
  55. $cat_name_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 3 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub,$cat_name_array['cat_id']))->find();
  56. }
  57. }
  58. //如果传送了四级目录
  59. if ($cat_name_sub_sub) {
  60. $cat_name_sub_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 4 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub_sub,$cat_name_sub_array['cat_id']))->find();
  61. //如果不存在则新建
  62. if (!$cat_name_sub_sub_array) {
  63. $add_data = array(
  64. "cat_name" => $cat_name_sub_sub,
  65. "item_id" => $item_id,
  66. "parent_cat_id" => $cat_name_sub_array['cat_id'],
  67. "addtime" => time(),
  68. "level" => 4
  69. );
  70. D("Catalog")->add($add_data);
  71. $cat_name_sub_sub_array = D("Catalog")->where(" item_id = '$item_id' and level = 4 and cat_name = '%s' and parent_cat_id = '%s' ",array($cat_name_sub_sub,$cat_name_sub_array['cat_id']))->find();
  72. }
  73. }
  74. if ($cat_name_array && $cat_name_array['cat_id'] > 0 ) {
  75. $cat_id = $cat_name_array['cat_id'] ;
  76. }
  77. if ($cat_name_sub_array && $cat_name_sub_array['cat_id'] > 0 ) {
  78. $cat_id = $cat_name_sub_array['cat_id'] ;
  79. }
  80. if ($cat_name_sub_sub_array && $cat_name_sub_sub_array['cat_id'] > 0 ) {
  81. $cat_id = $cat_name_sub_sub_array['cat_id'] ;
  82. }
  83. $this->cat_name_id[$cat_name] = $cat_id ;
  84. if ($page_content) {
  85. $page_array = D("Page")->where(" item_id = '$item_id' and is_del = 0 and cat_id = '$cat_id' and page_title ='%s' ",array($page_title))->find();
  86. //如果不存在则新建
  87. if (!$page_array) {
  88. $add_data = array(
  89. "author_username" => "update_by_content",
  90. "item_id" => $item_id,
  91. "cat_id" => $cat_id,
  92. "page_title" => $page_title,
  93. "page_content" => $page_content,
  94. "s_number" => $s_number,
  95. "addtime" => time(),
  96. );
  97. $page_id = D("Page")->add($add_data);
  98. }else{
  99. $page_id = $page_array['page_id'] ;
  100. $update_data = array(
  101. "author_username" => "update_by_content",
  102. "item_id" => $item_id,
  103. "cat_id" => $cat_id,
  104. "page_title" => $page_title,
  105. "page_content" => $page_content,
  106. "s_number" => $s_number,
  107. );
  108. D("Page")->where(" page_id = '$page_id' ")->save($update_data);
  109. }
  110. }
  111. return $page_id ;
  112. }
  113. //软删除页面
  114. public function softDeletePage($page_id){
  115. //放入回收站
  116. $login_user = session('login_user');
  117. $page = D("Page")->field("item_id,page_title")->where(" page_id = '$page_id' ")->find() ;
  118. D("Recycle")->add(array(
  119. "item_id" =>$page['item_id'],
  120. "page_id" =>$page_id,
  121. "page_title" =>$page['page_title'],
  122. "del_by_uid" =>$login_user['uid'],
  123. "del_by_username" =>$login_user['username'],
  124. "del_time" =>time()
  125. ));
  126. $ret = M("Page")->where(" page_id = '$page_id' ")->save(array("is_del"=>1 ,"addtime"=>time()));
  127. return $ret;
  128. }
  129. //删除页面
  130. public function deletePage($page_id){
  131. $ret = M("Page")->where(" page_id = '$page_id' ")->delete();
  132. return $ret;
  133. }
  134. public function deleteFile($file_id){
  135. return D("Attachment")->deleteFile($file_id) ;
  136. }
  137. }