Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

192 linhas
5.4KB

  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. use common\helpers\Upload;
  16. /**
  17. * ProduitController implements the CRUD actions for Produit model.
  18. */
  19. class ProduitController extends Controller
  20. {
  21. var $enableCsrfValidation = false ;
  22. public function behaviors()
  23. {
  24. return [
  25. 'verbs' => [
  26. 'class' => VerbFilter::className(),
  27. 'actions' => [
  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_etablissement'=>Yii::$app->user->identity->id_etablissement])
  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. Upload::uploadFile($model, 'illustration') ;
  85. Upload::uploadFile($model, 'photo') ;
  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. Upload::uploadFile($model, 'illustration', $illustration_filename_old) ;
  117. Upload::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. /**
  136. * Deletes an existing Produit model.
  137. * If deletion is successful, the browser will be redirected to the 'index' page.
  138. * @param integer $id
  139. * @return mixed
  140. */
  141. public function actionDelete($id)
  142. {
  143. $this->findModel($id)->delete();
  144. $productions_produits = ProductionProduit::find()->where(['id_produit'=>$id])->all() ;
  145. foreach($productions_produits as $pp)
  146. {
  147. $pp->delete() ;
  148. }
  149. return $this->redirect(['index']);
  150. }
  151. /**
  152. * Finds the Produit model based on its primary key value.
  153. * If the model is not found, a 404 HTTP exception will be thrown.
  154. * @param integer $id
  155. * @return Produit the loaded model
  156. * @throws NotFoundHttpException if the model cannot be found
  157. */
  158. protected function findModel($id)
  159. {
  160. if (($model = Produit::findOne($id)) !== null) {
  161. return $model;
  162. } else {
  163. throw new NotFoundHttpException('The requested page does not exist.');
  164. }
  165. }
  166. }