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 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 100
OrganizationTableController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 7
420.00
0.00% covered (danger)
0.00%
0 / 100
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 21
 actionCreate
0.00% covered (danger)
0.00%
0 / 1
20.00
0.00% covered (danger)
0.00%
0 / 16
 actionAssign
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 24
 actionDetach
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 12
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
12.00
0.00% covered (danger)
0.00%
0 / 13
 findModel
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
 findTableModel
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\Organization;
use common\models\OrganizationTable;
use common\models\Table;
use yii\data\ActiveDataProvider;
use yii\web\Controller;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
/**
 * OrganizationTableController implements the CRUD actions for OrganizationTable model.
 */
class OrganizationTableController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['create', 'assign', 'detach', 'delete'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'detach' => ['post'],
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Creates a new Table 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->findModel($id);
        if(!Yii::$app->user->can('createTable', ['organization' => $organization]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model = new Table();
        $model->is_special = true;
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->link('organizations', $organization);
            return $this->redirect(['organization/view', 'id' => $id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'organization' => $organization,
            ]);
        }
    }
    /**
     * Assign an existing selected Table 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 actionAssign($id)
    {
        $organization = $this->findModel($id);
        if(!Yii::$app->user->can('assignTable', ['organization' => $organization]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model = new OrganizationTable();
        $model->organization_id = $organization->id;
        $tables = Table::find()
            ->isSpecial()
            ->isAssigned(false)
            ->orderBy('on')
            ->all();
        if($tables == null){
            return $this->redirect(['create', 'id' => $id]);
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['organization/view', 'id' => $id]);
        } else {
            return $this->render('assign', [
                'model' => $model,
                'organization' => $organization,
                'tables' => $tables,
            ]);
        }
    }
    /**
     * Detach an existing Table from an existing Organization organization_id = $id.
     * The browser will be redirected to the 'organization/view' page.
     * @return mixed
     */
    public function actionDetach($id, $table)
    {
        $organization = $this->findModel($id);
        if(!Yii::$app->user->can('detachTable', ['organization' => $organization]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $table = $this->findTableModel($table);
        if($table->getMeetings()->count() == 0){
            $organization->unlink('tables', $table, true);
        }else{
            Yii::$app->session->setFlash('warning', Yii::t('app', 'Noting was changed. Some meeting are already created.'));
        }
        return $this->redirect(['organization/view', 'id' => $id]);
    }
    /**
     * Delete an existing Table.
     * The browser will be redirected to the 'organization/view' page.
     * @return mixed
     * @todo Poprawic, aby supervisor nie mogl usunac stolika uzywanego rozniez przez inna organizacje (nie nadzorowana przez niego)
     */
    public function actionDelete($id, $table)
    {
        $organization = $this->findModel($id);
        if(!Yii::$app->user->can('deleteTable', ['organization' => $organization]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $table = $this->findTableModel($table);
        if($table->getMeetings()->count() == 0){
            $table->unlinkAll('organizations', true);
            $table->delete();
        }else{
            Yii::$app->session->setFlash('warning', Yii::t('app', 'Noting was changed. Some meeting are already created.'));
        }
        return $this->redirect(['organization/view', 'id' => $id]);
    }
    /**
     * Finds the Organization model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Organization the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Organization::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
    /**
     * Finds the Table model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Table the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findTableModel($id)
    {
        if (($model = Table::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}