/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { /*params group */ $group = $params['ChatSearch']['GROUP']; // componen user $profile = Yii::$app->getUserOpt->profile_user()->emp; $emp_id = $profile->EMP_ID; if ($params['chat'] == 'group') { $query = Chat::find()->where(['GROUP_ID' => $group]); } else { $query = Chat::find()->where(['CREATED_BY' => $emp_id])->orwhere(['GROUP_ID' => $emp_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; } $query->andFilterWhere(['ID' => $this->ID, 'MESSAGE_STS' => $this->MESSAGE_STS, 'MESSAGE_SHOW' => $this->MESSAGE_SHOW, 'GROUP_ID' => $this->GROUP_ID, 'UPDATED_TIME' => $this->UPDATED_TIME]); $query->andFilterWhere(['like', 'MESSAGE', $this->MESSAGE])->andFilterWhere(['like', 'MESSAGE_ATTACH', $this->MESSAGE_ATTACH])->andFilterWhere(['like', 'GROUP_ID', $this->GROUP_ID]); // ->andFilterWhere(['like', 'sc0003a.CREATED_BY', $this->CREATED_BY]); return $dataProvider; }
public function actionMessage($notifOnly = false) { $MAX_CONN = 15; // 15 second $sse = new \common\components\SSE(); try { $lastTime = Yii::$app->getRequest()->getHeaders()->get('last-event-id', 0); $profile = Yii::$app->getUserOpt->profile_user()->emp; $emp_id = $profile->EMP_ID; $lastCount = -1; for ($i = 0; $i <= $MAX_CONN; $i++) { // load message if (!$notifOnly) { $rows = Chat::find()->where('UPDATED_TIME <:ctime', [':ctime' => $lastTime])->orderBy(['UPDATED_TIME' => SORT_DESC])->asArray()->all(); $lastTime = microtime(true); $msgs = []; foreach (array_reverse($rows) as $row) { $self = $row['CREATED_BY'] == $emp_id; if ($row['UPDATED_TIME'] < $lastTime - 86400) { // lebih dari sehari $formatTime = date('d M H:i', $row['UPDATED_TIME']); } else { $formatTime = date('H:i:s', $row['UPDATED_TIME']); } $msgs[] = ['self' => $self, 'time' => $formatTime, 'name' => $self ? 'Me' : $profile->EMP_NM . ' ' . $profile->EMP_NM_BLK, 'text' => $row['MESSAGE']]; } if (count($msgs) || $i % 4 == 2) { $sse->event('chat', ['msgs' => $msgs]); $sse->flush(); } } sleep(1); } } catch (\Exception $exc) { $sse->event('msgerror', ['msg' => $exc->getMessage()]); $sse->flush(); } $sse->id($lastTime); $sse->flush(); exit; }