Guest User

Untitled

a guest
Oct 16th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.37 KB | None | 0 0
  1. <?php
  2.  
  3. namespace frontend\models\search;
  4.  
  5. use Yii;
  6. use yii\base\Model;
  7. use yii\data\ActiveDataProvider;
  8. use frontend\models\Task;
  9.  
  10. /**
  11. * TaskSearch represents the model behind the search form about `frontend\models\Task`.
  12. */
  13. class TaskSearch extends Task
  14. {
  15. public $statusDescription;
  16. public $ownerName;
  17. public $requesterName;
  18.  
  19. /**
  20. * @inheritdoc
  21. */
  22. public function rules()
  23. {
  24. return [
  25. [['id', 'project_id', 'status_id', 'created_by', 'updated_by'], 'integer'],
  26. [['name', 'description', 'created_at', 'updated_at', 'statusDescription', 'ownerName', 'requesterName'], 'safe'],
  27. ];
  28. }
  29.  
  30. /**
  31. * @inheritdoc
  32. */
  33. public function scenarios()
  34. {
  35. // bypass scenarios() implementation in the parent class
  36. return Model::scenarios();
  37. }
  38.  
  39. /**
  40. * Creates data provider instance with search query applied
  41. *
  42. * @param array $params
  43. *
  44. * @return ActiveDataProvider
  45. */
  46. public function search($params, $project_id = null)
  47. {
  48. if ($project_id)
  49. $query = Task::find()->where(['project_id' => $project_id]);
  50. else
  51. $query = Task::find();
  52.  
  53. // add conditions that should always apply here
  54.  
  55. $dataProvider = new ActiveDataProvider([
  56. 'query' => $query,
  57. ]);
  58.  
  59. $dataProvider->setSort([
  60. 'attributes' => [
  61. 'name',
  62. 'description',
  63. 'statusDescription' => [
  64. 'asc' => ['status.description' => SORT_ASC],
  65. 'desc' => ['status.description' => SORT_DESC],
  66. 'label' => 'Status'
  67. ],
  68. 'ownerName' => [
  69. 'asc' => ['owner.username' => SORT_ASC],
  70. 'desc' => ['owner.username' => SORT_DESC],
  71. 'label' => 'Owner'
  72. ],
  73. 'requesterName' => [
  74. 'asc' => ['requester.username' => SORT_ASC],
  75. 'desc' => ['requester.username' => SORT_DESC],
  76. 'label' => 'Requester'
  77. ],
  78. ]
  79. ]);
  80.  
  81. $this->load($params);
  82.  
  83. if (!$this->validate()) {
  84. // uncomment the following line if you do not want to return any records when validation fails
  85. // $query->where('0=1');
  86. return $dataProvider;
  87. }
  88.  
  89. // grid filtering conditions
  90. $query->andFilterWhere([
  91. 'id' => $this->id,
  92. 'project_id' => $this->project_id,
  93. 'status_id' => $this->status_id,
  94. 'created_at' => $this->created_at,
  95. 'updated_at' => $this->updated_at,
  96. 'created_by' => $this->created_by,
  97. 'updated_by' => $this->updated_by,
  98. ]);
  99.  
  100. $query->andFilterWhere(['like', 'name', $this->name])
  101. ->andFilterWhere(['like', 'description', $this->description]);
  102.  
  103. $query->joinWith(['status' => function ($q) {
  104.  
  105. $q->andFilterWhere(['=', 'status.id', $this->statusDescription]);
  106.  
  107. }]);
  108.  
  109. $query->joinWith(['owner' => function ($q) {
  110.  
  111. $q->andFilterWhere(['like', 'owner.username', $this->ownerName]);
  112.  
  113. }]);
  114.  
  115. $query->joinWith(['requester' => function ($q) {
  116.  
  117. $q->andFilterWhere(['like', 'requester.username', $this->requesterName]);
  118.  
  119. }]);
  120.  
  121. return $dataProvider;
  122. }
  123. }
Add Comment
Please, Sign In to add comment