fitness-web/common/models/CardSearch.php

134 lines
3.9 KiB
PHP

<?php
namespace common\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\Card;
use yii\db\Query;
use yii\helpers\ArrayHelper;
/**
* CardSearch represents the model behind the search form about `common\models\Card`.
*/
class CardSearch extends Card
{
public $customerName;
public $towel;
/**
* @inheritdoc
*/
public function rules()
{
return [
['customerName','string','max' =>200],
['number','string','max' =>200],
['towel','integer']
];
}
/**
* @inheritdoc
*/
public function scenarios()
{
// bypass scenarios() implementation in the parent class
return Model::scenarios();
}
public function attributeLabels(){
return ArrayHelper::merge(parent::attributeLabels(),
[
'card_number' => 'Kártya szám',
'customerName' => 'Vendég',
'towel' => 'Törölköző'
]
);
}
/**
* Creates data provider instance with search query applied
*
* @param array $params
*
* @return ActiveDataProvider
*/
public function search($params)
{
$query = new Query();
$query->select(['card.id_card as card_id_card', 'card.number as card_number' , 'customer.name as customer_name', 'customer.email as customer_email','customer.phone as customer_phone','customer.towel_count as towel_count']);
$query->from('card');
$query->innerJoin('customer','card.id_card = customer.id_customer_card');
$query->leftJoin("card_key_assignment", 'card.id_card = card_key_assignment.id_card');
$query->leftJoin("key", 'key.id_key = card_key_assignment.id_key');
// $query->orderBy(['customer.name' => SORT_ASC]);
$dataProvider = new ActiveDataProvider([
'query' => $query,
'sort' =>[
'attributes' => [
'card_number' => [
'asc' => ['card.number' => SORT_ASC ],
'desc' => ['card.number' => SORT_DESC],
],
'customer_name' => [
'asc' => ['customer.name' => SORT_ASC ],
'desc' => ['customer.name' => SORT_DESC],
],
'customer_phone' => [
'asc' => ['customer.phone' => SORT_ASC ],
'desc' => ['customer.phone' => SORT_DESC],
],
'customer_email' => [
'asc' => ['customer.email' => SORT_ASC ],
'desc' => ['customer.email' => SORT_DESC],
],
'towel_count' => [
'asc' => ['customer.towel_count' => SORT_ASC ],
'desc' => ['customer.towel_count' => SORT_DESC],
],
],
'defaultOrder' =>[
'customer_name' => SORT_ASC,
]
]
]);
$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;
}
// $query->andFilterWhere([
// 'lower(card.number)' => $this->customerName
// ]);
if ( !empty($this->number)){
$query->andWhere(['or',
['and',[ 'in','card.number' , [$this->number]],"trim(coalesce(card.number, '')) <>'' " ],
['and', ['in','card.rfid_key' ,[ $this->number] ],"trim(coalesce(card.rfid_key, '')) <>'' "],
['and',[ 'in','key.number' , [$this->number]],"trim(coalesce(key.number, '')) <>'' " ],
['and', ['in','key.rfid_key' ,[ $this->number] ],"trim(coalesce(key.rfid_key, '')) <>'' "]
]);
}
if ( isset($this->towel) && !empty($this->towel)){
$query->andWhere(['>','customer.towel_count',0]);
}
$query->andFilterWhere(['like', 'customer.name', $this->customerName]);
return $dataProvider;
}
}