Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

161 line
4.3KB

  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. }
  98. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  99. $model->gestionPointFabrication() ;
  100. $model->gestionAccesRestreint() ;
  101. return $this->redirect(['index']);
  102. } else {
  103. return $this->render('update', array_merge($this->initForm($id),[
  104. 'model' => $model,
  105. ]));
  106. }
  107. }
  108. public function initForm($id = 0)
  109. {
  110. $users = User::find()->orderBy('nom ASC')->all() ;
  111. return [
  112. 'users' => $users
  113. ] ;
  114. }
  115. /**
  116. * Deletes an existing PointVente model.
  117. * If deletion is successful, the browser will be redirected to the 'index' page.
  118. * @param integer $id
  119. * @return mixed
  120. */
  121. public function actionDelete($id)
  122. {
  123. $this->findModel($id)->delete();
  124. PointVenteUser::deleteAll(['id_point_vente' => $id]) ;
  125. return $this->redirect(['index']);
  126. }
  127. /**
  128. * Finds the PointVente model based on its primary key value.
  129. * If the model is not found, a 404 HTTP exception will be thrown.
  130. * @param integer $id
  131. * @return PointVente the loaded model
  132. * @throws NotFoundHttpException if the model cannot be found
  133. */
  134. protected function findModel($id)
  135. {
  136. if (($model = PointVente::findOne($id)) !== null) {
  137. return $model;
  138. } else {
  139. throw new NotFoundHttpException('The requested page does not exist.');
  140. }
  141. }
  142. }