Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 59
MessageController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 6
182.00
0.00% covered (danger)
0.00%
0 / 59
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 10
 actionIndex
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 8
 actionView
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 actionCreate
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 19
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
 findModel
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
<?php
namespace backend\controllers;
use Yii;
use common\models\Message;
use common\models\User;
use backend\models\MessageForm;
use yii\data\ActiveDataProvider;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
 * MessageController implements the CRUD actions for Message model.
 */
class MessageController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }
    /**
     * Lists all Message models.
     * @return mixed
     */
    public function actionIndex()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Message::find()->where(['reciver_user_id' => Yii::$app->user->id]),
        ]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single Message model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $message = $this->findModel($id);
        if(!Yii::$app->user->can('viewMessage', ['message' => $message]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        return $this->render('view', [
            'model' => $message,
        ]);
    }
    /**
     * Creates a new Message model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        if(!Yii::$app->user->can('createMessage'))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model = new MessageForm();
        $contacts = User::find()->where(['NOT', ['id' => Yii::$app->user->id]])->orderBy('lastname', 'firstname')->all();
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if ($model->sendMessage()) {
                Yii::$app->session->setFlash('success', 'Message has been sended.');
            } else {
                Yii::$app->session->setFlash('error', 'There was an error sending message.');
            }
            return $this->refresh();
        } else {
            return $this->render('create', [
                'model' => $model,
                'contacts' => ArrayHelper::map($contacts, 'id', 'fullname'),
            ]);
        }
    }
    /**
     * Deletes an existing Message model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $message = $this->findModel($id);
        if(!Yii::$app->user->can('deleteMessage', ['message' => $message]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $message->delete();
        return $this->redirect(['index']);
    }
    /**
     * Finds the Message model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Message the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Message::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}