return number_format($this->prix_libre, 2, ',', false) . ' €'; | return number_format($this->prix_libre, 2, ',', false) . ' €'; | ||||
} | } | ||||
} | } | ||||
public static function addUser($id_user, $id_producer) { | |||||
$user_producer = UserEtablissement::find() | |||||
->where([ | |||||
'id_user' => $id_user, | |||||
'id_etablissement' => $id_producer | |||||
])->one(); | |||||
if (!$user_producer) { | |||||
$new_user_producer = new UserEtablissement; | |||||
$new_user_producer->id_etablissement = $id_producer; | |||||
$new_user_producer->id_user = $id_user; | |||||
$new_user_producer->credit = 0; | |||||
$new_user_producer->actif = 1; | |||||
$new_user_producer->save(); | |||||
} else { | |||||
if (!$user_producer->actif) { | |||||
$user_producer->actif = 1; | |||||
$user_producer->save(); | |||||
} | |||||
} | |||||
} | |||||
} | } | ||||
public $rememberMe = true; | public $rememberMe = true; | ||||
public $email; | public $email; | ||||
private $_user = false; | private $_user = false; | ||||
public $code ; | |||||
public $id_etablissement ; | |||||
/** | /** | ||||
* @inheritdoc | * @inheritdoc | ||||
['rememberMe', 'boolean'], | ['rememberMe', 'boolean'], | ||||
// password is validated by validatePassword() | // password is validated by validatePassword() | ||||
['password', 'validatePassword'], | ['password', 'validatePassword'], | ||||
['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { | |||||
$etablissement = Etablissement::findOne($this->id_etablissement); | |||||
if ($etablissement) { | |||||
return strlen($etablissement->code); | |||||
} else { | |||||
return false; | |||||
} | |||||
}], | |||||
['code', function($attribute, $params) { | |||||
$code = $this->$attribute; | |||||
$etablissement = Etablissement::findOne($this->id_etablissement); | |||||
if ($etablissement && strtolower(trim($code)) != strtolower(trim($etablissement->code))) { | |||||
$this->addError($attribute, 'Code incorrect'); | |||||
} | |||||
}], | |||||
['id_etablissement', 'integer'], | |||||
['id_etablissement', function($attribute, $params) { | |||||
if ($this->id_etablissement) { | |||||
$etablissement = Etablissement::findOne($this->id_etablissement); | |||||
if (!$etablissement) { | |||||
$this->addError($attribute, 'Ce producteur n\'existe pas.'); | |||||
} | |||||
} | |||||
}], | |||||
]; | ]; | ||||
} | } | ||||
{ | { | ||||
$exception = Yii::$app->errorHandler->exception; | $exception = Yii::$app->errorHandler->exception; | ||||
if ($exception !== null) { | |||||
return $this->render('error', ['exception' => $exception]); | |||||
if($exception->getMessage() == 'Établissement introuvable' || Yii::$app->getRequest()->getQueryParam('producer_not_found')) { | |||||
return $this->render('error-404-producer', ['exception' => $exception]); | |||||
} | } | ||||
if( Yii::$app->getRequest()->getQueryParam('producer_not_found')) { | |||||
return $this->render('error-404-producer', ['exception' => $exception]); | |||||
if ($exception !== null) { | |||||
return $this->render('error', ['exception' => $exception]); | |||||
} | } | ||||
} | } | ||||
public function actionCreditpain() { | public function actionCreditpain() { | ||||
return $this->render('creditpain'); | return $this->render('creditpain'); | ||||
} | } | ||||
public function actionProducerCode($id) { | |||||
$producer = Etablissement::findOne($id); | |||||
if(!$producer) | |||||
throw new \yii\web\HttpException(404, 'Établissement introuvable'); | |||||
$model_producer_code = new ProducerCodeForm ; | |||||
$model_producer_code->id_producer = $id ; | |||||
if($model_producer_code->load(Yii::$app->request->post()) && $model_producer_code->validate()) { | |||||
Etablissement::addUser(Yii::$app->user->id, $id) ; | |||||
$this->redirect(Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index','slug_producer' => $producer->slug])); | |||||
} | |||||
return $this->render('producer_code',[ | |||||
'producer' => $producer, | |||||
'model_producer_code' => $model_producer_code, | |||||
]) ; | |||||
} | |||||
public function actionEtablissement($id_etablissement) { | |||||
public function actionProducer($id) { | |||||
$model_login = new LoginForm(); | $model_login = new LoginForm(); | ||||
$model_signup = new SignupForm(); | $model_signup = new SignupForm(); | ||||
$etablissement = Etablissement::findOne($id_etablissement); | |||||
$etablissement = Etablissement::findOne($id); | |||||
$model_login->id_etablissement = $id; | |||||
$model_signup->id_etablissement = $id; | |||||
$model_signup->option_client_boulanger = 'client'; | |||||
$url_redirect = Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index','slug_producer' => $etablissement->slug]) ; | |||||
if (Yii::$app->user->isGuest) { | if (Yii::$app->user->isGuest) { | ||||
if ($model_login->load(Yii::$app->request->post()) && $model_login->login()) { | if ($model_login->load(Yii::$app->request->post()) && $model_login->login()) { | ||||
// ajout de l'établissement en favoris si ce n'est pas encore le cas | |||||
$user_etablissement_exist = UserEtablissement::find() | |||||
->where([ | |||||
'id_user' => Yii::$app->user->id, | |||||
'id_etablissement' => $id_etablissement | |||||
])->one(); | |||||
if (!$user_etablissement_exist) { | |||||
$etab_user = new UserEtablissement; | |||||
$etab_user->id_etablissement = $id_etablissement; | |||||
$etab_user->id_user = Yii::$app->user->id; | |||||
$etab_user->credit = 0; | |||||
$etab_user->actif = 1; | |||||
$etab_user->save(); | |||||
} else { | |||||
if (!$user_etablissement_exist->actif) { | |||||
$user_etablissement_exist->actif = 1; | |||||
$user_etablissement_exist->save(); | |||||
} | |||||
} | |||||
$this->redirect(['commande/index']); | |||||
Etablissement::addUser(Yii::$app->user->id, $id) ; | |||||
$this->redirect($url_redirect); | |||||
} | } | ||||
if ($model_signup->load(Yii::$app->request->post())) { | if ($model_signup->load(Yii::$app->request->post())) { | ||||
$model_signup->id_etablissement = $id_etablissement; | |||||
$model_signup->option_client_boulanger = 'client'; | |||||
if ($user = $model_signup->signup()) { | if ($user = $model_signup->signup()) { | ||||
if (Yii::$app->getUser()->login($user)) { | if (Yii::$app->getUser()->login($user)) { | ||||
$this->redirect(['commande/index']); | |||||
$this->redirect($url_redirect); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
} else { | } else { | ||||
$this->redirect(['commande/create', 'id_etablissement' => $id_etablissement]); | |||||
$this->redirect($url_redirect); | |||||
} | } | ||||
return $this->render('etablissement', [ | |||||
return $this->render('producer', [ | |||||
'model_login' => $model_login, | 'model_login' => $model_login, | ||||
'model_signup' => $model_signup, | 'model_signup' => $model_signup, | ||||
'etablissement' => $etablissement, | 'etablissement' => $etablissement, |
<?php | |||||
namespace frontend\models; | |||||
use Yii; | |||||
use yii\base\Model; | |||||
/** | |||||
* Producer Code form | |||||
*/ | |||||
class ProducerCodeForm extends Model { | |||||
public $id_producer ; | |||||
public $code; | |||||
/** | |||||
* @inheritdoc | |||||
*/ | |||||
public function rules() { | |||||
return [ | |||||
['id_producer','required','message' => 'Champs obligatoire'], | |||||
['id_producer', 'integer'], | |||||
['id_producer', function($attribute, $params) { | |||||
if ($this->id_producer) { | |||||
$producer = Etablissement::findOne($this->id_producer); | |||||
if (!$producer) { | |||||
$this->addError($attribute, 'Ce producteur n\'existe pas.'); | |||||
} | |||||
} | |||||
}], | |||||
['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) { | |||||
$producer = Etablissement::findOne($this->id_producer); | |||||
if ($producer) { | |||||
return strlen($producer->code); | |||||
} else { | |||||
return false; | |||||
} | |||||
}], | |||||
['code', function($attribute, $params) { | |||||
$code = $this->$attribute; | |||||
$producer = Etablissement::findOne($this->id_producer); | |||||
if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) { | |||||
$this->addError($attribute, 'Code incorrect'); | |||||
} | |||||
}], | |||||
]; | |||||
} | |||||
public function attributeLabels() { | |||||
return [ | |||||
'id_producer' => 'Producteur', | |||||
'code' => 'Code de l\'établissement', | |||||
]; | |||||
} | |||||
} |
if ($this->id_etablissement) { | if ($this->id_etablissement) { | ||||
$etablissement = Etablissement::find()->where(['id' => $this->id_etablissement])->one(); | $etablissement = Etablissement::find()->where(['id' => $this->id_etablissement])->one(); | ||||
if ($etablissement) { | if ($etablissement) { | ||||
$etab_user = new UserEtablissement; | |||||
$etab_user->id_etablissement = $this->id_etablissement; | |||||
$etab_user->id_user = $user->id; | |||||
$etab_user->credit = 0; | |||||
$etab_user->actif = 1; | |||||
$etab_user->save(); | |||||
Etablissement::addUser($user->id, $this->id_etablissement) ; | |||||
// envoi d'un email à l'utilisateur | // envoi d'un email à l'utilisateur | ||||
Yii::$app->mailer->compose( | Yii::$app->mailer->compose( | ||||
[ | |||||
'html' => 'signup-html', | |||||
'text' => 'signup-text' | |||||
], [ | |||||
'user' => $user, | |||||
'etablissement' => $etablissement | |||||
]) | |||||
->setTo($user->email) | |||||
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) | |||||
->setSubject('[La boîte à pain] Inscription') | |||||
->send(); | |||||
[ | |||||
'html' => 'signup-html', | |||||
'text' => 'signup-text' | |||||
], | |||||
[ | |||||
'user' => $user, | |||||
'etablissement' => $etablissement | |||||
]) | |||||
->setTo($user->email) | |||||
->setFrom([Yii::$app->params['adminEmail'] => 'La boîte à pain']) | |||||
->setSubject('[La boîte à pain] Inscription') | |||||
->send(); | |||||
} | } | ||||
} | } | ||||
} | } |
/* @var $message string */ | /* @var $message string */ | ||||
/* @var $exception Exception */ | /* @var $exception Exception */ | ||||
$this->title = $name; | |||||
$this->title = $exception->getName(); | |||||
?> | ?> | ||||
<div class="site-error"> | <div class="site-error"> | ||||
<h1><?= Html::encode($this->title) ?></h1> | <h1><?= Html::encode($this->title) ?></h1> | ||||
<div class="alert alert-danger"> | <div class="alert alert-danger"> | ||||
<?= nl2br(Html::encode($message)) ?> | |||||
<?= nl2br(Html::encode($exception->getMessage())) ?> | |||||
</div> | </div> | ||||
<p><?= Html::a("Retour à l'accueil", ['site/index']) ?></p> | <p><?= Html::a("Retour à l'accueil", ['site/index']) ?></p> |
<p> | <p> | ||||
Si vous avez oublié votre mot de passe, vous pouvez le <?= Html::a('réinitialiser', ['site/request-password-reset']) ?>. | Si vous avez oublié votre mot de passe, vous pouvez le <?= Html::a('réinitialiser', ['site/request-password-reset']) ?>. | ||||
</p> | </p> | ||||
<?php if(strlen($etablissement->code)): ?> | |||||
<?= $form->field($model_login, 'code',[ | |||||
'inputTemplate' => '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>{input}</div>', | |||||
]) | |||||
->label('Code de l\'établissement') | |||||
->hint('Renseignez-vous auprès de votre producteur pour qu\'il vous fournisse le code d\'accès') ; ?> | |||||
<?php endif; ?> | |||||
<div class="form-group"> | <div class="form-group"> | ||||
<?= Html::submitButton('Connexion', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?> | <?= Html::submitButton('Connexion', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?> | ||||
</div> | </div> | ||||
<?= $form->field($model_signup, 'nom') ?> | <?= $form->field($model_signup, 'nom') ?> | ||||
<?= $form->field($model_signup, 'prenom') ?> | <?= $form->field($model_signup, 'prenom') ?> | ||||
<?= $form->field($model_signup, 'telephone') ?> | <?= $form->field($model_signup, 'telephone') ?> | ||||
<?php //$form->field($model, 'is_boulanger')->checkbox() ?> | |||||
<?php if(strlen($etablissement->code)): ?> | <?php if(strlen($etablissement->code)): ?> | ||||
<?= $form->field($model_signup, 'code',[ | <?= $form->field($model_signup, 'code',[ | ||||
'inputTemplate' => '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>{input}</div>', | 'inputTemplate' => '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>{input}</div>', | ||||
]) | ]) | ||||
->label('Code') | |||||
->label('Code de l\'établissement') | |||||
->hint('Renseignez-vous auprès de votre producteur pour qu\'il vous fournisse le code d\'accès') ; ?> | ->hint('Renseignez-vous auprès de votre producteur pour qu\'il vous fournisse le code d\'accès') ; ?> | ||||
<?php endif; ?> | <?php endif; ?> | ||||
<?php | |||||
use yii\bootstrap\ActiveForm; | |||||
$this->title = 'Producteur '.Html::encode($producer->nom) ; | |||||
?> | |||||
<div id="page-producer-code"> | |||||
<h1 class="title-systeme-commande"><?= Html::encode($producer->nom) ?></h1> | |||||
<p class="info"><span class="alert alert-warning">Cet établissement est protègé par un code d'accès.</span></p> | |||||
<?php $form = ActiveForm::begin(['id' => 'producer-code','enableClientValidation'=> false]); ?> | |||||
<?= $form->field($model_producer_code, 'code',[ | |||||
'inputTemplate' => '<div class="input-group"><span class="input-group-addon"><span class="glyphicon glyphicon-lock"></span></span>{input}</div>', | |||||
]) | |||||
->label('Code de l\'établissement') | |||||
->hint('Renseignez-vous auprès de votre producteur pour qu\'il vous fournisse le code d\'accès') ; ?> | |||||
<div class="form-group buttons"> | |||||
<?= Html::submitButton('Entrer', ['class' => 'btn btn-primary', 'name' => 'valider-button']) ?> | |||||
</div> | |||||
<?php ActiveForm::end(); ?> | |||||
</div> |
'label' => 'Lien', | 'label' => 'Lien', | ||||
'format' => 'raw', | 'format' => 'raw', | ||||
'value' => function($model) { | 'value' => function($model) { | ||||
return Html::a('Visiter',Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index', 'slug_producer' => $model->slug]), ['class'=>'btn btn-primary']) ; | |||||
$icon_lock = (strlen($model->code)) ? ' <span class="glyphicon glyphicon-lock"></span>' : '' ; | |||||
$html = Html::a('Visiter',Yii::$app->urlManagerProducer->createAbsoluteUrl(['site/index', 'slug_producer' => $model->slug]), ['class'=>'btn btn-primary']) ; | |||||
$html .= $icon_lock ; | |||||
return $html ; | |||||
} | } | ||||
], | ], | ||||
] | ] |
color: black; | color: black; | ||||
} | } | ||||
/* line 667, ../sass/_systeme_commandes.scss */ | |||||
#page-etablissement h1 { | |||||
/* line 668, ../sass/_systeme_commandes.scss */ | |||||
#page-etablissement h1, | |||||
#page-producer-code h1 { | |||||
font-size: 40px; | font-size: 40px; | ||||
} | } | ||||
/* line 670, ../sass/_systeme_commandes.scss */ | |||||
#page-etablissement .info { | |||||
/* line 671, ../sass/_systeme_commandes.scss */ | |||||
#page-etablissement .info, | |||||
#page-producer-code .info { | |||||
text-align: center; | |||||
} | |||||
/* line 677, ../sass/_systeme_commandes.scss */ | |||||
#page-producer-code form#producer-code { | |||||
max-width: 400px; | |||||
margin: 0px auto; | |||||
} | |||||
/* line 681, ../sass/_systeme_commandes.scss */ | |||||
#page-producer-code form#producer-code .form-group.buttons { | |||||
text-align: center; | text-align: center; | ||||
} | } | ||||
} | } | ||||
#page-etablissement { | |||||
#page-etablissement, | |||||
#page-producer-code { | |||||
h1 { | h1 { | ||||
font-size: 40px ; | font-size: 40px ; | ||||
} | } | ||||
.info { | .info { | ||||
text-align: center ; | text-align: center ; | ||||
} | } | ||||
} | |||||
#page-producer-code { | |||||
form#producer-code { | |||||
max-width: 400px ; | |||||
margin: 0px auto ; | |||||
.form-group.buttons { | |||||
text-align: center ; | |||||
} | |||||
} | |||||
} | } |
return []; | return []; | ||||
} | } | ||||
public function beforeAction($event) { | |||||
$producer = $this->getProducer() ; | |||||
/* | |||||
* Producteur protègé par un code | |||||
*/ | |||||
if(strlen($producer->code)) { | |||||
// Si l'utilisateur n'est pas connecté, on le redirige vers une page qui lui permet | |||||
// de se connecter ou de s'inscrire en saisissant le code du producteur | |||||
if(Yii::$app->user->isGuest) { | |||||
$this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ; | |||||
} | |||||
// si l'utilisateur est connecté et qu'il n'a pas encore saisi de code | |||||
else { | |||||
$user_etablissement = UserEtablissement::find() | |||||
->where([ | |||||
'id_user' => Yii::$app->user->id, | |||||
'id_etablissement' => $producer->id | |||||
]) | |||||
->one() ; | |||||
if(!$user_etablissement || ($user_etablissement && !$user_etablissement->actif)) { | |||||
$this->redirect(Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer-code','id' => $producer->id])) ; | |||||
} | |||||
} | |||||
} | |||||
return parent::beforeAction($event); | |||||
} | |||||
public function getProducer() { | public function getProducer() { | ||||
if($this->producer) { | if($this->producer) { | ||||
return $this->producer ; | return $this->producer ; |