Custom.php 23 KB

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