Channel.php 11 KB

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