/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Number::find();
$query->with('owner', 'documents');
$dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 30]]);
$dataProvider->setSort(['attributes' => ['number', 'destination', 'comment'], 'defaultOrder' => ['number' => SORT_ASC]]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
if (is_numeric($this->searchText)) {
$query->andFilterWhere(['like', 'number', $this->searchText]);
} elseif (!empty($this->searchText)) {
$ownerId = [];
foreach (Employee::findByName($this->searchText, true)->all() as $owner) {
$ownerId[] = $owner->getPrimaryKey();
}
$query->andWhere(['in', 'ownerId', $ownerId]);
}
if ($this->operatorId !== self::OPERATOR_ANY) {
$query->andFilterWhere(['operatorId' => $this->operatorId]);
}
if ($this->destination !== self::DESTINATION_ANY) {
$query->andFilterWhere(['destination' => $this->destination]);
}
$query->andFilterWhere(['like', 'comment', $this->comment]);
return $dataProvider;
}