You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.7KB

  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. ['id_etablissement','integer'],
  22. ['id_etablissement', 'required'],
  23. ['id_etablissement', function($attribute, $params) {
  24. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  25. if(!$etablissement)
  26. {
  27. $this->addError($attribute, 'Cette boulangerie n\'existe pas.') ;
  28. }
  29. $user_etablissement_exist = UserEtablissement::find()
  30. ->where([
  31. 'id_user' => Yii::$app->user->identity->id,
  32. 'id_etablissement' => $this->id_etablissement,
  33. 'actif' => 1
  34. ])
  35. ->one() ;
  36. if($user_etablissement_exist)
  37. {
  38. $this->addError($attribute, 'Cette boulangerie est déjà sur votre tableau de bord.') ;
  39. }
  40. }],
  41. ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
  42. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  43. if($etablissement)
  44. {
  45. return strlen($etablissement->code) ;
  46. }
  47. else {
  48. return false ;
  49. }
  50. }],
  51. ['code', function($attribute, $params) {
  52. $code = $this->$attribute ;
  53. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  54. if($etablissement && strtolower(trim($code)) != strtolower(trim($etablissement->code)))
  55. {
  56. $this->addError($attribute, 'Code incorrect');
  57. }
  58. }],
  59. ];
  60. }
  61. /**
  62. * @inheritdoc
  63. */
  64. public function attributeLabels()
  65. {
  66. return [
  67. 'id_etablissement' => 'Établissement',
  68. 'code' => 'Code',
  69. ];
  70. }
  71. /**
  72. * Sends an email to the specified email address using the information collected by this model.
  73. *
  74. * @param string $email the target email address
  75. * @return boolean whether the email was sent
  76. */
  77. public function add()
  78. {
  79. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  80. $user_etablissement_exist = UserEtablissement::find()
  81. ->where([
  82. 'id_user' => Yii::$app->user->identity->id,
  83. 'id_etablissement' => $this->id_etablissement,
  84. 'actif' => 0
  85. ])
  86. ->one() ;
  87. if($user_etablissement_exist)
  88. {
  89. $user_etablissement_exist->actif = 1 ;
  90. $user_etablissement_exist->save() ;
  91. }
  92. else {
  93. $user_etablissement = new UserEtablissement() ;
  94. $user_etablissement->id_user = Yii::$app->user->identity->id ;
  95. $user_etablissement->id_etablissement = $this->id_etablissement ;
  96. $user_etablissement->credit = 0 ;
  97. $user_etablissement->actif = 1 ;
  98. $user_etablissement->save() ;
  99. }
  100. Yii::$app->session->setFlash('success', 'La boulangerie <strong>'.Html::encode($etablissement->nom).'</strong> a bien été ajoutée à votre tableau de bord.') ;
  101. }
  102. }