id_producer); if (!$producer) { $this->addError($attribute, 'Ce producteur n\'existe pas.'); } $userProducerExist = UserProducer::searchOne([ 'id_user' => Yii::$app->user->identity->id, 'active' => 1 ]) ; if ($userProducerExist) { $this->addError($attribute, 'Ce producteur est déjà sur votre tableau de bord.'); } }], ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { $producer = Producer::findOne($this->id_producer); if ($producer) { return strlen($producer->code); } else { return false; } }], ['code', function($attribute, $params) { $code = $this->$attribute; $producer = Producer::findOne($this->id_producer); if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { $this->addError($attribute, 'Code incorrect'); } }], ]; } /** * @inheritdoc */ public function attributeLabels() { return [ 'id_producer' => 'Producteur', 'code' => 'Code', ]; } /** * Sends an email to the specified email address using the information collected by this model. * * @param string $email the target email address * @return boolean whether the email was sent */ public function add() { $producer = Producer::findOne($this->id_producer); $userProducerExist = UserProducer::searchOne([ 'id_user' => User::getCurrentId(), 'active' => 0 ]) ; if ($userProducerExist) { $userProducerExist->active = 1; $userProducerExist->save(); } else { $userProducer = new UserProducer(); $userProducer->id_user = User::getCurrentId(); $userProducer->id_producer = $this->id_producr; $userProducer->credit = 0; $userProducer->actif = 1; $userProducer->save(); } Yii::$app->session->setFlash('success', 'Le producteur ' . Html::encode($producer->name) . ' a bien été ajoutée à votre tableau de bord.'); } }