[ 'class' => VerbFilter::className(), 'actions' => [ 'delete' => ['post'], ], ], 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return Yii::$app->user->identity->status == USER::STATUS_ADMIN ; } ] ], ], ]; } /** * Lists all Produit models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider([ 'query' => Produit::find()->where('(vrac IS NULL OR vrac = 0)')->orderBy('order ASC'), ]); return $this->render('index', [ 'dataProvider' => $dataProvider, ]); } /** * Displays a single Produit model. * @param integer $id * @return mixed */ public function actionView($id) { return $this->render('view', [ 'model' => $this->findModel($id), ]); } /** * Creates a new Produit model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Produit(); if ($model->load(Yii::$app->request->post()) && $model->save()) { self::uploadFile($model, 'illustration') ; self::uploadFile($model, 'photo') ; // on ajoute un enregistrement ProductionProduit pour chaque production $productions = Production::find()->where('date > '.date('Y-m-d'))->all() ; foreach($productions as $prod) { $production_produit = new ProductionProduit ; $production_produit->id_production = $prod->id ; $production_produit->id_produit = $model->id ; $production_produit->actif = 0 ; $production_produit->save() ; } return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } /** * Updates an existing Produit model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $request = Yii::$app->request ; $model = $this->findModel($id); $illustration_filename_old = $model->illustration ; $photo_filename_old = $model->photo ; if ($model->load(Yii::$app->request->post()) && $model->save()) { self::uploadFile($model, 'illustration', $illustration_filename_old) ; self::uploadFile($model, 'photo', $photo_filename_old) ; $delete_illustration = $request->post('delete_illustration',0) ; if($delete_illustration) { $model->illustration = '' ; $model->save() ; } $delete_photo = $request->post('delete_photo',0) ; if($delete_photo) { $model->photo = '' ; $model->save() ; } return $this->redirect(['index']); } else { return $this->render('update', [ 'model' => $model, ]); } } public static function uploadFile($model, $champs, $filename_old = '') { $file = UploadedFile::getInstance($model, $champs); if($file) { $file_name = $file->baseName.'-'.uniqid().'.' . $file->extension ; $file->saveAs('../../frontend/web/uploads/' . $file_name); $model->$champs = $file_name ; } else { $model->$champs = $filename_old ; } $model->save() ; } /** * Deletes an existing Produit model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $this->findModel($id)->delete(); $productions_produits = ProductionProduit::find()->where(['id_produit'=>$id])->all() ; foreach($productions_produits as $pp) { $pp->delete() ; } return $this->redirect(['index']); } /** * Finds the Produit model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Produit the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Produit::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }