Переглянути джерело

Adaptations refactoring/traduction modèles UserPointSale, Producer, User, UserProducer

dev
Guillaume Bourgeois 6 роки тому
джерело
коміт
7439956b69
4 змінених файлів з 139 додано та 85 видалено
  1. +15
    -1
      common/models/Producer.php
  2. +100
    -74
      common/models/User.php
  3. +2
    -2
      common/models/UserPointSale.php
  4. +22
    -8
      common/models/UserProducer.php

+ 15
- 1
common/models/Producer.php Переглянути файл

@@ -149,7 +149,7 @@ class Producer extends \yii\db\ActiveRecord
'with' => [],
'join_with' => [],
'orderby' => 'name ASC',
'attribute_id_producer' => ''
'attribute_id_producer' => 'producer.id'
] ;
}

@@ -359,6 +359,20 @@ class Producer extends \yii\db\ActiveRecord
return false ;
}
/**
* Retourne le producteur courant.
*
* @return Producer|boolean
*/
public static function getCurrent()
{
if(self::getCurrentId()) {
return self::searchOne() ;
}
return false ;
}

}

+ 100
- 74
common/models/User.php Переглянути файл

@@ -64,7 +64,7 @@ class User extends ActiveRecord implements IdentityInterface
const STATUS_PRODUCER = 11;
const STATUS_ADMIN = 13;

const ID_USER_SYSTEM = 0 ;
const ID_USER_SYSTEM = 0 ;
var $password_old;
var $password_new;
@@ -108,6 +108,43 @@ class User extends ActiveRecord implements IdentityInterface
[['date_derniere_connexion', 'password_old', 'password_new', 'password_new_confirm', 'password_hash'], 'safe'],
];
}
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Identifiant',
'password' => 'Mot de passe',
'rememberMe' => 'Se souvenir de moi',
'confiance' => 'De confiance',
'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes',
'mail_prod_lundi' => 'Lundi',
'mail_prod_mardi' => 'Mardi',
'mail_prod_mercredi' => 'Mercredi',
'mail_prod_jeudi' => 'Jeudi',
'mail_prod_vendredi' => 'Vendredi',
'mail_prod_samedi' => 'Samedi',
'mail_prod_dimanche' => 'Dimanche',
'password_old' => 'Ancien mot de passe',
'password_new' => 'Nouveau mot de passe',
'password_new_confirm' => 'Confirmation du nouveau mot de passe',
'prix_libre' => 'Prix libre',
];
}
/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
* @return array
*/
public static function defaultOptionsSearch() {
return [
'with' => [],
'join_with' => [],
'orderby' => 'user.name ASC, user.lastname ASC',
'attribute_id_producer' => ''
] ;
}

/**
* Vérifie le mot de passe envoyé par l'utilisateur.
@@ -172,8 +209,9 @@ class User extends ActiveRecord implements IdentityInterface
{
$user = User::find()->where("email LIKE :email AND id != :id")->params(array(':email' => '%' . $this->email . '%', ':id' => $this->id))->one();

if ($user)
if ($user) {
$this->addError($attribute, 'Cette adresse email est déjà utilisée par un autre utilisateur ');
}
}

/*
@@ -190,7 +228,7 @@ class User extends ActiveRecord implements IdentityInterface
*/
public static function findIdentity($id)
{
return static::findOne(['id' => $id/* , 'status' => self::STATUS_ACTIVE */]);
return static::findOne(['id' => $id]);
}

