public function search($params) { $query = Mailbox::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'sender' => $this->sender, 'receiver' => $this->receiver, 'readed' => $this->readed, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'subject', $this->subject])->andFilterWhere(['like', 'content', $this->content]); return $dataProvider; }
public function actionGetMailbox() { $session = Yii::$app->session; $stream = false; $objMailboxs = Mailbox::find()->where(['receiver' => Yii::$app->user->id, 'readed' => 0, 'status' => 1])->orderBy('updated_at DESC')->one(); if ($objMailboxs) { if ($session->has('message_time')) { if ($session->get('message_time') == $objMailboxs->updated_at) { // TETAP $stream = 1; } else { // BERUBAH $stream = 1; } } else { $session->set('message_time', $objMailboxs->updated_at); // BERUBAH $stream = 1; } } else { // BERUBAH $stream = 1; } header('Content-Type: text/event-stream'); if ($stream) { header('Cache-Control: no-cache'); echo "retry: 10000\n"; // Optional. We tell the browser to retry after 10 seconds $countMailbox = 0; $objMailboxs = Mailbox::find()->where(['receiver' => Yii::$app->user->id, 'readed' => 0, 'status' => 1])->orderBy('updated_at DESC')->all(); $listMailbox = ""; if ($objMailboxs) { $subjects = []; $photos = []; $i = 1; foreach ($objMailboxs as $message) { $ds = DIRECTORY_SEPARATOR; $global_path = Yii::getAlias("@common") . $ds . 'files' . $ds; $path = $global_path . $ds . 'users' . $ds; if (file_exists($path . $message->sender . '.jpg')) { $photos[] = Url::to(['site/download', 'file' => 'users/' . $message->sender . '.jpg', 'preview' => true]); } else { $photos[] = $AdminLTEAsset->baseUrl . '/img/user2-160x160.jpg'; } $subjects[] = "" . $message->subject . ""; $urls[] = Url::to(['message/view', 'id' => $message->id]); $times[] = $message->updated_at; $i++; } $subjects = '' . implode('|', $subjects) . ''; $urls = '' . implode('|', $urls) . ''; $times = '' . implode('|', $times) . ''; $photos = '' . implode('|', $photos) . ''; $countMailbox = $i - 1; } echo "data: {\"subjects\": \"" . $subjects . "\",\"times\": \"" . $times . "\",\"photos\": \"" . $photos . "\",\"urls\": \"" . $urls . "\",\"count\": \"" . $countMailbox . "\"}\n\n"; flush(); } }