'trim'], ['email', 'required', 'message' => 'Champs obligatoire'], ['email', 'email'], ['email', 'unique', 'targetClass' => '\common\models\User', 'message' => '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') ; }"], ['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', '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', 'string'], ['type', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { return $model->option_user_producer == 'producer'; }, 'whenClient' => "function (attribute, value) { return $('#option-producer').prop('checked') ; }"], ['siret', 'string'], ['siret', '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.'); } } }], ['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'], ]; } 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 du producteur', 'siret' => 'Numéro SIRET', 'postcode' => 'Code postal', 'city' => 'Commune', 'id_producer' => 'Producteur', 'type' => 'Type de producteur', 'free_price' => 'Prix libre' ]; } /** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->email; $user->email = $this->email; $user->name = $this->name; $user->lastname = $this->lastname; $user->phone = $this->phone; if ($this->option_user_producer == 'producer') { // etablissement $producer = new Producer; $producer->name = $this->name_producer; $producer->type = $this->type; $producer->siret = $this->siret; $producer->postcode = $this->postcode; $producer->city = $this->city; $producer->order_deadline = 20; $producer->order_delay = 1; $producer->free_price = (float) abs($this->free_price); $producer->slug = \common\helpers\Url::slugify($this->name_producer) ; // génération d'un code do { $code = Password::generate(); $producer->code = $code; } while (Producer::findOne(['code' => $code])); $producer->save(); // user $user->id_producer = $producer->id; $user->status = User::STATUS_PRODUCER; // envoi d'un email à l'administrateur pour le prévenir Yii::$app->mailer->compose( [ 'html' => 'new-producer-html', 'text' => 'new-producer-text' ], [ 'producer' => $producer ]) ->setTo(Yii::$app->params['adminEmail']) ->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) ->setSubject('[La boîte à pain] Nouveau producteur') ->send(); } $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { // on ajoute l'établissement sélectionnée par l'user en favoris if ($this->option_user_producer == 'producer') { if ($this->id_producer) { $producer = Producer::find()->where(['id' => $this->id_producer])->one(); if ($producer) { Producer::addUser($user->id, $this->id_producer) ; // envoi d'un email à l'utilisateur Yii::$app->mailer->compose( [ 'html' => 'signup-html', 'text' => 'signup-text' ], [ 'user' => $user, 'producer' => $producer ]) ->setTo($user->email) ->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) ->setSubject('[La boîte à pain] Inscription') ->send(); } } } if ($this->option_user_producer == 'producer') { $userProducer = new UserProducer; $userProducer->id_producer = $producer->id; $userProducer->id_user = $user->id; $userProducer->credit = 0; $userProducer->active = 1; $userProducer->save(); } return $user; } } return null; } }