Procházet zdrojové kódy

Refactoring/traduction modèles formulaires

refactoring
Guillaume Bourgeois před 6 roky
rodič
revize
f831982e78
5 změnil soubory, kde provedl 79 přidání a 84 odebrání
  1. +31
    -30
      backend/models/CreditForm.php
  2. +1
    -1
      backend/models/MailForm.php
  3. +39
    -45
      frontend/models/AddProducerForm.php
  4. +4
    -4
      frontend/models/ProducerCodeForm.php
  5. +4
    -4
      frontend/models/SignupForm.php

+ 31
- 30
backend/models/CreditForm.php Zobrazit soubor

@@ -40,10 +40,10 @@ namespace backend\models;

use Yii;
use yii\base\Model;
use common\models\CreditHistorique ;
use common\models\CreditHistory ;
use common\models\User ;
use common\models\Etablissement ;
use common\models\UserEtablissement ;
use common\models\Prtoducer ;
use common\models\UserProducer ;
use common\helpers\Mail ;

/**
@@ -54,11 +54,11 @@ class CreditForm extends Model

public $id_user ;
public $id_user_action ;
public $id_etablissement ;
public $id_producer ;
public $type ;
public $montant ;
public $moyen_paiement ;
public $commentaire ;
public $amount ;
public $means_payment ;
public $comment ;
public $send_mail ;

/**
@@ -67,11 +67,11 @@ class CreditForm extends Model
public function rules()
{
return [
[['montant'], 'required'],
[['id_user', 'id_user_action', 'id_etablissement'], 'integer'],
[['amount'], 'required'],
[['id_user', 'id_user_action', 'id_producer'], 'integer'],
[['date','send_mail'], 'safe'],
[['montant'], 'double'],
[['type', 'moyen_paiement', 'commentaire'], 'string', 'max' => 255],
[['amount'], 'double'],
[['type', 'means_payment', 'comment'], 'string', 'max' => 255],
];
}

@@ -84,11 +84,11 @@ class CreditForm extends Model
'id_user' => 'Utilisateur',
'id_user_action' => 'Utilisateur',
'date' => 'Date',
'montant' => 'Montant',
'amount' => 'Montant',
'type' => 'Type',
'id_etablissement' => 'Établissement',
'moyen_paiement' => 'Moyen de paiement',
'commentaire' => 'Commentaire',
'id_producer' => 'Producteur',
'means_payment' => 'Moyen de paiement',
'comment' => 'Commentaire',
'send_mail' => 'Prévenir l\'utilisateur',
];
}
@@ -99,27 +99,28 @@ class CreditForm extends Model
public function save()
{
if ($this->validate()) {
$credit_historique = new CreditHistorique ;
$credit_historique->id_user = $this->id_user;
$credit_historique->id_user_action = Yii::$app->user->identity->id;
$credit_historique->id_etablissement = Yii::$app->user->identity->id_etablissement;
$credit_historique->type = $this->type ;
$credit_historique->commentaire = $this->commentaire ;
$credit_historique->montant = $this->montant ;
$credit_historique->moyen_paiement = $this->moyen_paiement ;
$credit_historique->save();
$creditHistory = new CreditHistory;
$creditHistory->id_user = $this->id_user;
$creditHistory->id_user_action = Yii::$app->user->identity->id;
$creditHistory->id_producer = Producer::getCurrentId() ;
$creditHistory->type = $this->type ;
$creditHistory->comment = $this->comment ;
$creditHistory->amount = $this->amount ;
$creditHistory->means_payment = $this->means_payment ;
$creditHistory->save();
// on prévient l'utilisateur que son compte vient d'être crédité
if($this->send_mail) {
$user = User::findOne($this->id_user) ;
$etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
$user_etablissement = UserEtablissement::findOne(['id_user' => $this->id_user, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]);
$producer = Producer::getCurrent() ;
$userProducer = UserProducer::searchOne([
'id_user' => $this->id_user
]);
Mail::send($user->email, 'Votre compte vient d\'être crédité','creditUser', [
'user' => $user,
'etablissement' => $etablissement,
'user_etablissement' => $user_etablissement,
'credit_form' => $this
'producer' => $producer,
'userProducer' => $userProducer,
'creditForm' => $this
]) ;
}
}

+ 1
- 1
backend/models/MailForm.php Zobrazit soubor

@@ -82,7 +82,7 @@ class MailForm extends Model
{
return Yii::$app->mailer->compose()
->setTo($email)
->setFrom(['matthieu@lechatdesnoisettes.com' => 'Le Chat des Noisettes'])
->setFrom(['contact@laboiteapain.net' => 'Laboîte à pain'])
->setSubject($this->subject)
->setTextBody($this->body)
->send();

frontend/models/AddEtablissementForm.php → frontend/models/AddProducerForm.php Zobrazit soubor

@@ -40,17 +40,17 @@ namespace frontend\models;

use Yii;
use yii\base\Model;
use common\models\UserEtablissement;
use common\models\Etablissement;
use common\models\UserProducer;
use common\models\Producer;
use yii\helpers\Html;

/**
* ContactForm is the model behind the contact form.
*/
class AddEtablissementForm extends Model
class AddProducerForm extends Model
{

public $id_etablissement;
public $id_producer;
public $code;

/**
@@ -59,40 +59,37 @@ class AddEtablissementForm extends Model
public function rules()
{
return [
['id_etablissement', 'integer'],
['id_etablissement', 'required'],
['id_etablissement', function($attribute, $params) {
['id_producer', 'integer'],
['id_producer', 'required'],
['id_producer', function($attribute, $params) {

$etablissement = Etablissement::findOne($this->id_etablissement);
if (!$etablissement) {
$this->addError($attribute, 'Cet établissement n\'existe pas.');
$producer = Producer::findOne($this->id_producer);
if (!$producer) {
$this->addError($attribute, 'Ce producteur n\'existe pas.');
}
$userProducerExist = UserProducer::searchOne([
'id_user' => Yii::$app->user->identity->id,
'active' => 1
]) ;

$user_etablissement_exist = UserEtablissement::find()
->where([
'id_user' => Yii::$app->user->identity->id,
'id_etablissement' => $this->id_etablissement,
'actif' => 1
])
->one();

if ($user_etablissement_exist) {
$this->addError($attribute, 'Cet établissement est déjà sur votre tableau de bord.');
if ($userProducerExist) {
$this->addError($attribute, 'Ce producteur est déjà sur votre tableau de bord.');
}
}],
['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');
}
}],
@@ -105,7 +102,7 @@ class AddEtablissementForm extends Model
public function attributeLabels()
{
return [
'id_etablissement' => 'Établissement',
'id_producer' => 'Producteur',
'code' => 'Code',
];
}
@@ -118,29 +115,26 @@ class AddEtablissementForm extends Model
*/
public function add()
{
$etablissement = Etablissement::findOne($this->id_etablissement);
$producer = Producer::findOne($this->id_producer);

$user_etablissement_exist = UserEtablissement::find()
->where([
'id_user' => Yii::$app->user->identity->id,
'id_etablissement' => $this->id_etablissement,
'actif' => 0
])
->one();

if ($user_etablissement_exist) {
$user_etablissement_exist->actif = 1;
$user_etablissement_exist->save();
$userProducerExist = UserProducer::searchOne([
'id_user' => User::getCurrentId(),
'active' => 0
]) ;

if ($userProducerExist) {
$userProducerExist->active = 1;
$userProducerExist->save();
} else {
$user_etablissement = new UserEtablissement();
$user_etablissement->id_user = Yii::$app->user->identity->id;
$user_etablissement->id_etablissement = $this->id_etablissement;
$user_etablissement->credit = 0;
$user_etablissement->actif = 1;
$user_etablissement->save();
$userProducer = new UserProducer();
$userProducer->id_user = User::getCurrentId();
$userProducer->id_producer = $this->id_producr;
$userProducer->credit = 0;
$userProducer->actif = 1;
$userProducer->save();
}

Yii::$app->session->setFlash('success', 'Le producteur <strong>' . Html::encode($etablissement->nom) . '</strong> a bien été ajoutée à votre tableau de bord.');
Yii::$app->session->setFlash('success', 'Le producteur <strong>' . Html::encode($producer->name) . '</strong> a bien été ajoutée à votre tableau de bord.');
}

}

+ 4
- 4
frontend/models/ProducerCodeForm.php Zobrazit soubor

@@ -60,14 +60,14 @@ class ProducerCodeForm extends Model
['id_producer', 'integer'],
['id_producer', function($attribute, $params) {
if ($this->id_producer) {
$producer = Etablissement::findOne($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 = Etablissement::findOne($this->id_producer);
$producer = Producer::findOne($this->id_producer);
if ($producer) {
return strlen($producer->code);
} else {
@@ -76,7 +76,7 @@ class ProducerCodeForm extends Model
}],
['code', function($attribute, $params) {
$code = $this->$attribute;
$producer = Etablissement::findOne($this->id_producer);
$producer = Producer::findOne($this->id_producer);

if ($producer && strtolower(trim($code)) != strtolower(trim($producer->code))) {
$this->addError($attribute, 'Code incorrect');
@@ -89,7 +89,7 @@ class ProducerCodeForm extends Model
{
return [
'id_producer' => 'Producteur',
'code' => 'Code de l\'établissement',
'code' => 'Code du producteur',
];
}


+ 4
- 4
frontend/models/SignupForm.php Zobrazit soubor

@@ -54,10 +54,10 @@ class SignupForm extends Model
public $username;
public $email;
public $password;
public $nom;
public $prenom;
public $telephone;
public $is_boulanger;
public $name;
public $lastname;
public $phone;
public $is_producer;
public $siret;
public $nom_magasin;
public $code_postal;

Načítá se…
Zrušit
Uložit