Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
CRAP
11.54% covered (danger)
11.54%
6 / 52
SignupAction
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (warning)
50.00%
1 / 2
193.22
11.54% covered (danger)
11.54%
6 / 52
 init
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 run
0.00% covered (danger)
0.00%
0 / 1
190.20
8.00% covered (danger)
8.00%
4 / 50
<?php namespace frontend\controllers\user;
use Yii;
use frontend\models\SignupForm;
use common\models\AccompanyingEvent;
use common\models\ActivityArea;
use common\models\Organization;
use common\models\OrganizationProfile;
use common\models\OrganizationType;
use common\models\Country;
use common\models\Participant;
use common\models\Event;
use common\models\Supervisor;
use yii\base\Action;
use yii\web\ForbiddenHttpException;
use yii\web\BadRequestHttpException;
/**
 * Creates new User, Participant, Organization models.
 * If creation is successful, the browser will be redirected to the 'index' page.
 * @return mixed
 */
class SignupAction extends Action
{
    protected $events;
    public function init()
    {
        $this->events = Event::find()->active()->canRegister()->indexBy('id')->all();
    }
    public function run()
    {
        if(!Yii::$app->user->can('createOrganization') || Yii::$app->user->can('eventUser'))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.'));
        $event = null;
        if (empty($this->events)) {
            return $this->controller->render('signupNoEvents');
        } elseif (count($this->events) > 1) {
            $event_id = Yii::$app->request->get('event');
            if (is_numeric($event_id) && isset($this->events[$event_id])) {
                $event = $this->events[$event_id];
            } else {
                return $this->controller->render('signupSelectEvent', [
                    'events' => $this->events
                ]);
            }
        } else {
            $event = reset($this->events);
        }
        $eventSpans = $event->getEventSpans()
            ->orderBy('date, time_from')->all();
        $accompanyingEvents = AccompanyingEvent::find()
            ->availableForEvent($event->id)->active()
            ->orderBy('date, time')->all();
        // Init models
        $userModel = new SignupForm();
        $participantModel = new Participant();
        $organizationModel = new Organization();
        $participantAttributes = $participantModel->activeAttributes();
        $participantAttributes = array_diff($participantAttributes, ['user_id', 'organization_id']);
        // Process input
        if ($userModel->load(Yii::$app->request->post()) & $organizationModel->load(Yii::$app->request->post()) & $participantModel->load(Yii::$app->request->post())) {
            $organizationModels = [];
            if(count($organizationModels) > 0) {
                $participantModel->organization_id = $organizationModels[0]->id;
            }
            $organizationModel->event_id = $event->id;
            if ($userModel->validate() & $participantModel->validate($participantAttributes) & (count($organizationModels) > 0 || $organizationModel->validate())) {
                if(count($organizationModels) == 0) {
                    $organizationModel->save();
                    $participantModel->organization_id = $organizationModel->id;
                }
                if(($user = $userModel->signup()) && $participantModel->organization_id > 0) {
                    $participantModel->user_id = $user->id;
                    if($participantModel->save()) {
                        $auth = Yii::$app->authManager;
                        $eventUser = $auth->getRole('eventUser');
                        $auth->assign($eventUser, $user->id);
                        return $this->controller->goHome();
                    }
                }
            }
        }
        
        $countries = Country::find()->select('name, code')->orderBy('name')->indexBy('code')->column();
        return $this->controller->render('signup', [
            'userModel' => $userModel,
            'organizationModel' => $organizationModel,
            'participantModel' => $participantModel,
            'eventSpans' => $eventSpans,
            'activityAreas' => ActivityArea::find()->where(['parent_id' => null])->indexBy('id')->all(),
            'organizationProfiles' => OrganizationProfile::find()->all(),
            'organizationTypes' => OrganizationType::find()->all(),
            'supervisors' => Supervisor::find()->all(),
            'countries' => $countries,
            'accompanyingEvents' => $accompanyingEvents,
        ]);
    }
}