Browse Source

[Administration] Refactoring SiteController

feature/souke
Guillaume Bourgeois 1 year ago
parent
commit
47e8794ee5
3 changed files with 21 additions and 14 deletions
  1. +13
    -12
      backend/controllers/SiteController.php
  2. +2
    -2
      backend/views/layouts/header.php
  3. +6
    -0
      common/logic/User/User/Service/UserBuilder.php

+ 13
- 12
backend/controllers/SiteController.php View File



namespace backend\controllers; namespace backend\controllers;


use common\helpers\Alwaysdata;
use common\helpers\GlobalParam;
use common\logic\Distribution\Distribution\Model\Distribution;
use common\logic\Order\Order\Model\Order;
use common\logic\PointSale\PointSale\Model\PointSale;
use common\logic\Product\Product\Model\Product;
use common\logic\Product\Product\Wrapper\ProductContainer;
use common\logic\User\User\Model\User; use common\logic\User\User\Model\User;
use Yii; use Yii;
use yii\filters\AccessControl; use yii\filters\AccessControl;
'allow' => true, 'allow' => true,
], ],
[ [
'actions' => ['logout', 'index'],
'actions' => ['logout'],
'allow' => true, 'allow' => true,
'roles' => ['@'], 'roles' => ['@'],
'matchCallback' => function ($rule, $action) { 'matchCallback' => function ($rule, $action) {
} }
], ],
[ [
'actions' => ['change-producer'],
'actions' => ['switch-producer'],
'allow' => true, 'allow' => true,
'roles' => ['@'], 'roles' => ['@'],
'matchCallback' => function ($rule, $action) { 'matchCallback' => function ($rule, $action) {
* Change le producteur courant de l'utilisateur connecté. * Change le producteur courant de l'utilisateur connecté.
* Permet de passer d'un producteur à un autre en tant qu'administrateur. * Permet de passer d'un producteur à un autre en tant qu'administrateur.
*/ */
public function actionChangeProducer(int $id)
public function actionSwitchProducer(int $id)
{ {
Yii::$app->user->identity->id_producer = $id;
Yii::$app->user->identity->save();
$user = $this->getUserCurrent();
$producer = $this->getProducerContainer()->getRepository()->findOneProducerById($id);

if($producer) {
$this->getUserContainer()->getBuilder()->switchProducer($user, $producer);
}
else {
$this->addFlash('error', 'Producteur introuvable.');
}

$this->redirect(['dashboard/index']); $this->redirect(['dashboard/index']);
} }
} }

+ 2
- 2
backend/views/layouts/header.php View File

<?php foreach ($producersArray as $producer): ?> <?php foreach ($producersArray as $producer): ?>
<?php if ($producer->active == 1): ?> <?php if ($producer->active == 1): ?>
<li class="producer"> <li class="producer">
<a href="<?= Yii::$app->urlManagerBackend->createUrl(['site/change-producer', 'id' => $producer->id]); ?>"><?= Html::encode($producer->name) ?></a>
<a href="<?= Yii::$app->urlManagerBackend->createUrl(['site/switch-producer', 'id' => $producer->id]); ?>"><?= Html::encode($producer->name) ?></a>
</li> </li>
<?php endif; ?> <?php endif; ?>
<?php endforeach; ?> <?php endforeach; ?>
<?php foreach ($producersArray as $producer): ?> <?php foreach ($producersArray as $producer): ?>
<?php if ($producer->active != 1): ?> <?php if ($producer->active != 1): ?>
<li class="producer"> <li class="producer">
<a href="<?= Yii::$app->urlManagerBackend->createUrl(['site/change-producer', 'id' => $producer->id]); ?>">
<a href="<?= Yii::$app->urlManagerBackend->createUrl(['site/switch-producer', 'id' => $producer->id]); ?>">
<label class="label label-danger">Hors-ligne</label> <label class="label label-danger">Hors-ligne</label>
<?= Html::encode($producer->name) ?> <?= Html::encode($producer->name) ?>
</a> </a>

+ 6
- 0
common/logic/User/User/Service/UserBuilder.php View File



return false; return false;
} }

public function switchProducer(User $user, Producer $producer)
{
$user->id_producer = $producer->id;
$this->update($user);
}
} }

Loading…
Cancel
Save