Channel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace addons\cms\controller;
  3. use addons\cms\library\Service;
  4. use addons\cms\model\Archives;
  5. use addons\cms\model\Channel as ChannelModel;
  6. use addons\cms\model\Fields;
  7. use addons\cms\model\Modelx;
  8. use think\Config;
  9. /**
  10. * 栏目控制器
  11. * Class Channel
  12. * @package addons\cms\controller
  13. */
  14. class Channel extends Base
  15. {
  16. public function index()
  17. {
  18. $config = get_addon_config('cms');
  19. $diyname = $this->request->param('diyname');
  20. if ($diyname && !is_numeric($diyname)) {
  21. $channel = ChannelModel::getByDiyname($diyname);
  22. } else {
  23. $id = $diyname ? $diyname : $this->request->param('id', '');
  24. $channel = ChannelModel::get($id);
  25. }
  26. if (!$channel) {
  27. $this->error(__('No specified channel found'));
  28. }
  29. $filter = $this->request->get('filter/a', []);
  30. $orderby = $this->request->get('orderby', '');
  31. $orderway = $this->request->get('orderway', '', 'strtolower');
  32. $multiple = $this->request->get('multiple/d', 0);
  33. $params = [];
  34. $filter = $this->request->get();
  35. $filter = array_diff_key($filter, array_flip(['orderby', 'orderway', 'page', 'multiple']));
  36. if (isset($filter['filter'])) {
  37. $filter = array_merge($filter, $filter['filter']);
  38. }
  39. if ($filter) {
  40. $filter = array_filter($filter, 'strlen');
  41. $params['filter'] = $filter;
  42. $params = $filter;
  43. }
  44. if ($orderby) {
  45. $params['orderby'] = $orderby;
  46. }
  47. if ($orderway) {
  48. $params['orderway'] = $orderway;
  49. }
  50. if ($multiple) {
  51. $params['multiple'] = $multiple;
  52. }
  53. if ($channel['type'] === 'link') {
  54. $this->redirect($channel['outlink']);
  55. }
  56. //加载模型数据
  57. $model = Modelx::get($channel['model_id']);
  58. if (!$model) {
  59. $this->error(__('No specified model found'));
  60. }
  61. //默认排序字段
  62. $orders = [
  63. ['name' => 'default', 'field' => 'weigh DESC,id DESC', 'title' => __('Default')],
  64. ];
  65. //合并主表筛选字段
  66. $orders = array_merge($orders, $model->getOrderFields());
  67. //获取过滤列表
  68. list($filterList, $filter, $params, $fields, $multiValueFields, $fieldsList) = Service::getFilterList('model', $model['id'], $filter, $params, $multiple);
  69. //获取排序列表
  70. list($orderList, $orderby, $orderway) = Service::getOrderList($orderby, $orderway, $orders, $params, $fieldsList);
  71. //获取过滤的条件和绑定参数
  72. list($filterWhere, $filterBind) = Service::getFilterWhereBind($filter, $multiValueFields, $multiple);
  73. //加载列表数据
  74. $pageList = Archives::with(['channel', 'user'])->alias('a')
  75. ->where('a.status', 'normal')
  76. ->whereNull('a.deletetime')
  77. ->where($filterWhere)
  78. ->bind($filterBind)
  79. ->join($model['table'] . ' n', 'a.id=n.id', 'LEFT')
  80. ->field('a.*')
  81. ->field('id,content', true, config('database.prefix') . $model['table'], 'n')
  82. ->where(function ($query) use ($channel) {
  83. $query->where(function ($query) use ($channel) {
  84. if ($channel['listtype'] <= 2) {
  85. $query->whereOr("channel_id", $channel['id']);
  86. }
  87. if ($channel['listtype'] == 1 || $channel['listtype'] == 3) {
  88. $query->whereOr('channel_id', 'in', function ($query) use ($channel) {
  89. $query->name("cms_channel")->where('parent_id', $channel['id'])->field("id");
  90. });
  91. }
  92. if ($channel['listtype'] == 0 || $channel['listtype'] == 4) {
  93. $childrenIds = \addons\cms\model\Channel::getChannelChildrenIds($channel['id'], false);
  94. if ($childrenIds) {
  95. $query->whereOr('channel_id', 'in', $childrenIds);
  96. }
  97. }
  98. })
  99. ->whereOr("(`channel_ids`!='' AND FIND_IN_SET('{$channel['id']}', `channel_ids`))");
  100. })
  101. ->where('model_id', $channel->model_id)
  102. ->order($orderby, $orderway)
  103. ->paginate($channel['pagesize'], $config['pagemode'] == 'simple', ['type' => '\\addons\\cms\\library\\Bootstrap']);
  104. $fieldsContentList = Fields::getFieldsContentList('model', $model->id);
  105. foreach ($pageList as $index => $item) {
  106. Service::appendTextAttr($fieldsContentList, $item);
  107. }
  108. $fieldsContentList = Fields::getFieldsContentList('channel');
  109. Service::appendTextAttr($fieldsContentList, $channel);
  110. $pageList->appends(array_filter($params));
  111. $this->view->assign("__FILTERLIST__", $filterList);
  112. $this->view->assign("__ORDERLIST__", $orderList);
  113. $this->view->assign("__PAGELIST__", $pageList);
  114. $this->view->assign("__CHANNEL__", $channel);
  115. //设置TKD
  116. Config::set('cms.title', isset($channel['seotitle']) && $channel['seotitle'] ? $channel['seotitle'] : $channel['name']);
  117. Config::set('cms.keywords', $channel['keywords']);
  118. Config::set('cms.description', $channel['description']);
  119. //读取模板
  120. $template = preg_replace('/\.html$/', '', $channel["{$channel['type']}tpl"]);
  121. if (!$template) {
  122. $this->error('请检查栏目是否配置相应的模板');
  123. }
  124. if ($this->request->isAjax()) {
  125. $this->success("", "", $this->view->fetch('common/' . $template . '_ajax'));
  126. }
  127. return $this->view->fetch('/' . $template);
  128. }
  129. }