Channel.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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 $navParentIds = null;
  29. protected static function init()
  30. {
  31. $config = get_addon_config('cms');
  32. self::$config = $config;
  33. }
  34. public function getAttr($name)
  35. {
  36. //获取自定义字段关联表数据
  37. if (!isset($this->data[$name]) && preg_match("/(.*)_value\$/i", $name, $matches)) {
  38. $key = $this->data[$matches[1]] ?? '';
  39. if (!$key) {
  40. return '';
  41. }
  42. return Service::getRelationFieldValue('channel', 0, $matches[1], $key);
  43. }
  44. return parent::getAttr($name);
  45. }
  46. public function getUrlAttr($value, $data)
  47. {
  48. return $this->buildUrl($value, $data);
  49. }
  50. public function getFullurlAttr($value, $data)
  51. {
  52. return $this->buildUrl($value, $data, true);
  53. }
  54. private function buildUrl($value, $data, $domain = false)
  55. {
  56. $diyname = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : $data['id'];
  57. $cateid = $data['id'] ?? 0;
  58. $catename = isset($data['diyname']) && $data['diyname'] ? $data['diyname'] : 'all';
  59. $time = $data['createtime'] ?? time();
  60. $vars = [
  61. ':id' => $data['id'],
  62. ':diyname' => $diyname,
  63. ':channel' => $cateid,
  64. ':catename' => $catename,
  65. ':cateid' => $cateid,
  66. ':year' => date("Y", $time),
  67. ':month' => date("m", $time),
  68. ':day' => date("d", $time)
  69. ];
  70. if (isset($data['type']) && isset($data['outlink']) && $data['type'] == 'link') {
  71. return $this->getAttr('outlink');
  72. }
  73. $suffix = static::$config['moduleurlsuffix']['channel'] ?? static::$config['urlsuffix'];
  74. return addon_url('cms/channel/index', $vars, $suffix, $domain);
  75. }
  76. public function getImageAttr($value, $data)
  77. {
  78. $value = $value ? $value : self::$config['default_channel_img'];
  79. return cdnurl($value);
  80. }
  81. public function getOutlinkAttr($value, $data)
  82. {
  83. $indexUrl = $view_replace_str = config('view_replace_str.__PUBLIC__');
  84. $indexUrl = rtrim($indexUrl, '/');
  85. return str_replace('__INDEX__', $indexUrl, $value);
  86. }
  87. public function getTagcolorAttr($value, $data)
  88. {
  89. $color = ['primary', 'default', 'success', 'warning', 'danger'];
  90. $index = $data['id'] % count($color);
  91. return isset($color[$index]) ? $color[$index] : $color[0];
  92. }
  93. public function getHasimageAttr($value, $data)
  94. {
  95. return $this->getData("image") ? true : false;
  96. }
  97. /**
  98. * 判断是否拥有子列表
  99. * @param $value
  100. * @param $data
  101. * @return bool|mixed
  102. */
  103. public function getHasChildAttr($value, $data)
  104. {
  105. static $checked = [];
  106. if (isset($checked[$data['id']])) {
  107. return $checked[$data['id']];
  108. }
  109. if (is_null(self::$parentIds)) {
  110. self::$parentIds = self::where('parent_id', '>', 0)->cache(true)->where('status', 'normal')->column('parent_id');
  111. }
  112. if (self::$parentIds && in_array($data['id'], self::$parentIds)) {
  113. return true;
  114. }
  115. return false;
  116. }
  117. /**
  118. * 判断导航是否拥有子列表
  119. * @param $value
  120. * @param $data
  121. * @return bool|mixed
  122. */
  123. public function getHasNavChildAttr($value, $data)
  124. {
  125. static $checked = [];
  126. if (isset($checked[$data['id']])) {
  127. return $checked[$data['id']];
  128. }
  129. if (is_null(self::$navParentIds)) {
  130. self::$navParentIds = self::where('parent_id', '>', 0)->cache(true)->where('status', 'normal')->where('isnav', 1)->column('parent_id');
  131. }
  132. if (self::$navParentIds && in_array($data['id'], self::$navParentIds)) {
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * 判断是否当前页面
  139. * @param $value
  140. * @param $data
  141. * @return bool
  142. */
  143. public function getIsActiveAttr($value, $data)
  144. {
  145. $url = request()->url();
  146. $channel = View::instance()->__CHANNEL__;
  147. if (($channel && ($channel['id'] == $this->id || $channel['parent_id'] == $this->id)) || $this->url == $url) {
  148. return true;
  149. } else {
  150. if ($this->has_child) {
  151. if (is_null(self::$outlinkParentIds)) {
  152. self::$outlinkParentIds = self::where('type', 'link')->where('status', 'normal')->column('outlink,parent_id');
  153. }
  154. if (self::$outlinkParentIds && isset(self::$outlinkParentIds[$url]) && self::$outlinkParentIds[$url] == $this->id) {
  155. return true;
  156. }
  157. }
  158. }
  159. return false;
  160. }
  161. public static function getContributeInfo($archives, $model = null)
  162. {
  163. // 读取可发布的栏目列表
  164. $channel = new Channel();
  165. $disabledIds = [];
  166. $channelList = collection(
  167. $channel->where('status', 'normal')
  168. ->order("weigh desc,id desc")
  169. ->cache(true)
  170. ->select()
  171. )->toArray();
  172. $channelParents = [];
  173. foreach ($channelList as $index => $item) {
  174. if ($item['parent_id'] && $item['iscontribute']) {
  175. $channelParents[] = $item['parent_id'];
  176. }
  177. }
  178. $channelList = collection(
  179. $channel->where('status', 'normal')
  180. ->where(function ($query) use ($channelParents, $archives) {
  181. $query->where("iscontribute", 1)->whereOr('id', 'in', $channelParents);
  182. if ($archives) {
  183. $query->whereOr('id', $archives->channel_id);
  184. }
  185. })
  186. ->order("weigh desc,id desc")
  187. ->select()
  188. )->toArray();
  189. foreach ($channelList as $index => $item) {
  190. if (!$item['iscontribute'] && !in_array($item['id'], $channelParents) && (!$archives || $archives->channel_id != $item['id'])) {
  191. unset($channelList[$index]);
  192. }
  193. }
  194. foreach ($channelList as $k => $v) {
  195. if ($v['type'] == 'link' || (($archives || input('model_id')) && $model && $model['id'] != $v['model_id']) || (!$v['iscontribute'])) {
  196. $disabledIds[] = $v['id'];
  197. }
  198. //if ($v['type'] == 'channel' && !in_array($v['id'], $channelParents)) {
  199. // unset($channelList[$k]);
  200. //}
  201. }
  202. return [$channelList, $disabledIds];
  203. }
  204. /**
  205. * 获取栏目所有子级的ID
  206. * @param mixed $ids 栏目ID或集合ID
  207. * @param bool $withself 是否包含自身
  208. * @return array
  209. */
  210. public static function getChannelChildrenIds($ids, $withself = true)
  211. {
  212. $cacheName = 'childrens-' . $ids . '-' . $withself;
  213. $result = Cache::get($cacheName);
  214. if ($result === false) {
  215. $channelList = Channel::where('status', 'normal')
  216. ->order('weigh desc,id desc')
  217. ->cache(true)
  218. ->select();
  219. $result = [];
  220. $tree = \fast\Tree::instance();
  221. $tree->init(collection($channelList)->toArray(), 'parent_id');
  222. $channelIds = is_array($ids) ? $ids : explode(',', $ids);
  223. foreach ($channelIds as $index => $channelId) {
  224. $result = array_merge($result, $tree->getChildrenIds($channelId, $withself));
  225. }
  226. Cache::set($cacheName, $result);
  227. }
  228. return $result;
  229. }
  230. /**
  231. * 获取栏目列表
  232. * @param $params
  233. * @return false|\PDOStatement|string|\think\Collection
  234. */
  235. public static function getChannelList($params)
  236. {
  237. $type = empty($params['type']) ? '' : $params['type'];
  238. $typeid = !isset($params['typeid']) ? '' : $params['typeid'];
  239. $model = !isset($params['model']) ? '' : $params['model'];
  240. $condition = empty($params['condition']) ? '' : $params['condition'];
  241. $field = empty($params['field']) ? '*' : $params['field'];
  242. $row = empty($params['row']) ? 10 : (int)$params['row'];
  243. $orderby = empty($params['orderby']) ? 'weigh' : $params['orderby'];
  244. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  245. $limit = empty($params['limit']) ? $row : $params['limit'];
  246. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  247. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  248. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  249. $paginate = !isset($params['paginate']) ? false : $params['paginate'];
  250. $where = ['status' => 'normal'];
  251. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('channellist', $params);
  252. self::$tagCount++;
  253. if ($type === 'top') {
  254. //顶级分类
  255. $where['parent_id'] = 0;
  256. } elseif ($type === 'brother') {
  257. $subQuery = self::where('id', 'in', $typeid)->field('parent_id')->buildSql();
  258. //同级
  259. $where['parent_id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
  260. } elseif ($type === 'son') {
  261. $subQuery = self::where('parent_id', 'in', $typeid)->field('id')->buildSql();
  262. //子级
  263. $where['id'] = ['exp', Db::raw(' IN ' . '(' . $subQuery . ')')];
  264. } elseif ($type === 'sons') {
  265. //所有子级
  266. $where['id'] = ['in', self::getChannelChildrenIds($typeid)];
  267. } else {
  268. if ($typeid !== '') {
  269. $where['id'] = ['in', $typeid];
  270. }
  271. }
  272. if ($model !== '') {
  273. $where['model_id'] = ['in', $model];
  274. }
  275. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  276. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  277. $channelModel = self::where($where)
  278. ->where($condition)
  279. ->field($field)
  280. ->orderRaw($order);
  281. if ($paginate) {
  282. list($listRows, $simple, $config) = Service::getPaginateParams('cpage' . self::$tagCount, $params);
  283. $list = $channelModel->paginate($listRows, $simple, $config);
  284. } else {
  285. $list = $channelModel->limit($limit)->cache($cacheKey, $cacheExpire)->select();
  286. }
  287. Service::appendTextAndList('channel', 0, $list, true);
  288. self::render($list, $imgwidth, $imgheight);
  289. return $list;
  290. }
  291. /**
  292. * 渲染数据
  293. * @param array $list
  294. * @param int $imgwidth
  295. * @param int $imgheight
  296. * @return array
  297. */
  298. public static function render(&$list, $imgwidth, $imgheight)
  299. {
  300. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  301. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  302. foreach ($list as $k => &$v) {
  303. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['name'] . '</a>';
  304. $v['channellink'] = '<a href="' . $v['url'] . '">' . $v['name'] . '</a>';
  305. $v['outlink'] = $v['outlink'];
  306. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['image'] . '" ' . $width . ' ' . $height . ' /></a>';
  307. $v['img'] = '<img src="' . $v['image'] . '" ' . $width . ' ' . $height . ' />';
  308. }
  309. return $list;
  310. }
  311. /**
  312. * 获取面包屑导航
  313. * @param array $channel
  314. * @param array $archives
  315. * @param array $tags
  316. * @param array $page
  317. * @param array $diyform
  318. * @param array $special
  319. * @return array
  320. */
  321. public static function getBreadcrumb($channel, $archives = [], $tags = [], $page = [], $diyform = [], $special = [])
  322. {
  323. $list = [];
  324. $list[] = ['name' => __('Home'), 'url' => addon_url('cms/index/index', [], false)];
  325. if ($channel) {
  326. if ($channel['parent_id']) {
  327. $channelList = self::where('status', 'normal')
  328. ->order('weigh desc,id desc')
  329. ->field('id,name,type,parent_id,diyname,outlink')
  330. ->cache(true)
  331. ->select();
  332. //获取栏目的所有上级栏目
  333. $parents = \fast\Tree::instance()->init(collection($channelList)->toArray(), 'parent_id')->getParents($channel['id']);
  334. foreach ($parents as $k => $v) {
  335. $list[] = ['name' => $v['name'], 'url' => $v['url']];
  336. }
  337. }
  338. $list[] = ['name' => $channel['name'], 'url' => $channel['url']];
  339. }
  340. if ($archives) {
  341. //$list[] = ['name' => $archives['title'], 'url' => $archives['url']];
  342. }
  343. foreach ([$tags, $page, $diyform, $special] as $index => $item) {
  344. if ($item && (!$channel || $channel['url'] != $item['url'])) {
  345. $list[] = ['name' => $item['title'] ?? $item['name'], 'url' => $item['url']];
  346. }
  347. }
  348. return $list;
  349. }
  350. /**
  351. * 获取导航栏目列表HTML
  352. * @param $channel
  353. * @param array $params
  354. * @return mixed|string
  355. * @throws \think\db\exception\DataNotFoundException
  356. * @throws \think\db\exception\ModelNotFoundException
  357. * @throws \think\exception\DbException
  358. */
  359. public static function getNav($channel, $params = [])
  360. {
  361. $config = get_addon_config('cms');
  362. $condition = empty($params['condition']) ? '' : $params['condition'];
  363. $maxLevel = !isset($params['maxlevel']) ? 0 : $params['maxlevel'];
  364. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('nav', $params);
  365. $cacheName = 'nav-' . md5(serialize($params));
  366. $result = Cache::get($cacheName);
  367. if ($result === false) {
  368. $channelList = Channel::where($condition)
  369. ->where('status', 'normal')
  370. ->order('weigh desc,id desc')
  371. ->cache($cacheKey, $cacheExpire)
  372. ->select();
  373. $tree = \fast\Tree::instance();
  374. $tree->init(collection($channelList)->toArray(), 'parent_id');
  375. $result = self::getTreeUl($tree, 0, $channel ? $channel['id'] : '', '', 1, $maxLevel);
  376. Cache::set($cacheName, $result);
  377. }
  378. return $result;
  379. }
  380. public static function getTreeUl($tree, $myid, $selectedids = '', $disabledids = '', $level = 1, $maxlevel = 0)
  381. {
  382. $str = '';
  383. $childs = $tree->getChild($myid);
  384. if ($childs) {
  385. foreach ($childs as $value) {
  386. $id = $value['id'];
  387. unset($value['child']);
  388. $selected = $selectedids && in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  389. $disabled = $disabledids && in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  390. $value = array_merge($value, array('selected' => $selected, 'disabled' => $disabled));
  391. $value = array_combine(array_map(function ($k) {
  392. return '@' . $k;
  393. }, array_keys($value)), $value);
  394. $itemtpl = '<li class="@dropdown" value=@id @selected @disabled><a data-toggle="@toggle" data-target="#" href="@url">@name @caret</a> @childlist</li>';
  395. $nstr = strtr($itemtpl, $value);
  396. $childlist = '';
  397. if (!$maxlevel || $level < $maxlevel) {
  398. $childdata = self::getTreeUl($tree, $id, $selectedids, $disabledids, $level + 1, $maxlevel);
  399. $childlist = $childdata ? '<ul class="dropdown-menu" role="menu">' . $childdata . '</ul>' : "";
  400. }
  401. $str .= strtr($nstr, [
  402. '@childlist' => $childlist,
  403. '@caret' => $childlist ? ($level == 1 ? '<span class="caret"></span>' : '') : '',
  404. '@dropdown' => $childlist ? ($level == 1 ? 'dropdown' : 'dropdown-submenu') : '',
  405. '@toggle' => $childlist ? 'dropdown' : ''
  406. ]);
  407. }
  408. }
  409. return $str;
  410. }
  411. public static function getChannelInfo($params)
  412. {
  413. $config = get_addon_config('cms');
  414. $cid = empty($params['cid']) ? '' : $params['cid'];
  415. $condition = empty($params['condition']) ? '' : $params['condition'];
  416. $field = empty($params['field']) ? '*' : $params['field'];
  417. $row = empty($params['row']) ? 10 : (int)$params['row'];
  418. $orderby = empty($params['orderby']) ? 'weigh' : $params['orderby'];
  419. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  420. $limit = empty($params['limit']) ? $row : $params['limit'];
  421. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  422. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  423. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  424. $where = [];
  425. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('channelinfo', $params);
  426. if ($cid !== '') {
  427. $where['id'] = $cid;
  428. }
  429. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  430. $order = $orderby == 'weigh' ? $order . ',id DESC' : $order;
  431. $data = self::where($where)
  432. ->where($condition)
  433. ->field($field)
  434. ->order($order)
  435. ->limit($limit)
  436. ->cache($cacheKey, $cacheExpire)
  437. ->find();
  438. if ($data) {
  439. $list = [$data];
  440. self::render($list, $imgwidth, $imgheight);
  441. return reset($list);
  442. } else {
  443. return false;
  444. }
  445. }
  446. public static function getChannelByLinktype($type, $source_id)
  447. {
  448. $channel = (new self())->where('linktype', $type)->where('linkid', $source_id)->order('weigh DESC,id DESC')->find();
  449. return $channel;
  450. }
  451. public function model()
  452. {
  453. return $this->belongsTo('Modelx', 'model_id')->setEagerlyType(0);
  454. }
  455. public function parent()
  456. {
  457. return $this->belongsTo("Channel", "parent_id");
  458. }
  459. }