Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VracController.php 5.6KB

vor 8 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. }