Archives.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use addons\cms\library\FulltextSearch;
  4. use app\admin\model\cms\Channel;
  5. use app\admin\model\cms\ChannelAdmin;
  6. use app\admin\model\cms\Modelx;
  7. use app\common\controller\Backend;
  8. use app\common\model\User;
  9. use fast\Tree;
  10. use think\Db;
  11. use think\db\Query;
  12. /**
  13. * 内容表
  14. *
  15. * @icon fa fa-file-text-o
  16. */
  17. class Archives extends Backend
  18. {
  19. /**
  20. * Archives模型对象
  21. */
  22. protected $model = null;
  23. protected $noNeedRight = ['get_fields_html', 'check_element_available', 'suggestion'];
  24. protected $channelIds = [];
  25. protected $isSuperAdmin = false;
  26. protected $searchFields = 'id,title';
  27. public function _initialize()
  28. {
  29. parent::_initialize();
  30. $this->model = new \app\admin\model\cms\Archives;
  31. $cms = get_addon_config('cms');
  32. if ($cms['archivesdatalimit'] != 'all') {
  33. $this->dataLimit = $cms['archivesdatalimit'];
  34. }
  35. //是否超级管理员
  36. $this->isSuperAdmin = $this->auth->isSuperAdmin();
  37. $channelList = [];
  38. $disabledIds = [];
  39. $all = collection(Channel::order("weigh desc,id desc")->select())->toArray();
  40. //允许的栏目
  41. $this->channelIds = $this->isSuperAdmin || !$cms['channelallocate'] ? Channel::column('id') : ChannelAdmin::getAdminChanneIds();
  42. $parentChannelIds = Channel::where('id', 'in', $this->channelIds)->column('parent_id');
  43. $parentChannelIds = array_unique($parentChannelIds);
  44. $parentChannelList = \think\Db::name('cms_channel')->where('id', 'in', $parentChannelIds)->where('parent_id', '<>', 0)->field('id,parent_id,name')->select();
  45. $tree = Tree::instance()->init($all, 'parent_id');
  46. foreach ($parentChannelList as $index => $channel) {
  47. $parentChannelIds = array_merge($parentChannelIds, $tree->getParentsIds($channel['parent_id'], true));
  48. }
  49. foreach ($all as $k => $v) {
  50. $state = ['opened' => true];
  51. if ($v['type'] == 'link') {
  52. $disabledIds[] = $v['id'];
  53. }
  54. if ($v['type'] == 'link') {
  55. $state['checkbox_disabled'] = true;
  56. }
  57. if (!$this->isSuperAdmin) {
  58. if (!in_array($v['id'], $parentChannelIds) && !in_array($v['id'], $this->channelIds)) {
  59. unset($all[$k]);
  60. continue;
  61. }
  62. }
  63. $channelList[] = [
  64. 'id' => $v['id'],
  65. 'parent' => $v['parent_id'] ? $v['parent_id'] : '#',
  66. 'text' => __($v['name']),
  67. 'type' => $v['type'],
  68. 'state' => $state
  69. ];
  70. }
  71. $tree = Tree::instance()->init($all, 'parent_id');
  72. $channelOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>", '', $disabledIds);
  73. $secondChannelOptions = $tree->getTree(0, "<option model='@model_id' value=@id disabled>@spacer@name</option>", '', $disabledIds);
  74. $this->view->assign('channelOptions', $channelOptions);
  75. $this->view->assign('secondChannelOptions', $secondChannelOptions);
  76. $this->assignconfig('channelList', $channelList);
  77. $this->assignconfig("flagList", $this->model->getFlagList());
  78. $this->view->assign("flagList", $this->model->getFlagList());
  79. $this->view->assign("statusList", $this->model->getStatusList());
  80. $this->assignconfig('cms', ['archiveseditmode' => $cms['archiveseditmode']]);
  81. }
  82. /**
  83. * 查看
  84. */
  85. public function index()
  86. {
  87. //设置过滤方法
  88. $this->request->filter(['strip_tags']);
  89. if ($this->request->isAjax()) {
  90. $this->relationSearch = true;
  91. //如果发送的来源是Selectpage,则转发到Selectpage
  92. if ($this->request->request('keyField')) {
  93. return $this->selectpage();
  94. }
  95. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  96. if (!$this->auth->isSuperAdmin()) {
  97. $this->model->where('channel_id', 'in', $this->channelIds);
  98. }
  99. $total = $this->model
  100. ->with('Channel')
  101. ->where($where)
  102. ->order($sort, $order)
  103. ->count();
  104. if (!$this->auth->isSuperAdmin()) {
  105. $this->model->where('channel_id', 'in', $this->channelIds);
  106. }
  107. $list = $this->model
  108. ->with(['Channel'])
  109. ->where($where)
  110. ->order($sort, $order)
  111. ->limit($offset, $limit)
  112. ->select();
  113. $result = array("total" => $total, "rows" => $list);
  114. return json($result);
  115. }
  116. $modelList = \app\admin\model\cms\Modelx::all();
  117. $specialList = \app\admin\model\cms\Special::where('status', 'normal')->select();
  118. $this->view->assign('modelList', $modelList);
  119. $this->view->assign('specialList', $specialList);
  120. return $this->view->fetch();
  121. }
  122. /**
  123. * 副表内容
  124. */
  125. public function content($model_id = null)
  126. {
  127. $model = \app\admin\model\cms\Modelx::get($model_id);
  128. if (!$model) {
  129. $this->error('未找到对应模型');
  130. }
  131. $fieldsList = \app\admin\model\cms\Fields::where('source', 'model')->where('source_id', $model['id'])->where('type', '<>', 'text')->select();
  132. //设置过滤方法
  133. $this->request->filter(['strip_tags', 'trim']);
  134. if ($this->request->isAjax()) {
  135. //如果发送的来源是Selectpage,则转发到Selectpage
  136. if ($this->request->request('keyField')) {
  137. return $this->selectpage();
  138. }
  139. $fields = [];
  140. foreach ($fieldsList as $index => $item) {
  141. $fields[] = "addon." . $item['name'];
  142. }
  143. $this->searchFields = "archives.id,archives.title";
  144. $this->relationSearch = true;
  145. $table = $this->model->getTable();
  146. list($where, $sort, $order, $offset, $limit, $page, $alias) = $this->buildparams();
  147. $sort = 'archives.id';
  148. $isSuperAdmin = $this->isSuperAdmin;
  149. $channelIds = $this->channelIds;
  150. $customWhere = function ($query) use ($isSuperAdmin, $channelIds, $model_id) {
  151. if (!$isSuperAdmin) {
  152. $query->where('archives.channel_id', 'in', $channelIds);
  153. }
  154. if ($model_id) {
  155. $query->where('archives.model_id', $model_id);
  156. }
  157. };
  158. $list = $this->model
  159. ->alias($alias)
  160. ->alias('archives')
  161. ->join('cms_channel channel', 'channel.id=archives.channel_id', 'LEFT')
  162. ->join($model['table'] . ' addon', 'addon.id=archives.id', 'LEFT')
  163. ->field('archives.*,channel.name as channel_name,addon.id as aid' . ($fields ? ',' . implode(',', $fields) : ''))
  164. ->where($customWhere)
  165. ->whereNull('deletetime')
  166. ->where($where)
  167. ->order($sort, $order)
  168. ->paginate($limit);
  169. $result = array("total" => $list->total(), "rows" => $list->items());
  170. return json($result);
  171. }
  172. $fields = [];
  173. foreach ($fieldsList as $index => $item) {
  174. $fields[] = ['field' => $item['name'], 'title' => $item['title'], 'type' => $item['type'], 'content' => $item['content_list']];
  175. }
  176. $this->assignconfig('fields', $fields);
  177. $this->view->assign('fieldsList', $fieldsList);
  178. $this->view->assign('model', $model);
  179. $this->assignconfig('model_id', $model_id);
  180. $modelList = \app\admin\model\cms\Modelx::all();
  181. $this->view->assign('modelList', $modelList);
  182. return $this->view->fetch();
  183. }
  184. /**
  185. * 编辑
  186. *
  187. * @param mixed $ids
  188. * @return string
  189. */
  190. public function edit($ids = null)
  191. {
  192. $row = $this->model->get($ids);
  193. if (!$row) {
  194. $this->error(__('No Results were found'));
  195. }
  196. $adminIds = $this->getDataLimitAdminIds();
  197. if (is_array($adminIds)) {
  198. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  199. $this->error(__('You have no permission'));
  200. }
  201. }
  202. if (!$this->isSuperAdmin && !in_array($row['channel_id'], $this->channelIds)) {
  203. $this->error(__('You have no permission'));
  204. }
  205. if ($this->request->isPost()) {
  206. return parent::edit($ids);
  207. }
  208. $channel = Channel::get($row['channel_id']);
  209. if (!$channel) {
  210. $this->error(__('No specified channel found'));
  211. }
  212. $model = \app\admin\model\cms\Modelx::get($channel['model_id']);
  213. if (!$model) {
  214. $this->error(__('No specified model found'));
  215. }
  216. $addon = db($model['table'])->where('id', $row['id'])->find();
  217. if ($addon) {
  218. $row->setData($addon);
  219. }
  220. $disabledIds = [];
  221. $all = collection(Channel::order("weigh desc,id desc")->select())->toArray();
  222. foreach ($all as $k => $v) {
  223. if ($v['type'] == 'link' || $v['model_id'] != $channel['model_id']) {
  224. $disabledIds[] = $v['id'];
  225. }
  226. }
  227. $disabledIds = array_diff($disabledIds, [$row['channel_id']]);
  228. $tree = Tree::instance()->init($all, 'parent_id');
  229. $channelOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>", $row['channel_id'], $disabledIds);
  230. $secondChannelOptions = $tree->getTree(0, "<option model='@model_id' value=@id @selected @disabled>@spacer@name</option>", explode(',', $row['channel_ids']), $disabledIds);
  231. $this->view->assign('channelOptions', $channelOptions);
  232. $this->view->assign('secondChannelOptions', $secondChannelOptions);
  233. $this->view->assign("row", $row);
  234. return $this->view->fetch();
  235. }
  236. /**
  237. * 删除
  238. * @param mixed $ids
  239. */
  240. public function del($ids = "")
  241. {
  242. \app\admin\model\cms\Archives::event('after_delete', function ($row) {
  243. Channel::where('id', $row['channel_id'])->where('items', '>', 0)->setDec('items');
  244. });
  245. parent::del($ids);
  246. }
  247. /**
  248. * 销毁
  249. * @param string $ids
  250. */
  251. public function destroy($ids = "")
  252. {
  253. \app\admin\model\cms\Archives::event('after_delete', function ($row) {
  254. //删除副表
  255. $channel = Channel::get($row->channel_id);
  256. if ($channel) {
  257. $model = Modelx::get($channel['model_id']);
  258. if ($model) {
  259. db($model['table'])->where("id", $row['id'])->delete();
  260. }
  261. }
  262. });
  263. parent::destroy($ids);
  264. }
  265. /**
  266. * 还原
  267. * @param mixed $ids
  268. */
  269. public function restore($ids = "")
  270. {
  271. if (!$this->request->isPost()) {
  272. $this->error(__("Invalid parameters"));
  273. }
  274. $pk = $this->model->getPk();
  275. $adminIds = $this->getDataLimitAdminIds();
  276. if (is_array($adminIds)) {
  277. $this->model->where($this->dataLimitField, 'in', $adminIds);
  278. }
  279. if ($ids) {
  280. $this->model->where($pk, 'in', $ids);
  281. }
  282. $config = get_addon_config('cms');
  283. $list = $this->model->onlyTrashed()->select();
  284. if ($list) {
  285. $ids = [];
  286. foreach ($list as $index => $item) {
  287. if ($item['status'] == 'normal') {
  288. Channel::where('id', $item['id'])->setInc('items');
  289. User::score($config['score']['postarchives'], $item['user_id'], '发布文章');
  290. }
  291. $ids[] = $item['id'];
  292. }
  293. $this->model->where('id', 'in', $ids);
  294. $this->model->restore('1=1');
  295. $this->success();
  296. }
  297. $this->error(__('No rows were updated'));
  298. }
  299. /**
  300. * 移动
  301. * @param string $ids
  302. */
  303. public function move($ids = "")
  304. {
  305. if ($ids) {
  306. if (!$this->request->isPost()) {
  307. $this->error(__("Invalid parameters"));
  308. }
  309. $channel_id = $this->request->post('channel_id');
  310. $pk = $this->model->getPk();
  311. $adminIds = $this->getDataLimitAdminIds();
  312. if (is_array($adminIds)) {
  313. $this->model->where($this->dataLimitField, 'in', $adminIds);
  314. }
  315. $this->model->where($pk, 'in', $ids);
  316. $channel = Channel::get($channel_id);
  317. if ($channel && $channel['type'] === 'list') {
  318. $channelNums = \app\admin\model\cms\Archives::
  319. with('channel')
  320. ->where('archives.' . $pk, 'in', $ids)
  321. ->where('channel_id', '<>', $channel['id'])
  322. ->field('channel_id,COUNT(*) AS nums')
  323. ->group('channel_id')
  324. ->select();
  325. $result = $this->model
  326. ->where('model_id', '=', $channel['model_id'])
  327. ->where('channel_id', '<>', $channel['id'])
  328. ->update(['channel_id' => $channel_id]);
  329. if ($result) {
  330. $count = 0;
  331. foreach ($channelNums as $k => $v) {
  332. if ($v['channel']) {
  333. Channel::where('id', $v['channel_id'])->where('items', '>', 0)->setDec('items', min($v['channel']['items'], $v['nums']));
  334. }
  335. $count += $v['nums'];
  336. }
  337. Channel::where('id', $channel_id)->setInc('items', $count);
  338. $this->success();
  339. } else {
  340. $this->error(__('No rows were updated'));
  341. }
  342. } else {
  343. $this->error(__('No rows were updated'));
  344. }
  345. $this->error(__('Parameter %s can not be empty', 'ids'));
  346. }
  347. }
  348. /**
  349. * 加入专题
  350. * @param string $ids
  351. */
  352. public function special($ids = "")
  353. {
  354. if ($ids) {
  355. $special_id = $this->request->post('special_id');
  356. $pk = $this->model->getPk();
  357. $adminIds = $this->getDataLimitAdminIds();
  358. if (is_array($adminIds)) {
  359. $this->model->where($this->dataLimitField, 'in', $adminIds);
  360. }
  361. $special = \app\admin\model\cms\Special::get($special_id);
  362. if ($special) {
  363. $archivesList = $this->model->where($pk, 'in', $ids)->select();
  364. foreach ($archivesList as $index => $item) {
  365. $special_ids = explode(',', $item['special_ids']);
  366. if (!in_array($special['id'], $special_ids)) {
  367. $special_ids[] = $special['id'];
  368. $item->save(['special_ids' => implode(',', array_unique(array_filter($special_ids)))]);
  369. }
  370. }
  371. $this->success();
  372. } else {
  373. $this->error(__('No rows were updated'));
  374. }
  375. $this->error(__('Parameter %s can not be empty', 'ids'));
  376. }
  377. }
  378. /**
  379. * 获取栏目列表
  380. * @internal
  381. */
  382. public function get_fields_html()
  383. {
  384. $this->view->engine->layout(false);
  385. $channel_id = $this->request->post('channel_id');
  386. $archives_id = $this->request->post('archives_id');
  387. $channel = Channel::get($channel_id, 'model');
  388. if ($channel) {
  389. $model_id = $channel['model_id'];
  390. $values = [];
  391. if ($archives_id) {
  392. $values = db($channel['model']['table'])->where('id', $archives_id)->find();
  393. //优先从栏目获取模型ID,再从文档获取
  394. $archives = \app\admin\model\cms\Archives::get($archives_id);
  395. $model_id = $archives ? $archives['model_id'] : $model_id;
  396. }
  397. $fields = \addons\cms\library\Service::getCustomFields('model', $model_id, $values);
  398. $this->view->assign('channel', $channel);
  399. $this->view->assign('fields', $fields);
  400. $this->view->assign('values', $values);
  401. $this->success('', null, ['html' => $this->view->fetch('cms/common/fields')]);
  402. } else {
  403. $this->error(__('Please select channel'));
  404. }
  405. $this->error(__('Parameter %s can not be empty', 'ids'));
  406. }
  407. /**
  408. * 检测元素是否可用
  409. * @internal
  410. */
  411. public function check_element_available()
  412. {
  413. $id = $this->request->request('id');
  414. $name = $this->request->request('name');
  415. $value = $this->request->request('value');
  416. $name = substr($name, 4, -1);
  417. if (!$name) {
  418. $this->error(__('Parameter %s can not be empty', 'name'));
  419. }
  420. if ($id) {
  421. $this->model->where('id', '<>', $id);
  422. }
  423. $exist = $this->model->where($name, $value)->find();
  424. if ($exist) {
  425. $this->error(__('The data already exist'));
  426. } else {
  427. $this->success();
  428. }
  429. }
  430. /**
  431. * 搜索建议
  432. * @internal
  433. */
  434. public function suggestion()
  435. {
  436. $config = get_addon_config('cms');
  437. $q = trim($this->request->request("q"));
  438. $id = trim($this->request->request("id/d"));
  439. $list = [];
  440. if ($config['searchtype'] == 'xunsearch') {
  441. $result = FulltextSearch::search($q, 1, 10);
  442. } else {
  443. $result = $this->model->where("title|keywords|description", "like", "%{$q}%")->where('id', '<>', $id)->limit(10)->order("id", "desc")->select();
  444. foreach ($result as $index => $item) {
  445. $item['image'] = $item['image'] ? $item['image'] : '/assets/addons/cms/img/noimage.png';
  446. $list[] = ['id' => $item['id'], 'url' => $item['fullurl'], 'image' => cdnurl($item['image']), 'title' => $item['title'], 'create_date' => datetime($item['createtime']), 'status' => $item['status'], 'status_text' => $item['status_text'], 'deletetime' => $item['deletetime']];
  447. }
  448. }
  449. return json($list);
  450. }
  451. }