Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AddEtablissementForm.php 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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(['id_user'=>Yii::$app->user->identity->id, 'id_etablissement' =>$this->id_etablissement])
  31. ->one() ;
  32. if($user_etablissement_exist)
  33. {
  34. $this->addError($attribute, 'Cette boulangerie 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. {
  41. return strlen($etablissement->code) ;
  42. }
  43. else {
  44. return false ;
  45. }
  46. }],
  47. ['code', function($attribute, $params) {
  48. $code = $this->$attribute ;
  49. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  50. if($etablissement && strtolower(trim($code)) != strtolower(trim($etablissement->code)))
  51. {
  52. $this->addError($attribute, 'Code incorrect');
  53. }
  54. }],
  55. ];
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function attributeLabels()
  61. {
  62. return [
  63. 'id_etablissement' => 'Établissement',
  64. 'code' => 'Code',
  65. ];
  66. }
  67. /**
  68. * Sends an email to the specified email address using the information collected by this model.
  69. *
  70. * @param string $email the target email address
  71. * @return boolean whether the email was sent
  72. */
  73. public function add()
  74. {
  75. $etablissement = Etablissement::findOne($this->id_etablissement) ;
  76. $user_etablissement = new UserEtablissement() ;
  77. $user_etablissement->id_user = Yii::$app->user->identity->id ;
  78. $user_etablissement->id_etablissement = $this->id_etablissement ;
  79. $user_etablissement->save() ;
  80. Yii::$app->session->setFlash('success', 'La boulangerie <strong>'.Html::encode($etablissement->nom).'</strong> a bien été ajoutée à votre tableau de bord.') ;
  81. }
  82. }