/**
@@ -236,7 +274,7 @@ class User extends ActiveRecord implements IdentityInterface
}

return static::findOne([
'password_reset_token' => $token,
'password_reset_token' => $token,
]);
}

@@ -249,52 +287,53 @@ class User extends ActiveRecord implements IdentityInterface
*/
public static function findBy($params = [])
{
if (!isset($params['id_etablissement']))
$params['id_etablissement'] = Yii::$app->user->identity->id_etablissement;
if (!isset($params['id_producer'])) {
$params['id_producer'] = Producer::getCurrentId() ;
}
$query = (new Query())
->select(['user.id AS user_id', 'user.prenom', 'user.nom', 'user.telephone', 'user.email', 'user.created_at', 'user.date_derniere_connexion', 'user_etablissement.*'])
->select(['user.id AS user_id', 'user.name', 'user.lastname', 'user.phone', 'user.email', 'user.created_at', 'user.date_last_connection', 'user_producer.*'])
->from('user');
$actif = (isset($params['inactifs']) && $params['inactifs']) ? 0 : 1 ;
$query->innerJoin('user_etablissement','user.id = user_etablissement.id_user AND user_etablissement.actif = '.$actif.' AND user_etablissement.id_etablissement = :id_etablissement', [':id_etablissement' => $params['id_etablissement']]) ;
$active = (isset($params['inactive']) && $params['inactive']) ? 0 : 1 ;
$query->innerJoin('user_producer','user.id = user_producer.id_user AND user_producer.active = '.$active.' AND user_producer.id_producer = :id_producer', [':id_producer' => $params['id_producer']]) ;
if(isset($params['id_point_vente']) && $params['id_point_vente']) {
$point_vente = PointVente::findOne(['id' => $params['id_point_vente']]) ;
if($point_vente->acces_restreint) {
$query->innerJoin('point_vente_user','user.id = point_vente_user.id_user AND point_vente_user.id_point_vente = :id_point_vente',[':id_point_vente' => $params['id_point_vente']]) ;
if(isset($params['id_point_sale']) && $params['id_point_sale']) {
$point_sale = PointSale::findOne(['id' => $params['id_point_sale']]) ;
if($point_sale->restricted_access) {
$query->innerJoin('user_point_sale','user.id = user_point_sale.id_user AND user_point_sale.id_point_sale = :id_point_sale',[':id_point_sale' => $params['id_point_sale']]) ;
}
else {
$query->innerJoin(
'commande',
'user.id = commande.id_user AND commande.id_point_vente = :id_point_vente',
[':id_point_vente' => $params['id_point_vente']]
'order',
'user.id = order.id_user AND order.id_point_sale = :id_point_sale',
[':id_point_sale' => $params['id_point_sale']]
)->groupBy('user.id') ;
}
}
if(isset($params['inactifs']) && $params['inactifs']) {
if(isset($params['inactive']) && $params['inactive']) {
$query->innerJoin(
'commande',
'user.id = commande.id_user'
'order',
'user.id = order.id_user'
)
->groupBy('user.id');
}
if (isset($params['nom'])) {
$query->andFilterWhere(['like', 'nom', $params['nom']]);
if (isset($params['name'])) {
$query->andFilterWhere(['like', 'name', $params['name']]);
}
if (isset($params['prenom'])) {
$query->andFilterWhere(['like', 'prenom', $params['prenom']]);
if (isset($params['lastname'])) {
$query->andFilterWhere(['like', 'lastname', $params['lastname']]);
}
if (isset($params['email'])) {
$query->andFilterWhere(['like', 'email', $params['email']]);
}
if (isset($params['telephone'])) {
$query->andFilterWhere(['like', 'telephone', $params['telephone']]);
if (isset($params['phone'])) {
$query->andFilterWhere(['like', 'phone', $params['phone']]);
}
return $query;
@@ -386,37 +425,14 @@ class User extends ActiveRecord implements IdentityInterface
$this->password_reset_token = null;
}

public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Identifiant',
'password' => 'Mot de passe',
'rememberMe' => 'Se souvenir de moi',
'confiance' => 'De confiance',
'no_mail' => 'Ne pas recevoir d\'email de la part du Chat des Noisettes',
'mail_prod_lundi' => 'Lundi',
'mail_prod_mardi' => 'Mardi',
'mail_prod_mercredi' => 'Mercredi',
'mail_prod_jeudi' => 'Jeudi',
'mail_prod_vendredi' => 'Vendredi',
'mail_prod_samedi' => 'Samedi',
'mail_prod_dimanche' => 'Dimanche',
'password_old' => 'Ancien mot de passe',
'password_new' => 'Nouveau mot de passe',
'password_new_confirm' => 'Confirmation du nouveau mot de passe',
'prix_libre' => 'Prix libre',
];
}

/**
* Retourne si l'utilisateur est un boulanger ou non.
*
* @return boolean
*/
public function isBoulanger()
public function isProducer()
{
return ($this->status == User::STATUS_ADMIN || $this->status == User::STATUS_BOULANGER) && $this->id_etablissement;
return ($this->status == User::STATUS_ADMIN || $this->status == User::STATUS_PRODUCER) && $this->id_producer;
}

/**
@@ -424,10 +440,10 @@ class User extends ActiveRecord implements IdentityInterface
*
* @return string
*/
public function getNomMagasin()
public function getNameProducer()
{
$etablissement = Etablissement::findOne($this->id_etablissement);
return $etablissement->nom;
$producer = Producer::findOne($this->id_producer);
return $producer->name;
}

/**
@@ -435,17 +451,17 @@ class User extends ActiveRecord implements IdentityInterface
*
* @return array
*/
public function getEtablissementsFavoris()
public function getBookmarksProducers()
{
$etabs = (new \yii\db\Query())
$producers = (new \yii\db\Query())
->select('*')
->from(['user_etablissement', 'etablissement'])
->where('user_etablissement.id_etablissement = etablissement.id')
->andWhere(['user_etablissement.id_user' => $this->id])
->andWhere(['user_etablissement.actif' => 1])
->from(['user_producer', 'producer'])
->where('user_producer.id_producer = producer.id')
->andWhere(['user_producer.id_user' => $this->id])
->andWhere(['user_producer.active' => 1])
->all();

return $etabs;
return $producers;
}

/**
@@ -454,17 +470,14 @@ class User extends ActiveRecord implements IdentityInterface
* @param integer $id_etablissement
* @return float
*/
public function getCredit($id_etablissement)
public function getCredit($idProducer)
{
$user_etablissement = UserEtablissement::find()
->where([
'id_user' => $this->id,
'id_etablissement' => $id_etablissement
])
->one();

if ($user_etablissement) {
return $user_etablissement->credit;
$userProducer = UserProducer::searchOne([
'id_user' => $this->id
]) ;

if ($userProducer) {
return $userProducer->credit;
}

return 0;
@@ -473,9 +486,9 @@ class User extends ActiveRecord implements IdentityInterface
/**
* Met à jour la date de dernière connexion de l'utilisateur.
*/
public function updateDerniereConnexion()
public function updateLastConnection()
{
$this->date_derniere_connexion = date('Y-m-d H:i:s');
$this->date_last_connection = date('Y-m-d H:i:s');
$this->save();
}
@@ -488,10 +501,10 @@ class User extends ActiveRecord implements IdentityInterface
public function sendMailWelcome($password)
{
if (strlen($this->email)) {
$etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement);
$producer = Producer::findOne(Producer::getCurrentId());
Yii::$app->mailer->compose();
$mail = Yii::$app->mailer->compose(
['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], ['user' => $this, 'etablissement' => $etablissement, 'password' => $password]
['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], ['user' => $this, 'producer' => $producer, 'password' => $password]
)
->setTo($this->email)
->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
@@ -499,5 +512,18 @@ class User extends ActiveRecord implements IdentityInterface
->send();
}
}
/**
* Retourne l'ID de l'utilisateur courant connecté.
*
* @return mixed
*/
public static function getCurrentId() {
if(!Yii::$app->user->isGuest) {
return Yii::$app->user->identity->id ;
}
return false ;
}

}

common/models/PointSaleUser.php → common/models/UserPointSale.php Переглянути файл

@@ -46,7 +46,7 @@ use Yii;
* @property integer $id_point_vente
* @property integer $id_user
*/
class PointSaleUser extends \yii\db\ActiveRecord
class UserPointSale extends \yii\db\ActiveRecord
{

/**
@@ -54,7 +54,7 @@ class PointSaleUser extends \yii\db\ActiveRecord
*/
public static function tableName()
{
return 'point_sale_user';
return 'user_point_sale';
}

/**

+ 22
- 8
common/models/UserProducer.php Переглянути файл

@@ -56,7 +56,7 @@ class UserProducer extends \yii\db\ActiveRecord
*/
public static function tableName()
{
return 'user_etablissement';
return 'user_producer';
}

/**
@@ -65,9 +65,9 @@ class UserProducer extends \yii\db\ActiveRecord
public function rules()
{
return [
[['id_user', 'id_etablissement'], 'required'],
[['id_user', 'id_etablissement'], 'integer'],
[['actif','favoris'], 'boolean'],
[['id_user', 'id_producer'], 'required'],
[['id_user', 'id_producer'], 'integer'],
[['active','favoris'], 'boolean'],
[['credit'], 'double'],
];
}
@@ -78,11 +78,25 @@ class UserProducer extends \yii\db\ActiveRecord
public function attributeLabels()
{
return [
'id_user' => 'Id User',
'id_etablissement' => 'Id Etablissement',
'actif' => 'Actif',
'favoris' => 'Favoris',
'id_user' => 'Utilisateur',
'id_producer' => 'Producteur',
'active' => 'Actif',
'bookmark' => 'Favoris',
];
}
/**
* Retourne les options de base nécessaires à la fonction de recherche.
*
* @return array
*/
public static function defaultOptionsSearch() {
return [
'with' => [],
'join_with' => [],
'orderby' => '',
'attribute_id_producer' => 'user_producer.id_producer'
] ;
}

}

Завантаження…
Відмінити
Зберегти