Channel.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <?php
  2. namespace app\admin\controller\cms;
  3. use addons\vip\library\Service;
  4. use app\admin\model\Admin;
  5. use app\admin\model\AuthGroupAccess;
  6. use app\admin\model\cms\ChannelAdmin;
  7. use app\admin\model\User;
  8. use app\common\controller\Backend;
  9. use app\admin\model\cms\Channel as ChannelModel;
  10. use fast\Tree;
  11. use think\Db;
  12. use think\Exception;
  13. use think\exception\PDOException;
  14. use think\exception\ValidateException;
  15. /**
  16. * 栏目表
  17. *
  18. * @icon fa fa-list
  19. */
  20. class Channel extends Backend
  21. {
  22. protected $channelList = [];
  23. protected $modelList = [];
  24. protected $multiFields = ['weigh', 'status', 'iscontribute', 'isnav'];
  25. /**
  26. * Channel模型对象
  27. */
  28. protected $model = null;
  29. protected $noNeedRight = ['get_fields_html', 'check_element_available', 'add_chief'];
  30. /**
  31. * @var Tree
  32. */
  33. protected $tree = null;
  34. public function _initialize()
  35. {
  36. parent::_initialize();
  37. $this->model = new \app\admin\model\cms\Channel;
  38. $this->tree = Tree::instance();
  39. $this->tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'parent_id');
  40. $this->channelList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
  41. $this->modelList = \app\admin\model\cms\Modelx::order('id asc')->select();
  42. $config = get_addon_config('cms');
  43. $this->assignconfig('spiderRecord', intval($config['spiderrecord']?? 0));
  44. $this->view->assign('channelTypeList', ChannelModel::getChannelTypeList());
  45. $this->view->assign("modelList", $this->modelList);
  46. $this->view->assign("channelList", $this->channelList);
  47. $this->view->assign("typeList", ChannelModel::getTypeList());
  48. $this->assignconfig("flagList", $this->model->getFlagList());
  49. $this->view->assign("flagList", $this->model->getFlagList());
  50. $this->view->assign("statusList", ChannelModel::getStatusList());
  51. $this->view->assign("secondaryList", ChannelModel::getSecondaryList());
  52. $this->view->assign("listtypeList", ChannelModel::getListtypeList());
  53. $this->view->assign("vipList", get_addon_info('vip') ? Service::getVipList() : []);
  54. }
  55. /**
  56. * 查看
  57. */
  58. public function index()
  59. {
  60. //设置过滤方法
  61. $this->request->filter(['strip_tags']);
  62. if ($this->request->isAjax()) {
  63. $search = $this->request->request("search");
  64. $type = $this->request->request("type", 'channel');
  65. //构造父类select列表选项数据
  66. $list = [];
  67. if ($search) {
  68. foreach ($this->channelList as $k => $v) {
  69. if (stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
  70. $list[] = $v;
  71. }
  72. }
  73. } else {
  74. $list = $this->channelList;
  75. }
  76. foreach ($list as $index => $item) {
  77. if ($type && $type != $item['type']) {
  78. unset($list[$index]);
  79. }
  80. }
  81. $list = array_values($list);
  82. $modelNameArr = [];
  83. foreach ($this->modelList as $v) {
  84. $modelNameArr[$v['id']] = $v['name'];
  85. }
  86. foreach ($list as &$v) {
  87. $v['pid'] = $v['parent_id'];
  88. $v['model_name'] = $v['model_id'] && isset($modelNameArr[$v['model_id']]) ? $modelNameArr[$v['model_id']] : __('None');
  89. }
  90. $total = count($list);
  91. \app\admin\model\cms\SpiderLog::render($list, 'channel');
  92. $result = array("total" => $total, "rows" => $list);
  93. return json($result);
  94. }
  95. return $this->view->fetch();
  96. }
  97. /**
  98. * 添加
  99. */
  100. public function add()
  101. {
  102. if ($this->request->isPost()) {
  103. $params = $this->request->post("row/a");
  104. if ($params) {
  105. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  106. $params[$this->dataLimitField] = $this->auth->id;
  107. }
  108. try {
  109. //是否采用模型验证
  110. if ($this->modelValidate) {
  111. $name = basename(str_replace('\\', '/', get_class($this->model)));
  112. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  113. $this->model->validate($validate);
  114. }
  115. $nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $params['name'])));
  116. // 获取上级是否为期刊,如是期刊则存入期刊id
  117. if ($params['channel_type'] == 'issues' || $params['channel_type'] == 'volume') {
  118. $parent_channel = \app\admin\model\cms\Channel::where(['id' => $params['parent_id']])->find();
  119. if ($params['channel_type'] == 'issues') {
  120. if ($parent_channel) {
  121. if ($parent_channel['channel_type'] == 'journal') {
  122. $params['journal_id'] = $parent_channel['id'];
  123. }
  124. }
  125. }
  126. if ($params['channel_type'] == 'volume') {
  127. if ($parent_channel) {
  128. $params['journal_id'] = $parent_channel['journal_id'];
  129. }
  130. }
  131. }
  132. if (count($nameArr) > 1) {
  133. foreach ($nameArr as $index => $item) {
  134. $itemArr = array_filter(explode('|', $item));
  135. $params['name'] = $itemArr[0];
  136. $params['diyname'] = isset($itemArr[1]) ? $itemArr[1] : '';
  137. $result = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
  138. }
  139. } else {
  140. $result = $this->model->allowField(true)->save($params);
  141. }
  142. if ($result !== false) {
  143. $this->success();
  144. } else {
  145. $this->error($this->model->getError());
  146. }
  147. } catch (\think\exception\PDOException $e) {
  148. $this->error($e->getMessage());
  149. } catch (\think\Exception $e) {
  150. $this->error($e->getMessage());
  151. }
  152. }
  153. $this->error(__('Parameter %s can not be empty', ''));
  154. }
  155. $values = [];
  156. $fields = \addons\cms\library\Service::getCustomFields('channel', 0, $values);
  157. $this->view->assign('fields', $fields);
  158. $this->view->assign('values', $values);
  159. return $this->view->fetch();
  160. }
  161. /**
  162. * 编辑
  163. */
  164. public function edit($ids = null)
  165. {
  166. $channel = \app\admin\model\cms\Channel::get($ids);
  167. if (!$channel) {
  168. $this->error(__('No Results were found'));
  169. }
  170. $linkdata = $channel['linkdata'];
  171. $channel = $channel->toArray();
  172. $fields = \addons\cms\library\Service::getCustomFields('channel', 0, $channel);
  173. $childrenIds = $this->tree->getChildrenIds($channel['id'], true);
  174. $hasArchives = \app\admin\model\cms\Archives::withTrashed()->where('channel_id', $channel['id'])->whereOr('FIND_IN_SET(:id, `channel_ids`)', ['id' => $channel['id']])->count();
  175. $this->view->assign('hasArchives', $hasArchives);
  176. $this->view->assign('fields', $fields);
  177. $this->view->assign('values', $channel);
  178. $this->view->assign('childrenIds', $childrenIds);
  179. $this->assignconfig('linkdata', $linkdata);
  180. $row = $this->model->get($ids);
  181. if (!$row) {
  182. $this->error(__('No Results were found'));
  183. }
  184. $adminIds = $this->getDataLimitAdminIds();
  185. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  186. $this->error(__('You have no permission'));
  187. }
  188. if (false === $this->request->isPost()) {
  189. $this->view->assign('row', $row);
  190. return $this->view->fetch();
  191. }
  192. $params = $this->request->post('row/a');
  193. if (empty($params)) {
  194. $this->error(__('Parameter %s can not be empty', ''));
  195. }
  196. $params = $this->preExcludeFields($params);
  197. $result = false;
  198. Db::startTrans();
  199. try {
  200. //是否采用模型验证
  201. if ($this->modelValidate) {
  202. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  203. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  204. $row->validateFailException()->validate($validate);
  205. }
  206. // 获取上级是否为期刊,如是期刊则存入期刊id
  207. if ($params['channel_type'] == 'issues' || $params['channel_type'] == 'volume') {
  208. $parent_channel = \app\admin\model\cms\Channel::where(['id' => $params['parent_id']])->find();
  209. if ($params['channel_type'] == 'issues') {
  210. if ($parent_channel) {
  211. if ($parent_channel['channel_type'] == 'journal') {
  212. $params['journal_id'] = $parent_channel['id'];
  213. }
  214. }
  215. }
  216. if ($params['channel_type'] == 'volume') {
  217. if ($parent_channel) {
  218. $params['journal_id'] = $parent_channel['journal_id'];
  219. }
  220. }
  221. }
  222. $result = $row->allowField(true)->save($params);
  223. Db::commit();
  224. } catch (ValidateException|PDOException|Exception $e) {
  225. Db::rollback();
  226. $this->error($e->getMessage());
  227. }
  228. if (false === $result) {
  229. $this->error(__('No rows were updated'));
  230. }
  231. $this->success();
  232. }
  233. /**
  234. * 栏目授权
  235. */
  236. public function admin()
  237. {
  238. $act = $this->request->param('act');
  239. $ids = $this->request->param('ids');
  240. if ($act && $ids) {
  241. if (!$this->request->isPost()) {
  242. $this->error(__("Invalid parameters"));
  243. }
  244. if ($act == 'remove') {
  245. ChannelAdmin::where('admin_id', $ids)->delete();
  246. $this->success('删除成功!');
  247. } elseif ($act == 'authorization') {
  248. $selected = ChannelAdmin::getAdminChanneIds($ids);
  249. $all = collection(ChannelModel::order("weigh desc,id desc")->select())->toArray();
  250. foreach ($all as $k => $v) {
  251. $state = ['opened' => true];
  252. if ($v['type'] == 'link') {
  253. $disabledIds[] = $v['id'];
  254. }
  255. if ($v['type'] == 'link') {
  256. $state['checkbox_disabled'] = true;
  257. }
  258. $state['selected'] = in_array($v['id'], $selected);
  259. $channelList[] = [
  260. 'id' => $v['id'],
  261. 'parent' => $v['parent_id'] ? $v['parent_id'] : '#',
  262. 'text' => __($v['name']),
  263. 'type' => $v['type'],
  264. 'state' => $state
  265. ];
  266. }
  267. $this->success('成功', '', $channelList);
  268. } elseif ($act == 'save') {
  269. \think\Db::startTrans();
  270. try {
  271. ChannelAdmin::where('admin_id', $ids)->delete();
  272. $channelIds = explode(",", $this->request->post("ids"));
  273. if ($channelIds) {
  274. $listChannelIds = ChannelModel::where('type', 'list')->column('id');
  275. $channelIds = array_intersect($channelIds, $listChannelIds);
  276. $data = [];
  277. foreach ($channelIds as $key => $item) {
  278. $data[] = ['admin_id' => $ids, 'channel_id' => $item];
  279. }
  280. $model = new ChannelAdmin();
  281. $model->saveAll($data, true);
  282. }
  283. \think\Db::commit();
  284. } catch (Exception $e) {
  285. \think\Db::rollback();
  286. $this->error($e->getMessage());
  287. }
  288. $this->success("保存成功!");
  289. }
  290. }
  291. if ($this->request->isAjax()) {
  292. $list = \think\Db::name("cms_channel_admin")
  293. ->group("admin_id")
  294. ->field("COUNT(*) as channels,admin_id")
  295. ->select();
  296. $adminChannelList = [];
  297. foreach ($list as $index => $item) {
  298. $adminChannelList[$item['admin_id']] = $item['channels'];
  299. }
  300. $superAdminIds = AuthGroupAccess::where('group_id', 1)->column('uid');
  301. $adminList = Admin::order('id', 'desc')->field('id,username,nickname')->select();
  302. foreach ($adminList as $index => $item) {
  303. $item->channels = isset($adminChannelList[$item['id']]) ? $adminChannelList[$item['id']] : 0;
  304. $item->superadmin = in_array($item['id'], $superAdminIds);
  305. }
  306. $total = count($adminList);
  307. $result = array("total" => $total, "rows" => $adminList);
  308. return json($result);
  309. }
  310. $config = get_addon_config('cms');
  311. $this->view->assign("isChannelAllocate", $config['channelallocate']);
  312. return $this->view->fetch();
  313. }
  314. /**
  315. * Selectpage搜索
  316. *
  317. * @internal
  318. */
  319. public function selectpage()
  320. {
  321. return parent::selectpage();
  322. }
  323. /**
  324. * 检测元素是否可用
  325. * @internal
  326. */
  327. public function check_element_available()
  328. {
  329. $id = $this->request->request('id');
  330. $name = $this->request->request('name');
  331. $value = $this->request->request('value');
  332. $name = substr($name, 4, -1);
  333. if (!$name) {
  334. $this->error(__('Parameter %s can not be empty', 'name'));
  335. }
  336. if ($name == 'diyname') {
  337. if ($id) {
  338. $this->model->where('id', '<>', $id);
  339. }
  340. $exist = $this->model->where($name, $value)->find();
  341. if ($exist) {
  342. $this->error(__('The data already exist'));
  343. } else {
  344. $this->success();
  345. }
  346. } elseif ($name == 'name') {
  347. $nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $value)));
  348. if (count($nameArr) > 1) {
  349. foreach ($nameArr as $index => $item) {
  350. $itemArr = array_filter(explode('|', $item));
  351. if (!isset($itemArr[1])) {
  352. $this->error('格式:分类名称|自定义名称');
  353. }
  354. $exist = \app\admin\model\cms\Channel::getByDiyname($itemArr[1]);
  355. if ($exist) {
  356. $this->error('自定义名称[' . $itemArr[1] . ']已经存在');
  357. }
  358. }
  359. $this->success();
  360. } else {
  361. $this->success();
  362. }
  363. }
  364. }
  365. /**
  366. * 添加编辑
  367. *
  368. * @return string|void
  369. * @throws Exception
  370. */
  371. public function add_chief($ids = null)
  372. {
  373. $row = $this->model->get($ids);
  374. // 获取编辑信息
  375. $chief_list = User::where(['is_chief' => 'correct'])->column('nickname', 'id');
  376. // 模板
  377. if (false === $this->request->isPost()) {
  378. $this->view->assign('row', $row);
  379. $this->view->assign('chief_list', $chief_list);
  380. return $this->view->fetch();
  381. }
  382. $params = $this->request->post('row/a');
  383. if (empty($params)) {
  384. $this->error(__('Parameter %s can not be empty', ''));
  385. }
  386. $params = $this->preExcludeFields($params);
  387. $result = false;
  388. Db::startTrans();
  389. try {
  390. //是否采用模型验证
  391. if ($this->modelValidate) {
  392. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  393. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  394. $row->validateFailException()->validate($validate);
  395. }
  396. $result = $row->allowField(true)->save($params);
  397. Db::commit();
  398. } catch (ValidateException|PDOException|Exception $e) {
  399. Db::rollback();
  400. $this->error($e->getMessage());
  401. }
  402. if (false === $result) {
  403. $this->error(__('No rows were updated'));
  404. }
  405. $this->success();
  406. }
  407. }