/**
  * @param null $id
  * @return array|string
  * @throws \yii\base\InvalidConfigException
  * @internal param null $id
  */
 public function actionIndex($id = null)
 {
     $messages = [];
     $uploader = $this->loadUploader();
     $dialogs = Yii::$app->messages->getDialogs();
     if (Yii::$app->request->isPost) {
         $message = new MessageForm();
         if ($message->load(Yii::$app->request->post())) {
             if ($result = Yii::$app->messages->send($message, User::findOne($message->to))) {
                 $uploader->messageModel = $result;
                 $uploader->saveTempFiles();
                 Yii::$app->response->format = Response::FORMAT_JSON;
                 return ['time' => time(), 'text' => $result->text, 'message' => $this->renderPartial('_message', ['message' => $result, 'firstToday' => !Date::isSameDay(Yii::$app->request->post('last_time'), time())])];
             }
         }
     }
     $activeDialog = null;
     if ($id) {
         foreach ($dialogs as $dialog) {
             if ($dialog->dialogId == $id) {
                 $activeDialog = $dialog;
                 Yii::$app->messages->markAsRead($activeDialog);
                 $messages = Yii::$app->messages->getDialogMessages($activeDialog);
                 break;
             }
         }
         if (!$activeDialog) {
             throw new NotFoundHttpException();
         }
     } else {
         if ($dialogs) {
             $activeDialog = $dialogs[0];
             Yii::$app->messages->markAsRead($activeDialog);
             $messages = Yii::$app->messages->getDialogMessages($activeDialog);
         }
     }
     $uploader->clearUserGarbage();
     return $this->render('index', ['dialogs' => Yii::$app->messages->getDialogs(), 'activeDialog' => $activeDialog, 'messages' => $messages]);
 }
Exemple #2
0
use yii\helpers\Html;
use frontend\helpers\Date;
/* @var $this yii\web\View */
/* @var $message \common\models\Message */
/* @var $firstToday boolean */
/* @var $user \common\models\User */
/* @var $photo \common\models\MessagePhoto */
$user = Yii::$app->user->identity;
?>

<?php 
if (isset($firstToday) && $firstToday) {
    ?>
    <div class="chat-message-date"><span><?php 
    echo Date::getToday();
    ?>
</span></div>
<?php 
}
?>

<div class="message chat-message-<?php 
echo $user->isOwnMessage($message) ? 'right' : 'left';
?>
">
    <div class="chat-message-ava">
        <?php 
echo Html::img($user->isOwnMessage($message) ? $user->getAvatar() : $message->sender->getAvatar());
?>
    </div>