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.

196 line
5.9KB

  1. <?php
  2. namespace backend\controllers;
  3. use common\models\ProductionProduit;
  4. use Yii;
  5. use yii\filters\AccessControl;
  6. use common\models\Produit;
  7. use common\models\Production;
  8. use common\models\User;
  9. use yii\data\ActiveDataProvider;
  10. use yii\web\Controller;
  11. use yii\web\NotFoundHttpException;
  12. use yii\filters\VerbFilter;
  13. use yii\web\UploadedFile;
  14. /**
  15. * ProduitController implements the CRUD actions for Produit model.
  16. */
  17. class VracController extends BackendController {
  18. var $enableCsrfValidation = false;
  19. public function behaviors() {
  20. return [
  21. 'verbs' => [
  22. 'class' => VerbFilter::className(),
  23. 'actions' => [
  24. 'delete' => ['post'],
  25. ],
  26. ],
  27. 'access' => [
  28. 'class' => AccessControl::className(),
  29. 'rules' => [
  30. [
  31. 'allow' => true,
  32. 'roles' => ['@'],
  33. 'matchCallback' => function ($rule, $action) {
  34. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  35. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  36. }
  37. ]
  38. ],
  39. ],
  40. ];
  41. }
  42. /**
  43. * Lists all Produit models.
  44. * @return mixed
  45. */
  46. public function actionIndex() {
  47. $dataProvider = new ActiveDataProvider([
  48. 'query' => Produit::find()->where('vrac = 1')->orderBy('order ASC'),
  49. ]);
  50. return $this->render('index', [
  51. 'dataProvider' => $dataProvider,
  52. ]);
  53. }
  54. /**
  55. * Displays a single Produit model.
  56. * @param integer $id
  57. * @return mixed
  58. */
  59. public function actionView($id) {
  60. return $this->render('view', [
  61. 'model' => $this->findModel($id),
  62. ]);
  63. }
  64. /**
  65. * Creates a new Produit model.
  66. * If creation is successful, the browser will be redirected to the 'view' page.
  67. * @return mixed
  68. */
  69. public function actionCreate() {
  70. $model = new Produit();
  71. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  72. self::uploadFile($model, 'illustration');
  73. self::uploadFile($model, 'photo');
  74. $model->vrac = 1;
  75. $model->lundi = 1;
  76. $model->mardi = 1;
  77. $model->mercredi = 1;
  78. $model->jeudi = 1;
  79. $model->vendredi = 1;
  80. $model->samedi = 1;
  81. $model->dimanche = 1;
  82. $model->save();
  83. // on ajoute un enregistrement ProductionProduit pour chaque production
  84. $productions = Production::find()->where('date > ' . date('Y-m-d'))->all();
  85. foreach ($productions as $prod) {
  86. $production_produit = new ProductionProduit;
  87. $production_produit->id_production = $prod->id;
  88. $production_produit->id_produit = $model->id;
  89. $production_produit->actif = 0;
  90. $production_produit->save();
  91. }
  92. return $this->redirect(['index']);
  93. } else {
  94. return $this->render('create', [
  95. 'model' => $model,
  96. ]);
  97. }
  98. }
  99. /**
  100. * Updates an existing Produit model.
  101. * If update is successful, the browser will be redirected to the 'view' page.
  102. * @param integer $id
  103. * @return mixed
  104. */
  105. public function actionUpdate($id) {
  106. $request = Yii::$app->request;
  107. $model = $this->findModel($id);
  108. $illustration_filename_old = $model->illustration;
  109. $photo_filename_old = $model->photo;
  110. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  111. self::uploadFile($model, 'illustration', $illustration_filename_old);
  112. self::uploadFile($model, 'photo', $photo_filename_old);
  113. $delete_illustration = $request->post('delete_illustration', 0);
  114. if ($delete_illustration) {
  115. $model->illustration = '';
  116. $model->save();
  117. }
  118. $delete_photo = $request->post('delete_photo', 0);
  119. if ($delete_photo) {
  120. $model->photo = '';
  121. $model->save();
  122. }
  123. return $this->redirect(['index']);
  124. } else {
  125. return $this->render('update', [
  126. 'model' => $model,
  127. ]);
  128. }
  129. }
  130. public static function uploadFile($model, $champs, $filename_old = '') {
  131. $file = UploadedFile::getInstance($model, $champs);
  132. if ($file) {
  133. $file_name = $file->baseName . '-' . uniqid() . '.' . $file->extension;
  134. $file->saveAs('../../frontend/web/uploads/' . $file_name);
  135. $model->$champs = $file_name;
  136. } else {
  137. $model->$champs = $filename_old;
  138. }
  139. $model->save();
  140. }
  141. /**
  142. * Deletes an existing Produit model.
  143. * If deletion is successful, the browser will be redirected to the 'index' page.
  144. * @param integer $id
  145. * @return mixed
  146. */
  147. public function actionDelete($id) {
  148. $this->findModel($id)->delete();
  149. $productions_produits = ProductionProduit::find()->where(['id_produit' => $id])->all();
  150. foreach ($productions_produits as $pp) {
  151. $pp->delete();
  152. }
  153. return $this->redirect(['index']);
  154. }
  155. /**
  156. * Finds the Produit model based on its primary key value.
  157. * If the model is not found, a 404 HTTP exception will be thrown.
  158. * @param integer $id
  159. * @return Produit the loaded model
  160. * @throws NotFoundHttpException if the model cannot be found
  161. */
  162. protected function findModel($id) {
  163. if (($model = Produit::findOne($id)) !== null) {
  164. return $model;
  165. } else {
  166. throw new NotFoundHttpException('The requested page does not exist.');
  167. }
  168. }
  169. }