Your IP : 216.73.216.79


Current Path : /var/www/html/drsa/backend/modules/chat/models/
Upload File :
Current File : /var/www/html/drsa/backend/modules/chat/models/ChatLiveSearch.php

<?php

namespace backend\modules\chat\models;

use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\chat\models\ChatLive;

/**
 * ChatLiveSearch represents the model behind the search form of `backend\modules\chat\models\ChatLive`.
 */
class ChatLiveSearch extends ChatLive {

    public $user;
    public $user_live_active;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['live_id', 'user_id', 'live_read_status', 'admin_id'], 'integer'],
            [['live_message', 'live_session', 'create_dttm', 'user', 'user_live_active'], '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 = ChatLive::find()
                ->select("chat_live.live_message, chat_live.user_id, MAX(chat_live.create_dttm) as create_dttm")
                ->groupBy(['chat_live.user_id']);
        $query->joinWith(['user']);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider(['query' => $query, 'pagination' => ['pageSize' => 10,]]);
        $dataProvider->setSort(['defaultOrder' => ['live_id' => SORT_DESC]]);

        $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;
        }

        // grid filtering conditions
        $query->andFilterWhere(['like', 'chat_user.user_name', $this->user]);

        $query->andFilterWhere(['<>', 'chat_user.user_id', '0']);
        $query->andFilterWhere(['=', 'chat_user.user_live_active', '1']);

        return $dataProvider;
    }

    public function searchIndividual($params) {

        $liveSessionArr = [];
        $query = ChatLive::find()->select('live_session')->distinct()->where(['=', 'user_id', $params['ChatLiveSearch']['user_id']])->all();
        foreach ($query as $q) {
            $liveSessionArr[] = $q->live_session;
        }

        $query = ChatLive::find()
                ->select("chat_live.live_message, chat_live.user_id, chat_live.create_dttm as create_dttm, admin_id");

        $query->joinWith(['user']);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort' => [
                'defaultOrder' => [
                    'create_dttm' => SORT_DESC,
                ]
            ],
        ]);

        $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;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'live_id' => $this->live_id,
            'create_dttm' => $this->create_dttm,
        ]);

        $query->orFilterWhere([
            'chat_live.user_id' => $this->user_id,
        ]);
        $query->orFilterWhere(['IN', 'live_session', $liveSessionArr]);

        $query->andFilterWhere(['like', 'live_message', $this->live_message])
                ->andFilterWhere(['like', 'live_session', $this->live_session]);


        return $dataProvider;
    }

}