|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\GlobalParam;
- use common\logic\Product\Product\Model\Product;
- use common\logic\Product\ProductCategory\Model\ProductCategorySearch;
- use Yii;
- use yii\filters\AccessControl;
- use yii\web\NotFoundHttpException;
- use yii\filters\VerbFilter;
- use yii\helpers\Html;
-
-
- class ProductCategoryController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'verbs' => [
- 'class' => VerbFilter::class,
- 'actions' => [
- ],
- ],
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return $this->getUserManager()->hasAccessBackend();
- }
- ],
- ],
- ],
- ];
- }
-
-
-
- public function actionIndex()
- {
- $searchModel = new ProductCategorySearch();
- $dataProvider = $searchModel->search(\Yii::$app->request->queryParams);
-
- return $this->render('index', [
- 'searchModel' => $searchModel,
- 'dataProvider' => $dataProvider,
- ]);
- }
-
-
-
- public function actionCreate()
- {
- $productCategoryManager = $this->getProductCategoryManager();
- $productCategory = $productCategoryManager->instanciateProductCategory();
-
- $productCategory->id_producer = GlobalParam::getCurrentProducerId();
-
- if ($productCategory->load(\Yii::$app->request->post()) && $productCategoryManager->saveCreate($productCategory)) {
- $this->setFlash('success', "Catégorie ajoutée.");
- return $this->redirect(['index']);
- } else {
- return $this->render('create', [
- 'model' => $productCategory,
- ]);
- }
- }
-
-
-
- public function actionUpdate(int $id)
- {
- $productCategoryManager = $this->getProductCategoryManager();
- $productCategory = $this->findModel($id);
-
- if ($productCategory->load(\Yii::$app->request->post()) && $productCategoryManager->saveUpdate($productCategory)) {
- $this->setFlash('success', "Catégorie modifiée.");
- return $this->redirect(['index']);
- } else {
- return $this->render('update', [
- 'model' => $productCategory,
- ]);
- }
- }
-
-
-
- public function actionDelete($id)
- {
- $productCategoryManager = $this->getProductCategoryManager();
- $productCategory = $this->findModel($id);
-
- $productCategoryManager->delete($productCategory);
- Product::updateAll(['id_product_category' => null], ['id_product_category' => $id]);
-
- $this->setFlash('success', 'Catégorie <strong>' . Html::encode($productCategory->name) . '</strong> supprimée.');
-
- return $this->redirect(['index']);
- }
-
-
-
- public function actionPosition()
- {
- $array = Yii::$app->request->post('array');
- $positionArray = json_decode(stripslashes($array));
-
- foreach ($positionArray as $id => $position) {
- $category = $this->findModel($id);
- $category->position = $position;
- $category->save();
- }
- }
-
-
-
- protected function findModel($id)
- {
- $productCategoryManager = $this->getProductCategoryManager();
- if (($productCategory = $productCategoryManager->findOneProductCategoryById($id)) !== null) {
- return $productCategory;
- } else {
- throw new NotFoundHttpException('The requested page does not exist.');
- }
- }
-
- }
|