|
|
@@ -40,9 +40,9 @@ namespace frontend\models; |
|
|
|
|
|
|
|
use Yii; |
|
|
|
use common\models\User; |
|
|
|
use common\models\Etablissement; |
|
|
|
use common\models\Producer; |
|
|
|
use yii\base\Model; |
|
|
|
use common\models\UserEtablissement; |
|
|
|
use common\models\UserProducer; |
|
|
|
use common\helpers\Password; |
|
|
|
|
|
|
|
/** |
|
|
@@ -59,15 +59,15 @@ class SignupForm extends Model |
|
|
|
public $phone; |
|
|
|
public $is_producer; |
|
|
|
public $siret; |
|
|
|
public $nom_magasin; |
|
|
|
public $code_postal; |
|
|
|
public $ville; |
|
|
|
public $id_etablissement; |
|
|
|
public $option_client_boulanger; |
|
|
|
public $name_producer; |
|
|
|
public $postcode; |
|
|
|
public $city; |
|
|
|
public $id_producer; |
|
|
|
public $option_user_producer; |
|
|
|
public $cgv; |
|
|
|
public $code; |
|
|
|
public $type; |
|
|
|
public $prix_libre ; |
|
|
|
public $free_price ; |
|
|
|
|
|
|
|
/** |
|
|
|
* @inheritdoc |
|
|
@@ -79,46 +79,46 @@ class SignupForm extends Model |
|
|
|
['email', 'required', 'message' => 'Champs obligatoire'], |
|
|
|
['email', 'email'], |
|
|
|
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'Cet email est déjà utilisé'], |
|
|
|
[['nom', 'prenom', 'telephone'], 'required', 'message' => 'Champs obligatoire'], |
|
|
|
[['nom', 'prenom', 'telephone', 'option_client_boulanger'], 'string', 'min' => 2, 'max' => 255], |
|
|
|
[['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_boulanger', 'boolean'], |
|
|
|
['is_producer', 'boolean'], |
|
|
|
['cgv', 'boolean'], |
|
|
|
['cgv', function($attribute, $params) { |
|
|
|
$cgv = $this->$attribute; |
|
|
|
|
|
|
|
if ($this->option_client_boulanger == 'boulanger' && !$cgv) { |
|
|
|
if ($this->option_user_producer == 'producer' && !$cgv) { |
|
|
|
$this->addError($attribute, 'Vous devez accepter les conditions générales de vente pour vous inscrire.'); |
|
|
|
} |
|
|
|
}], |
|
|
|
['code_postal', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
['postcode', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
|
}"], |
|
|
|
['ville', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
['city', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
|
}"], |
|
|
|
['nom_magasin', 'string'], |
|
|
|
['nom_magasin', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
['name_producer', 'string'], |
|
|
|
['name_producer', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
|
}"], |
|
|
|
['type', 'string'], |
|
|
|
['type', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
|
}"], |
|
|
|
['siret', 'string'], |
|
|
|
['siret', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { |
|
|
|
return $model->option_client_boulanger == 'boulanger'; |
|
|
|
return $model->option_user_producer == 'producer'; |
|
|
|
}, 'whenClient' => "function (attribute, value) { |
|
|
|
return $('#option-boulanger').prop('checked') ; |
|
|
|
return $('#option-producer').prop('checked') ; |
|
|
|
}"], |
|
|
|
['siret', function($attribute, $params) { |
|
|
|
|
|
|
@@ -154,32 +154,32 @@ class SignupForm extends Model |
|
|
|
if (($sum % 10) != 0) |
|
|
|
$this->addError($attribute, 'Numéro SIRET invalide'); |
|
|
|
}], |
|
|
|
['id_etablissement', 'integer'], |
|
|
|
['id_etablissement', function($attribute, $params) { |
|
|
|
if ($this->id_etablissement) { |
|
|
|
$etablissement = Etablissement::findOne($this->id_etablissement); |
|
|
|
if (!$etablissement) { |
|
|
|
['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) { |
|
|
|
$etablissement = Etablissement::findOne($this->id_etablissement); |
|
|
|
if ($etablissement) { |
|
|
|
return strlen($etablissement->code); |
|
|
|
$producer = Producer::findOne($this->id_producer); |
|
|
|
if ($producer) { |
|
|
|
return strlen($producer->code); |
|
|
|
} else { |
|
|
|
return false; |
|
|
|
} |
|
|
|
}], |
|
|
|
['code', function($attribute, $params) { |
|
|
|
$code = $this->$attribute; |
|
|
|
$etablissement = Etablissement::findOne($this->id_etablissement); |
|
|
|
$producer = Producer::findOne($this->id_producer); |
|
|
|
|
|
|
|
if ($etablissement && strtolower(trim($code)) != strtolower(trim($etablissement->code))) { |
|
|
|
if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { |
|
|
|
$this->addError($attribute, 'Code incorrect'); |
|
|
|
} |
|
|
|
}], |
|
|
|
['prix_libre', 'number'], |
|
|
|
['free_price', 'number'], |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
@@ -190,16 +190,17 @@ class SignupForm extends Model |
|
|
|
'username' => 'Identifiant', |
|
|
|
'password' => 'Mot de passe', |
|
|
|
'rememberMe' => 'Se souvenir de moi', |
|
|
|
'prenom' => 'Prénom', |
|
|
|
'telephone' => 'Téléphone', |
|
|
|
'is_boulanger' => "Je suis professionnel et souhaite mettre en place un système de réservation dans mon établissement", |
|
|
|
'nom_magasin' => 'Nom de l\'établissement', |
|
|
|
'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', |
|
|
|
'code_postal' => 'Code postal', |
|
|
|
'ville' => 'Commune', |
|
|
|
'id_etablissement' => 'Producteur', |
|
|
|
'type' => 'Type d\'établissement', |
|
|
|
'prix_libre' => 'Prix libre' |
|
|
|
'postcode' => 'Code postal', |
|
|
|
'city' => 'Commune', |
|
|
|
'id_producer' => 'Producteur', |
|
|
|
'type' => 'Type de producteur', |
|
|
|
'free_price' => 'Prix libre' |
|
|
|
]; |
|
|
|
} |
|
|
|
|
|
|
@@ -214,44 +215,42 @@ class SignupForm extends Model |
|
|
|
$user = new User(); |
|
|
|
$user->username = $this->email; |
|
|
|
$user->email = $this->email; |
|
|
|
$user->nom = $this->nom; |
|
|
|
$user->prenom = $this->prenom; |
|
|
|
$user->telephone = $this->telephone; |
|
|
|
$user->confiance = 1; |
|
|
|
$user->name = $this->name; |
|
|
|
$user->lastname = $this->lastname; |
|
|
|
$user->phone = $this->phone; |
|
|
|
|
|
|
|
if ($this->option_client_boulanger == 'boulanger') { |
|
|
|
if ($this->option_user_producer == 'producer') { |
|
|
|
// etablissement |
|
|
|
$etablissement = new Etablissement; |
|
|
|
$etablissement->nom = $this->nom_magasin; |
|
|
|
$etablissement->type = $this->type; |
|
|
|
$etablissement->siret = $this->siret; |
|
|
|
$etablissement->code_postal = $this->code_postal; |
|
|
|
$etablissement->ville = $this->ville; |
|
|
|
$etablissement->heure_limite_commande = 20; |
|
|
|
$etablissement->delai_commande = 1; |
|
|
|
$etablissement->gratuit = 1; |
|
|
|
$etablissement->prix_libre = (float) abs($this->prix_libre); |
|
|
|
$etablissement->slug = \common\helpers\Url::slugify($this->nom_magasin) ; |
|
|
|
$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(); |
|
|
|
$etablissement->code = $code; |
|
|
|
} while (Etablissement::findOne(['code' => $code])); |
|
|
|
$producer->code = $code; |
|
|
|
} while (Producer::findOne(['code' => $code])); |
|
|
|
|
|
|
|
$etablissement->save(); |
|
|
|
$producer->save(); |
|
|
|
|
|
|
|
// user |
|
|
|
$user->id_etablissement = $etablissement->id; |
|
|
|
$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-boulanger-html', |
|
|
|
'text' => 'new-boulanger-text' |
|
|
|
'html' => 'new-producer-html', |
|
|
|
'text' => 'new-producer-text' |
|
|
|
], [ |
|
|
|
'etablissement' => $etablissement |
|
|
|
'producer' => $producer |
|
|
|
]) |
|
|
|
->setTo(Yii::$app->params['adminEmail']) |
|
|
|
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) |
|
|
@@ -264,11 +263,11 @@ class SignupForm extends Model |
|
|
|
if ($user->save()) { |
|
|
|
|
|
|
|
// on ajoute l'établissement sélectionnée par l'user en favoris |
|
|
|
if ($this->option_client_boulanger == 'client') { |
|
|
|
if ($this->id_etablissement) { |
|
|
|
$etablissement = Etablissement::find()->where(['id' => $this->id_etablissement])->one(); |
|
|
|
if ($etablissement) { |
|
|
|
Etablissement::addUser($user->id, $this->id_etablissement) ; |
|
|
|
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( |
|
|
@@ -278,7 +277,7 @@ class SignupForm extends Model |
|
|
|
], |
|
|
|
[ |
|
|
|
'user' => $user, |
|
|
|
'etablissement' => $etablissement |
|
|
|
'producer' => $producer |
|
|
|
]) |
|
|
|
->setTo($user->email) |
|
|
|
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) |
|
|
@@ -288,13 +287,13 @@ class SignupForm extends Model |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if ($this->option_client_boulanger == 'boulanger') { |
|
|
|
$etab_user = new UserEtablissement; |
|
|
|
$etab_user->id_etablissement = $etablissement->id; |
|
|
|
$etab_user->id_user = $user->id; |
|
|
|
$etab_user->credit = 0; |
|
|
|
$etab_user->actif = 1; |
|
|
|
$etab_user->save(); |
|
|
|
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; |