Column.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 海豚PHP框架 [ DolphinPHP ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://dolphinphp.com
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
  10. // +----------------------------------------------------------------------
  11. namespace app\cms\model;
  12. use think\Model as ThinkModel;
  13. use util\Tree;
  14. /**
  15. * 栏目模型
  16. * @package app\cms\model
  17. */
  18. class Column extends ThinkModel
  19. {
  20. // 设置当前模型对应的完整数据表名称
  21. protected $table = '__CMS_COLUMN__';
  22. // 自动写入时间戳
  23. protected $autoWriteTimestamp = true;
  24. /**
  25. * 标题获取器
  26. * @param $value
  27. * @param $data
  28. * @author 蔡伟明 <314013107@qq.com>
  29. */
  30. protected function getTitleAttr($value, $data) {
  31. switch ($data['type']) {
  32. case 0: // 栏目
  33. break;
  34. case 1: // 单页
  35. break;
  36. }
  37. }
  38. /**
  39. * 获取栏目列表
  40. * @author 蔡伟明 <314013107@qq.com>
  41. * @return array|mixed
  42. */
  43. public static function getList()
  44. {
  45. $data_list = cache('cms_column_list');
  46. if (!$data_list) {
  47. $data_list = self::where('status', 1)->column(true, 'id');
  48. // 非开发模式,缓存数据
  49. if (config('develop_mode') == 0) {
  50. cache('cms_column_list', $data_list);
  51. }
  52. }
  53. return $data_list;
  54. }
  55. /**
  56. * 获取树状栏目
  57. * @param int $id 需要隐藏的栏目id
  58. * @param string $default 默认第一个节点项,默认为“顶级栏目”,如果为false则不显示,也可传入其他名称
  59. * @author 蔡伟明 <314013107@qq.com>
  60. * @return array|mixed
  61. */
  62. public static function getTreeList($id = 0, $default = '')
  63. {
  64. $result[0] = '顶级栏目';
  65. // 排除指定节点及其子节点
  66. if ($id !== 0) {
  67. $hide_ids = array_merge([$id], self::getChildsId($id));
  68. $where['id'] = ['notin', $hide_ids];
  69. }
  70. $where['status'] = 1;
  71. $data_list = Tree::config(['title' => 'name'])->toList(self::where($where)->order('pid,id')->column('id,pid,name'));
  72. foreach ($data_list as $item) {
  73. $result[$item['id']] = $item['title_display'];
  74. }
  75. // 设置默认节点项标题
  76. if ($default != '') {
  77. $result[0] = $default;
  78. }
  79. // 隐藏默认节点项
  80. if ($default === false) {
  81. unset($result[0]);
  82. }
  83. return $result;
  84. }
  85. /**
  86. * 获取所有子栏目id
  87. * @param int $pid 父级id
  88. * @author 蔡伟明 <314013107@qq.com>
  89. * @return array
  90. */
  91. public static function getChildsId($pid = 0)
  92. {
  93. $ids = self::where('pid', $pid)->column('id');
  94. foreach ($ids as $value) {
  95. $ids = array_merge($ids, self::getChildsId($value));
  96. }
  97. return $ids;
  98. }
  99. /**
  100. * 获取指定栏目数据
  101. * @param int $cid 栏目id
  102. * @author 蔡伟明 <314013107@qq.com>
  103. * @return mixed|static
  104. */
  105. public static function getInfo($cid = 0)
  106. {
  107. $result = cache('cms_column_info_'. $cid);
  108. if (!$result) {
  109. $result = self::get($cid);
  110. // 非开发模式,缓存数据
  111. if (config('develop_mode') == 0) {
  112. cache('cms_column_info_'. $cid, $result);
  113. }
  114. }
  115. return $result;
  116. }
  117. }