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.

VracController.php 5.7KB

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