Article.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. <?php
  2. /**
  3. * 易优CMS
  4. * ============================================================================
  5. * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
  6. * 网站地址: http://www.eyoucms.com
  7. * ----------------------------------------------------------------------------
  8. * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
  9. * ============================================================================
  10. * Author: 小虎哥 <1105415366@qq.com>
  11. * Date: 2018-4-3
  12. */
  13. namespace app\admin\controller;
  14. use think\Page;
  15. use think\Db;
  16. use think\Config;
  17. class Article extends Base
  18. {
  19. // 模型标识
  20. public $nid = 'article';
  21. // 模型ID
  22. public $channeltype = '';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. $channeltype_list = config('global.channeltype_list');
  27. $this->channeltype = $channeltype_list[$this->nid];
  28. empty($this->channeltype) && $this->channeltype = 1;
  29. $this->assign('nid', $this->nid);
  30. $this->assign('channeltype', $this->channeltype);
  31. }
  32. /**
  33. * 文章列表
  34. */
  35. public function index()
  36. {
  37. $assign_data = array();
  38. $condition = array();
  39. // 获取到所有GET参数
  40. $param = input('param.');
  41. $flag = input('flag/s');
  42. $typeid = input('typeid/d', 0);
  43. $begin = strtotime(input('add_time_begin'));
  44. $end = strtotime(input('add_time_end'));
  45. // 应用搜索条件
  46. foreach (['keywords','typeid','flag','is_release'] as $key) {
  47. if (isset($param[$key]) && $param[$key] !== '') {
  48. if ($key == 'keywords') {
  49. $condition['a.title'] = array('LIKE', "%{$param[$key]}%");
  50. } else if ($key == 'typeid') {
  51. $typeid = $param[$key];
  52. $hasRow = model('Arctype')->getHasChildren($typeid);
  53. $typeids = get_arr_column($hasRow, 'id');
  54. /*权限控制 by 小虎哥*/
  55. $admin_info = session('admin_info');
  56. if (0 < intval($admin_info['role_id'])) {
  57. $auth_role_info = $admin_info['auth_role_info'];
  58. if(! empty($auth_role_info)){
  59. if(! empty($auth_role_info['permission']['arctype'])){
  60. if (!empty($typeid)) {
  61. $typeids = array_intersect($typeids, $auth_role_info['permission']['arctype']);
  62. }
  63. }
  64. }
  65. }
  66. /*--end*/
  67. $condition['a.typeid'] = array('IN', $typeids);
  68. } else if ($key == 'flag') {
  69. if ('is_release' == $param[$key]) {
  70. $condition['a.users_id'] = array('gt', 0);
  71. } else {
  72. $condition['a.'.$param[$key]] = array('eq', 1);
  73. }
  74. // } else if ($key == 'is_release') {
  75. // if (0 < intval($param[$key])) {
  76. // $condition['a.users_id'] = array('gt', intval($param[$key]));
  77. // }
  78. } else {
  79. $condition['a.'.$key] = array('eq', $param[$key]);
  80. }
  81. }
  82. }
  83. /*权限控制 by 小虎哥*/
  84. $admin_info = session('admin_info');
  85. if (0 < intval($admin_info['role_id'])) {
  86. $auth_role_info = $admin_info['auth_role_info'];
  87. if(! empty($auth_role_info)){
  88. if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']){
  89. $condition['a.admin_id'] = $admin_info['admin_id'];
  90. }
  91. }
  92. }
  93. /*--end*/
  94. // 时间检索
  95. if ($begin > 0 && $end > 0) {
  96. $condition['a.add_time'] = array('between',"$begin,$end");
  97. } else if ($begin > 0) {
  98. $condition['a.add_time'] = array('egt', $begin);
  99. } else if ($end > 0) {
  100. $condition['a.add_time'] = array('elt', $end);
  101. }
  102. // 模型ID
  103. $condition['a.channel'] = array('eq', $this->channeltype);
  104. // 多语言
  105. $condition['a.lang'] = array('eq', $this->admin_lang);
  106. // 回收站
  107. $condition['a.is_del'] = array('eq', 0);
  108. /*自定义排序*/
  109. $orderby = input('param.orderby/s');
  110. $orderway = input('param.orderway/s');
  111. if (!empty($orderby)) {
  112. $orderby = "a.{$orderby} {$orderway}";
  113. $orderby .= ", a.aid desc";
  114. } else {
  115. $orderby = "a.aid desc";
  116. }
  117. /*end*/
  118. /**
  119. * 数据查询,搜索出主键ID的值
  120. */
  121. $count = DB::name('archives')->alias('a')->where($condition)->count('aid');// 查询满足要求的总记录数
  122. $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
  123. $list = DB::name('archives')
  124. ->field("a.aid")
  125. ->alias('a')
  126. ->where($condition)
  127. ->order($orderby)
  128. ->limit($Page->firstRow.','.$Page->listRows)
  129. ->getAllWithIndex('aid');
  130. /**
  131. * 完善数据集信息
  132. * 在数据量大的情况下,经过优化的搜索逻辑,先搜索出主键ID,再通过ID将其他信息补充完整;
  133. */
  134. if ($list) {
  135. $aids = array_keys($list);
  136. $fields = "b.*, a.*, a.aid as aid";
  137. $row = DB::name('archives')
  138. ->field($fields)
  139. ->alias('a')
  140. ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
  141. ->where('a.aid', 'in', $aids)
  142. ->getAllWithIndex('aid');
  143. foreach ($list as $key => $val) {
  144. $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
  145. $row[$val['aid']]['litpic'] = handle_subdir_pic($row[$val['aid']]['litpic']); // 支持子目录
  146. $list[$key] = $row[$val['aid']];
  147. }
  148. }
  149. $show = $Page->show(); // 分页显示输出
  150. $assign_data['page'] = $show; // 赋值分页输出
  151. $assign_data['list'] = $list; // 赋值数据集
  152. $assign_data['pager'] = $Page; // 赋值分页对象
  153. // 栏目ID
  154. $assign_data['typeid'] = $typeid; // 栏目ID
  155. /*当前栏目信息*/
  156. $arctype_info = array();
  157. if ($typeid > 0) {
  158. $arctype_info = M('arctype')->field('typename')->find($typeid);
  159. }
  160. $assign_data['arctype_info'] = $arctype_info;
  161. /*--end*/
  162. /*选项卡*/
  163. $tab = input('param.tab/d', 3);
  164. $assign_data['tab'] = $tab;
  165. /*--end*/
  166. /*前台URL模式*/
  167. $assign_data['seo_pseudo'] = tpCache('seo.seo_pseudo');
  168. $this->assign($assign_data);
  169. return $this->fetch();
  170. }
  171. /**
  172. * 添加
  173. */
  174. public function add()
  175. {
  176. if (IS_POST) {
  177. $post = input('post.');
  178. $content = input('post.addonFieldExt.content', '', null);
  179. // 根据标题自动提取相关的关键字
  180. $seo_keywords = $post['seo_keywords'];
  181. if (!empty($seo_keywords)) {
  182. $seo_keywords = str_replace(',', ',', $seo_keywords);
  183. } else {
  184. // $seo_keywords = get_split_word($post['title'], $content);
  185. }
  186. // 自动获取内容第一张图片作为封面图
  187. $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
  188. $litpic = '';
  189. if ($is_remote == 1) {
  190. $litpic = $post['litpic_remote'];
  191. } else {
  192. $litpic = $post['litpic_local'];
  193. }
  194. if (empty($litpic)) {
  195. $litpic = get_html_first_imgurl($content);
  196. }
  197. $post['litpic'] = $litpic;
  198. /*是否有封面图*/
  199. if (empty($post['litpic'])) {
  200. $is_litpic = 0; // 无封面图
  201. } else {
  202. $is_litpic = 1; // 有封面图
  203. }
  204. // SEO描述
  205. $seo_description = '';
  206. if (empty($post['seo_description']) && !empty($content)) {
  207. $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
  208. } else {
  209. $seo_description = $post['seo_description'];
  210. }
  211. // 外部链接跳转
  212. $jumplinks = '';
  213. $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
  214. if (intval($is_jump) > 0) {
  215. $jumplinks = $post['jumplinks'];
  216. }
  217. // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
  218. if ($post['type_tempview'] == $post['tempview']) {
  219. unset($post['type_tempview']);
  220. unset($post['tempview']);
  221. }
  222. //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
  223. if (!empty($post['htmlfilename'])) {
  224. $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
  225. $post['htmlfilename'] = strtolower($post['htmlfilename']);
  226. //判断是否存在相同的自定义文件名
  227. $filenameCount = Db::name('archives')->where('htmlfilename', $post['htmlfilename'])->count();
  228. if (!empty($filenameCount)) {
  229. $this->error("自定义文件名已存在,请重新设置!");
  230. }
  231. }
  232. // --存储数据
  233. $newData = array(
  234. 'typeid'=> empty($post['typeid']) ? 0 : $post['typeid'],
  235. 'channel' => $this->channeltype,
  236. 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
  237. 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
  238. 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
  239. 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
  240. 'is_jump' => $is_jump,
  241. 'is_litpic' => $is_litpic,
  242. 'jumplinks' => $jumplinks,
  243. 'seo_keywords' => $seo_keywords,
  244. 'seo_description' => $seo_description,
  245. 'admin_id' => session('admin_info.admin_id'),
  246. 'lang' => $this->admin_lang,
  247. 'sort_order' => 100,
  248. 'add_time' => strtotime($post['add_time']),
  249. 'update_time' => strtotime($post['add_time']),
  250. );
  251. $data = array_merge($post, $newData);
  252. $aid = M('archives')->insertGetId($data);
  253. $_POST['aid'] = $aid;
  254. if ($aid) {
  255. // ---------后置操作
  256. model('Article')->afterSave($aid, $data, 'add');
  257. // ---------end
  258. adminLog('新增文章:'.$data['title']);
  259. // 生成静态页面代码
  260. $successData = [
  261. 'aid' => $aid,
  262. 'tid' => $post['typeid'],
  263. ];
  264. $this->success("操作成功!", null, $successData);
  265. exit;
  266. }
  267. $this->error("操作失败!");
  268. exit;
  269. }
  270. $typeid = input('param.typeid/d', 0);
  271. $assign_data['typeid'] = $typeid; // 栏目ID
  272. // 栏目信息
  273. $arctypeInfo = Db::name('arctype')->find($typeid);
  274. /*允许发布文档列表的栏目*/
  275. $arctype_html = allow_release_arctype($typeid, array($this->channeltype));
  276. $assign_data['arctype_html'] = $arctype_html;
  277. /*--end*/
  278. /*自定义字段*/
  279. $addonFieldExtList = model('Field')->getChannelFieldList($this->channeltype);
  280. $channelfieldBindRow = Db::name('channelfield_bind')->where([
  281. 'typeid' => ['IN', [0,$typeid]],
  282. ])->column('field_id');
  283. if (!empty($channelfieldBindRow)) {
  284. foreach ($addonFieldExtList as $key => $val) {
  285. if (!in_array($val['id'], $channelfieldBindRow)) {
  286. unset($addonFieldExtList[$key]);
  287. }
  288. }
  289. }
  290. $assign_data['addonFieldExtList'] = $addonFieldExtList;
  291. $assign_data['aid'] = 0;
  292. /*--end*/
  293. // 阅读权限
  294. $arcrank_list = get_arcrank_list();
  295. $assign_data['arcrank_list'] = $arcrank_list;
  296. /*模板列表*/
  297. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  298. $templateList = $archivesLogic->getTemplateList($this->nid);
  299. $this->assign('templateList', $templateList);
  300. /*--end*/
  301. /*默认模板文件*/
  302. $tempview = 'view_'.$this->nid.'.'.config('template.view_suffix');
  303. !empty($arctypeInfo['tempview']) && $tempview = $arctypeInfo['tempview'];
  304. $this->assign('tempview', $tempview);
  305. /*--end*/
  306. // URL模式
  307. $tpcache = config('tpcache');
  308. $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
  309. $this->assign($assign_data);
  310. return $this->fetch();
  311. }
  312. /**
  313. * 编辑
  314. */
  315. public function edit()
  316. {
  317. if (IS_POST) {
  318. $post = input('post.');
  319. $typeid = input('post.typeid/d', 0);
  320. $content = input('post.addonFieldExt.content', '', null);
  321. // 根据标题自动提取相关的关键字
  322. $seo_keywords = $post['seo_keywords'];
  323. if (!empty($seo_keywords)) {
  324. $seo_keywords = str_replace(',', ',', $seo_keywords);
  325. } else {
  326. // $seo_keywords = get_split_word($post['title'], $content);
  327. }
  328. // 自动获取内容第一张图片作为封面图
  329. $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
  330. $litpic = '';
  331. if ($is_remote == 1) {
  332. $litpic = $post['litpic_remote'];
  333. } else {
  334. $litpic = $post['litpic_local'];
  335. }
  336. if (empty($litpic)) {
  337. $litpic = get_html_first_imgurl($content);
  338. }
  339. $post['litpic'] = $litpic;
  340. /*是否有封面图*/
  341. if (empty($post['litpic'])) {
  342. $is_litpic = 0; // 无封面图
  343. } else {
  344. $is_litpic = !empty($post['is_litpic']) ? $post['is_litpic'] : 0; // 有封面图
  345. }
  346. // SEO描述
  347. $seo_description = '';
  348. if (empty($post['seo_description']) && !empty($content)) {
  349. $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
  350. } else {
  351. $seo_description = $post['seo_description'];
  352. }
  353. // --外部链接
  354. $jumplinks = '';
  355. $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
  356. if (intval($is_jump) > 0) {
  357. $jumplinks = $post['jumplinks'];
  358. }
  359. // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
  360. if ($post['type_tempview'] == $post['tempview']) {
  361. unset($post['type_tempview']);
  362. unset($post['tempview']);
  363. }
  364. // 同步栏目切换模型之后的文档模型
  365. $channel = Db::name('arctype')->where(['id'=>$typeid])->getField('current_channel');
  366. //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
  367. if (!empty($post['htmlfilename'])) {
  368. $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
  369. $post['htmlfilename'] = strtolower($post['htmlfilename']);
  370. //判断是否存在相同的自定义文件名
  371. $filenameCount = Db::name('archives')->where([
  372. 'aid' => ['NEQ', $post['aid']],
  373. 'htmlfilename' => $post['htmlfilename'],
  374. ])->count();
  375. if (!empty($filenameCount)) {
  376. $this->error("自定义文件名已存在,请重新设置!");
  377. }
  378. }
  379. // --存储数据
  380. $newData = array(
  381. 'typeid'=> $typeid,
  382. 'channel' => $channel,
  383. 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
  384. 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
  385. 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
  386. 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
  387. 'is_jump' => $is_jump,
  388. 'is_litpic' => $is_litpic,
  389. 'jumplinks' => $jumplinks,
  390. 'seo_keywords' => $seo_keywords,
  391. 'seo_description' => $seo_description,
  392. 'add_time' => strtotime($post['add_time']),
  393. 'update_time' => getTime(),
  394. );
  395. $data = array_merge($post, $newData);
  396. $r = M('archives')->where([
  397. 'aid' => $data['aid'],
  398. 'lang' => $this->admin_lang,
  399. ])->update($data);
  400. if ($r) {
  401. // ---------后置操作
  402. model('Article')->afterSave($data['aid'], $data, 'edit');
  403. // ---------end
  404. adminLog('编辑文章:'.$data['title']);
  405. // 生成静态页面代码
  406. $successData = [
  407. 'aid' => $data['aid'],
  408. 'tid' => $typeid,
  409. ];
  410. $this->success("操作成功!", null, $successData);
  411. exit;
  412. }
  413. $this->error("操作失败!");
  414. exit;
  415. }
  416. $assign_data = array();
  417. $id = input('id/d');
  418. $info = model('Article')->getInfo($id, null, false);
  419. if (empty($info)) {
  420. $this->error('数据不存在,请联系管理员!');
  421. exit;
  422. }
  423. /*兼容采集没有归属栏目的文档*/
  424. if (empty($info['channel'])) {
  425. $channelRow = Db::name('channeltype')->field('id as channel')
  426. ->where('id',$this->channeltype)
  427. ->find();
  428. $info = array_merge($info, $channelRow);
  429. }
  430. /*--end*/
  431. $typeid = $info['typeid'];
  432. // 栏目信息
  433. $arctypeInfo = Db::name('arctype')->find($typeid);
  434. $info['channel'] = $arctypeInfo['current_channel'];
  435. if (is_http_url($info['litpic'])) {
  436. $info['is_remote'] = 1;
  437. $info['litpic_remote'] = handle_subdir_pic($info['litpic']);
  438. } else {
  439. $info['is_remote'] = 0;
  440. $info['litpic_local'] = handle_subdir_pic($info['litpic']);
  441. }
  442. // SEO描述
  443. if (!empty($info['seo_description'])) {
  444. $info['seo_description'] = @msubstr(checkStrHtml($info['seo_description']), 0, config('global.arc_seo_description_length'), false);
  445. }
  446. $assign_data['field'] = $info;
  447. /*允许发布文档列表的栏目,文档所在模型以栏目所在模型为主,兼容切换模型之后的数据编辑*/
  448. $arctype_html = allow_release_arctype($typeid, array($info['channel']));
  449. $assign_data['arctype_html'] = $arctype_html;
  450. /*--end*/
  451. /*自定义字段*/
  452. $addonFieldExtList = model('Field')->getChannelFieldList($info['channel'], 0, $id, $info);
  453. $channelfieldBindRow = Db::name('channelfield_bind')->where([
  454. 'typeid' => ['IN', [0,$typeid]],
  455. ])->column('field_id');
  456. if (!empty($channelfieldBindRow)) {
  457. foreach ($addonFieldExtList as $key => $val) {
  458. if (!in_array($val['id'], $channelfieldBindRow)) {
  459. unset($addonFieldExtList[$key]);
  460. }
  461. }
  462. }
  463. $assign_data['addonFieldExtList'] = $addonFieldExtList;
  464. $assign_data['aid'] = $id;
  465. /*--end*/
  466. // 阅读权限
  467. $arcrank_list = get_arcrank_list();
  468. $assign_data['arcrank_list'] = $arcrank_list;
  469. /*模板列表*/
  470. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  471. $templateList = $archivesLogic->getTemplateList($this->nid);
  472. $this->assign('templateList', $templateList);
  473. /*--end*/
  474. /*默认模板文件*/
  475. $tempview = $info['tempview'];
  476. empty($tempview) && $tempview = $arctypeInfo['tempview'];
  477. $this->assign('tempview', $tempview);
  478. /*--end*/
  479. // URL模式
  480. $tpcache = config('tpcache');
  481. $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
  482. $this->assign($assign_data);
  483. return $this->fetch();
  484. }
  485. /**
  486. * 删除
  487. */
  488. public function del()
  489. {
  490. if (IS_POST) {
  491. $archivesLogic = new \app\admin\logic\ArchivesLogic;
  492. $archivesLogic->del();
  493. }
  494. }
  495. }