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