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.

210 satır
6.0KB

  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 BackendController
  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. $model->actif = 1 ;
  84. $model->id_etablissement = Yii::$app->user->identity->id_etablissement ;
  85. $model->saison = 'all' ;
  86. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  87. Upload::uploadFile($model, 'illustration') ;
  88. Upload::uploadFile($model, 'photo') ;
  89. $model->save() ;
  90. // on ajoute un enregistrement ProductionProduit pour chaque production
  91. $productions = Production::find()
  92. ->where('date > '.date('Y-m-d'))
  93. ->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
  94. ->all() ;
  95. foreach($productions as $prod) {
  96. $production_produit = new ProductionProduit ;
  97. $production_produit->id_production = $prod->id ;
  98. $production_produit->id_produit = $model->id ;
  99. $production_produit->actif = 0 ;
  100. $production_produit->save() ;
  101. }
  102. return $this->redirect(['index']);
  103. } else {
  104. return $this->render('create', [
  105. 'model' => $model,
  106. ]);
  107. }
  108. }
  109. /**
  110. * Updates an existing Produit model.
  111. * If update is successful, the browser will be redirected to the 'view' page.
  112. * @param integer $id
  113. * @return mixed
  114. */
  115. public function actionUpdate($id)
  116. {
  117. $request = Yii::$app->request ;
  118. $model = $this->findModel($id);
  119. $illustration_filename_old = $model->illustration ;
  120. $photo_filename_old = $model->photo ;
  121. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  122. Upload::uploadFile($model, 'illustration', $illustration_filename_old) ;
  123. Upload::uploadFile($model, 'photo', $photo_filename_old) ;
  124. $delete_illustration = $request->post('delete_illustration',0) ;
  125. if($delete_illustration) {
  126. $model->illustration = '' ;
  127. $model->save() ;
  128. }
  129. $delete_photo = $request->post('delete_photo',0) ;
  130. if($delete_photo) {
  131. $model->photo = '' ;
  132. $model->save() ;
  133. }
  134. return $this->redirect(['index']);
  135. } else {
  136. return $this->render('update', [
  137. 'model' => $model,
  138. ]);
  139. }
  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. {
  153. $pp->delete() ;
  154. }
  155. return $this->redirect(['index']);
  156. }
  157. public function actionOrdre($tab)
  158. {
  159. $tab_ordre = json_decode(stripslashes($tab));
  160. foreach($tab_ordre as $id => $o)
  161. {
  162. $produit = $this->findModel($id) ;
  163. $produit->order = $o ;
  164. $produit->save() ;
  165. }
  166. }
  167. /**
  168. * Finds the Produit model based on its primary key value.
  169. * If the model is not found, a 404 HTTP exception will be thrown.
  170. * @param integer $id
  171. * @return Produit the loaded model
  172. * @throws NotFoundHttpException if the model cannot be found
  173. */
  174. protected function findModel($id)
  175. {
  176. if (($model = Produit::findOne($id)) !== null) {
  177. return $model;
  178. } else {
  179. throw new NotFoundHttpException('The requested page does not exist.');
  180. }
  181. }
  182. }