'trim'], ['email', 'required', 'message' => 'Champs obligatoire'], ['email', 'email'], ['email', function ($attribute, $params) { $email = $this->$attribute; $userExist = User::searchOne([ 'type' => 'individual', 'email' => $email ]); if ($userExist) { $this->addError($attribute, 'Cet email est déjà utilisé.'); } }], [['name', 'lastname', 'phone'], 'required', 'message' => 'Champs obligatoire'], [['name', 'lastname', 'phone', 'option_user_producer'], 'string', 'min' => 2, 'max' => 255], ['password', 'required', 'message' => 'Champs obligatoire'], ['password', 'string', 'min' => 6, 'tooShort' => 'Votre mot de passe doit contenir au moins 6 caractères'], ['is_producer', 'boolean'], ['cgv', 'boolean'], ['cgv', function ($attribute, $params) { $cgv = $this->$attribute; if ($this->option_user_producer == 'producer' && !$cgv) { $this->addError($attribute, 'Vous devez accepter les conditions générales de vente pour vous inscrire.'); } }], ['postcode', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { return $model->option_user_producer == 'producer'; }, 'whenClient' => "function (attribute, value) { return $('#option-producer').prop('checked') ; }"], ['id_tax_rate_default', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { return $model->option_user_producer == 'producer'; }, 'whenClient' => "function (attribute, value) { return $('#option-producer').prop('checked') ; }"], ['city', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { return $model->option_user_producer == 'producer'; }, 'whenClient' => "function (attribute, value) { return $('#option-producer').prop('checked') ; }"], [['name_producer', 'type', 'siret', 'is_test'], 'string'], ['name_producer', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { return $model->option_user_producer == 'producer'; }, 'whenClient' => "function (attribute, value) { return $('#option-producer').prop('checked') ; }"], ['type', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { return $model->option_user_producer == 'producer'; }, 'whenClient' => "function (attribute, value) { return $('#option-producer').prop('checked') ; }"], ['siret', function ($attribute, $params) { $siret = $this->$attribute; $siret = str_replace(' ', '', $siret); if (strlen($siret) != 14) { $this->addError($attribute, 'Le numéro SIRET doit contenir 14 caractères'); return; } if (!is_numeric($siret)) { $this->addError($attribute, 'Le numéro SIRET ne doit contenir que des chiffres'); return; } // on prend chaque chiffre un par un // si son index (position dans la chaîne en commence à 0 au premier caractère) est pair // on double sa valeur et si cette dernière est supérieure à 9, on lui retranche 9 // on ajoute cette valeur à la somme totale $sum = 0; for ($index = 0; $index < 14; $index++) { $number = (int)$siret[$index]; if (($index % 2) == 0) { if (($number *= 2) > 9) $number -= 9; } $sum += $number; } // le numéro est valide si la somme des chiffres est multiple de 10 if (($sum % 10) != 0) $this->addError($attribute, 'Numéro SIRET invalide'); }], ['id_producer', 'integer'], ['id_producer', function ($attribute, $params) { if ($this->id_producer) { $producer = Producer::findOne($this->id_producer); if (!$producer) { $this->addError($attribute, 'Ce producteur n\'existe pas.'); } } }], ['id_producer', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) { return $this->option_user_producer == 'user'; }], ['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'); } }], //['free_price', 'number'], ['id_tax_rate_default', 'exist', 'targetClass' => TaxRate::className(), 'targetAttribute' => ['id_tax_rate_default' => 'id']], ['verifyCode', 'captcha', 'message' => 'Veuillez recopier le code de vérification', 'when' => function($model) { return $model->is_test != 'isTest'; }], ]; } public function attributeLabels() { return [ 'id' => 'ID', 'username' => 'Identifiant', 'password' => 'Mot de passe', 'rememberMe' => 'Se souvenir de moi', 'name' => 'Prénom', 'lastname' => 'Nom', 'phone' => 'Téléphone', 'is_producer' => "Je suis professionnel et souhaite mettre en place un système de réservation dans mon établissement", 'name_producer' => 'Nom de votre structure', 'siret' => 'Numéro SIRET', 'postcode' => 'Code postal', 'city' => 'Commune', 'id_producer' => 'Producteur', 'type' => 'Type de producteur', // 'free_price' => 'Prix libre', 'id_tax_rate_default' => 'TVA par défaut', 'verifyCode' => 'Code de vérification', ]; } /** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { $userManager = \Yii::$app->logic->getUserManager(); $producerManager = \Yii::$app->logic->getProducerManager(); if ($this->validate()) { $user = $userManager->instanciateUser(); $this->populateUser($user); if ($this->isProducer()) { $producer = $this->processFormProducer($user); $idProducer = $producer->id; } else { $idProducer = $this->id_producer; } $producer = $producerManager->findOneProducerById($idProducer); if ($user->save() && $producer) { // Liaison User / Producer $producerManager->addUser($user, $producer); // Envoi d'un email de bienvenue à l'utilisateur if ($this->isCustomer()) { $userManager->sendEmailSignup($user, $producer); } return $user; } } return null; } public function isCustomer() { return $this->option_user_producer == 'user'; } public function isProducer() { return $this->option_user_producer == 'producer'; } public function processFormProducer($user) { $userManager = UserManager::getInstance(); $producerManager = ProducerManager::getInstance(); // Création du producteur $producer = $producerManager->instanciateProducer(); $this->populateProducer($producer); $producerManager->initProducer($producer); $producer->save(); /* * Envoi d'un email à l'administrateur pour le prévenir * qu'un nouveau producteur s'est inscrit */ $producerManager->sendEmailNewProducer($producer); // Initialisation de l'utilisateur (statut et liaison au producteur) $userManager->initProducer($user, $producer); return $producer; } public function populateUser($user) { $userManager = UserManager::getInstance(); $userManager->initPassword($user, $this->password); $user->username = $this->email; $user->email = $this->email; $user->name = $this->name; $user->lastname = $this->lastname; $user->phone = $this->phone; } public function populateProducer($producer) { $producer->name = $this->name_producer; $producer->type = $this->type; $producer->siret = $this->siret; $producer->postcode = $this->postcode; $producer->city = $this->city; $producer->id_tax_rate_default = $this->id_tax_rate_default; } }