You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 line
4.4KB

  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use common\models\PointVente;
  6. use yii\data\ActiveDataProvider;
  7. use yii\web\Controller;
  8. use yii\web\NotFoundHttpException;
  9. use yii\filters\VerbFilter;
  10. use common\models\User;
  11. use common\models\PointVenteUser ;
  12. /**
  13. * PointVenteController implements the CRUD actions for PointVente model.
  14. */
  15. class PointVenteController extends BackendController
  16. {
  17. public function behaviors()
  18. {
  19. return [
  20. 'verbs' => [
  21. 'class' => VerbFilter::className(),
  22. 'actions' => [
  23. ],
  24. ],
  25. 'access' => [
  26. 'class' => AccessControl::className(),
  27. 'rules' => [
  28. [
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. 'matchCallback' => function ($rule, $action) {
  32. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  33. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  34. }
  35. ],
  36. ],
  37. ],
  38. ];
  39. }
  40. /**
  41. * Lists all PointVente models.
  42. * @return mixed
  43. */
  44. public function actionIndex()
  45. {
  46. $dataProvider = new ActiveDataProvider([
  47. 'query' => PointVente::find()->where(['id_etablissement'=>Yii::$app->user->identity->id_etablissement])
  48. ]);
  49. return $this->render('index', [
  50. 'dataProvider' => $dataProvider,
  51. ]);
  52. }
  53. /**
  54. * Displays a single PointVente model.
  55. * @param integer $id
  56. * @return mixed
  57. */
  58. public function actionView($id)
  59. {
  60. return $this->render('view', [
  61. 'model' => $this->findModel($id),
  62. ]);
  63. }
  64. /**
  65. * Creates a new PointVente model.
  66. * If creation is successful, the browser will be redirected to the 'view' page.
  67. * @return mixed
  68. */
  69. public function actionCreate()
  70. {
  71. $model = new PointVente();
  72. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  73. $model->gestionPointFabrication() ;
  74. $model->gestionAccesRestreint() ;
  75. return $this->redirect(['index']);
  76. } else {
  77. return $this->render('update', array_merge($this->initForm(),[
  78. 'model' => $model,
  79. ]));
  80. }
  81. }
  82. /**
  83. * Updates an existing PointVente model.
  84. * If update is successful, the browser will be redirected to the 'view' page.
  85. * @param integer $id
  86. * @return mixed
  87. */
  88. public function actionUpdate($id)
  89. {
  90. $model = PointVente::find()
  91. ->with('pointVenteUser')
  92. ->where(['id' => $id])
  93. ->one() ;
  94. foreach($model->pointVenteUser as $u)
  95. {
  96. $model->users[] = $u->id_user ;
  97. $model->users_commentaire[$u->id_user] = $u->commentaire ;
  98. }
  99. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  100. $model->gestionPointFabrication() ;
  101. $model->gestionAccesRestreint() ;
  102. return $this->redirect(['index']);
  103. } else {
  104. return $this->render('update', array_merge($this->initForm($id),[
  105. 'model' => $model,
  106. ]));
  107. }
  108. }
  109. public function initForm($id = 0)
  110. {
  111. $users = User::findBy()
  112. ->orderBy('nom ASC')
  113. ->all() ;
  114. return [
  115. 'users' => $users
  116. ] ;
  117. }
  118. /**
  119. * Deletes an existing PointVente model.
  120. * If deletion is successful, the browser will be redirected to the 'index' page.
  121. * @param integer $id
  122. * @return mixed
  123. */
  124. public function actionDelete($id)
  125. {
  126. $this->findModel($id)->delete();
  127. PointVenteUser::deleteAll(['id_point_vente' => $id]) ;
  128. return $this->redirect(['index']);
  129. }
  130. /**
  131. * Finds the PointVente model based on its primary key value.
  132. * If the model is not found, a 404 HTTP exception will be thrown.
  133. * @param integer $id
  134. * @return PointVente the loaded model
  135. * @throws NotFoundHttpException if the model cannot be found
  136. */
  137. protected function findModel($id)
  138. {
  139. if (($model = PointVente::findOne($id)) !== null) {
  140. return $model;
  141. } else {
  142. throw new NotFoundHttpException('The requested page does not exist.');
  143. }
  144. }
  145. }