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.

207 satır
5.8KB

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