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.

293 lines
9.3 KiB

8 years ago
7 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
8 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Admin\CommonController;
  4. use DB;
  5. use Illuminate\Http\Request;
  6. use App\Http\Logic\ArticleLogic;
  7. class ArticleController extends CommonController
  8. {
  9. public function __construct()
  10. {
  11. parent::__construct();
  12. }
  13. public function getLogic()
  14. {
  15. return new ArticleLogic();
  16. }
  17. public function index()
  18. {
  19. $res = '';
  20. $where = function ($query) use ($res) {
  21. if(isset($_REQUEST["keyword"]))
  22. {
  23. $query->where('title', 'like', '%'.$_REQUEST['keyword'].'%');
  24. }
  25. if(isset($_REQUEST["typeid"]) && $_REQUEST["typeid"]!=0)
  26. {
  27. $query->where('typeid', $_REQUEST["typeid"]);
  28. }
  29. if(isset($_REQUEST["id"]))
  30. {
  31. $query->where('typeid', $_REQUEST["id"]);
  32. }
  33. if(isset($_REQUEST["ischeck"]))
  34. {
  35. $query->where('ischeck', $_REQUEST["ischeck"]); //未审核过的文章
  36. }
  37. };
  38. $posts = $this->getLogic()->getPaginate($where);
  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. //数据验证
  73. $validate = new ArticleRequest();
  74. $validator = Validator::make($_REQUEST, $validate->getSceneRules('add'), $validate->getSceneRulesMessages());
  75. if ($validator->fails())
  76. {
  77. dd($validator->errors()->first());
  78. //$validator->errors()->all();
  79. error_jump('参数错误');
  80. }
  81. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  82. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  83. $content="";if(!empty($_POST["body"])){$content = $_POST["body"];}
  84. $_POST['pubdate'] = time();//更新时间
  85. $_POST['addtime'] = time();//添加时间
  86. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 发布者id
  87. //关键词
  88. if(!empty($_POST["keywords"]))
  89. {
  90. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  91. }
  92. else
  93. {
  94. if(!empty($_POST["title"]))
  95. {
  96. $title=$_POST["title"];
  97. $title=str_replace("","",$title);
  98. $title=str_replace(",","",$title);
  99. $_POST['keywords']=get_keywords($title);//标题分词
  100. }
  101. }
  102. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(sysconfig('CMS_BASEHOST')));} //删除非站内链接
  103. $_POST['body']=$content;
  104. //提取第一个图片为缩略图
  105. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  106. {
  107. if(getfirstpic($content))
  108. {
  109. //获取文章内容的第一张图片
  110. $imagepath = '.'.getfirstpic($content);
  111. //获取后缀名
  112. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  113. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  114. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  115. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  116. //缩略图路径
  117. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  118. }
  119. }
  120. unset($_POST["dellink"]);
  121. unset($_POST["autolitpic"]);
  122. unset($_POST["_token"]);
  123. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  124. if(DB::table('article')->insert($_POST))
  125. {
  126. success_jump("添加成功!");
  127. }
  128. else
  129. {
  130. error_jump("添加失败!请修改后重新添加");
  131. }
  132. }
  133. public function edit()
  134. {
  135. if(!empty($_GET["id"])){$id = $_GET["id"];}else {$id="";}if(preg_match('/[0-9]*/',$id)){}else{exit;}
  136. $data['id'] = $id;
  137. $data['post'] = object_to_array(DB::table('article')->where('id', $id)->first(), 1);
  138. return view('admin.article.edit', $data);
  139. }
  140. public function doedit()
  141. {
  142. if(!empty($_POST["id"])){$id = $_POST["id"];unset($_POST["id"]);}else{$id="";exit;}
  143. $litpic="";if(!empty($_POST["litpic"])){$litpic = $_POST["litpic"];}else{$_POST['litpic']="";} //缩略图
  144. if(empty($_POST["description"])){if(!empty($_POST["body"])){$_POST['description']=cut_str($_POST["body"]);}} //description
  145. $content="";if(!empty($_POST["body"])){$content = $_POST["body"];}
  146. $_POST['pubdate'] = time();//更新时间
  147. $_POST['user_id'] = $_SESSION['admin_user_info']['id']; // 修改者id
  148. if(!empty($_POST["keywords"]))
  149. {
  150. $_POST['keywords']=str_replace("",",",$_POST["keywords"]);
  151. }
  152. else
  153. {
  154. if(!empty($_POST["title"]))
  155. {
  156. $title=$_POST["title"];
  157. $title=str_replace("","",$title);
  158. $title=str_replace(",","",$title);
  159. $_POST['keywords']=get_keywords($title);//标题分词
  160. }
  161. }
  162. if(isset($_POST["dellink"]) && $_POST["dellink"]==1 && !empty($content)){$content=replacelinks($content,array(CMS_BASEHOST));} //删除非站内链接
  163. $_POST['body']=$content;
  164. //提取第一个图片为缩略图
  165. if(isset($_POST["autolitpic"]) && $_POST["autolitpic"] && empty($litpic))
  166. {
  167. if(getfirstpic($content))
  168. {
  169. //获取文章内容的第一张图片
  170. $imagepath = '.'.getfirstpic($content);
  171. //获取后缀名
  172. preg_match_all ("/\/(.+)\.(gif|jpg|jpeg|bmp|png)$/iU",$imagepath,$out, PREG_PATTERN_ORDER);
  173. $saveimage='./uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  174. //生成缩略图,按照原图的比例生成一个最大为240*180的缩略图
  175. \Intervention\Image\Facades\Image::make($imagepath)->resize(sysconfig('CMS_IMGWIDTH'), sysconfig('CMS_IMGHEIGHT'))->save($saveimage);
  176. //缩略图路径
  177. $_POST['litpic']='/uploads/'.date('Y/m',time()).'/'.basename($imagepath,'.'.$out[2][0]).'-lp.'.$out[2][0];
  178. }
  179. }
  180. unset($_POST["dellink"]);
  181. unset($_POST["autolitpic"]);
  182. unset($_POST["_token"]);
  183. if(isset($_POST['editorValue'])){unset($_POST['editorValue']);}
  184. if(DB::table('article')->where('id', $id)->update($_POST))
  185. {
  186. success_jump("修改成功!", route('admin_article'));
  187. }
  188. else
  189. {
  190. error_jump("修改失败!");
  191. }
  192. }
  193. //删除文章
  194. public function del()
  195. {
  196. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("删除失败!请重新提交");}
  197. if(DB::table("article")->whereIn("id", explode(',', $id))->delete())
  198. {
  199. success_jump("$id ,删除成功");
  200. }
  201. else
  202. {
  203. error_jump("$id ,删除失败!请重新提交");
  204. }
  205. }
  206. //重复文章列表
  207. public function repetarc()
  208. {
  209. $data['posts'] = object_to_array(DB::table('article')->select(DB::raw('title,count(*) AS count'))->orderBy('count', 'desc')->groupBy('title')->having('count', '>', 1)->get());
  210. return view('admin.article.repetarc', $data);
  211. }
  212. //推荐文章
  213. public function recommendarc()
  214. {
  215. if(!empty($_GET["id"])){$id = $_GET["id"];}else{error_jump("您访问的页面不存在或已被删除!");} //if(preg_match('/[0-9]*/',$id)){}else{exit;}
  216. $data['tuijian'] = 1;
  217. if(DB::table("article")->whereIn("id", explode(',', $id))->update($data))
  218. {
  219. success_jump("$id ,推荐成功");
  220. }
  221. else
  222. {
  223. error_jump("$id ,推荐失败!请重新提交");
  224. }
  225. }
  226. //检测重复文章数量
  227. public function articleexists()
  228. {
  229. $res = '';
  230. $where = function ($query) use ($res) {
  231. if(isset($_REQUEST["title"]))
  232. {
  233. $query->where('title', $_REQUEST["title"]);
  234. }
  235. if(isset($_REQUEST["id"]))
  236. {
  237. $query->where('id', '<>', $_REQUEST["id"]);
  238. }
  239. };
  240. return DB::table("article")->where($where)->count();
  241. }
  242. }