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.

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