fitness-web/backend/models/ProductSearch.php

85 lines
2.2 KiB
PHP

<?php
namespace backend\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Product;
use common\components\RoleDefinition;
/**
* ProductSearch represents the model behind the search form about `common\models\Product`.
*/
class ProductSearch extends Product
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[[ 'id_product_category', 'id_account', 'status'], 'integer'],
[['product_number', 'barcode' ,'name'], 'safe'],
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = Product::find();
if ( !RoleDefinition::isAdmin() ){
$query->innerJoin("user_account_assignment",'product.id_account = user_account_assignment.id_account' );
$query->andWhere(['user_account_assignment.id_user' => Yii::$app->user->id ]);
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$this->load($params);
if (!$this->validate()) {
// uncomment the following line if you do not want to return any records when validation fails
// $query->where('0=1');
return $dataProvider;
}
if ( !empty($this->barcode)){
$this->barcode = strtolower($this->barcode);
}
if ( !empty($this->product_number)){
$this->product_number = strtolower($this->product_number);
}
$query->andFilterWhere([
'product.id_product_category' => $this->id_product_category,
'product.id_account' => $this->id_account,
'product.status' => $this->status,
'lower(product.product_number)' => $this->product_number,
'lower(product.barcode)' => $this->barcode,
]);
$query->andFilterWhere(['like', 'name', $this->name]);
return $dataProvider;
}
}