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 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 125
OrganizationParticipantController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 8
1122.00
0.00% covered (danger)
0.00%
0 / 125
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 20
 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 / 9
 actionCreate
0.00% covered (danger)
0.00%
0 / 1
90.00
0.00% covered (danger)
0.00%
0 / 30
 actionUpdate
0.00% covered (danger)
0.00%
0 / 1
90.00
0.00% covered (danger)
0.00%
0 / 22
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
56.00
0.00% covered (danger)
0.00%
0 / 22
 findModel
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
 findOrganizationModel
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\ParticipantSearch;
use backend\models\UserForm;
use common\models\Organization;
use common\models\Participant;
use common\models\Event;
use common\models\User;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use yii\helpers\ArrayHelper;
/**
 * OrganizationParticipantController implements the CRUD actions for Participant model.
 */
class OrganizationParticipantController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['view', 'create', 'update', 'delete'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Lists all Participant models.
     * @return mixed
     * @deprecated List of all participants will be removed. You can get list of comapny participants in OrganizationController
     * @see \backend\controllers\OrganizationController
     */
    public function actionIndex()
    {
        $searchModel = new ParticipantSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single Participant model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('viewParticipant', ['participant' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        return $this->render('view', [
            'model' => $model,
            'organization' => $model->organization,
        ]);
    }
    /**
     * Creates a new Participant model and assign with an existing Organization organization_id = $id.
     * If creation is successful, the browser will be redirected to the 'organization/view' page.
     * @return mixed
     */
    public function actionCreate($id)
    {
        $organization = $this->findOrganizationModel($id);
        if(!Yii::$app->user->can('createParticipant', ['organization' => $organization]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model = new Participant();
        $userModel = new UserForm();
        if ($model->load(Yii::$app->request->post()) && $userModel->load(Yii::$app->request->post())){
            $model->organization_id = $organization->id;
            $model->user_id = 0;
            if($model->validate() && $userModel->validate()){
                if($user = $userModel->save()){
                    $model->user_id = $user->id;
                    if($model->save()){
                        $auth = Yii::$app->authManager;
                        $eventUser = $auth->getRole('eventUser');
                        $auth->assign($eventUser, $user->id);
                        return $this->redirect(['organization/view', 'id' => $model->organization_id]);
                    }
                }
            }
        }
        
        $event = Event::find()->active()->one();
        $eventSpans = empty($event) ? [] : $event->getEventSpans()->orderBy('date, time_from')->all();
        
        return $this->render('create', [
            'model' => $model,
            'eventSpans' => ArrayHelper::map($eventSpans, 'id', 'title'),
            'userModel' => $userModel,
            'organization' => $organization,
        ]);
    }
    /**
     * Updates an existing Participant model.
     * If update is successful, the browser will be redirected to the 'organization/view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('updateParticipant', ['participant' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $user = $model->user;
        $userModel = new UserForm($user);
        if ($model->load(Yii::$app->request->post()) && $userModel->load(Yii::$app->request->post())){
            if($model->validate() && $userModel->validate()){
                if($model->save() && $userModel->save()){
                    return $this->redirect(['organization/view', 'id' => $model->organization_id]);
                }
            }
        }
        
        $event = Event::find()->active()->one();
        $eventSpans = empty($event) ?: $event->getEventSpans()->orderBy('date, time_from')->all();
        
        return $this->render('update', [
            'model' => $model,
            'eventSpans' => ArrayHelper::map($eventSpans, 'id', 'title'),
            'userModel' => $userModel,
            'organization' => $model->organization,
        ]);
    }
    /**
     * Deletes an existing Participant model.
     * If deletion is successful, the browser will be redirected to the 'organization/view' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('deleteParticipant', ['participant' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $organization = $model->organization;
        if($model->getMeetings()->exists() === false && $model->getInvitations()->exists() === false){
            $user = $model->user;
            $model->unlinkAll('eventSpans', true);
            if($model->delete()){
                $auth = Yii::$app->authManager;
                if(!$auth->checkAccess($user->id, 'supervisor') && !$auth->checkAccess($user->id, 'admin')){
                    $user->delete();
                }else{
                    $eventUser = $auth->getRole('eventUser');
                    $auth->revoke($eventUser, $user->id);
                }
            }
        }else{
            Yii::$app->session->setFlash('warning', Yii::t('app', 'Participant can be removed. Have meetings'));
        }
        return $this->redirect(['organization/view', 'id' => $organization->id]);
    }
    /**
     * Finds the Participant model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Participant the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Participant::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /**
     * Finds the Organization model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return Organization the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findOrganizationModel($id)
    {
        if (($model = Organization::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}