123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <?php
- namespace app\index\controller\cms;
- use addons\cms\library\FulltextSearch;
- use addons\cms\library\Service;
- use addons\cms\model\Channel;
- use addons\cms\model\Modelx;
- use addons\cms\model\Tag;
- use app\common\controller\Frontend;
- use app\common\model\User;
- use fast\Tree;
- use think\Db;
- use think\Exception;
- use think\Validate;
- /**
- * 会员文档
- */
- class Archives extends Frontend
- {
- protected $layout = 'default';
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- /**
- * 发表文章
- */
- public function post()
- {
- $config = get_addon_config('cms');
- $id = $this->request->get('id');
- $archives = $id ? \app\admin\model\cms\Archives::get($id) : null;
- if ($archives) {
- $channel = Channel::get($archives['channel_id']);
- if (!$channel) {
- $this->error(__('未找到指定栏目'));
- }
- $model = \addons\cms\model\Modelx::get($channel['model_id']);
- if (!$model) {
- $this->error(__('未找到指定模型'));
- }
- if ($archives['user_id'] != $this->auth->id) {
- $this->error("无法进行越权操作!");
- }
- } else {
- $model = null;
- $model_id = $this->request->request('model_id');
- // 如果有model_id则调用指定模型
- if ($model_id) {
- $model = Modelx::get($model_id);
- }
- }
- // 如果来源于提交
- if ($this->request->isPost()) {
- $this->token();
- if ($this->auth->score < $config['limitscore']['postarchives']) {
- $this->error("积分必须大于{$config['limitscore']['postarchives']}才可以发布文章");
- }
- $row = $this->request->post('row/a', '', 'trim,xss_clean');
- $rule = [
- 'title|标题' => 'require|length:3,100',
- 'channel_id|栏目' => 'require|integer',
- ];
- $msg = [
- 'title.require' => '标题不能为空',
- 'title.length' => '标题长度限制在3~100个字符',
- 'channel_id' => '栏目不能为空',
- 'content.require' => '内容不能为空',
- ];
- $validate = new Validate($rule, $msg);
- $result = $validate->check($row);
- if (!$result) {
- $this->error($validate->getError());
- }
- $channelIds = isset($row['channel_ids']) ? (is_array($row['channel_ids']) ? $row['channel_ids'] : explode(',', $row['channel_ids'])) : [];
- $channelIds = array_merge([$row['channel_id']], $channelIds);
- $channelIds = array_filter($channelIds);
- $count = Channel::where('id', 'in', $channelIds)->where('iscontribute', 0)->count();
- if ($count > 0) {
- $this->error("栏目不允许投稿");
- }
- //审核状态
- $status = 'normal';
- if ($config['isarchivesaudit'] == 1) {
- $status = 'hidden';
- } elseif ($config['isarchivesaudit'] == 0) {
- $status = 'normal';
- } else {
- $textArr = array_map(function ($item) {
- return is_array($item) ? json_encode($item, JSON_UNESCAPED_UNICODE) : $item;
- }, $row);
- if (!Service::isContentLegal(implode(' ', $textArr))) {
- $status = 'hidden';
- }
- }
- $row['user_id'] = $this->auth->id;
- $row['status'] = $status;
- $row['publishtime'] = time();
- Db::startTrans();
- try {
- if ($archives) {
- $archives->allowField(true)->save($row);
- } else {
- (new \app\admin\model\cms\Archives)->allowField(true)->save($row);
- }
- //增加积分
- $status == 'normal' && User::score($config['score']['postarchives'], $this->auth->id, '发布文章');
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error("发生错误:" . $e->getMessage());
- }
- if ($status === 'hidden') {
- //发送通知
- $status === 'hidden' && Service::notice('CMS收到一篇新的文章审核', $config['auditnotice'], $config['noticetemplateid']);
- $this->success("发布成功!请等待审核!");
- } else {
- $this->success("发布成功!");
- }
- }
- $channel = new Channel();
- // 合并主副表
- if ($archives) {
- $addon = db($model['table'])->where('id', $archives['id'])->find();
- if ($addon) {
- $archives->setData($addon);
- }
- }
- // 读取可发布的栏目列表
- $disabledIds = [];
- $channelList = collection(
- $channel->where('status', 'normal')
- ->where("iscontribute", 1)
- ->order("weigh desc,id desc")
- ->cache(true)
- ->select()
- )->toArray();
- $channelParents = [];
- foreach ($channelList as $index => $item) {
- if ($item['parent_id']) {
- $channelParents[] = $item['parent_id'];
- }
- }
- foreach ($channelList as $k => $v) {
- if ($v['type'] == 'link' || ($model && $model['id'] != $v['model_id'])) {
- $disabledIds[] = $v['id'];
- }
- //if ($v['type'] == 'channel' && !in_array($v['id'], $channelParents)) {
- // unset($channelList[$k]);
- //}
- }
- $tree = Tree::instance()->init($channelList, 'parent_id');
- $channelOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>", $archives ? $archives['channel_id'] : '', $disabledIds);
- $this->view->assign('channelOptions', $channelOptions);
- $this->view->assign([
- 'archives' => $archives,
- 'channelOptions' => $channelOptions,
- 'categoryList' => ''
- ]);
- $this->assignconfig('archives_id', $archives ? $archives['id'] : 0);
- //读取副栏目
- $disabledIds = [];
- $all = collection(Channel::order("weigh desc,id desc")->select())->toArray();
- foreach ($all as $k => $v) {
- if ($v['type'] == 'link' || ($archives && $v['model_id'] != $archives->model_id)) {
- $disabledIds[] = $v['id'];
- }
- }
- $tree = Tree::instance()->init($all, 'parent_id');
- $secondChannelOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>", explode(',', $archives ? $archives['channel_ids'] : ''), $disabledIds);
- $this->view->assign('secondChannelOptions', $secondChannelOptions);
- $modelName = $model ? $model['name'] : '文章';
- $this->view->assign('title', $archives ? "修改{$modelName}" : "发布{$modelName}");
- $this->view->assign('model', $model);
- return $this->view->fetch();
- }
- /**
- * 我的发布
- */
- public function my()
- {
- $archives = new \addons\cms\model\Archives;
- $model = null;
- $model_id = (int)$this->request->request('model_id');
- $channel_id = (int)$this->request->request('channel_id');
- $q = $this->request->request('q');
- $config = ['query' => []];
- // 指定模型
- if ($model_id) {
- $model = Modelx::get($model_id);
- if ($model) {
- $archives->where('model_id', $model_id);
- $config['query']['model_id'] = $model_id;
- }
- }
- // 搜索关键字
- if ($q) {
- $archives->where('title|keywords|description', 'like', '%' . $q . '%');
- $config['query']['q'] = $q;
- }
- // 栏目
- if ($channel_id) {
- $archives->where('channel_id', $channel_id);
- $config['query']['channel_id'] = $channel_id;
- }
- $user_id = $this->auth->id;
- $archivesList = $archives->where('user_id', $user_id)
- ->order('id', 'desc')
- ->paginate(10, null, $config);
- $channelList = Channel::where('id', 'in', function ($query) use ($user_id) {
- $query->name('cms_archives')->where('user_id', $user_id)->field('channel_id');
- })->where('status', 'normal')->select();
- $this->view->assign('archivesList', $archivesList);
- $this->view->assign('channelList', $channelList);
- $this->view->assign('title', '我发布的' . ($model ? $model['name'] : '文档'));
- $this->view->assign('model', $model);
- return $this->view->fetch();
- }
- /**
- * 删除文档
- */
- public function delete()
- {
- $id = (int)$this->request->request('id/d');
- if (!$id) {
- $this->error("参数不正确");
- }
- $archives = \addons\cms\model\Archives::where('id', $id)->where('user_id', $this->auth->id)->find();
- if (!$archives) {
- $this->error("未找到指定的文档");
- }
- Db::startTrans();
- try {
- $archives->delete();
- if ($archives->channel->items > 0) {
- $archives->channel->setDec("items");
- }
- Db::commit();
- } catch (Exception $e) {
- Db::rollback();
- $this->error("删除文档失败");
- }
- $this->success("删除文档成功");
- }
- /**
- * 获取栏目列表
- * @internal
- */
- public function get_channel_fields()
- {
- $this->view->engine->layout(false);
- $channel_id = $this->request->post('channel_id');
- $archives_id = $this->request->post('archives_id');
- $channel = Channel::get($channel_id, 'model');
- if ($channel && $channel['type'] != 'link') {
- $model_id = $channel['model_id'];
- $values = [];
- if ($archives_id) {
- $values = db($channel['model']['table'])->where('id', $archives_id)->find();
- //优先从栏目获取模型ID,再从文档获取
- $archives = \app\admin\model\cms\Archives::get($archives_id);
- $model_id = $archives ? $archives['model_id'] : $model_id;
- }
- $fields = Service::getCustomFields('model', $model_id, $values, ['iscontribute' => 1]);
- $this->view->assign('fields', $fields);
- $this->view->assign('values', $values);
- $setting = $channel->getRelation('model')->setting ?? [];
- $this->success('', null, ['contributefields' => $setting && isset($setting['contributefields']) ? $setting['contributefields'] : [], 'html' => $this->view->fetch('cms/common/fields')]);
- } else {
- $this->error(__('请选择栏目'));
- }
- $this->error(__('参数不能为空', 'ids'));
- }
- /**
- * 标签自动完成
- * @internal
- */
- public function tags_autocomplete()
- {
- $q = $this->request->request('q');
- $list = \addons\cms\model\Tag::where('name', 'like', '%' . $q . '%')->limit(10)->column('name');
- echo json_encode($list);
- return;
- }
- /**
- * 搜索建议
- * @internal
- */
- public function suggestion()
- {
- $q = trim($this->request->request("q"));
- $id = trim($this->request->request("id/d"));
- $list = [];
- $result = \addons\cms\model\Archives::where("title|keywords|description", "like", "%{$q}%")->where('id', '<>', $id)->limit(10)->order("id", "desc")->select();
- foreach ($result as $index => $item) {
- $list[] = ['id' => $item['id'], 'url' => $item['fullurl'], 'image' => cdnurl($item['image']), 'title' => $item['title'], 'create_date' => datetime($item['createtime'])];
- }
- return json($list);
- }
- }
|