<?php

namespace common\modules\event\models;

use common\components\RoleDefinition;
use common\models\Event;
use common\models\Trainer;

class EventCreate extends Event
{

    public function rules()
    {
        $basicRules = parent::rules();
        $basicRules[] = [
            ['id_trainer']   ,'validateTrainers'
        ];
        return $basicRules;
    }


    public function validateTrainers($attribute, $params){

        $query = Trainer::find();
        if (RoleDefinition::isAdmin() == false) {
            $query = $query->innerJoinWith('userTrainerAssignments')
                ->andWhere(
                    [
                        'user_trainer_assignment.id_user' => \Yii::$app->user->id
                    ]
                );
        }
       $trainers = $query->all();

       $trainerAllowed = false;
       foreach ($trainers as $trainer){
            if ( $trainer->id == $this->id_trainer){
                $trainerAllowed = true;
            }
       }

       if ( !$trainerAllowed ){
           $this->addError($attribute,"Hibás paraméter: edző");
       }

    }


}
