PageModel.class.php 6.8 KB

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