[ 'class' => VerbFilter::className(), 'actions' => [ ], ], 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return User::hasAccessBackend(); } ] ], ], ]; } /** * Liste les modèles Produit. * * @return mixed */ public function actionIndex() { $searchModel = new ProductSearch() ; $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', [ 'searchModel' => $searchModel, 'dataProvider' => $dataProvider, ]); } /** * Crée un modèle Produit. * Si la création réussit, le navigateur est redirigé vers la page 'index'. * * @return mixed */ public function actionCreate() { $model = new Product(); $model->active = 1; $model->id_producer = Producer::getId(); if ($model->load(Yii::$app->request->post()) && $model->save()) { Upload::uploadFile($model, 'photo'); $model->save(); // link product / distribution Distribution::linkProductIncomingDistributions($model) ; Yii::$app->getSession()->setFlash('success', 'Produit '.Html::encode($model->name).' ajouté'); return $this->redirect(['index']); } else { return $this->render('create', [ 'model' => $model, ]); } } /** * Modifie un modèle Produit existant. * Si la modification réussit, le navigateur est redirigé vers la page 'index'. * * @param integer $id * @return mixed */ public function actionUpdate($id) { $request = Yii::$app->request; $model = $this->findModel($id); $photoFilenameOld = $model->photo; if ($model->load(Yii::$app->request->post()) && $model->save()) { Upload::uploadFile($model, 'photo', $photoFilenameOld); $deletePhoto = $request->post('delete_photo', 0); if ($deletePhoto) { $model->photo = ''; $model->save(); } if($model->apply_distributions) { // link product / distribution Distribution::linkProductIncomingDistributions($model) ; } Yii::$app->getSession()->setFlash('success', 'Produit '.Html::encode($model->name).' modifié'); return $this->redirect(['index']); } else { return $this->render('update', [ 'model' => $model, ]); } } /** * Supprime un modèle Produit. * Si la suppression réussit, le navigateur est redirigé vers la page * 'index'. * * @param integer $id * @return mixed */ public function actionDelete($id) { $model = $this->findModel($id) ; $model->delete(); ProductDistribution::deleteAll(['id_product' => $id]) ; Yii::$app->getSession()->setFlash('success', 'Produit '.Html::encode($model->name).' supprimé'); return $this->redirect(['index']); } /** * Modifie l'ordre des produits. * * @param array $array */ public function actionOrder($array) { $orderArray = json_decode(stripslashes($array)); foreach($orderArray as $id => $order) { $product = $this->findModel($id); $product->order = $order; $product->save(); } } /** * Recherche un produit en fonction de son ID. * * @param integer $id * @return Produit * @throws NotFoundHttpException si le modèle n'est pas trouvé */ protected function findModel($id) { if (($model = Product::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } } }