Channel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. namespace addons\cms\model;
  3. use addons\cms\library\Service;
  4. use think\Cache;
  5. use think\Db;
  6. use think\Model;
  7. use think\View;
  8. /**
  9. * 栏目模型
  10. */
  11. class Channel extends Model
  12. {
  13. protected $name = "cms_channel";
  14. // 开启自动写入时间戳字段
  15. protected $autoWriteTimestamp = 'int';
  16. // 定义时间戳字段名
  17. protected $createTime = 'createtime';
  18. protected $updateTime = 'updatetime';
  19. // 追加属性
  20. protected $append = [
  21. 'url',
  22. 'fullurl'
  23. ];
  24. protected static $config = [];
  25. protected static $tagCount = 0;
  26. protected static $parentIds = null;
  27. protected static $outlinkParentIds = null;
  28. protected static function init()
  29. {
  30. $config = get_addon_config('cms');
  31. self::$config = $config;
  32. }
  33. public function getUrlAttr($value, $data)
  34. {
  35. return $this->buildUrl($value, $data);
  36. }
  37. public function getFullurlAttr($value, $data)
  38. {
  39. return $this->buildUrl($value, $data, true);
  40. }
  41. private function buildUrl($value, $data, $domain = false)
  42. {
  43. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  44. $cateid = $data['id'] ?? 0;
  45. $catename = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : 'all';
  46. $time = $data['createtime'] ?? time();
  47. $vars = [
  48. ':id' => $data['id'],
  49. ':diyname' => $diyname,
  50. ':channel' => $cateid,
  51. ':catename' => $catename,
  52. ':cateid' => $cateid,
  53. ':year' => date("Y", $time),
  54. ':month' => date("m", $time),
  55. ':day' => date("d", $time)
  56. ];
  57. if (isset($data['type']) && isset($data['outlink']) && $data['type'] == 'link') {
  58. return $this->getAttr('outlink');
  59. }
  60. $suffix = static::$config['moduleurlsuffix']['channel'] ?? static::$config['urlsuffix'];
  61. return addon_url('cms/channel/index', $vars, $suffix, $domain);
  62. }
  63. public function getImageAttr($value, $data)
  64. {
  65. $value = $value ? $value : self::$config['default_channel_img'];
  66. return cdnurl($value);
  67. }
  68. public function getOutlinkAttr($value, $data)
  69. {
  70. $indexUrl = $view_replace_str = config('view_replace_str.__PUBLIC__');
  71. $indexUrl = rtrim($indexUrl, '/');
  72. return str_replace('__INDEX__', $indexUrl, $value);
  73. }
  74. public function getTagcolorAttr($value, $data)
  75. {
  76. $color = ['primary', 'default', 'success', 'warning', 'danger'];
  77. $index = $data['id'] % count($color);
  78. return isset($color[$index]) ? $color[$index] : $color[0];
  79. }
  80. public function getHasimageAttr($value, $data)
  81. {
  82. return $this->getData("image") ? true : false;
  83. }
  84. /**
  85. * 判断是否拥有子列表
  86. * @param $value
  87. * @param $data
  88. * @return bool|mixed
  89. */
  90. public function getHasChildAttr($value, $data)
  91. {
  92. static $checked = [];
  93. if (isset($checked[$data['id']])) {
  94. return $checked[$data['id']];
  95. }
  96. if (is_null(self::$parentIds)) {
  97. self::$parentIds = self::where('parent_id', '>', 0)->cache(false)->where('status', 'normal')->column('parent_id');
  98. }
  99. if (self::$parentIds && in_array($data['id'], self::$parentIds)) {
  100. return true;
  101. }
  102. return false;
  103. }
  104. /**
  105. * 判断是否当前页面
  106. * @param $value
  107. * @param $data
  108. * @return bool
  109. */
  110. public function getIsActiveAttr($value, $data)
  111. {
  112. $url = request()->url();
  113. $channel = View::instance()->__CHANNEL__;
  114. if (($channel && ($channel['id'] == $this->id || $channel['parent_id'] == $this->id)) || $this->url == $url) {
  115. return true;
  116. } else {
  117. if ($this->has_child) {
  118. if (is_null(self::$outlinkParentIds)) {
  119. self::$outlinkParentIds = self::where('type', 'link')->where('status', 'normal')->column('outlink,parent_id');
  120. }
  121. if (self::$outlinkParentIds && isset(self::$outlinkParentIds[$url]) && self::$outlinkParentIds[$url] == $this->id) {
  122. return true;
  123. }
  124. }
  125. }
  126. return false;
  127. }
  128. /**
  129. * 获取栏目所有子级的ID
  130. * @param mixed $ids 栏目ID或集合ID
  131. * @param bool $withself 是否包含自身
  132. * @return array
  133. */
  134. public static function getChannelChildrenIds($ids, $withself = true)
  135. {
  136. $cacheName = 'childrens-' . $ids . '-' . $withself;
  137. $result = Cache::get($cacheName);
  138. if ($result === false) {
  139. $channelList = Channel::where('status', 'normal')
  140. ->order('weigh desc,id desc')
  141. ->cache(true)
  142. ->select();
  143. $result = [];
  144. $tree = \fast\Tree::instance();
  145. $tree->init(collection($channelList)->toArray(), 'parent_id');
  146. $channelIds = is_array($ids) ? $ids : explode(',', $ids);
  147. foreach ($channelIds as $index => $channelId) {
  148. $result = array_merge($result, $tree->getChildrenIds($channelId, $withself));
  149. }
  150. Cache::set($cacheName, $result);
  151. }
  152. return $result;
  153. }
  154. /**
  155. * 获取栏目列表
  156. * @param $tag
  157. * @return false|\PDOStatement|string|\think\Collection
  158. */
  159. public static function getChannelList($tag)
  160. {
  161. $config = get_addon_config('cms');
  162. $type = empty($tag['type']) ? '' : $tag['type'];
  163. $typeid = !isset($tag['typeid']) ? '' : $tag['typeid'];
  164. $model = !isset($tag['model']) ? '' : $tag['model'];
  165. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  166. $field = empty($tag['field']) ? '*' : $tag['field'];
  167. $row = empty($tag['row']) ? 10 : (int)$tag['row'];
  168. $orderby = empty($tag['orderby']) ? 'weigh' : $tag['orderby'];
  169. $orderway = empty($tag['orderway']) ? 'desc' : strtolower($tag['orderway']);
  170. $limit = empty($tag['limit']) ? $row : $tag['limit'];
  171. $cache = !isset($tag['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$tag['cache'];
  172. $imgwidth = empty($tag['imgwidth']) ? '' : $tag['imgwidth'];
  173. $imgheight = empty($tag['imgheight']) ? '' : $tag['imgheight'];
  174. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  175. $paginate = !isset($tag['paginate']) ? false : $tag['paginate'];
  176. $cache = !$cache ? false : $cache;
  177. $where = ['status' => 'normal'];
  178. self::$tagCount++;
  179. if ($type === 'top') {
  180. //顶级分类
  181. $where['parent_id'] = 0;
  182. } elseif ($type === 'brother') {
  183. $subQuery = self::where('id', 'in', $typeid)->field('parent_id')->buildSql();
  184. //同级
  185. $where['parent_id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
  186. } elseif ($type === 'son') {
  187. $subQuery = self::where('parent_id', 'in', $typeid)->field('id')->buildSql();
  188. //子级
  189. $where['id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
  190. } elseif ($type === 'sons') {
  191. //所有子级
  192. $where['id'] = ['in', self::getChannelChildrenIds($typeid)];
  193. } else {
  194. if ($typeid !== '') {
  195. $where['id'] = ['in', $typeid];
  196. }
  197. }
  198. if ($model !== '') {
  199. $where['model_id'] = ['in', $model];
  200. }
  201. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  202. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  203. $channelModel = self::where($where)
  204. ->where($condition)
  205. ->field($field)
  206. ->orderRaw($order);
  207. if ($paginate) {
  208. $paginateArr = explode(',', $paginate);
  209. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  210. $config = [];
  211. $config['var_page'] = isset($paginateArr[2]) ? $paginateArr[2] : 'cpage' . self::$tagCount;
  212. $config['path'] = isset($paginateArr[3]) ? $paginateArr[3] : '';
  213. $config['fragment'] = isset($paginateArr[4]) ? $paginateArr[4] : '';
  214. $config['query'] = request()->get();
  215. $list = $channelModel->paginate($listRows, (isset($paginateArr[1]) ? $paginateArr[1] : false), $config);
  216. } else {
  217. $list = $channelModel->limit($limit)->cache($cache)->select();
  218. }
  219. $fieldsContentList = Fields::getFieldsContentList('channel');
  220. foreach ($list as $index => $item) {
  221. Service::appendTextAttr($fieldsContentList, $item);
  222. }
  223. self::render($list, $imgwidth, $imgheight);
  224. return $list;
  225. }
  226. /**
  227. * 渲染数据
  228. * @param array $list
  229. * @param int $imgwidth
  230. * @param int $imgheight
  231. * @return array
  232. */
  233. public static function render(&$list, $imgwidth, $imgheight)
  234. {
  235. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  236. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  237. foreach ($list as $k => &$v) {
  238. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['name'] . '</a>';
  239. $v['channellink'] = '<a href="' . $v['url'] . '">' . $v['name'] . '</a>';
  240. $v['outlink'] = $v['outlink'];
  241. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
  242. $v['img'] = '<img src="' . $v['image'] . '" border="" ' . $width . ' ' . $height . ' />';
  243. }
  244. return $list;
  245. }
  246. /**
  247. * 获取面包屑导航
  248. * @param array $channel
  249. * @param array $archives
  250. * @param array $tags
  251. * @param array $page
  252. * @param array $diyform
  253. * @return array
  254. */
  255. public static function getBreadcrumb($channel, $archives = [], $tags = [], $page = [], $diyform = [])
  256. {
  257. $list = [];
  258. $list[] = ['name' => __('Home'), 'url' => addon_url('cms/index/index', [], false)];
  259. if ($channel) {
  260. if ($channel['parent_id']) {
  261. $channelList = self::where('status', 'normal')
  262. ->order('weigh desc,id desc')
  263. ->field('id,name,type,parent_id,diyname,outlink')
  264. ->cache(true)
  265. ->select();
  266. //获取栏目的所有上级栏目
  267. $parents = \fast\Tree::instance()->init(collection($channelList)->toArray(), 'parent_id')->getParents($channel['id']);
  268. foreach ($parents as $k => $v) {
  269. $list[] = ['name' => $v['name'], 'url' => $v['url']];
  270. }
  271. }
  272. $list[] = ['name' => $channel['name'], 'url' => $channel['url']];
  273. }
  274. if ($archives) {
  275. //$list[] = ['name' => $archives['title'], 'url' => $archives['url']];
  276. }
  277. if ($tags) {
  278. $list[] = ['name' => $tags['name'], 'url' => $tags['url']];
  279. }
  280. if ($page) {
  281. $list[] = ['name' => $page['title'], 'url' => $page['url']];
  282. }
  283. if ($diyform) {
  284. $list[] = ['name' => $diyform['name'], 'url' => $diyform['url']];
  285. }
  286. return $list;
  287. }
  288. /**
  289. * 获取导航栏目列表HTML
  290. * @param $channel
  291. * @param array $tag
  292. * @return mixed|string
  293. * @throws \think\db\exception\DataNotFoundException
  294. * @throws \think\db\exception\ModelNotFoundException
  295. * @throws \think\exception\DbException
  296. */
  297. public static function getNav($channel, $tag = [])
  298. {
  299. $config = get_addon_config('cms');
  300. $condition = empty($tag['condition']) ? '' : $tag['condition'];
  301. $cache = !isset($tag['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$tag['cache'];
  302. $maxLevel = !isset($tag['maxlevel']) ? 0 : $tag['maxlevel'];
  303. $cache = !$cache ? false : $cache;
  304. $cacheName = 'nav-' . md5(serialize($tag));
  305. $result = Cache::get($cacheName);
  306. if ($result === false) {
  307. $channelList = Channel::where($condition)
  308. ->where('status', 'normal')
  309. ->order('weigh desc,id desc')
  310. ->cache($cache)
  311. ->select();
  312. $tree = \fast\Tree::instance();
  313. $tree->init(collection($channelList)->toArray(), 'parent_id');
  314. $result = self::getTreeUl($tree, 0, $channel ? $channel['id'] : '', '', 1, $maxLevel);
  315. Cache::set($cacheName, $result);
  316. }
  317. return $result;
  318. }
  319. public static function getTreeUl($tree, $myid, $selectedids = '', $disabledids = '', $level = 1, $maxlevel = 0)
  320. {
  321. $str = '';
  322. $childs = $tree->getChild($myid);
  323. if ($childs) {
  324. foreach ($childs as $value) {
  325. $id = $value['id'];
  326. unset($value['child']);
  327. $selected = $selectedids && in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  328. $disabled = $disabledids && in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  329. $value = array_merge($value, array('selected' => $selected, 'disabled' => $disabled));
  330. $value = array_combine(array_map(function ($k) {
  331. return '@' . $k;
  332. }, array_keys($value)), $value);
  333. $itemtpl = '<li class="@dropdown" value=@id @selected @disabled><a data-toggle="@toggle" data-target="#" href="@url">@name @caret</a> @childlist</li>';
  334. $nstr = strtr($itemtpl, $value);
  335. $childlist = '';
  336. if (!$maxlevel || $level < $maxlevel) {
  337. $childdata = self::getTreeUl($tree, $id, $selectedids, $disabledids, $level + 1, $maxlevel);
  338. $childlist = $childdata ? '<ul class="dropdown-menu" role="menu">' . $childdata . '</ul>' : "";
  339. }
  340. $str .= strtr($nstr, [
  341. '@childlist' => $childlist,
  342. '@caret' => $childlist ? ($level == 1 ? '<span class="caret"></span>' : '') : '',
  343. '@dropdown' => $childlist ? ($level == 1 ? 'dropdown' : 'dropdown-submenu') : '',
  344. '@toggle' => $childlist ? 'dropdown' : ''
  345. ]);
  346. }
  347. }
  348. return $str;
  349. }
  350. public function model()
  351. {
  352. return $this->belongsTo('Modelx', 'model_id')->setEagerlyType(0);
  353. }
  354. public function parent()
  355. {
  356. return $this->belongsTo("Channel", "parent_id");
  357. }
  358. }