|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\Ajax;
- use yii\filters\AccessControl;
- use yii\helpers\Html;
- use yii\web\NotFoundHttpException;
-
-
- class FeatureAdminController extends BackendController
- {
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::class,
- 'rules' => [
- [
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return $this->getUserModule()
- ->getAuthorizationChecker()
- ->isGrantedAsAdministrator($this->getUserCurrent());
- }
- ]
- ],
- ],
- ];
- }
-
- public function actionIndex()
- {
- $featureModule = $this->getFeatureModule();
- $dataProviderFeatures = $featureModule->getRepository()->queryDefaultAll()->getDataProvider(100);
-
- return $this->render('index', [
- 'producerCurrent' => $this->getProducerCurrent(),
- 'dataProviderFeatures' => $dataProviderFeatures
- ]);
- }
-
- public function actionAjaxToggleStatus($id, $status)
- {
- $featureManager = $this->getFeatureModule()->getManager();
- $feature = $this->findModel($id);
-
- if($status) {
- $featureManager->enableFeature($feature);
- $messageResponse = 'La fonctionnalité "'.Html::encode($feature->name).'" a bien été activée';
- }
- else {
- $featureManager->disableFeature($feature);
- $messageResponse = 'La fonctionnalité "'.Html::encode($feature->name).'" a bien été désactivée';
- }
-
- return Ajax::responseSuccess($messageResponse);
- }
-
- public function actionToggleStatusFeatureProducer(int $id, int $status = null)
- {
- $featureManager = $this->getFeatureModule()->getManager();
- $feature = $this->findModel($id);
-
- if(is_null($status)) {
- $featureManager->defaultFeatureForProducer($feature);
- }
- elseif($status == 0) {
- $featureManager->disableFeatureForProducer($feature);
- }
- elseif($status == 1) {
- $featureManager->enableFeatureForProducer($feature);
- }
-
- return $this->redirectReferer();
- }
-
- public function actionUpdate($id)
- {
- $model = $this->findModel($id);
-
- if ($model->load(\Yii::$app->request->post()) && $model->save()) {
- $this->setFlash('success', 'Fonctionnalité modifiée.');
- return $this->redirect(['index']);
- } else {
- return $this->render('update', [
- 'model' => $model,
- ]);
- }
- }
-
- public function actionPosition()
- {
- $array = \Yii::$app->request->post('array');
- $positionArray = json_decode(stripslashes($array));
-
- foreach ($positionArray as $id => $position) {
- $feature = $this->findModel($id);
- $feature->position = $position;
- $feature->save();
- }
- }
-
- protected function findModel($id)
- {
- $featureModule = $this->getFeatureModule();
- if (($feature = $featureModule->getRepository()->findOneFeatureById($id)) !== null) {
- return $feature;
- } else {
- throw new NotFoundHttpException('La fonctionnalité demandée est introuvable.');
- }
- }
- }
|