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 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 60
UpdateAction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
420.00
0.00% covered (danger)
0.00%
0 / 60
 run
0.00% covered (danger)
0.00%
0 / 1
342.00
0.00% covered (danger)
0.00%
0 / 53
 findUserParticipantModel
0.00% covered (danger)
0.00%
0 / 1
6.00
0.00% covered (danger)
0.00%
0 / 7
<?php namespace frontend\controllers\organization;
use Yii;
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\helpers\ArrayHelper;
use yii\web\UploadedFile;
use yii\web\ForbiddenHttpException;
use yii\web\NotFoundHttpException;
use yii\web\BadRequestHttpException;
/**
 * Update a Organization model.
 * If creation is successful, the success flash will be shown.
 * @return mixed
 */
class UpdateAction extends Action
{
    public function run()
    {
        $participantModel = $this->findUserParticipantModel(Yii::$app->user->id);
        if(!Yii::$app->user->can('updateParticipant', ['participant' => $participantModel]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.').' '.Yii::t('app', 'You can not update not own principal account.'));
        $organizationModel = $participantModel->organization;
        if(!Yii::$app->user->can('updateOrganization', ['organization' => $organizationModel]))
            throw new ForbiddenHttpException(Yii::t('app', 'You are not allowed to perform this action.').' '.Yii::t('app', 'You can not update not own organization account.'));
        $isOrganizationChanged = $organizationModel->load(Yii::$app->request->post()) && $organizationModel->save();
        $isParticipantChanged = $participantModel->load(Yii::$app->request->post()) && $participantModel->save();
        if ($isOrganizationChanged || $isParticipantChanged)
        {
            $lastOrganizationImage = $organizationModel->image;
            $organizationModel->scenario = 'updateImage';
            $organizationModel->image = UploadedFile::getInstance($organizationModel, 'image');
            if ($organizationModel->image && $organizationModel->validate()) {
                if(is_file($lastOrganizationImage)) unlink($lastOrganizationImage);
                $path = 'uploads/' . md5(microtime()) . 'c.' . $organizationModel->image->extension;
                if($organizationModel->image->saveAs($path)){
                    $organizationModel->image = $path;
                    $organizationModel->save();
                }
            }
            $lastParticipantImage = $participantModel->image;
            $participantModel->scenario = 'updateImage';
            $participantModel->image = UploadedFile::getInstance($participantModel, 'image');
            if ($participantModel->image && $participantModel->validate()) {
                if(is_file($lastParticipantImage)) unlink($lastParticipantImage);
                $path = 'uploads/' . md5(microtime()) . 'e.' . $participantModel->image->extension;
                if($participantModel->image->saveAs($path)){
                    $participantModel->image = $path;
                    $participantModel->save();
                }
            }
            Yii::$app->getSession()->setFlash('success', Yii::t('app', 'Organization was changed'));
        }
        $event = Event::find()->active()->one();
        $events = empty($event) ? [] : [$event];
        $eventSpans = empty($events) ? [] : $event->getEventSpans()->orderBy('date, time_from')->all();
        $accompanyingEvents = empty($events) ? [] : AccompanyingEvent::find()->availableForEvent($event->id)->active()->orderBy('date, time')->all();
        $countries = Country::find()->select(['code', 'name'])->orderBy('name')->asArray()->all();
        return $this->controller->render('update', [
            'organizationModel' => $organizationModel,
            'participantModel' => $participantModel,
            'events' => $events,
            'eventSpans' => $eventSpans,
            'activityAreas' => ActivityArea::find()->all(),
            'organizationProfiles' => OrganizationProfile::find()->all(),
            'organizationTypes' => OrganizationType::find()->all(),
            'supervisors' => Supervisor::find()->all(),
            'countries' => ArrayHelper::map($countries, 'code', 'name'),
            'accompanyingEvents' => $accompanyingEvents,
        ]);
    }
    /**
     * Finds the Participant model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $user_id
     * @return Participant the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findUserParticipantModel($user_id)
    {
        if (($model = Participant::findOne(['user_id' => $user_id])) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}