Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

74 linhas
2.0KB

  1. <?php
  2. namespace frontend\models;
  3. use Yii;
  4. use yii\base\Model;
  5. use common\models\UserEtablissement;
  6. use common\models\Etablissement ;
  7. use yii\helpers\Html ;
  8. /**
  9. * ContactForm is the model behind the contact form.
  10. */
  11. class AddEtablissementForm extends Model
  12. {
  13. public $id_etablissement;
  14. public $code ;
  15. /**
  16. * @inheritdoc
  17. */
  18. public function rules()
  19. {
  20. return [
  21. ['code', 'required', 'message' => 'Champs obligatoire'],
  22. ];
  23. }
  24. /**
  25. * @inheritdoc
  26. */
  27. public function attributeLabels()
  28. {
  29. return [
  30. 'code' => 'Code',
  31. ];
  32. }
  33. /**
  34. * Sends an email to the specified email address using the information collected by this model.
  35. *
  36. * @param string $email the target email address
  37. * @return boolean whether the email was sent
  38. */
  39. public function add()
  40. {
  41. $etablissement = Etablissement::findOne(['code' => $this->code]) ;
  42. if($etablissement)
  43. {
  44. $user_etablissement_exist = UserEtablissement::find()
  45. ->where(['id_user'=>Yii::$app->user->identity->id, 'id_etablissement' =>$etablissement->id])
  46. ->one() ;
  47. if(!$user_etablissement_exist)
  48. {
  49. $user_etablissement = new UserEtablissement() ;
  50. $user_etablissement->id_user = Yii::$app->user->identity->id ;
  51. $user_etablissement->id_etablissement = $etablissement->id ;
  52. $user_etablissement->save() ;
  53. Yii::$app->session->setFlash('success', 'La boulangerie <strong>'.Html::encode($etablissement->nom).'</strong> a bien été ajoutée à votre tableau de bord.') ;
  54. }
  55. else {
  56. Yii::$app->session->setFlash('error', 'Cette boulangerie est déjà sur votre tableau de bord.') ;
  57. }
  58. }
  59. else {
  60. Yii::$app->session->setFlash('error', 'Aucun établissement ne possède ce code d\'accès.') ;
  61. }
  62. }
  63. }