File: //proc/1122/cwd/common/models/BotuseractionSearch.php
<?php
namespace common\models;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\BotUserAction;
/**
* BotuseractionSearch represents the model behind the search form about `common\models\BotUserAction`.
*/
class BotuseractionSearch extends BotUserAction
{
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'user_id', 'bot_id', 'chat_id'], 'integer'],
[['message', 'image', 'file', 'admin_comment', 'created_at'], '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
* user_id - user system id
* id - user_chat_id
*/
public function search($params, $user_id, $id, $bot_id)
{
$query = BotUserAction::find();
$query->where(['>', 'id', 0]);
if ($user_id) {
$query->andWhere(['user_id' => $user_id]);
}
$botId = $bot_id !== null && $bot_id !== '' ? (int) $bot_id : 0;
$chatIdKey = $id !== null && $id !== '' ? (string) $id : '';
// Thread key is bot_id + chat_id (same Telegram user in different bots = different chats).
if ($botId > 0 && $chatIdKey !== '') {
$query->andWhere(['bot_id' => $botId, 'chat_id' => $chatIdKey]);
} elseif ($chatIdKey !== '') {
$query->andWhere('0=1');
}
$dataProvider = new ActiveDataProvider([
'query' => $query,
]);
$query->orderBy(['id' => SORT_DESC]);
if (!($this->load($params) && $this->validate())) {
return $dataProvider;
}
$query->andFilterWhere([
'id' => $this->id,
'user_id' => $this->user_id,
'bot_id' => $this->bot_id,
'chat_id' => $this->chat_id,
]);
$query->andFilterWhere(['like', 'message', $this->message])
->andFilterWhere(['like', 'image', $this->image])
->andFilterWhere(['like', 'file', $this->file])
->andFilterWhere(['like', 'admin_comment', $this->admin_comment])
->andFilterWhere(['like', 'created_at', $this->created_at]);
return $dataProvider;
}
}