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.

205 lines
5.6KB

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