You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

281 lines
9.0 KiB

8 years ago
8 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Admin\CommonController;
  4. use DB;
  5. class ArticleController extends CommonController
  6. {
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. }
  11. public function index()
  12. {
  13. $res = '';
  14. $where = function ($query) use ($res) {
  15. if(isset($_REQUEST["keyword"]))
  16. {
  17. $query->where('title', 'like', '%'.$_REQUEST['keyword'].'%');
  18. }
  19. if(isset($_REQUEST["typeid"]) && $_REQUEST["typeid"]!=0)
  20. {
  21. $query->where('typeid', $_REQUEST["typeid"]);
  22. }
  23. if(isset($_REQUEST["id"]))
  24. {
  25. $query->where('typeid', $_REQUEST["id"]);
  26. }
  27. if(isset($_REQUEST["ischeck"]))
  28. {
  29. $query->where('ischeck', $_REQUEST["ischeck"]); //未审核过的文章
  30. }
  31. };
  32. $posts = parent::pageList('article', $where);
  33. foreach($posts as $key=>$value)
  34. {
  35. $info = DB::table('arctype')->select('name')->where("id", $value->typeid)->first();
  36. $posts[$key]->name = $info->name;
  37. $posts[$key]->body = '';
  38. }
  39. $data['posts'] = $posts;
  40. return view('admin.article.index', $data);
  41. //if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  42. /* if(!empty($id)){$map['typeid']=$id;}
  43. $Article = M("Article")->field('id')->where($map);
  44. $counts = $Article->count();
  45. $pagesize =CMS_PAGESIZE;$page =0;
  46. if($counts % $pagesize){ //取总数据量除以每页数的余数
  47. $pages = intval($counts/$pagesize) + 1; //如果有余数,则页数等于总数据量除以每页数的结果取整再加一,如果没有余数,则页数等于总数据量除以每页数的结果
  48. }else{$pages = $counts/$pagesize;}
  49. if(!empty($_GET["page"])){$page = $_GET["page"]-1;$nextpage=$_GET["page"]+1;$previouspage=$_GET["page"]-1;}else{$page = 0;$nextpage=2;$previouspage=0;}
  50. if($counts>0){if($page>$pages-1){exit;}}
  51. $start = $page*$pagesize;
  52. $Article = M("Article")->field('id,typeid,title,pubdate,click,litpic,tuijian')->where($map)->order('id desc')->limit($start,$pagesize)->select();
  53. $this->counts = $counts;
  54. $this->pages = $pages;
  55. $this->page = $page;
  56. $this->nextpage = $nextpage;
  57. $this->previouspage = $previouspage;
  58. $this->id = $id;
  59. $this->posts = $Article; */
  60. //echo '<pre>';
  61. //print_r($Article);
  62. //return $this->fetch();
  63. }
  64. public function add()
  65. {
  66. $data = '';
  67. if(!empty($_REQUEST["catid"])){$data['catid'] = $_REQUEST["catid"];}else{$data['catid'] = 0;}
  68. return view('admin.article.add', $data);
  69. }
  70. public function doadd()
  71. {
  72. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  73. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  74. $content="";if(!empty($_POST["body"])){$content = $_POST["body"];}
  75. $_POST['pubdate'] = time();//更新时间
  76. $_POST['addtime'] = time();//添加时间
  77. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 发布者id
  78. //关键词
  79. if(!empty($_POST["keywords"]))
  80. {
  81. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  82. }
  83. else
  84. {
  85. if(!empty($_POST["title"]))
  86. {
  87. $title=$_POST["title"];
  88. $title=str_replace("","",$title);
  89. $title=str_replace(",","",$title);
  90. $_POST['keywords']=get_keywords($title);//标题分词
  91. }
  92. }
  93. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(sysconfig('CMS_BASEHOST')));} //删除非站内链接
  94. $_POST['body']=$content;
  95. //提取第一个图片为缩略图
  96. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  97. {
  98. if(getfirstpic($content))
  99. {
  100. //获取文章内容的第一张图片
  101. $imagepath = '.'.getfirstpic($content);
  102. //获取后缀名
  103. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  104. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  105. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  106. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  107. //缩略图路径
  108. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  109. }
  110. }
  111. unset($_POST["dellink"]);
  112. unset($_POST["autolitpic"]);
  113. unset($_POST["_token"]);
  114. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  115. if(DB::table('article')->insert($_POST))
  116. {
  117. success_jump("添加成功!");
  118. }
  119. else
  120. {
  121. error_jump("添加失败!请修改后重新添加");
  122. }
  123. }
  124. public function edit()
  125. {
  126. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  127. $data['id'] = $id;
  128. $data['post'] = object_to_array(DB::table('article')->where('id', $id)->first(), 1);
  129. return view('admin.article.edit', $data);
  130. }
  131. public function doedit()
  132. {
  133. if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else{$id="";exit;}
  134. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  135. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  136. $content="";if(!empty($_POST["body"])){$content = $_POST["body"];}
  137. $_POST['pubdate'] = time();//更新时间
  138. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 修改者id
  139. if(!empty($_POST["keywords"]))
  140. {
  141. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  142. }
  143. else
  144. {
  145. if(!empty($_POST["title"]))
  146. {
  147. $title=$_POST["title"];
  148. $title=str_replace("","",$title);
  149. $title=str_replace(",","",$title);
  150. $_POST['keywords']=get_keywords($title);//标题分词
  151. }
  152. }
  153. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(CMS_BASEHOST));} //删除非站内链接
  154. $_POST['body']=$content;
  155. //提取第一个图片为缩略图
  156. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  157. {
  158. if(getfirstpic($content))
  159. {
  160. //获取文章内容的第一张图片
  161. $imagepath = '.'.getfirstpic($content);
  162. //获取后缀名
  163. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  164. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  165. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  166. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  167. //缩略图路径
  168. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  169. }
  170. }
  171. unset($_POST["dellink"]);
  172. unset($_POST["autolitpic"]);
  173. unset($_POST["_token"]);
  174. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  175. if(DB::table('article')->where('id', $id)->update($_POST))
  176. {
  177. success_jump("修改成功!", route('admin_article'));
  178. }
  179. else
  180. {
  181. error_jump("修改失败!");
  182. }
  183. }
  184. //删除文章
  185. public function del()
  186. {
  187. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("删除失败!请重新提交");}
  188. if(DB::table("article")->whereIn("id", explode(',', $id))->delete())
  189. {
  190. success_jump("$id ,删除成功");
  191. }
  192. else
  193. {
  194. error_jump("$id ,删除失败!请重新提交");
  195. }
  196. }
  197. //重复文章列表
  198. public function repetarc()
  199. {
  200. $data['posts'] = object_to_array(DB::table('article')->select(DB::raw('title,count(*) AS count'))->orderBy('count', 'desc')->groupBy('title')->having('count', '>', 1)->get());
  201. return view('admin.article.repetarc', $data);
  202. }
  203. //推荐文章
  204. public function recommendarc()
  205. {
  206. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("您访问的页面不存在或已被删除!");} //if(preg_match('/[0-9]*/',$id)){}else{exit;}
  207. $data['tuijian'] = 1;
  208. if(DB::table("article")->whereIn("id", explode(',', $id))->update($data))
  209. {
  210. success_jump("$id ,推荐成功");
  211. }
  212. else
  213. {
  214. error_jump("$id ,推荐失败!请重新提交");
  215. }
  216. }
  217. //检测重复文章数量
  218. public function articleexists()
  219. {
  220. $res = '';
  221. $where = function ($query) use ($res) {
  222. if(isset($_REQUEST["title"]))
  223. {
  224. $query->where('title', $_REQUEST["title"]);
  225. }
  226. if(isset($_REQUEST["id"]))
  227. {
  228. $query->where('id', '<>', $_REQUEST["id"]);
  229. }
  230. };
  231. return DB::table("article")->where($where)->count();
  232. }
  233. }