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.

203 lines
5.7KB

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