Created
January 8, 2018 17:37
-
-
Save tihoho/685ae00ff6d8ec9ef34e6d573ef3ddfe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace common\models; | |
use Yii; | |
use yii\base\Model; | |
use yii\data\ActiveDataProvider; | |
use common\models\User; | |
use common\models\Balance; | |
/** | |
* Поисковая модель для балансов | |
* | |
* @property $user_id integer | |
*/ | |
class BalanceSearch extends User | |
{ | |
public $globalSearch; | |
public $user_id; | |
public $order_id; | |
public $date_create; | |
public $date_create_range; | |
public $comment; | |
public $summ; | |
public $fullName; | |
/** | |
* @inheritdoc | |
*/ | |
public function rules() | |
{ | |
return [ | |
[['id', 'user_id', 'order_id'], 'integer'], | |
[['date_create', 'comment', 'fullName'], 'safe'], | |
[['summ'], 'number'], | |
['date_create_range', 'safe'] | |
]; | |
} | |
/** | |
* @inheritdoc | |
*/ | |
public function scenarios() | |
{ | |
return Model::scenarios(); | |
} | |
/** | |
* Creates data provider instance with search query applied | |
* | |
* @param array $params | |
* | |
* @return ActiveDataProvider | |
*/ | |
public function search($params) | |
{ | |
$query = Balance::find(); | |
$query->joinWith('user'); | |
// add conditions that should always apply here | |
$dataProvider = new ActiveDataProvider([ | |
'query' => $query, | |
'sort'=> ['defaultOrder' => ['date_create' => SORT_DESC]], | |
]); | |
$dataProvider->sort->attributes['fullName'] = [ | |
'asc' => ['fio' => SORT_ASC, 'email' => SORT_ASC], | |
'desc' => ['fio' => SORT_DESC, 'email' => SORT_DESC], | |
//'label' => 'ФИО / Почта', | |
//'default' => SORT_ASC | |
]; | |
$this->load($params); | |
if (!$this->validate()) { | |
return $dataProvider; | |
} | |
// Если поиск по всем полям | |
if($this->globalSearch) { | |
$query->orFilterWhere(['like', 'id', $this->globalSearch]) | |
->orFilterWhere(['like', 'user_id', $this->globalSearch]) | |
->orFilterWhere(['like', 'order_id', $this->globalSearch]) | |
->orFilterWhere(['like', 'summ', $this->globalSearch]) | |
->orFilterWhere(['like', 'comment', $this->globalSearch]); | |
} | |
// Если поиск по некоторым полям | |
else { | |
// grid filtering conditions | |
$query->andFilterWhere([ | |
'id' => $this->id, | |
'user_id' => $this->user_id, | |
'order_id' => $this->created_at, | |
'comment' => $this->updated_at, | |
'date_create' => $this->phone_internal, | |
'summ' => $this->summ, | |
]); | |
$query->andFilterWhere(['like', 'id', $this->id]) | |
->andFilterWhere(['like', 'user_id', $this->user_id]) | |
->andFilterWhere(['like', 'order_id', $this->order_id]) | |
->andFilterWhere(['like', 'summ', $this->summ]) | |
->andFilterWhere(['like', 'comment', $this->comment]); | |
// Фильтр по диапазону дат | |
if(!is_null($this->date_create_range)) { | |
$date = explode(' - ', $this->date_create_range); | |
$dateStart = $date[0] . ' 00:00:00'; | |
$dateEnd = $date[1] . ' 23:59:59'; | |
$query->andFilterWhere(['between', 'date_create', $dateStart, $dateEnd]); | |
} | |
// фильтр по fullName | |
$query->andWhere('user.fio LIKE "%' . $this->fullName . '%" ' . 'OR user.email LIKE "%' . $this->fullName . '%"'); | |
} | |
return $dataProvider; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment