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 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 198
MeetingController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 10
420.00
0.00% covered (danger)
0.00%
0 / 198
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 26
 actions
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 13
 actionIndex
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 16
 actionView
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 actionUpdate
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 12
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 8
 actionPrint
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 35
 actionPrintPerParticipant
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 35
 actionPrintPerTable
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 38
 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 backend\models\MeetingSearch;
use common\models\Organization;
use common\models\Participant;
use common\models\Meeting;
use common\models\Supervisor;
use common\models\Table;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
/**
 * MeetingController implements the CRUD actions for Meeting model.
 */
class MeetingController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'view', 'choose-host', 'choose-guest', 'create', 'update', 'delete'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                    [
                        'actions' => ['print', 'print-per-participant', 'print-per-table'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                    'create' => ['post'],
                ],
            ],
        ];
    }
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'choose-host' => [
                'class' => 'backend\controllers\meeting\ChooseHostAction',
            ],
            'choose-guest' => [
                'class' => 'backend\controllers\meeting\ChooseGuestAction',
            ],
            'create' => [
                'class' => 'backend\controllers\meeting\CreateAction',
            ],
        ];
    }
    /**
     * Lists all Meeting models.
     * @return mixed
     * @todo Ukryc przyciski do ktorych nie ma uprawnien
     */
    public function actionIndex()
    {
        $searchModel = new MeetingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->innerJoin(['hostOrganization' => Organization::tableName()], 'hostOrganization.id = hostParticipant.organization_id')
                ->innerJoin(['guestOrganization' => Organization::tableName()], 'guestOrganization.id = guestParticipant.organization_id')
                ->innerJoin(['hostSupervisor' => Supervisor::tableName()], 'hostSupervisor.id = hostOrganization.supervisor_id')
                ->innerJoin(['guestSupervisor' => Supervisor::tableName()], 'guestSupervisor.id = guestOrganization.supervisor_id')
                ->andWhere(['or', ['hostSupervisor.user_id' => Yii::$app->user->id], ['guestSupervisor.user_id' => Yii::$app->user->id]]);
        }
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single Meeting model.
     * @param integer $id
     * @return mixed
     * @todo Ukryc przyciski do ktorych nie ma uprawnien
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('viewMeeting', ['meeting' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        return $this->render('view', [
            'model' => $model,
        ]);
    }
    /**
     * Updates an existing Meeting model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('updateInvitationState', ['meeting' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }
    /**
     * Deletes an existing Meeting model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('deleteMeeting', ['meeting' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model->sendCanceledByAdminEmail();
        $model->delete();
        return $this->redirect(['index']);
    }
    /**
     * Create a PDF document with meetings schedule.
     * @return mixed
     */
    public function actionPrint()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Meeting::find()
                ->joinWith(['eventSpan', 'table'])
                ->orderBy([
                    '{{%event_span}}.[[date]]' => 'ASC',
                    '{{%event_span}}.[[time_from]]' => 'ASC',
                    'event_span_time_on' => 'ASC',
                    '{{%table}}.[[on]]' => 'ASC',
                ])
                ->with([
                    'eventSpan',
                    'table',
                    'hostParticipant',
                    'guestParticipant',
                    'hostParticipant.organization',
                    'guestParticipant.organization',
                    'hostParticipant.user',
                    'guestParticipant.user',
                ]),
        ]);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith(['hostParticipant' => function($q){ $q->from(['hostParticipant' => '{{%participant}}']); }], false)
                ->joinWith(['guestParticipant' => function($q){ $q->from(['guestParticipant' => '{{%participant}}']); }], false)
                ->innerJoin(['hostOrganization' => Organization::tableName()], 'hostOrganization.id = hostParticipant.organization_id')
                ->innerJoin(['guestOrganization' => Organization::tableName()], 'guestOrganization.id = guestParticipant.organization_id')
                ->innerJoin(['hostSupervisor' => Supervisor::tableName()], 'hostSupervisor.id = hostOrganization.supervisor_id')
                ->innerJoin(['guestSupervisor' => Supervisor::tableName()], 'guestSupervisor.id = guestOrganization.supervisor_id')
                ->andWhere(['or', ['hostSupervisor.user_id' => Yii::$app->user->id], ['guestSupervisor.user_id' => Yii::$app->user->id]]);
        }
        $this->layout = false;
        Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->render('print', [
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Create a PDF document with meetings schedule gruped per participant.
     * @return mixed
     */
    public function actionPrintPerParticipant()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Participant::find()
                ->joinWith(['organization', 'user'])
                ->joinWith(['meetings' => function($q){ $q->from(['meetings' => Meeting::tableName()]); }])
                ->joinWith(['invitations' => function($q){ $q->from(['invitations' => Meeting::tableName()]); }])
                ->groupBy(Participant::tableName().'.[[id]]')
                ->having('(COUNT({{meetings}}.[[id]]) > 0) OR (COUNT({{invitations}}.[[id]]) > 0)')
                ->with([
                    'meetings',
                    'user',
                    'organization',
                    'meetings.eventSpan',
                    'meetings.table',
                    'meetings.hostParticipant',
                    'meetings.guestParticipant',
                    'meetings.hostParticipant.user',
                    'meetings.guestParticipant.user',
                    'invitations.eventSpan',
                    'invitations.table',
                    'invitations.hostParticipant',
                    'invitations.guestParticipant',
                    'invitations.hostParticipant.user',
                    'invitations.guestParticipant.user',
                ]),
        ]);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith('organization')->joinWith('organization.supervisor')
                ->andWhere(['{{%supervisor}}.[[user_id]]' => Yii::$app->user->id]);
        }
        $this->layout = false;
        Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->render('print_per_participant', [
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Create a PDF document with meetings schedule gruped per table.
     * @return mixed
     */
    public function actionPrintPerTable()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Table::find()
                ->with([
                    'meetings',
                    'meetings.eventSpan',
                    'meetings.table',
                    'meetings.hostParticipant',
                    'meetings.guestParticipant',
                    'meetings.hostParticipant.organization',
                    'meetings.guestParticipant.organization',
                    'meetings.hostParticipant.user',
                    'meetings.guestParticipant.user',
                ]),
        ]);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith(['meetings.hostParticipant' => function($q){ $q->from(['hostParticipant' => '{{%participant}}']); }], false)
                ->joinWith(['meetings.guestParticipant' => function($q){ $q->from(['guestParticipant' => '{{%participant}}']); }], false)
                ->innerJoin(['hostOrganization' => Organization::tableName()], 'hostOrganization.id = hostParticipant.organization_id')
                ->innerJoin(['guestOrganization' => Organization::tableName()], 'guestOrganization.id = guestParticipant.organization_id')
                ->innerJoin(['hostSupervisor' => Supervisor::tableName()], 'hostSupervisor.id = hostOrganization.supervisor_id')
                ->innerJoin(['guestSupervisor' => Supervisor::tableName()], 'guestSupervisor.id = guestOrganization.supervisor_id')
                ->andWhere(['or', ['hostSupervisor.user_id' => Yii::$app->user->id], ['guestSupervisor.user_id' => Yii::$app->user->id]])
                ->with([
                    'meetings' => function($query){
                        $query
                            ->joinWith(['hostParticipant' => function($q){ $q->from(['hostParticipant' => '{{%participant}}']); }], false)
                            ->joinWith(['guestParticipant' => function($q){ $q->from(['guestParticipant' => '{{%participant}}']); }], false)
                            ->innerJoin(['hostOrganization' => Organization::tableName()], 'hostOrganization.id = hostParticipant.organization_id')
                            ->innerJoin(['guestOrganization' => Organization::tableName()], 'guestOrganization.id = guestParticipant.organization_id')
                            ->innerJoin(['hostSupervisor' => Supervisor::tableName()], 'hostSupervisor.id = hostOrganization.supervisor_id')
                            ->innerJoin(['guestSupervisor' => Supervisor::tableName()], 'guestSupervisor.id = guestOrganization.supervisor_id')
                            ->andWhere(['or', ['hostSupervisor.user_id' => Yii::$app->user->id], ['guestSupervisor.user_id' => Yii::$app->user->id]]);
                    }
                ]);
        }
        $this->layout = false;
        Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->render('print_per_table', [
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Finds the Meeting model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Meeting the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Meeting::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}