197 linhas
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. var $enableCsrfValidation = false;
  21. public function behaviors() {
  22. return [
  23. 'verbs' => [
  24. 'class' => VerbFilter::className(),
  25. 'actions' => [
  26. ],
  27. ],
  28. 'access' => [
  29. 'class' => AccessControl::className(),
  30. 'rules' => [
  31. [
  32. 'allow' => true,
  33. 'roles' => ['@'],
  34. 'matchCallback' => function ($rule, $action) {
  35. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  36. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  37. }
  38. ]
  39. ],
  40. ],
  41. ];
  42. }
  43. /**
  44. * Lists all Produit models.
  45. * @return mixed
  46. */
  47. public function actionIndex() {
  48. $dataProvider = new ActiveDataProvider([
  49. 'query' => Produit::find()
  50. ->where('(vrac IS NULL OR vrac = 0)')
  51. ->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
  52. ->orderBy('order ASC'),
  53. 'pagination' => [
  54. 'pageSize' => 1000,
  55. ],
  56. ]);
  57. return $this->render('index', [
  58. 'dataProvider' => $dataProvider,
  59. ]);
  60. }
  61. /**
  62. * Displays a single Produit model.
  63. * @param integer $id
  64. * @return mixed
  65. */
  66. public function actionView($id) {
  67. return $this->render('view', [
  68. 'model' => $this->findModel($id),
  69. ]);
  70. }
  71. /**
  72. * Creates a new Produit model.
  73. * If creation is successful, the browser will be redirected to the 'view' page.
  74. * @return mixed
  75. */
  76. public function actionCreate() {
  77. $model = new Produit();
  78. $model->actif = 1;
  79. $model->id_etablissement = Yii::$app->user->identity->id_etablissement;
  80. $model->saison = 'all';
  81. if ($model->load(Yii::$app->request->post()) && $model->save()) {
  82. Upload::uploadFile($model, 'illustration');
  83. Upload::uploadFile($model, 'photo');
  84. $model->save();
  85. // on ajoute un enregistrement ProductionProduit pour chaque production
  86. $productions = Production::find()
  87. ->where('date > ' . date('Y-m-d'))
  88. ->andWhere(['id_etablissement' => Yii::$app->user->identity->id_etablissement])
  89. ->all();
  90. foreach ($productions as $prod) {
  91. $production_produit = new ProductionProduit;
  92. $production_produit->id_production = $prod->id;
  93. $production_produit->id_produit = $model->id;
  94. $production_produit->actif = 0;
  95. $production_produit->save();
  96. }
  97. return $this->redirect(['index']);
  98. } else {
  99. return $this->render('create', [
  100. 'model' => $model,
  101. ]);
  102. }
  103. }
  104. /**
  105. * Updates an existing Produit model.
  106. * If update is successful, the browser will be redirected to the 'view' page.
  107. * @param integer $id
  108. * @return mixed
  109. */
  110. public function actionUpdate($id) {
  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. $this->findModel($id)->delete();
  143. $productions_produits = ProductionProduit::find()->where(['id_produit' => $id])->all();
  144. foreach ($productions_produits as $pp) {
  145. $pp->delete();
  146. }
  147. return $this->redirect(['index']);
  148. }
  149. public function actionOrdre($tab) {
  150. $tab_ordre = json_decode(stripslashes($tab));
  151. foreach ($tab_ordre as $id => $o) {
  152. $produit = $this->findModel($id);
  153. $produit->order = $o;
  154. $produit->save();
  155. }
  156. }
  157. /**
  158. * Finds the Produit model based on its primary key value.
  159. * If the model is not found, a 404 HTTP exception will be thrown.
  160. * @param integer $id
  161. * @return Produit the loaded model
  162. * @throws NotFoundHttpException if the model cannot be found
  163. */
  164. protected function findModel($id) {
  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. }