Browse Source

Clients : envoi d'un email de bienvenue dès que l'email est défini

On peut créer un compte client sans adresse email. L'idée est d'envoyer le mail de bienvenue dès que cette adresse est saisie.
dev
keun 6 years ago
parent
commit
60a6387a91
2 changed files with 28 additions and 13 deletions
  1. +14
    -13
      backend/controllers/UserController.php
  2. +14
    -0
      common/models/User.php

+ 14
- 13
backend/controllers/UserController.php View File

$user_etablissement->actif = 1; $user_etablissement->actif = 1;
$user_etablissement->save(); $user_etablissement->save();


// send mail
if (strlen($model->email)) {
$etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement);
Yii::$app->mailer->compose();
$mail = Yii::$app->mailer->compose(
['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], ['user' => $model, 'etablissement' => $etablissement, 'password' => $password])
->setTo($model->email)
->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
->setSubject('[La boîte à pain] Inscription')
->send();
}
$model->sendMailWelcome($password) ;


return $this->redirect(['index']); return $this->redirect(['index']);
} else { } else {
return $this->render('create', [ return $this->render('create', [
'model' => $model,
'model' => $model,
]); ]);
} }
} }
public function actionUpdate($id) { public function actionUpdate($id) {
$model = $this->findModel($id); $model = $this->findModel($id);


$previous_mail = $model->email ;
$user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one(); $user = User::find()->with('userEtablissement')->where(['id' => $model['id']])->one();
$user_appartient_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]); $user_appartient_etablissement = UserEtablissement::findOne(['id_user' => $id, 'id_etablissement' => Yii::$app->user->identity->id_etablissement]);
if (($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) { if (($user_appartient_etablissement && count($user->userEtablissement) == 1) || Yii::$app->user->identity->status == USER::STATUS_ADMIN) {
if ($model->load(Yii::$app->request->post()) && $model->save()) { if ($model->load(Yii::$app->request->post()) && $model->save()) {
// on envoie le mail de bienvenue si le mail vient d'être défini
if(!strlen($previous_mail) && strlen($model->email)) {
$password = Password::generate();
$model->setPassword($password);
$model->username = $model->email;
$model->sendMailWelcome($password) ;
}
return $this->redirect(['index']); return $this->redirect(['index']);
} else { } else {
return $this->render('update', [ return $this->render('update', [
'model' => $model,
'model' => $model,
]); ]);
} }
} else { } else {

+ 14
- 0
common/models/User.php View File

$this->date_derniere_connexion = date('Y-m-d H:i:s'); $this->date_derniere_connexion = date('Y-m-d H:i:s');
$this->save(); $this->save();
} }
public function sendMailWelcome($password) {
if (strlen($this->email)) {
$etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement);
Yii::$app->mailer->compose();
$mail = Yii::$app->mailer->compose(
['html' => 'createUserAdmin-html', 'text' => 'createUserAdmin-text'], ['user' => $this, 'etablissement' => $etablissement, 'password' => $password]
)
->setTo($this->email)
->setFrom(['contact@laboiteapain.net' => 'La boîte à pain'])
->setSubject('[La boîte à pain] Inscription')
->send();
}
}


} }

Loading…
Cancel
Save