瀏覽代碼

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

refactoring
Guillaume Bourgeois 5 年之前
父節點
當前提交
a8ae9d9561
共有 4 個檔案被更改,包括 64 行新增23 行删除
  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 查看文件

@@ -44,6 +44,7 @@ use common\models\Distribution ;
use backend\models\MailForm ;
use common\models\UserProducer ;
use common\models\UserPointSale ;
use common\models\PointSale ;

/**
* UserController implements the CRUD actions for User model.
@@ -113,17 +114,30 @@ class UserController extends BackendController
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 [
'pointsSaleArray' => $pointsSaleArray
] ;
@@ -161,13 +175,16 @@ class UserController extends BackendController

$model->sendMailWelcome($password) ;
$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,
]));
}

/**
@@ -195,16 +212,16 @@ class UserController extends BackendController
$model->sendMailWelcome($password) ;
}
$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.");
}
return $this->render('update', array_merge($this->initForm($model),[
'model' => $model,
]));
}
/**
@@ -214,12 +231,12 @@ class UserController extends BackendController
*/
public function processLinkPointSale($modelUser)
{
$posts = Yii::$app->request->post() ;
UserPointSale::deleteAll([
'id_user' => $modelUser->id
]) ;
if(is_array($modelUser->points_sale) && count($modelUser->points_sale) > 0) {
foreach($modelUser->points_sale as $pointSaleId) {
echo $modelUser->id.' / '.$pointSaleId.' <br />' ;
$userPointSale = UserPointSale::searchOne([
'id_user' => $modelUser->id,
'id_point_sale' => $pointSaleId
@@ -228,6 +245,7 @@ class UserController extends BackendController
$userPointSale = new UserPointSale ;
$userPointSale->id_user = $modelUser->id ;
$userPointSale->id_point_sale = $pointSaleId ;
$userPointSale->comment = isset($posts['User']['comment_point_sale_'.$pointSaleId]) ? $posts['User']['comment_point_sale_'.$pointSaleId] : '' ;
$userPointSale->save() ;
}
}

+ 12
- 1
backend/views/user/_form.php 查看文件

@@ -53,7 +53,18 @@ use yii\widgets\ActiveForm;
<?= $form->field($model, 'phone')->textInput() ?>
<?= $form->field($model, 'email')->textInput() ?>
<?= $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">
<?= Html::submitButton($model->isNewRecord ? 'Ajouter' : 'Modifier', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>

+ 7
- 0
backend/web/css/screen.css 查看文件

@@ -1977,6 +1977,13 @@ termes.
padding: 5px 10px;
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 */
.producer-update #nav-params {

+ 5
- 0
backend/web/sass/user/_form.scss 查看文件

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

Loading…
取消
儲存