- <?php
-
-
-
- namespace frontend\forms;
-
- use domain\Config\TaxRate\TaxRate;
- use domain\Producer\Producer\Producer;
- use domain\Producer\Producer\ProducerModule;
- use domain\User\User\User;
- use domain\User\User\UserModule;
- use domain\User\UserGroup\UserGroupModule;
- use yii\base\Model;
-
-
- class SignupForm extends Model
- {
- public $signup_producer_only = false;
- public $username;
- public $email;
- public $password;
- public $name;
- public $lastname;
- public $phone;
- public $is_producer;
- public $siret;
- public $name_producer;
- public $postcode;
- public $city;
- public $id_producer;
- public $option_user_producer;
- public $cgv;
- public $code;
- public $type;
-
- public $id_tax_rate_default;
- public $verifyCode;
- public $is_test;
- public $newsletter = false;
- public $newsletter_souke = false;
- public $sponsored_by = false;
-
-
-
- public function rules()
- {
- return [
- ['email', 'filter', 'filter' => 'trim'],
- ['email', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
- return !$model->signup_producer_only;
- }],
- ['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', 'when' => function($model) {
- return !$model->signup_producer_only;
- }],
- [['name', 'lastname', 'phone', 'option_user_producer'], 'string', 'min' => 2, 'max' => 255],
- ['password', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
- return !$model->signup_producer_only;
- }],
- ['password', 'string', 'min' => 6, 'tooShort' => 'Votre mot de passe doit contenir au moins 6 caractères'],
- [['is_producer', 'newsletter', 'newsletter_souke'], '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';
- }],
- ['id_tax_rate_default', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) {
- return $model->option_user_producer == 'producer';
- }],
- ['city', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) {
- return $model->option_user_producer == 'producer';
- }],
- [['name_producer', 'type', 'siret', 'is_test'], 'string'],
- ['name_producer', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) {
- return $model->option_user_producer == 'producer';
- }],
- ['type', 'required', 'message' => 'Champs obligatoire', 'when' => function ($model) {
- return $model->option_user_producer == 'producer';
- }],
- ['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;
- }
-
-
-
-
-
-
- $sum = 0;
- for ($index = 0; $index < 14; $index++) {
- $number = (int)$siret[$index];
- if (($index % 2) == 0) {
- if (($number *= 2) > 9)
- $number -= 9;
- }
- $sum += $number;
- }
-
-
- 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';
- }],
- ['sponsored_by', 'safe'],
- ['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');
- }
- }],
-
- ['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',
- 'id_tax_rate_default' => 'TVA par défaut',
- 'verifyCode' => 'Code de vérification',
- 'newsletter' => "S'abonner à l'infolettre de ce producteur",
- 'newsletter_souke' => "S'abonner à l'infolettre de Souke",
- ];
- }
-
-
-
- public function signup($user = null)
- {
- $userGroupModule = UserGroupModule::getInstance();
- $userModule = UserModule::getInstance();
- $producerModule = \Yii::$app->logic->getProducerModule();
-
- if ($this->validate()) {
-
- $sendEmailSignup = false;
- if(!$user) {
- $sendEmailSignup = true;
- $user = $userModule->instanciateUser(User::TYPE_INDIVIDUAL);
- $this->populateUser($user);
- }
-
- if ($this->isProducer()) {
- $producer = $this->processFormProducer($user);
- $idProducer = $producer->id;
- } else {
- $idProducer = $this->id_producer;
- }
-
- $producer = $producerModule->findOneProducerById($idProducer);
-
- if ($user->save() && $producer) {
- \Yii::$app->logic->setProducerContext($producer);
-
- if(!$this->isProducer()) {
- if($producer->id_user_group_default) {
- $userGroupDefault = $userGroupModule->getRepository()->findOneUserGroupById($producer->id_user_group_default);
- $userModule->getBuilder()->addUserGroup($user, $userGroupDefault);
- }
- }
- $producerModule->addUser($user, $producer, true, $this->newsletter);
- if($sendEmailSignup) {
- $userModule->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)
- {
- $userModule = UserModule::getInstance();
- $producerModule = ProducerModule::getInstance();
-
-
- $producer = $producerModule->instanciateProducer();
- $this->populateProducer($producer);
- $producerModule->initProducer($producer);
- $producer->save();
-
-
- if($this->sponsored_by) {
- $sponsorshipProducerSponsor = $producerModule->getRepository()->findOneProducerById($this->sponsored_by);
- if($sponsorshipProducerSponsor) {
- $producer->sponsored_by = $this->sponsored_by;
- $producer->save();
- }
- }
-
-
-
- $producerModule->sendEmailNewProducer($producer);
-
-
- $userModule->initProducer($user, $producer);
-
- return $producer;
- }
-
- public function populateUser($user)
- {
- $userModule = UserModule::getInstance();
- $userModule->initPassword($user, $this->password);
-
- $user->username = $this->email;
- $user->email = $this->email;
- $user->name = $this->name;
- $user->lastname = $this->lastname;
- $user->phone = $this->phone;
- $user->newsletter_souke = $this->newsletter_souke;
- }
-
- 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;
- }
- }
|