[ 'class' => AccessControl::class, 'rules' => [ [ 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserModule() ->getAuthorizationChecker() ->isGrantedAsProducer($this->getUserCurrent()) && $this->getFeatureModule()->getChecker() ->isEnabled(Feature::ALIAS_ROTATING_PRODUCT); } ] ], ], ]; } public function actionIndex() { return $this->render('index', [ 'dataProvider' => $this->getRotatingModule()->getRepository() ->queryRotatings()->getDataProvider(20), ]); } public function actionCreate() { $rotatingModule = $this->getRotatingModule(); $rotatingModel = $rotatingModule->getBuilder()->instanciateRotating($this->getProducerCurrent()); if ($rotatingModel->load(\Yii::$app->request->post()) && $rotatingModel->validate()) { $rotating = $rotatingModule->getManager()->createRotating( $this->getProducerCurrent(), $rotatingModel->getName(), $rotatingModel->getDay(), $rotatingModel->getSelectedProductsIds() ); $this->setFlash('success', "Produit tournant ajouté"); return $this->redirectAfterSave('rotating', $rotating->getId()); } return $this->render('create', [ 'rotating' => $this->initFormModel($rotatingModel), 'productsArray' => $this->findProducts() ]); } public function actionUpdate(int $id) { $rotating = $this->findRotating($id); if ($rotating->load(\Yii::$app->request->post()) && $rotating->validate() && $rotating->save()) { $this->getRotatingModule()->getManager()->manageRotatingProducts( $rotating, $rotating->getSelectedProductsIds() ); return $this->redirectAfterSave('rotating', $rotating->getId()); } return $this->render('update', [ 'rotating' => $this->initFormModel($rotating), 'productsArray' => $this->findProducts() ]); } public function actionDelete(int $id): Response { $rotatingModule = $this->getRotatingModule(); $rotating = $this->findRotating($id); if($rotatingModule->getManager()->deleteRotating($rotating)) { $this->setFlash('success', "Produit tournant supprimé"); } return $this->redirect('index'); } protected function findRotating($id): Rotating { if (($rotating = $this->getRotatingModule()->getRepository()->findOneRotatingById($id)) !== null) { return $rotating; } else { throw new NotFoundHttpException("Le produit tournant n'a pas été trouvé."); } } public function findProducts(): array { return $this->getProductModule()->getRepository()->findProducts(true); } public function initFormModel(Rotating $rotating) { $this->getRotatingModule()->getBuilder()->initSelectedProductsIds($rotating); return $rotating; } /*public function actionCreate() { $accessoryModule = $this->getAccessoryModule(); $accessoryModel = $accessoryModule->getBuilder()->instanciateAccessory($this->getProducerCurrent()); if ($accessoryModel->load(\Yii::$app->request->post()) && $accessoryModel->validate()) { $accessory = $accessoryModule->getManager()->createAccessory( $this->getProducerCurrent(), $accessoryModel->getName(), $accessoryModel->getQuantity() ); $this->afterSave($accessory, $accessoryModel->getSelectedProductsIds()); $this->setFlash('success', "Accessoire ajouté"); return $this->redirectAfterSave('accessory', $accessory->getId()); } return $this->render('create', [ 'accessory' => $this->initFormModel($accessoryModel), 'productsArray' => $this->findProducts() ]); } public function actionUpdate(int $id) { $accessory = $this->findAccessory($id); if ($accessory->load(\Yii::$app->request->post()) && $accessory->validate() && $accessory->save()) { $this->afterSave($accessory, $accessory->getSelectedProductsIds()); return $this->redirectAfterSave('accessory', $accessory->getId()); } return $this->render('update', [ 'accessory' => $this->initFormModel($accessory), 'productsArray' => $this->findProducts() ]); } public function actionDelete(int $id): Response { $accessory = $this->findAccessory($id); if($accessory->delete()) { $this->setFlash('success', "Accessoire supprimé"); } return $this->redirect('index'); } public function afterSave(Accessory $accessory, $selectedProductsIdsArray): void { if(!is_array($selectedProductsIdsArray)) { $selectedProductsIdsArray = []; } $this->getAccessoryModule()->getManager()->manageProducts($accessory, $selectedProductsIdsArray); } public function findProducts(): array { return $this->getProductModule()->getRepository()->findProducts(true); } protected function findAccessory($id): Accessory { if (($accessory = $this->getAccessoryModule()->getRepository()->findOneAccessoryById($id)) !== null) { return $accessory; } else { throw new NotFoundHttpException("L'accessoire n'a pas été trouvé."); } } public function initFormModel(Accessory $accessoryModel) { $this->getAccessoryModule()->getBuilder()->initSelectedProductsIds($accessoryModel); return $accessoryModel; } */ }