[
'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 ".Html::encode($rotating->getName())." ajouté");
return $this->redirectAfterSave('rotating', $rotating->getId());
}
return $this->render('create', [
'rotatingModel' => $rotatingModel,
'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()) {
$this->getRotatingModule()->getManager()->updateRotating(
$rotating,
$rotating->getSelectedProductsIds()
);
$this->setFlash('success', "Produit tournant ".Html::encode($rotating->getName())." modifié");
return $this->redirectAfterSave('rotating', $rotating->getId());
}
return $this->render('update', [
'rotatingModel' => $rotating,
'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;
}
}