You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

72 lines
1.9 KiB

<?php
namespace backend\forms;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use core\entities\user\User;
use yii\helpers\ArrayHelper;
class UserSearch extends Model
{
public ?int $id = null;
public ?string $date_from = null;
public ?string $date_to = null;
public ?string $username = null;
public ?string $email = null;
public ?int $status = null;
public ?string $role = null;
public function rules(): array
{
return [
[['id', 'status'], 'integer'],
[['username', 'email', 'role'], 'safe'],
[['date_from', 'date_to'], 'date', 'format' => 'php:Y-m-d'],
];
}
/**
* @param array $params
* @return ActiveDataProvider
*/
public function search(array $params): ActiveDataProvider
{
$query = User::find()->alias('u');
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
$query->where('0=1');
return $dataProvider;
}
$query->andFilterWhere([
'u.id' => $this->id,
'u.status' => $this->status,
]);
if (!empty($this->role)) {
$query->innerJoin('{{%auth_assignments}} a', 'a.user_id = u.id');
$query->andWhere(['a.item_name' => $this->role]);
}
$query
->andFilterWhere(['like', 'u.username', $this->username])
->andFilterWhere(['like', 'u.email', $this->email])
->andFilterWhere(['>=', 'u.created_at', $this->date_from ? strtotime($this->date_from . ' 00:00:00') : null])
->andFilterWhere(['<=', 'u.created_at', $this->date_to ? strtotime($this->date_to . ' 23:59:59') : null]);
return $dataProvider;
}
public function rolesList(): array
{
return ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description');
}
}