Channel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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\common\controller\Backend;
  8. use app\admin\model\cms\Channel as ChannelModel;
  9. use fast\Tree;
  10. use think\Exception;
  11. /**
  12. * 栏目表
  13. *
  14. * @icon fa fa-list
  15. */
  16. class Channel extends Backend
  17. {
  18. protected $channelList = [];
  19. protected $modelList = [];
  20. protected $multiFields = ['weigh', 'status', 'iscontribute', 'isnav'];
  21. /**
  22. * Channel模型对象
  23. */
  24. protected $model = null;
  25. protected $noNeedRight = ['get_fields_html', 'check_element_available'];
  26. /**
  27. * @var Tree
  28. */
  29. protected $tree = null;
  30. public function _initialize()
  31. {
  32. parent::_initialize();
  33. $this->model = new \app\admin\model\cms\Channel;
  34. $this->tree = Tree::instance();
  35. $this->tree->init(collection($this->model->order('weigh desc,id desc')->select())->toArray(), 'parent_id');
  36. $this->channelList = $this->tree->getTreeList($this->tree->getTreeArray(0), 'name');
  37. $this->modelList = \app\admin\model\cms\Modelx::order('id asc')->select();
  38. $config = get_addon_config('cms');
  39. $this->assignconfig('spiderRecord', intval($config['spiderrecord']?? 0));
  40. $this->view->assign("modelList", $this->modelList);
  41. $this->view->assign("channelList", $this->channelList);
  42. $this->view->assign("typeList", ChannelModel::getTypeList());
  43. $this->assignconfig("flagList", $this->model->getFlagList());
  44. $this->view->assign("flagList", $this->model->getFlagList());
  45. $this->view->assign("statusList", ChannelModel::getStatusList());
  46. $this->view->assign("listtypeList", ChannelModel::getListtypeList());
  47. $this->view->assign("vipList", get_addon_info('vip') ? Service::getVipList() : []);
  48. }
  49. /**
  50. * 查看
  51. */
  52. public function index()
  53. {
  54. //设置过滤方法
  55. $this->request->filter(['strip_tags']);
  56. if ($this->request->isAjax()) {
  57. $search = $this->request->request("search");
  58. $model_id = $this->request->request("model_id");
  59. //构造父类select列表选项数据
  60. $list = [];
  61. if ($search) {
  62. foreach ($this->channelList as $k => $v) {
  63. if (stripos($v['name'], $search) !== false || stripos($v['nickname'], $search) !== false) {
  64. $list[] = $v;
  65. }
  66. }
  67. } else {
  68. $list = $this->channelList;
  69. }
  70. foreach ($list as $index => $item) {
  71. if ($model_id && $model_id != $item['model_id']) {
  72. unset($list[$index]);
  73. }
  74. }
  75. $list = array_values($list);
  76. $modelNameArr = [];
  77. foreach ($this->modelList as $k => $v) {
  78. $modelNameArr[$v['id']] = $v['name'];
  79. }
  80. foreach ($list as $k => &$v) {
  81. $v['pid'] = $v['parent_id'];
  82. $v['model_name'] = $v['model_id'] && isset($modelNameArr[$v['model_id']]) ? $modelNameArr[$v['model_id']] : __('None');
  83. }
  84. $total = count($list);
  85. \app\admin\model\cms\SpiderLog::render($list, 'channel');
  86. $result = array("total" => $total, "rows" => $list);
  87. return json($result);
  88. }
  89. return $this->view->fetch();
  90. }
  91. /**
  92. * 添加
  93. */
  94. public function add()
  95. {
  96. if ($this->request->isPost()) {
  97. $params = $this->request->post("row/a");
  98. if ($params) {
  99. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  100. $params[$this->dataLimitField] = $this->auth->id;
  101. }
  102. try {
  103. //是否采用模型验证
  104. if ($this->modelValidate) {
  105. $name = basename(str_replace('\\', '/', get_class($this->model)));
  106. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : true) : $this->modelValidate;
  107. $this->model->validate($validate);
  108. }
  109. $nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $params['name'])));
  110. if (count($nameArr) > 1) {
  111. foreach ($nameArr as $index => $item) {
  112. $itemArr = array_filter(explode('|', $item));
  113. $params['name'] = $itemArr[0];
  114. $params['diyname'] = isset($itemArr[1]) ? $itemArr[1] : '';
  115. $result = $this->model->allowField(true)->isUpdate(false)->data($params)->save();
  116. }
  117. } else {
  118. $result = $this->model->allowField(true)->save($params);
  119. }
  120. if ($result !== false) {
  121. $this->success();
  122. } else {
  123. $this->error($this->model->getError());
  124. }
  125. } catch (\think\exception\PDOException $e) {
  126. $this->error($e->getMessage());
  127. } catch (\think\Exception $e) {
  128. $this->error($e->getMessage());
  129. }
  130. }
  131. $this->error(__('Parameter %s can not be empty', ''));
  132. }
  133. $values = [];
  134. $fields = \addons\cms\library\Service::getCustomFields('channel', 0, $values);
  135. $this->view->assign('fields', $fields);
  136. $this->view->assign('values', $values);
  137. return $this->view->fetch();
  138. }
  139. /**
  140. * 编辑
  141. */
  142. public function edit($ids = null)
  143. {
  144. $channel = \app\admin\model\cms\Channel::get($ids);
  145. if (!$channel) {
  146. $this->error(__('No Results were found'));
  147. }
  148. $linkdata = $channel['linkdata'];
  149. $channel = $channel->toArray();
  150. $fields = \addons\cms\library\Service::getCustomFields('channel', 0, $channel);
  151. $childrenIds = $this->tree->getChildrenIds($channel['id'], true);
  152. $hasArchives = \app\admin\model\cms\Archives::withTrashed()->where('channel_id', $channel['id'])->whereOr('FIND_IN_SET(:id, `channel_ids`)', ['id' => $channel['id']])->count();
  153. $this->view->assign('hasArchives', $hasArchives);
  154. $this->view->assign('fields', $fields);
  155. $this->view->assign('values', $channel);
  156. $this->view->assign('childrenIds', $childrenIds);
  157. $this->assignconfig('linkdata', $linkdata);
  158. return parent::edit($ids);
  159. }
  160. /**
  161. * 栏目授权
  162. */
  163. public function admin()
  164. {
  165. $act = $this->request->param('act');
  166. $ids = $this->request->param('ids');
  167. if ($act && $ids) {
  168. if (!$this->request->isPost()) {
  169. $this->error(__("Invalid parameters"));
  170. }
  171. if ($act == 'remove') {
  172. ChannelAdmin::where('admin_id', $ids)->delete();
  173. $this->success('删除成功!');
  174. } elseif ($act == 'authorization') {
  175. $selected = ChannelAdmin::getAdminChanneIds($ids);
  176. $all = collection(ChannelModel::order("weigh desc,id desc")->select())->toArray();
  177. foreach ($all as $k => $v) {
  178. $state = ['opened' => true];
  179. if ($v['type'] == 'link') {
  180. $disabledIds[] = $v['id'];
  181. }
  182. if ($v['type'] == 'link') {
  183. $state['checkbox_disabled'] = true;
  184. }
  185. $state['selected'] = in_array($v['id'], $selected);
  186. $channelList[] = [
  187. 'id' => $v['id'],
  188. 'parent' => $v['parent_id'] ? $v['parent_id'] : '#',
  189. 'text' => __($v['name']),
  190. 'type' => $v['type'],
  191. 'state' => $state
  192. ];
  193. }
  194. $this->success('成功', '', $channelList);
  195. } elseif ($act == 'save') {
  196. \think\Db::startTrans();
  197. try {
  198. ChannelAdmin::where('admin_id', $ids)->delete();
  199. $channelIds = explode(",", $this->request->post("ids"));
  200. if ($channelIds) {
  201. $listChannelIds = ChannelModel::where('type', 'list')->column('id');
  202. $channelIds = array_intersect($channelIds, $listChannelIds);
  203. $data = [];
  204. foreach ($channelIds as $key => $item) {
  205. $data[] = ['admin_id' => $ids, 'channel_id' => $item];
  206. }
  207. $model = new ChannelAdmin();
  208. $model->saveAll($data, true);
  209. }
  210. \think\Db::commit();
  211. } catch (Exception $e) {
  212. \think\Db::rollback();
  213. $this->error($e->getMessage());
  214. }
  215. $this->success("保存成功!");
  216. }
  217. }
  218. if ($this->request->isAjax()) {
  219. $list = \think\Db::name("cms_channel_admin")
  220. ->group("admin_id")
  221. ->field("COUNT(*) as channels,admin_id")
  222. ->select();
  223. $adminChannelList = [];
  224. foreach ($list as $index => $item) {
  225. $adminChannelList[$item['admin_id']] = $item['channels'];
  226. }
  227. $superAdminIds = AuthGroupAccess::where('group_id', 1)->column('uid');
  228. $adminList = Admin::order('id', 'desc')->field('id,username,nickname')->select();
  229. foreach ($adminList as $index => $item) {
  230. $item->channels = isset($adminChannelList[$item['id']]) ? $adminChannelList[$item['id']] : 0;
  231. $item->superadmin = in_array($item['id'], $superAdminIds);
  232. }
  233. $total = count($adminList);
  234. $result = array("total" => $total, "rows" => $adminList);
  235. return json($result);
  236. }
  237. $config = get_addon_config('cms');
  238. $this->view->assign("isChannelAllocate", $config['channelallocate']);
  239. return $this->view->fetch();
  240. }
  241. /**
  242. * Selectpage搜索
  243. *
  244. * @internal
  245. */
  246. public function selectpage()
  247. {
  248. return parent::selectpage();
  249. }
  250. /**
  251. * 检测元素是否可用
  252. * @internal
  253. */
  254. public function check_element_available()
  255. {
  256. $id = $this->request->request('id');
  257. $name = $this->request->request('name');
  258. $value = $this->request->request('value');
  259. $name = substr($name, 4, -1);
  260. if (!$name) {
  261. $this->error(__('Parameter %s can not be empty', 'name'));
  262. }
  263. if ($name == 'diyname') {
  264. if ($id) {
  265. $this->model->where('id', '<>', $id);
  266. }
  267. $exist = $this->model->where($name, $value)->find();
  268. if ($exist) {
  269. $this->error(__('The data already exist'));
  270. } else {
  271. $this->success();
  272. }
  273. } elseif ($name == 'name') {
  274. $nameArr = array_filter(explode("\n", str_replace("\r\n", "\n", $value)));
  275. if (count($nameArr) > 1) {
  276. foreach ($nameArr as $index => $item) {
  277. $itemArr = array_filter(explode('|', $item));
  278. if (!isset($itemArr[1])) {
  279. $this->error('格式:分类名称|自定义名称');
  280. }
  281. $exist = \app\admin\model\cms\Channel::getByDiyname($itemArr[1]);
  282. if ($exist) {
  283. $this->error('自定义名称[' . $itemArr[1] . ']已经存在');
  284. }
  285. }
  286. $this->success();
  287. } else {
  288. $this->success();
  289. }
  290. }
  291. }
  292. }