選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ProduitController.php 5.7KB

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