FriendlyLinkSearch.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Author: lf
  4. * Blog: https://blog.feehi.com
  5. * Email: job@feehi.com
  6. * Created at: 2017-10-16 16:02
  7. */
  8. namespace backend\models\search;
  9. use Yii;
  10. use common\models\FriendlyLink;
  11. use common\libs\Constants;
  12. use backend\behaviors\TimeSearchBehavior;
  13. use backend\components\search\SearchEvent;
  14. use yii\data\ActiveDataProvider;
  15. class FriendlyLinkSearch extends FriendlyLink implements SearchInterface
  16. {
  17. public function behaviors()
  18. {
  19. return [
  20. TimeSearchBehavior::className()
  21. ];
  22. }
  23. /**
  24. * @inheritdoc
  25. */
  26. public function rules()
  27. {
  28. return [
  29. [['name', 'url'], 'string'],
  30. [['status', 'image'], 'integer'],
  31. [['created_at', 'updated_at'], 'string'],
  32. ];
  33. }
  34. /**
  35. * @param array $params
  36. * @param array $options
  37. * @return object|ActiveDataProvider
  38. * @throws \yii\base\InvalidConfigException
  39. */
  40. public function search(array $params = [], array $options = [])
  41. {
  42. $query = FriendlyLink::find()->orderBy(['sort'=>SORT_ASC, 'id'=>SORT_DESC]);
  43. $dataProvider = Yii::createObject([
  44. 'class' => ActiveDataProvider::className(),
  45. 'query' => $query,
  46. 'sort' => [
  47. 'defaultOrder' => [
  48. 'sort' => SORT_ASC,
  49. 'id' => SORT_DESC,
  50. ]
  51. ]
  52. ]);
  53. /** @var $dataProvider ActiveDataProvider */
  54. $this->load($params);
  55. if (! $this->validate()) {
  56. return $dataProvider;
  57. }
  58. $query->andFilterWhere(['like', 'name', $this->name])
  59. ->andFilterWhere(['status' => $this->status])
  60. ->andFilterWhere(['like', 'url', $this->url]);
  61. if ($this->image == Constants::YesNo_Yes) {
  62. $query->andWhere(['<>', 'image', '']);
  63. } else {
  64. if ($this->image === '0') {
  65. $query->andWhere(['image' => '']);
  66. }
  67. }
  68. $this->trigger(SearchEvent::BEFORE_SEARCH, Yii::createObject(['class' => SearchEvent::className(), 'query'=>$query]));
  69. return $dataProvider;
  70. }
  71. }