123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595 |
- <?php
- /**
- * 易优CMS
- * ============================================================================
- * 版权所有 2016-2028 海南赞赞网络科技有限公司,并保留所有权利。
- * 网站地址: http://www.eyoucms.com
- * ----------------------------------------------------------------------------
- * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
- * ============================================================================
- * Author: 陈风任 <491085389@qq.com>
- * Date: 2019-1-7
- */
- namespace app\admin\controller;
- use think\Page;
- use think\Db;
- class Custom extends Base
- {
- // 模型标识
- public $nid = '';
- // 模型ID
- public $channeltype = 0;
- // 模型附加表
- public $table = '';
- /*
- * 初始化操作
- */
- public function _initialize()
- {
- parent::_initialize();
- $this->archives_db = Db::name('archives');
-
- $this->channeltype = input('param.channel/d', 0);
- $channeltypeRow = Db::name('channeltype')->field('nid,table')->where(['id'=>['eq',$this->channeltype]])->find();
- if (empty($this->channeltype) || empty($channeltypeRow)) {
- $this->error('自定义模型ID丢失,打开失败!');
- }
- $this->nid = $channeltypeRow['nid'];
- $this->assign('nid', $this->nid);
- $this->assign('channeltype', $this->channeltype);
- }
- /**
- * 列表
- */
- public function index()
- {
- $assign_data = array();
- $condition = array();
- // 获取到所有GET参数
- $param = input('param.');
- $flag = input('flag/s');
- $typeid = input('typeid/d', 0);
- $begin = strtotime(input('add_time_begin'));
- $end = strtotime(input('add_time_end'));
- // 应用搜索条件
- foreach (['keywords','typeid','flag','is_release'] as $key) {
- if (isset($param[$key]) && $param[$key] !== '') {
- if ($key == 'keywords') {
- $condition['a.title'] = array('LIKE', "%{$param[$key]}%");
- } else if ($key == 'typeid') {
- $typeid = $param[$key];
- $hasRow = model('Arctype')->getHasChildren($typeid);
- $typeids = get_arr_column($hasRow, 'id');
- /*权限控制 by 小虎哥*/
- $admin_info = session('admin_info');
- if (0 < intval($admin_info['role_id'])) {
- $auth_role_info = $admin_info['auth_role_info'];
- if(! empty($auth_role_info)){
- if(! empty($auth_role_info['permission']['arctype'])){
- if (!empty($typeid)) {
- $typeids = array_intersect($typeids, $auth_role_info['permission']['arctype']);
- }
- }
- }
- }
- /*--end*/
- $condition['a.typeid'] = array('IN', $typeids);
- } else if ($key == 'flag') {
- if ('is_release' == $param[$key]) {
- $condition['a.users_id'] = array('gt', 0);
- } else {
- $condition['a.'.$param[$key]] = array('eq', 1);
- }
- // } else if ($key == 'is_release') {
- // if (0 < intval($param[$key])) {
- // $condition['a.users_id'] = array('gt', intval($param[$key]));
- // }
- } else {
- $condition['a.'.$key] = array('eq', $param[$key]);
- }
- }
- }
- /*权限控制 by 小虎哥*/
- $admin_info = session('admin_info');
- if (0 < intval($admin_info['role_id'])) {
- $auth_role_info = $admin_info['auth_role_info'];
- if(! empty($auth_role_info)){
- if(isset($auth_role_info['only_oneself']) && 1 == $auth_role_info['only_oneself']){
- $condition['a.admin_id'] = $admin_info['admin_id'];
- }
- }
- }
- /*--end*/
-
- // 时间检索
- if ($begin > 0 && $end > 0) {
- $condition['a.add_time'] = array('between',"$begin,$end");
- } else if ($begin > 0) {
- $condition['a.add_time'] = array('egt', $begin);
- } else if ($end > 0) {
- $condition['a.add_time'] = array('elt', $end);
- }
- // 模型ID
- $condition['a.channel'] = array('eq', $this->channeltype);
- // 多语言
- $condition['a.lang'] = array('eq', $this->admin_lang);
- // 回收站
- $condition['a.is_del'] = array('eq', 0);
- /*自定义排序*/
- $orderby = input('param.orderby/s');
- $orderway = input('param.orderway/s');
- if (!empty($orderby)) {
- $orderby = "a.{$orderby} {$orderway}";
- $orderby .= ", a.aid desc";
- } else {
- $orderby = "a.aid desc";
- }
- /*end*/
- /**
- * 数据查询,搜索出主键ID的值
- */
- $count = $this->archives_db->alias('a')->where($condition)->count('aid');// 查询满足要求的总记录数
- $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
- $list = $this->archives_db
- ->field("a.aid")
- ->alias('a')
- ->where($condition)
- ->order($orderby)
- ->limit($Page->firstRow.','.$Page->listRows)
- ->getAllWithIndex('aid');
- /**
- * 完善数据集信息
- * 在数据量大的情况下,经过优化的搜索逻辑,先搜索出主键ID,再通过ID将其他信息补充完整;
- */
- if ($list) {
- $aids = array_keys($list);
- $fields = "b.*, a.*, a.aid as aid";
- $row = $this->archives_db
- ->field($fields)
- ->alias('a')
- ->join('__ARCTYPE__ b', 'a.typeid = b.id', 'LEFT')
- ->where('a.aid', 'in', $aids)
- ->getAllWithIndex('aid');
- foreach ($list as $key => $val) {
- $row[$val['aid']]['arcurl'] = get_arcurl($row[$val['aid']]);
- $row[$val['aid']]['litpic'] = handle_subdir_pic($row[$val['aid']]['litpic']); // 支持子目录
- $list[$key] = $row[$val['aid']];
- }
- }
- $show = $Page->show(); // 分页显示输出
- $assign_data['page'] = $show; // 赋值分页输出
- $assign_data['list'] = $list; // 赋值数据集
- $assign_data['pager'] = $Page; // 赋值分页对象
- // 栏目ID
- $assign_data['typeid'] = $typeid; // 栏目ID
- /*当前栏目信息*/
- $arctype_info = array();
- if ($typeid > 0) {
- $arctype_info = M('arctype')->field('typename')->find($typeid);
- }
- $assign_data['arctype_info'] = $arctype_info;
- /*--end*/
- /*选项卡*/
- $tab = input('param.tab/d', 3);
- $assign_data['tab'] = $tab;
- /*--end*/
- $this->assign($assign_data);
-
- return $this->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- if (IS_POST) {
- $post = input('post.');
- /*获取第一个html类型的内容,作为文档的内容来截取SEO描述*/
- $contentField = Db::name('channelfield')->where([
- 'channel_id' => $this->channeltype,
- 'dtype' => 'htmltext',
- ])->getField('name');
- $content = input('post.addonFieldExt.'.$contentField, '', null);
- /*--end*/
- // 根据标题自动提取相关的关键字
- $seo_keywords = $post['seo_keywords'];
- if (!empty($seo_keywords)) {
- $seo_keywords = str_replace(',', ',', $seo_keywords);
- } else {
- // $seo_keywords = get_split_word($post['title'], $content);
- }
- // 自动获取内容第一张图片作为封面图
- $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
- $litpic = '';
- if ($is_remote == 1) {
- $litpic = $post['litpic_remote'];
- } else {
- $litpic = $post['litpic_local'];
- }
- if (empty($litpic)) {
- $litpic = get_html_first_imgurl($content);
- }
- $post['litpic'] = $litpic;
- /*是否有封面图*/
- if (empty($post['litpic'])) {
- $is_litpic = 0; // 无封面图
- } else {
- $is_litpic = 1; // 有封面图
- }
- // SEO描述
- $seo_description = '';
- if (empty($post['seo_description']) && !empty($content)) {
- $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
- } else {
- $seo_description = $post['seo_description'];
- }
- // 外部链接跳转
- $jumplinks = '';
- $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
- if (intval($is_jump) > 0) {
- $jumplinks = $post['jumplinks'];
- }
- // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
- if ($post['type_tempview'] == $post['tempview']) {
- unset($post['type_tempview']);
- unset($post['tempview']);
- }
- //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
- if (!empty($post['htmlfilename'])) {
- $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
- $post['htmlfilename'] = strtolower($post['htmlfilename']);
- //判断是否存在相同的自定义文件名
- $filenameCount = Db::name('archives')->where('htmlfilename', $post['htmlfilename'])->count();
- if (!empty($filenameCount)) {
- $this->error("自定义文件名已存在,请重新设置!");
- }
- }
- // --存储数据
- $newData = array(
- 'typeid'=> empty($post['typeid']) ? 0 : $post['typeid'],
- 'channel' => $this->channeltype,
- 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
- 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
- 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
- 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
- 'is_jump' => $is_jump,
- 'is_litpic' => $is_litpic,
- 'jumplinks' => $jumplinks,
- 'seo_keywords' => $seo_keywords,
- 'seo_description' => $seo_description,
- 'admin_id' => session('admin_info.admin_id'),
- 'lang' => $this->admin_lang,
- 'sort_order' => 100,
- 'add_time' => strtotime($post['add_time']),
- 'update_time' => strtotime($post['add_time']),
- );
- $data = array_merge($post, $newData);
- $aid = $this->archives_db->insertGetId($data);
- $_POST['aid'] = $aid;
- if ($aid) {
- // ---------后置操作
- model('Custom')->afterSave($aid, $data, 'add');
- // ---------end
- adminLog('新增数据:'.$data['title']);
- // 生成静态页面代码
- $successData = [
- 'aid' => $aid,
- 'tid' => $post['typeid'],
- ];
- $this->success("操作成功!", null, $successData);
- exit;
- }
- $this->error("操作失败!");
- exit;
- }
- $typeid = input('param.typeid/d', 0);
- $assign_data['typeid'] = $typeid; // 栏目ID
- // 栏目信息
- $arctypeInfo = Db::name('arctype')->find($typeid);
- /*允许发布文档列表的栏目*/
- $arctype_html = allow_release_arctype($typeid, array($this->channeltype));
- $assign_data['arctype_html'] = $arctype_html;
- /*--end*/
- /*自定义字段*/
- $addonFieldExtList = model('Field')->getChannelFieldList($this->channeltype);
- $channelfieldBindRow = Db::name('channelfield_bind')->where([
- 'typeid' => ['IN', [0,$typeid]],
- ])->column('field_id');
- if (!empty($channelfieldBindRow)) {
- foreach ($addonFieldExtList as $key => $val) {
- if (!in_array($val['id'], $channelfieldBindRow)) {
- unset($addonFieldExtList[$key]);
- }
- }
- }
- $assign_data['addonFieldExtList'] = $addonFieldExtList;
- $assign_data['aid'] = 0;
- /*--end*/
- // 阅读权限
- $arcrank_list = get_arcrank_list();
- $assign_data['arcrank_list'] = $arcrank_list;
-
- /*获取可显示的系统字段*/
- $condition['ifcontrol'] = 0;
- $condition['channel_id'] = $this->channeltype;
- $channelfield_row = Db::name('channelfield')->where($condition)->field('name,ifeditable')->getAllWithIndex('name');
- $assign_data['channelfield_row'] = $channelfield_row;
- /*--end*/
- /*模板列表*/
- $archivesLogic = new \app\admin\logic\ArchivesLogic;
- $templateList = $archivesLogic->getTemplateList($this->nid);
- $this->assign('templateList', $templateList);
- /*--end*/
- /*默认模板文件*/
- $tempview = 'view_'.$this->nid.'.'.config('template.view_suffix');
- !empty($arctypeInfo['tempview']) && $tempview = $arctypeInfo['tempview'];
- $this->assign('tempview', $tempview);
- /*--end*/
- // URL模式
- $tpcache = config('tpcache');
- $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
- $this->assign($assign_data);
- return $this->fetch();
- }
-
- /**
- * 编辑
- */
- public function edit()
- {
- if (IS_POST) {
- $post = input('post.');
- $typeid = input('post.typeid/d', 0);
- /*获取第一个html类型的内容,作为文档的内容来截取SEO描述*/
- $contentField = Db::name('channelfield')->where([
- 'channel_id' => $this->channeltype,
- 'dtype' => 'htmltext',
- ])->getField('name');
- $content = input('post.addonFieldExt.'.$contentField, '', null);
- /*--end*/
- // 根据标题自动提取相关的关键字
- $seo_keywords = $post['seo_keywords'];
- if (!empty($seo_keywords)) {
- $seo_keywords = str_replace(',', ',', $seo_keywords);
- } else {
- // $seo_keywords = get_split_word($post['title'], $content);
- }
- // 自动获取内容第一张图片作为封面图
- $is_remote = !empty($post['is_remote']) ? $post['is_remote'] : 0;
- $litpic = '';
- if ($is_remote == 1) {
- $litpic = $post['litpic_remote'];
- } else {
- $litpic = $post['litpic_local'];
- }
- if (empty($litpic)) {
- $litpic = get_html_first_imgurl($content);
- }
- $post['litpic'] = $litpic;
- /*是否有封面图*/
- if (empty($post['litpic'])) {
- $is_litpic = 0; // 无封面图
- } else {
- $is_litpic = !empty($post['is_litpic']) ? $post['is_litpic'] : 0; // 有封面图
- }
- // SEO描述
- $seo_description = '';
- if (empty($post['seo_description']) && !empty($content)) {
- $seo_description = @msubstr(checkStrHtml($content), 0, config('global.arc_seo_description_length'), false);
- } else {
- $seo_description = $post['seo_description'];
- }
- // --外部链接
- $jumplinks = '';
- $is_jump = isset($post['is_jump']) ? $post['is_jump'] : 0;
- if (intval($is_jump) > 0) {
- $jumplinks = $post['jumplinks'];
- }
- // 模板文件,如果文档模板名与栏目指定的一致,默认就为空。让它跟随栏目的指定而变
- if ($post['type_tempview'] == $post['tempview']) {
- unset($post['type_tempview']);
- unset($post['tempview']);
- }
- // 同步栏目切换模型之后的文档模型
- $channel = Db::name('arctype')->where(['id'=>$typeid])->getField('current_channel');
- //处理自定义文件名,仅由字母数字下划线和短横杆组成,大写强制转换为小写
- if (!empty($post['htmlfilename'])) {
- $post['htmlfilename'] = preg_replace("/[^a-zA-Z0-9_-]+/", "", $post['htmlfilename']);
- $post['htmlfilename'] = strtolower($post['htmlfilename']);
- //判断是否存在相同的自定义文件名
- $filenameCount = Db::name('archives')->where([
- 'aid' => ['NEQ', $post['aid']],
- 'htmlfilename' => $post['htmlfilename'],
- ])->count();
- if (!empty($filenameCount)) {
- $this->error("自定义文件名已存在,请重新设置!");
- }
- }
-
- // --存储数据
- $newData = array(
- 'typeid'=> $typeid,
- 'channel' => $channel,
- 'is_b' => empty($post['is_b']) ? 0 : $post['is_b'],
- 'is_head' => empty($post['is_head']) ? 0 : $post['is_head'],
- 'is_special' => empty($post['is_special']) ? 0 : $post['is_special'],
- 'is_recom' => empty($post['is_recom']) ? 0 : $post['is_recom'],
- 'is_jump' => $is_jump,
- 'is_litpic' => $is_litpic,
- 'jumplinks' => $jumplinks,
- 'seo_keywords' => $seo_keywords,
- 'seo_description' => $seo_description,
- 'add_time' => strtotime($post['add_time']),
- 'update_time' => getTime(),
- );
- $data = array_merge($post, $newData);
- $r = M('archives')->where([
- 'aid' => $data['aid'],
- 'lang' => $this->admin_lang,
- ])->update($data);
-
- if ($r) {
- // ---------后置操作
- model('Custom')->afterSave($data['aid'], $data, 'edit');
- // ---------end
- adminLog('编辑文章:'.$data['title']);
- // 生成静态页面代码
- $successData = [
- 'aid' => $data['aid'],
- 'tid' => $typeid,
- ];
- $this->success("操作成功!", null, $successData);
- exit;
- }
- $this->error("操作失败!");
- exit;
- }
- $assign_data = array();
- $id = input('id/d');
- $info = model('Custom')->getInfo($id, null, false);
- if (empty($info)) {
- $this->error('数据不存在,请联系管理员!');
- exit;
- }
- /*兼容采集没有归属栏目的文档*/
- if (empty($info['channel'])) {
- $channelRow = Db::name('channeltype')->field('id as channel')
- ->where('id',$this->channeltype)
- ->find();
- $info = array_merge($info, $channelRow);
- }
- /*--end*/
- $typeid = $info['typeid'];
- // 栏目信息
- $arctypeInfo = Db::name('arctype')->find($typeid);
- $info['channel'] = $arctypeInfo['current_channel'];
- if (is_http_url($info['litpic'])) {
- $info['is_remote'] = 1;
- $info['litpic_remote'] = handle_subdir_pic($info['litpic']);
- } else {
- $info['is_remote'] = 0;
- $info['litpic_local'] = handle_subdir_pic($info['litpic']);
- }
-
- // SEO描述
- if (!empty($info['seo_description'])) {
- $info['seo_description'] = @msubstr(checkStrHtml($info['seo_description']), 0, config('global.arc_seo_description_length'), false);
- }
- $assign_data['field'] = $info;
- /*允许发布文档列表的栏目,文档所在模型以栏目所在模型为主,兼容切换模型之后的数据编辑*/
- $arctype_html = allow_release_arctype($typeid, array($info['channel']));
- $assign_data['arctype_html'] = $arctype_html;
- /*--end*/
-
- /*自定义字段*/
- $addonFieldExtList = model('Field')->getChannelFieldList($info['channel'], 0, $id, $info);
- $channelfieldBindRow = Db::name('channelfield_bind')->where([
- 'typeid' => ['IN', [0,$typeid]],
- ])->column('field_id');
- if (!empty($channelfieldBindRow)) {
- foreach ($addonFieldExtList as $key => $val) {
- if (!in_array($val['id'], $channelfieldBindRow)) {
- unset($addonFieldExtList[$key]);
- }
- }
- }
- $assign_data['addonFieldExtList'] = $addonFieldExtList;
- $assign_data['aid'] = $id;
- /*--end*/
- /*获取可显示的系统字段*/
- $condition['ifcontrol'] = 0;
- $condition['channel_id'] = $this->channeltype;
- $channelfield_row = Db::name('channelfield')->where($condition)->field('name,ifeditable')->getAllWithIndex('name');
- $assign_data['channelfield_row'] = $channelfield_row;
- /*--end*/
- // 阅读权限
- $arcrank_list = get_arcrank_list();
- $assign_data['arcrank_list'] = $arcrank_list;
- /*模板列表*/
- $archivesLogic = new \app\admin\logic\ArchivesLogic;
- $templateList = $archivesLogic->getTemplateList($this->nid);
- $this->assign('templateList', $templateList);
- /*--end*/
- /*默认模板文件*/
- $tempview = $info['tempview'];
- empty($tempview) && $tempview = $arctypeInfo['tempview'];
- $this->assign('tempview', $tempview);
- /*--end*/
- // URL模式
- $tpcache = config('tpcache');
- $assign_data['seo_pseudo'] = !empty($tpcache['seo_pseudo']) ? $tpcache['seo_pseudo'] : 1;
- $this->assign($assign_data);
- return $this->fetch();
- }
-
- /**
- * 删除
- */
- public function del()
- {
- $archivesLogic = new \app\admin\logic\ArchivesLogic;
- $archivesLogic->del();
- }
- }
|