Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AddEtablissementForm.php 3.6KB

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