ArticleCategory.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/02
  6. */
  7. namespace app\admin\model\article;
  8. use crmeb\traits\ModelTrait;
  9. use crmeb\basic\BaseModel;
  10. use app\admin\model\article\Article as ArticleModel;
  11. /**
  12. * 文章分类model
  13. * Class ArticleCategory
  14. * @package app\admin\model\wechat
  15. */
  16. class ArticleCategory extends BaseModel
  17. {
  18. use ModelTrait;
  19. protected $pk = 'id';
  20. protected $name = 'article_category';
  21. /**
  22. * 获取系统分页数据 分类
  23. * @param array $where
  24. * @return array
  25. */
  26. public static function systemPage($where = [])
  27. {
  28. $model = new self;
  29. if ($where['title'] !== '') $model = $model->where('title', 'LIKE', "%$where[title]%");
  30. if ($where['status'] !== '') $model = $model->where('status', $where['status']);
  31. $model = $model->where('is_del', 0);
  32. $model = $model->where('hidden', 0);
  33. return self::page($model);
  34. }
  35. /**
  36. * 删除分类
  37. * @param $id
  38. * @return bool
  39. */
  40. public static function delArticleCategory($id)
  41. {
  42. if (count(self::getArticle($id, '*')) > 0)
  43. return self::setErrorInfo('请先删除改分类下的文章!');
  44. return self::edit(['is_del' => 1], $id, 'id');
  45. }
  46. /**
  47. * 获取分类名称和id
  48. * @return array
  49. */
  50. public static function getField()
  51. {
  52. return self::where('is_del', 0)->where('status', 1)->where('hidden', 0)->column('title', 'id');
  53. }
  54. /**
  55. * 分级排序列表
  56. * @param null $model
  57. * @return array
  58. */
  59. public static function getTierList($model = null)
  60. {
  61. if ($model === null) $model = new self();
  62. return sort_list_tier($model->where('is_del', 0)->where('status', 1)->select()->toArray());
  63. }
  64. /**
  65. * 获取分类底下的文章
  66. * id 分类表中的分类id
  67. * return array
  68. * */
  69. public static function getArticle($id, $field)
  70. {
  71. $res = ArticleModel::where('status', 1)->where('hide', 0)->column($field, 'id');
  72. $new_res = array();
  73. foreach ($res as $k => $v) {
  74. $cid_arr = explode(',', $v['cid']);
  75. if (in_array($id, $cid_arr)) {
  76. $new_res[$k] = $res[$k];
  77. }
  78. }
  79. return $new_res;
  80. }
  81. /**
  82. * TODO 获取文章分类
  83. * @return array
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\ModelNotFoundException
  86. * @throws \think\exception\DbException
  87. */
  88. public static function getArticleCategoryList()
  89. {
  90. $list = self::where('is_del', 0)->where('status', 1)->select();
  91. if ($list) return $list->toArray();
  92. return [];
  93. }
  94. /**
  95. * TODO 获取文章分类信息
  96. * @param $id
  97. * @param string $field
  98. * @return mixed
  99. */
  100. public static function getArticleCategoryInfo($id, $field = 'title')
  101. {
  102. $model = new self;
  103. if ($id) $model = $model->where('id', $id);
  104. $model = $model->where('is_del', 0);
  105. $model = $model->where('status', 1);
  106. return $model->column($field, 'id');
  107. }
  108. }