|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- <?php
-
-
-
- namespace backend\controllers;
-
- use domain\Product\Accessory\Accessory;
- use yii\base\Response;
- use yii\db\StaleObjectException;
- use yii\filters\AccessControl;
- use yii\web\NotFoundHttpException;
-
- class AccessoryController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return $this->getUserModule()
- ->getAuthorizationChecker()
- ->isGrantedAsProducer($this->getUserCurrent());
- }
- ]
- ],
- ],
- ];
- }
-
- public function actionIndex()
- {
- $accessoryModule = $this->getAccessoryModule();
-
- return $this->render('index', [
- 'searchModel' => $accessoryModule->getBuilder()->instanciateAccessory($this->getProducerCurrent()),
- 'dataProvider' => $accessoryModule->getRepository()->queryAll()->getDataProvider(20),
- ]);
- }
-
- public function actionCreate()
- {
- $accessoryModule = $this->getAccessoryModule();
- $accessory = $accessoryModule->getBuilder()->instanciateAccessory($this->getProducerCurrent());
-
- if ($accessory->load(\Yii::$app->request->post()) && $accessory->validate()) {
- $accessory = $accessoryModule->getManager()->createAccessory(
- $this->getProducerCurrent(),
- $accessory->getName(),
- $accessory->getQuantity()
- );
- $this->setFlash('success', "Accessoire ajouté");
- return $this->redirectAfterSave('accessory', $accessory->getId());
- }
-
- return $this->render('create', [
- 'accessory' => $accessory,
- ]);
- }
-
-
-
- public function actionUpdate(int $id)
- {
- $accessory = $this->findAccessory($id);
-
- if ($accessory->load(\Yii::$app->request->post()) && $accessory->validate() && $accessory->save()) {
- $this->setFlash('success', "Accessoire modifié");
- return $this->redirectAfterSave('accessory', $accessory->getId());
- }
-
- return $this->render('update', [
- 'accessory' => $accessory,
- ]);
- }
-
-
-
- public function actionDelete(int $id): Response
- {
- $accessory = $this->findAccessory($id);
- if($accessory->delete()) {
- $this->setFlash('success', "Accessoire supprimé");
- }
-
- return $this->redirect('index');
- }
-
-
-
- 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é.");
- }
- }
- }
|