Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

157 lines
5.0KB

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