Bladeren bron

Accès aux producteurs protègés par un code

refactoring
keun 6 jaren geleden
bovenliggende
commit
3a1e73c50c
13 gewijzigde bestanden met toevoegingen van 255 en 64 verwijderingen
  1. +22
    -0
      common/models/Etablissement.php
  2. +27
    -0
      common/models/LoginForm.php
  3. +39
    -37
      frontend/controllers/SiteController.php
  4. +57
    -0
      frontend/models/ProducerCodeForm.php
  5. +13
    -17
      frontend/models/SignupForm.php
  6. +2
    -2
      frontend/views/site/error.php
  7. +8
    -2
      frontend/views/site/producer.php
  8. +21
    -0
      frontend/views/site/producer_code.php
  9. +4
    -1
      frontend/views/site/producers.php
  10. BIN
      frontend/web/.sass-cache/a737daaa3b038cd28cb74dc0777f663fd8a4a5ae/_systeme_commandes.scssc
  11. +16
    -4
      frontend/web/css/screen.css
  12. +14
    -1
      frontend/web/sass/_systeme_commandes.scss
  13. +32
    -0
      producer/controllers/ProducerBaseController.php

+ 22
- 0
common/models/Etablissement.php Bestand weergeven

@@ -229,6 +229,28 @@ class Etablissement extends \yii\db\ActiveRecord {
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();
}
}
}

}

+ 27
- 0
common/models/LoginForm.php Bestand weergeven

@@ -15,6 +15,8 @@ class LoginForm extends Model {
public $rememberMe = true;
public $email;
private $_user = false;
public $code ;
public $id_etablissement ;

/**
* @inheritdoc
@@ -31,6 +33,31 @@ class LoginForm extends Model {
['rememberMe', 'boolean'],
// password is validated by 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.');
}
}
}],
];
}


+ 39
- 37
frontend/controllers/SiteController.php Bestand weergeven

@@ -75,12 +75,12 @@ class SiteController extends FrontendController {
{
$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]);
}
}

@@ -219,55 +219,57 @@ class SiteController extends FrontendController {
public function actionCreditpain() {
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_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 ($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())) {

$model_signup->id_etablissement = $id_etablissement;
$model_signup->option_client_boulanger = 'client';

if ($user = $model_signup->signup()) {
if (Yii::$app->getUser()->login($user)) {
$this->redirect(['commande/index']);
$this->redirect($url_redirect);
}
}
}
} 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_signup' => $model_signup,
'etablissement' => $etablissement,

+ 57
- 0
frontend/models/ProducerCodeForm.php Bestand weergeven

@@ -0,0 +1,57 @@
<?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',
];
}

}

+ 13
- 17
frontend/models/SignupForm.php Bestand weergeven

@@ -206,26 +206,22 @@ class SignupForm extends Model {
if ($this->id_etablissement) {
$etablissement = Etablissement::find()->where(['id' => $this->id_etablissement])->one();
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
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();
}
}
}

+ 2
- 2
frontend/views/site/error.php Bestand weergeven

@@ -7,14 +7,14 @@ use yii\helpers\Html;
/* @var $message string */
/* @var $exception Exception */

$this->title = $name;
$this->title = $exception->getName();
?>
<div class="site-error">

<h1><?= Html::encode($this->title) ?></h1>

<div class="alert alert-danger">
<?= nl2br(Html::encode($message)) ?>
<?= nl2br(Html::encode($exception->getMessage())) ?>
</div>

<p><?= Html::a("Retour à l'accueil", ['site/index']) ?></p>

frontend/views/site/etablissement.php → frontend/views/site/producer.php Bestand weergeven

@@ -30,6 +30,13 @@ $this->title = 'Producteur '.Html::encode($etablissement->nom) ;
<p>
Si vous avez oublié votre mot de passe, vous pouvez le <?= Html::a('réinitialiser', ['site/request-password-reset']) ?>.
</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">
<?= Html::submitButton('Connexion', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
@@ -44,13 +51,12 @@ $this->title = 'Producteur '.Html::encode($etablissement->nom) ;
<?= $form->field($model_signup, 'nom') ?>
<?= $form->field($model_signup, 'prenom') ?>
<?= $form->field($model_signup, 'telephone') ?>
<?php //$form->field($model, 'is_boulanger')->checkbox() ?>

<?php if(strlen($etablissement->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>',
])
->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') ; ?>
<?php endif; ?>

+ 21
- 0
frontend/views/site/producer_code.php Bestand weergeven

@@ -0,0 +1,21 @@
<?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>

+ 4
- 1
frontend/views/site/producers.php Bestand weergeven

@@ -28,7 +28,10 @@ GridView::widget([
'label' => 'Lien',
'format' => 'raw',
'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 ;
}
],
]

BIN
frontend/web/.sass-cache/a737daaa3b038cd28cb74dc0777f663fd8a4a5ae/_systeme_commandes.scssc Bestand weergeven


+ 16
- 4
frontend/web/css/screen.css Bestand weergeven

@@ -1512,12 +1512,24 @@ h2 {
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;
}
/* 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;
}


+ 14
- 1
frontend/web/sass/_systeme_commandes.scss Bestand weergeven

@@ -663,11 +663,24 @@ h2 {
}

#page-etablissement {
#page-etablissement,
#page-producer-code {
h1 {
font-size: 40px ;
}
.info {
text-align: center ;
}
}

#page-producer-code {
form#producer-code {
max-width: 400px ;
margin: 0px auto ;
.form-group.buttons {
text-align: center ;
}
}
}

+ 32
- 0
producer/controllers/ProducerBaseController.php Bestand weergeven

@@ -17,6 +17,38 @@ class ProducerBaseController extends CommonController {
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() {
if($this->producer) {
return $this->producer ;

Laden…
Annuleren
Opslaan