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 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 238
OrganizationController
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 11
756.00
0.00% covered (danger)
0.00%
0 / 238
 behaviors
0.00% covered (danger)
0.00%
0 / 1
2.00
0.00% covered (danger)
0.00%
0 / 30
 actionIndex
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 13
 actionView
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 22
 actionCreate
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 27
 actionUpdate
0.00% covered (danger)
0.00%
0 / 1
30.00
0.00% covered (danger)
0.00%
0 / 27
 actionDelete
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
 actionPrint
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 19
 actionPrintAttendance
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 21
 actionPrintNotActive
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 23
 actionExport
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 42
 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\OrganizationSearch;
use backend\models\ParticipantSearch;
use backend\models\TableSearch;
use common\models\Organization;
use common\models\Participant;
use common\models\Event;
use common\models\Meeting;
use common\models\ActivityArea;
use common\models\OrganizationProfile;
use common\models\OrganizationType;
use common\models\Supervisor;
use common\models\User;
use common\models\Country;
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;
/**
 * OrganizationController implements the CRUD actions for Organization model.
 */
class OrganizationController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['index', 'view', 'create', 'update', 'delete'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                    [
                        'actions' => ['print', 'print-attendance', 'print-not-active'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                    [
                        'actions' => ['export'],
                        'allow' => true,
                        'roles' => ['supervisor', 'admin'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }
    /**
     * Lists all Organization models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new OrganizationSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith('supervisor.user')
                ->andWhere(['{{%user}}.id' => Yii::$app->user->id]);
        }
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Displays a single Organization model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('viewOrganization', ['organization' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $participantsSearchModel = new ParticipantSearch();
        $participantsDataProvider = $participantsSearchModel->search(
            Yii::$app->request->queryParams,
            $model->getParticipants()
        );
        $tablesSearchModel = new TableSearch();
        $tablesDataProvider = $tablesSearchModel->search(
            Yii::$app->request->queryParams,
            $model->getTables()
        );
        return $this->render('view', [
            'model' => $model,
            'participantsSearchModel' => $participantsSearchModel,
            'participantsDataProvider' => $participantsDataProvider,
            'tablesSearchModel' => $tablesSearchModel,
            'tablesDataProvider' => $tablesDataProvider,
        ]);
    }
    /**
     * Creates a new Organization model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Organization();
        if(!Yii::$app->user->can('createOrganization'))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        if(!Yii::$app->user->can('admin')){
            $supervisors = Supervisor::find()
                ->where(['user_id' => Yii::$app->user->id])
                ->all();
        }else{
            $supervisors = Supervisor::find()
                ->all();
        }
        
        $countries = Country::find()->select('name, code')->orderBy('name')->indexBy('code')->column();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('create', [
                'model' => $model,
                'events' => Event::find()->all(),
                'activityAreas' => ActivityArea::find()->where(['parent_id' => null])->indexBy('id')->all(),
                'organizationProfiles' => OrganizationProfile::find()->all(),
                'organizationTypes' => OrganizationType::find()->all(),
                'supervisors' => $supervisors,
                'countries' => $countries,
            ]);
        }
    }
    /**
     * Updates an existing Organization model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @todo Dodac uprawnienie do organization gdy supervisor
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('updateOrganization', ['organization' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        if(!Yii::$app->user->can('admin')){
            $supervisors = Supervisor::find()
                ->where(['user_id' => Yii::$app->user->id])
                ->all();
        }else{
            $supervisors = Supervisor::find()
                ->all();
        }
        
        $countries = Country::find()->select('name, code')->orderBy('name')->indexBy('code')->column();
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'events' => Event::find()->all(),
                'activityAreas' => ActivityArea::find()->where(['parent_id' => null])->indexBy('id')->all(),
                'organizationProfiles' => OrganizationProfile::find()->all(),
                'organizationTypes' => OrganizationType::find()->all(),
                'supervisors' => $supervisors,
                'countries' => $countries,
            ]);
        }
    }
    /**
     * Deletes an existing Organization model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @todo Dodac uprawnienie do organization gdy supervisor
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        if(!Yii::$app->user->can('deleteOrganization', ['organization' => $model]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $model->delete();
        return $this->redirect(['index']);
    }
    /**
     * Create a PDF document with organization catalog.
     * @return mixed
     * @todo Sprawdzic sortowanie
     */
    public function actionPrint()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Organization::find()
                ->with(['activityAreas', 'organizationProfile', 'organizationType', 'participants', 'participants.user'])
                ->orderBy([
                    Organization::tableName().'.[[name]]' => 'ASC',
                ]),
        ]);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith('supervisor.user')
                ->andWhere([User::tableName().'.[[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 organization attendance.
     * @return mixed
     * @todo Sprawdzic sortowanie
     */
    public function actionPrintAttendance()
    {
        $dataProvider = new ActiveDataProvider([
            'query' => Participant::find()
                ->joinWith(['organization', 'user'])
                ->orderBy([
                    Organization::tableName().'.[[name]]' => 'ASC',
                    User::tableName().'.[[lastname]]' => 'ASC',
                    User::tableName().'.[[firstname]]' => 'ASC',
                ]),
        ]);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith('supervisor.user')
                ->andWhere([User::tableName().'.[[id]]' => Yii::$app->user->id]);
        }
        $this->layout = false;
        Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->render('print_attendance', [
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Create a PDF document with not active organizations.
     * @return mixed
     * @todo Sprawdzic sortowanie
     */
    public function actionPrintNotActive()
    {
        $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 AND count({{invitations}}.[[id]]) = 0')
                ->orderBy([
                    Organization::tableName().'.[[name]]' => 'ASC',
                    User::tableName().'.[[lastname]]' => 'ASC',
                    User::tableName().'.[[firstname]]' => 'ASC',
                ]),
        ]);
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith('supervisor.user')
                ->andWhere([User::tableName().'.[[id]]' => Yii::$app->user->id]);
        }
        $this->layout = false;
        Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->render('print_not_active', [
            'dataProvider' => $dataProvider,
        ]);
    }
    /**
     * Create a XLSX document with user list.
     * @return mixed
     */
    public function actionExport()
    {
        if(!Yii::$app->user->can('admin')){
            $dataProvider->query
                ->joinWith('supervisor.user')
                ->andWhere([User::tableName().'.[[id]]' => Yii::$app->user->id]);
        }
        $data = Participant::find()
            ->select([
                'o.[[name]]',
                'u.[[firstname]]',
                'u.[[lastname]]',
                'u.[[email]]',
                'p.[[phone]]',
                'o.[[address]]',
                'o.[[postal_code]]',
                'o.[[city]]',
                'c.[[name]]',
                'o.[[website]]',
                'o.[[email]]',
                'o.[[founded]]',
                's.[[office_name]]',
                'p.[[created_at]]'
            ])
            ->from(['p' => Participant::tableName()])
            ->leftJoin(['o' => Organization::tableName()], 'o.id = p.organization_id')
            ->leftJoin(['u' => User::tableName()], 'u.id = p.user_id')
            ->leftJoin(['s' => Supervisor::tableName()], 's.id = o.supervisor_id')
            ->leftJoin(['c' => Country::tableName()], 'c.code = o.country')
            ->orderBy([
                'o.[[name]]' => 'ASC',
                'u.[[lastname]]' => 'ASC',
                'u.[[firstname]]' => 'ASC',
            ])
            ->asArray()
            ->createCommand()
            ->queryAll(\PDO::FETCH_NUM);
        $this->layout = false;
        Yii::$app->response->format = Response::FORMAT_RAW;
        return $this->render('export', [
            'data' => $data,
        ]);
    }
    /**
     * 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.');
        }
    }
}