ArticleSearch.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-03-15 21:16
  7. */
  8. namespace backend\models\search;
  9. use Yii;
  10. use backend\behaviors\TimeSearchBehavior;
  11. use backend\components\search\SearchEvent;
  12. use common\models\Article;
  13. use common\models\Category;
  14. use yii\base\Model;
  15. use yii\data\ActiveDataProvider;
  16. use yii\helpers\ArrayHelper;
  17. class ArticleSearch extends Article implements SearchInterface
  18. {
  19. public $content;
  20. /**
  21. * @inheritdoc
  22. */
  23. public function rules()
  24. {
  25. return [
  26. [['title', 'author_name', 'cid', 'seo_keywords', 'content', 'sub_title', 'summary', 'seo_title'], 'string'],
  27. [['created_at', 'updated_at'], 'string'],
  28. [
  29. [
  30. 'id',
  31. 'status',
  32. 'flag_headline',
  33. 'flag_recommend',
  34. 'flag_slide_show',
  35. 'flag_special_recommend',
  36. 'flag_roll',
  37. 'flag_bold',
  38. 'flag_picture',
  39. 'thumb',
  40. 'sort',
  41. 'visibility',
  42. 'can_comment',
  43. 'password',
  44. ],
  45. 'integer',
  46. ],
  47. ];
  48. }
  49. public function behaviors()
  50. {
  51. return [
  52. TimeSearchBehavior::className()
  53. ];
  54. }
  55. public function scenarios()
  56. {
  57. return Model::scenarios();
  58. }
  59. /**
  60. * @param array $params
  61. * @param array $options
  62. * @return ActiveDataProvider
  63. * @throws \yii\base\InvalidConfigException
  64. */
  65. public function search(array $params = [], array $options = [])
  66. {
  67. $query = Article::find()->select([])->where(['type' => $options['type']])->with('category')->joinWith("articleContent");
  68. /** @var $dataProvider ActiveDataProvider */
  69. $dataProvider = Yii::createObject([
  70. 'class' => ActiveDataProvider::className(),
  71. 'query' => $query,
  72. 'sort' => [
  73. 'defaultOrder' => [
  74. 'sort' => SORT_ASC,
  75. 'id' => SORT_DESC,
  76. ]
  77. ]
  78. ]);
  79. $this->load($params);
  80. if (! $this->validate()) {
  81. return $dataProvider;
  82. }
  83. $query->alias("article")
  84. ->andFilterWhere(['like', 'title', $this->title])
  85. ->andFilterWhere(['article.id' => $this->id])
  86. ->andFilterWhere(['status' => $this->status])
  87. ->andFilterWhere(['flag_headline' => $this->flag_headline])
  88. ->andFilterWhere(['flag_recommend' => $this->flag_recommend])
  89. ->andFilterWhere(['flag_slide_show' => $this->flag_slide_show])
  90. ->andFilterWhere(['flag_special_recommend' => $this->flag_special_recommend])
  91. ->andFilterWhere(['flag_roll' => $this->flag_roll])
  92. ->andFilterWhere(['flag_bold' => $this->flag_bold])
  93. ->andFilterWhere(['flag_picture' => $this->flag_picture])
  94. ->andFilterWhere(['like', 'author_name', $this->author_name])
  95. ->andFilterWhere(['sort' => $this->sort])
  96. ->andFilterWhere(['visibility' => $this->visibility])
  97. ->andFilterWhere(['can_comment' => $this->can_comment])
  98. ->andFilterWhere(['like', 'seo_keywords', $this->seo_keywords])
  99. ->andFilterWhere(['like', 'content', $this->content])
  100. ->andFilterWhere(['like', 'sub_title', $this->sub_title])
  101. ->andFilterWhere(['like', 'summary', $this->summary])
  102. ->andFilterWhere(['like', 'seo_title', $this->seo_title]);
  103. if ($this->thumb == 1) {
  104. $query->andWhere(['<>', 'thumb', '']);
  105. } else {
  106. if ($this->thumb === '0') {
  107. $query->andWhere(['thumb' => '']);
  108. }
  109. }
  110. if ($this->password == 1) {
  111. $query->andWhere(['<>', 'password', '']);
  112. } else {
  113. if ($this->password === '0') {
  114. $query->andWhere(['password' => '']);
  115. }
  116. }
  117. if ($this->cid === '0') {
  118. $query->andWhere(['cid' => 0]);
  119. } else {
  120. if (! empty($this->cid)) {
  121. $cids = ArrayHelper::getColumn((new Category())->getDescendants($this->cid), 'id');
  122. if (count($cids) <= 0) {
  123. $query->andFilterWhere(['cid' => $this->cid]);
  124. } else {
  125. $cids[] = $this->cid;
  126. $query->andFilterWhere(['cid' => $cids]);
  127. }
  128. }
  129. }
  130. $this->trigger(SearchEvent::BEFORE_SEARCH, Yii::createObject(['class' => SearchEvent::className(), 'query'=>$query]));
  131. return $dataProvider;
  132. }
  133. }