|
- <?php
-
- namespace frontend\models;
-
- use Yii;
- use yii\base\Model;
- use common\models\UserEtablissement;
- use common\models\Etablissement ;
- use yii\helpers\Html ;
-
- /**
- * ContactForm is the model behind the contact form.
- */
- class AddEtablissementForm extends Model
- {
- public $id_etablissement;
- public $code ;
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- ['id_etablissement','integer'],
- ['id_etablissement', 'required'],
- ['id_etablissement', function($attribute, $params) {
-
- $etablissement = Etablissement::findOne($this->id_etablissement) ;
- if(!$etablissement)
- {
- $this->addError($attribute, 'Cette boulangerie n\'existe pas.') ;
- }
-
- $user_etablissement_exist = UserEtablissement::find()
- ->where(['id_user'=>Yii::$app->user->identity->id, 'id_etablissement' =>$this->id_etablissement])
- ->one() ;
-
- if($user_etablissement_exist)
- {
- $this->addError($attribute, 'Cette boulangerie est déjà sur votre tableau de bord.') ;
- }
- }],
- ['code', 'required', 'message' => 'Champs obligatoire', 'when' => function($model) {
- $etablissement = Etablissement::findOne($this->id_etablissement) ;
- if($etablissement)
- {
- return strlen($etablissement->code) ;
- }
- else {
- return false ;
- }
- }],
- ['code', function($attribute, $params) {
- $code = $this->$attribute ;
- $etablissement = Etablissement::findOne($this->id_etablissement) ;
-
- if($etablissement && strtolower(trim($code)) != strtolower(trim($etablissement->code)))
- {
- $this->addError($attribute, 'Code incorrect');
- }
- }],
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'id_etablissement' => 'Établissement',
- 'code' => 'Code',
- ];
- }
-
- /**
- * Sends an email to the specified email address using the information collected by this model.
- *
- * @param string $email the target email address
- * @return boolean whether the email was sent
- */
- public function add()
- {
- $etablissement = Etablissement::findOne($this->id_etablissement) ;
-
- $user_etablissement = new UserEtablissement() ;
- $user_etablissement->id_user = Yii::$app->user->identity->id ;
- $user_etablissement->id_etablissement = $this->id_etablissement ;
- $user_etablissement->save() ;
-
- Yii::$app->session->setFlash('success', 'La boulangerie <strong>'.Html::encode($etablissement->nom).'</strong> a bien été ajoutée à votre tableau de bord.') ;
- }
- }
|