Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

198 lines
5.5KB

  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. 'pagination' => [
  52. 'pageSize' => 1000,
  53. ],
  54. ]);
  55. return $this->render('index', [
  56. 'dataProvider' => $dataProvider,
  57. ]);
  58. }
  59. /**
  60. * Displays a single Produit model.
  61. * @param integer $id
  62. * @return mixed
  63. */
  64. public function actionView($id)
  65. {
  66. return $this->render('view', [
  67. 'model' => $this->findModel($id),
  68. ]);
  69. }
  70. /**
  71. * Creates a new Produit model.
  72. * If creation is successful, the browser will be redirected to the 'view' page.
  73. * @return mixed
  74. */
  75. public function actionCreate()
  76. {
  77. $model = new Produit();
  78. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  79. self::uploadFile($model, 'illustration') ;
  80. self::uploadFile($model, 'photo') ;
  81. // on ajoute un enregistrement ProductionProduit pour chaque production
  82. $productions = Production::find()->where('date > '.date('Y-m-d'))->all() ;
  83. foreach($productions as $prod) {
  84. $production_produit = new ProductionProduit ;
  85. $production_produit->id_production = $prod->id ;
  86. $production_produit->id_produit = $model->id ;
  87. $production_produit->actif = 0 ;
  88. $production_produit->save() ;
  89. }
  90. return $this->redirect(['index']);
  91. } else {
  92. return $this->render('create', [
  93. 'model' => $model,
  94. ]);
  95. }
  96. }
  97. /**
  98. * Updates an existing Produit model.
  99. * If update is successful, the browser will be redirected to the 'view' page.
  100. * @param integer $id
  101. * @return mixed
  102. */
  103. public function actionUpdate($id)
  104. {
  105. $request = Yii::$app->request ;
  106. $model = $this->findModel($id);
  107. $illustration_filename_old = $model->illustration ;
  108. $photo_filename_old = $model->photo ;
  109. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  110. self::uploadFile($model, 'illustration', $illustration_filename_old) ;
  111. self::uploadFile($model, 'photo', $photo_filename_old) ;
  112. $delete_illustration = $request->post('delete_illustration',0) ;
  113. if($delete_illustration) {
  114. $model->illustration = '' ;
  115. $model->save() ;
  116. }
  117. $delete_photo = $request->post('delete_photo',0) ;
  118. if($delete_photo) {
  119. $model->photo = '' ;
  120. $model->save() ;
  121. }
  122. return $this->redirect(['index']);
  123. } else {
  124. return $this->render('update', [
  125. 'model' => $model,
  126. ]);
  127. }
  128. }
  129. public static function uploadFile($model, $champs, $filename_old = '') {
  130. $file = UploadedFile::getInstance($model, $champs);
  131. if($file) {
  132. $file_name = $file->baseName.'-'.uniqid().'.' . $file->extension ;
  133. $file->saveAs('../../frontend/web/uploads/' . $file_name);
  134. $model->$champs = $file_name ;
  135. }
  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. {
  149. $this->findModel($id)->delete();
  150. $productions_produits = ProductionProduit::find()->where(['id_produit'=>$id])->all() ;
  151. foreach($productions_produits as $pp) {
  152. $pp->delete() ;
  153. }
  154. return $this->redirect(['index']);
  155. }
  156. /**
  157. * Finds the Produit model based on its primary key value.
  158. * If the model is not found, a 404 HTTP exception will be thrown.
  159. * @param integer $id
  160. * @return Produit the loaded model
  161. * @throws NotFoundHttpException if the model cannot be found
  162. */
  163. protected function findModel($id)
  164. {
  165. if (($model = Produit::findOne($id)) !== null) {
  166. return $model;
  167. } else {
  168. throw new NotFoundHttpException('The requested page does not exist.');
  169. }
  170. }
  171. }