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.

ProduitController.php 5.4KB

vor 8 Jahren
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 ProduitController 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 IS NULL OR vrac = 0)')->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. // on ajoute un enregistrement ProductionProduit pour chaque production
  79. $productions = Production::find()->where('date > '.date('Y-m-d'))->all() ;
  80. foreach($productions as $prod) {
  81. $production_produit = new ProductionProduit ;
  82. $production_produit->id_production = $prod->id ;
  83. $production_produit->id_produit = $model->id ;
  84. $production_produit->actif = 0 ;
  85. $production_produit->save() ;
  86. }
  87. return $this->redirect(['index']);
  88. } else {
  89. return $this->render('create', [
  90. 'model' => $model,
  91. ]);
  92. }
  93. }
  94. /**
  95. * Updates an existing Produit model.
  96. * If update is successful, the browser will be redirected to the 'view' page.
  97. * @param integer $id
  98. * @return mixed
  99. */
  100. public function actionUpdate($id)
  101. {
  102. $request = Yii::$app->request ;
  103. $model = $this->findModel($id);
  104. $illustration_filename_old = $model->illustration ;
  105. $photo_filename_old = $model->photo ;
  106. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  107. self::uploadFile($model, 'illustration', $illustration_filename_old) ;
  108. self::uploadFile($model, 'photo', $photo_filename_old) ;
  109. $delete_illustration = $request->post('delete_illustration',0) ;
  110. if($delete_illustration) {
  111. $model->illustration = '' ;
  112. $model->save() ;
  113. }
  114. $delete_photo = $request->post('delete_photo',0) ;
  115. if($delete_photo) {
  116. $model->photo = '' ;
  117. $model->save() ;
  118. }
  119. return $this->redirect(['index']);
  120. } else {
  121. return $this->render('update', [
  122. 'model' => $model,
  123. ]);
  124. }
  125. }
  126. public static function uploadFile($model, $champs, $filename_old = '') {
  127. $file = UploadedFile::getInstance($model, $champs);
  128. if($file) {
  129. $file_name = $file->baseName.'-'.uniqid().'.' . $file->extension ;
  130. $file->saveAs('../../frontend/web/uploads/' . $file_name);
  131. $model->$champs = $file_name ;
  132. }
  133. else {
  134. $model->$champs = $filename_old ;
  135. }
  136. $model->save() ;
  137. }
  138. /**
  139. * Deletes an existing Produit model.
  140. * If deletion is successful, the browser will be redirected to the 'index' page.
  141. * @param integer $id
  142. * @return mixed
  143. */
  144. public function actionDelete($id)
  145. {
  146. $this->findModel($id)->delete();
  147. $productions_produits = ProductionProduit::find()->where(['id_produit'=>$id])->all() ;
  148. foreach($productions_produits as $pp) {
  149. $pp->delete() ;
  150. }
  151. return $this->redirect(['index']);
  152. }
  153. /**
  154. * Finds the Produit model based on its primary key value.
  155. * If the model is not found, a 404 HTTP exception will be thrown.
  156. * @param integer $id
  157. * @return Produit the loaded model
  158. * @throws NotFoundHttpException if the model cannot be found
  159. */
  160. protected function findModel($id)
  161. {
  162. if (($model = Produit::findOne($id)) !== null) {
  163. return $model;
  164. } else {
  165. throw new NotFoundHttpException('The requested page does not exist.');
  166. }
  167. }
  168. }