Browse Source

Gestion des commentaires UserPointSale dans l'update/create de User

dev
Guillaume Bourgeois 5 years ago
parent
commit
a8ae9d9561
4 changed files with 64 additions and 23 deletions
  1. +40
    -22
      backend/controllers/UserController.php
  2. +12
    -1
      backend/views/user/_form.php
  3. +7
    -0
      backend/web/css/screen.css
  4. +5
    -0
      backend/web/sass/user/_form.scss

+ 40
- 22
backend/controllers/UserController.php View File

use backend\models\MailForm ; use backend\models\MailForm ;
use common\models\UserProducer ; use common\models\UserProducer ;
use common\models\UserPointSale ; use common\models\UserPointSale ;
use common\models\PointSale ;


/** /**
* UserController implements the CRUD actions for User model. * UserController implements the CRUD actions for User model.
public function initForm($model) public function initForm($model)
{ {
$userPointSaleArray = UserPointSale::searchAll([
'id_user' => $model->id
]) ;
if($userPointSaleArray && count($userPointSaleArray) > 0) {
foreach($userPointSaleArray as $userPointSaleArray) {
$model->points_sale[] = $userPointSaleArray->id_point_sale ;
// init points de vente sélectionnés
if($model->id) {
$userPointSaleArray = UserPointSale::searchAll([
'id_user' => $model->id
]) ;
if($userPointSaleArray && count($userPointSaleArray) > 0) {
foreach($userPointSaleArray as $userPointSaleArray) {
$model->points_sale[] = $userPointSaleArray->id_point_sale ;
}
} }
} }
$pointsSaleArray = PointSale::searchAll() ;
// points de vente
$pointsSaleArray = PointSale::find()
->where([
'id_producer' => Producer::getId(),
])
->joinWith(['userPointSale' => function($query) use($model) {
if($model->id) {
$query->andOnCondition('user_point_sale.id_user = '.$model->id) ;
}
}])
->all();
return [ return [
'pointsSaleArray' => $pointsSaleArray 'pointsSaleArray' => $pointsSaleArray
] ; ] ;


$model->sendMailWelcome($password) ; $model->sendMailWelcome($password) ;
$this->processLinkPointSale($model) ; $this->processLinkPointSale($model) ;

return $this->redirect(['index']);
} else {
return $this->render('create', array_merge($this->initForm($model),[
'model' => $model,
]));
Yii::$app->getSession()->setFlash('success', 'Utilisateur créé.');
$model = new User();
} }
return $this->render('create', array_merge($this->initForm($model),[
'model' => $model,
]));
} }


/** /**
$model->sendMailWelcome($password) ; $model->sendMailWelcome($password) ;
} }
$this->processLinkPointSale($model) ; $this->processLinkPointSale($model) ;
return $this->redirect(['index']);
} else {
return $this->render('update', array_merge($this->initForm($model),[
'model' => $model,
]));
Yii::$app->getSession()->setFlash('success', 'Utilisateur modifié.');
} }
} else {
}
else {
throw new UserException("Vous ne pouvez pas modifier cet utilisateur, soit parce qu'il appartient à plusieurs établissements, soit parce qu'il n'est pas lié au votre."); throw new UserException("Vous ne pouvez pas modifier cet utilisateur, soit parce qu'il appartient à plusieurs établissements, soit parce qu'il n'est pas lié au votre.");
} }
return $this->render('update', array_merge($this->initForm($model),[
'model' => $model,
]));
} }
/** /**
*/ */
public function processLinkPointSale($modelUser) public function processLinkPointSale($modelUser)
{ {
$posts = Yii::$app->request->post() ;
UserPointSale::deleteAll([ UserPointSale::deleteAll([
'id_user' => $modelUser->id 'id_user' => $modelUser->id
]) ; ]) ;
if(is_array($modelUser->points_sale) && count($modelUser->points_sale) > 0) { if(is_array($modelUser->points_sale) && count($modelUser->points_sale) > 0) {
foreach($modelUser->points_sale as $pointSaleId) { foreach($modelUser->points_sale as $pointSaleId) {
echo $modelUser->id.' / '.$pointSaleId.' <br />' ;
$userPointSale = UserPointSale::searchOne([ $userPointSale = UserPointSale::searchOne([
'id_user' => $modelUser->id, 'id_user' => $modelUser->id,
'id_point_sale' => $pointSaleId 'id_point_sale' => $pointSaleId
$userPointSale = new UserPointSale ; $userPointSale = new UserPointSale ;
$userPointSale->id_user = $modelUser->id ; $userPointSale->id_user = $modelUser->id ;
$userPointSale->id_point_sale = $pointSaleId ; $userPointSale->id_point_sale = $pointSaleId ;
$userPointSale->comment = isset($posts['User']['comment_point_sale_'.$pointSaleId]) ? $posts['User']['comment_point_sale_'.$pointSaleId] : '' ;
$userPointSale->save() ; $userPointSale->save() ;
} }
} }

+ 12
- 1
backend/views/user/_form.php View File

<?= $form->field($model, 'phone')->textInput() ?> <?= $form->field($model, 'phone')->textInput() ?>
<?= $form->field($model, 'email')->textInput() ?> <?= $form->field($model, 'email')->textInput() ?>
<?= $form->field($model, 'address')->textarea() ?> <?= $form->field($model, 'address')->textarea() ?>
<?= $form->field($model, 'points_sale')->checkboxlist(ArrayHelper::map($pointsSaleArray, 'id',function($pointSale) { return Html::encode($pointSale->name);})); ?>
<?= $form->field($model, 'points_sale')->checkboxlist(
ArrayHelper::map($pointsSaleArray, 'id',function($pointSale) use ($model) {
$commentUserPointSale = isset($pointSale->userPointSale[0]) ? $pointSale->userPointSale[0]->comment : '' ;
$html = Html::encode($pointSale->name);
if($pointSale->restricted_access) {
$html .= '<input type="text" placeholder="Commentaire" class="form-control" name="User[comment_point_sale_'.$pointSale->id.']" value="'.(($model->id) ? Html::encode($commentUserPointSale) : '').'" />' ;
}
return $html ;
}), [
'encode' => false
]);
?>
<div class="form-group"> <div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> <?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

+ 7
- 0
backend/web/css/screen.css View File

padding: 5px 10px; padding: 5px 10px;
border: solid 1px #e0e0e0; border: solid 1px #e0e0e0;
} }
/* line 13, ../sass/user/_form.scss */
.user-create #user-points_sale label .form-control,
.user-update #user-points_sale label .form-control {
display: inline;
width: 200px;
margin-left: 15px;
}


/* line 4, ../sass/producer/_update.scss */ /* line 4, ../sass/producer/_update.scss */
.producer-update #nav-params { .producer-update #nav-params {

+ 5
- 0
backend/web/sass/user/_form.scss View File

padding: 5px 10px ; padding: 5px 10px ;
border: solid 1px #e0e0e0 ; border: solid 1px #e0e0e0 ;
.form-control {
display: inline ;
width: 200px;
margin-left: 15px ;
}
} }
} }
} }

Loading…
Cancel
Save