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.

295 lines
9.4 KiB

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