SubscriptionsSearch.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use yii\data\ActiveDataProvider;
  6. use app\models\Subscriptions;
  7. /**
  8. * SubscriptionsSearch represents the model behind the search form about `app\models\Subscriptions`.
  9. */
  10. class SubscriptionsSearch extends Subscriptions
  11. {
  12. public $category_name;
  13. public $fio;
  14. public $email;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function rules()
  19. {
  20. return [
  21. [['category_id'], 'integer'],
  22. [['fio', 'category_name', 'email'], 'string'],
  23. ];
  24. }
  25. /**
  26. * @inheritdoc
  27. */
  28. public function scenarios()
  29. {
  30. // bypass scenarios() implementation in the parent class
  31. return Model::scenarios();
  32. }
  33. /**
  34. * Creates data provider instance with search query applied
  35. *
  36. * @param array $params
  37. *
  38. * @return ActiveDataProvider
  39. */
  40. public function search($params)
  41. {
  42. $query = Subscriptions::find()->joinWith(['category', 'user']);
  43. $dataProvider = new ActiveDataProvider([
  44. 'query' => $query,
  45. 'sort' => [
  46. 'defaultOrder' => ['created_at' => SORT_DESC],
  47. ]
  48. ]);
  49. $dataProvider->sort->attributes['email'] = [
  50. 'asc' => ['user.email' => SORT_ASC],
  51. 'desc' => ['user.email' => SORT_DESC],
  52. ];
  53. $dataProvider->sort->attributes['fio'] = [
  54. 'asc' => ['user.fio' => SORT_ASC],
  55. 'desc' => ['user.fio' => SORT_DESC],
  56. ];
  57. $this->load($params);
  58. if (!$this->validate()) {
  59. // uncomment the following line if you do not want to return any records when validation fails
  60. // $query->where('0=1');
  61. return $dataProvider;
  62. }
  63. if(!Yii::$app->user->can('admin')){
  64. $query->andWhere(['user_id' => Yii::$app->user->getId()]);
  65. }
  66. $query->andFilterWhere([
  67. 'category_id' => $this->category_id,
  68. ]);
  69. $query->andFilterWhere(['like', 'user.email', $this->email])
  70. ->andFilterWhere(['like', 'user.fio', $this->fio]);
  71. return $dataProvider;
  72. }
  73. }