|
- <?php
-
-
-
- namespace producer\controllers;
-
- use common\controllers\CommonController;
- use common\helpers\GlobalParam;
- use domain\Producer\Producer\Producer;
- use domain\User\UserProducer\UserProducer;
-
- class ProducerBaseController extends CommonController
- {
- var $producer ;
-
-
-
- public function behaviors()
- {
- return [];
- }
-
- public function actions()
- {
- return [];
- }
-
- public function beforeAction($event)
- {
- $producer = $this->getProducerCurrent() ;
-
- if(!\Yii::$app->params['maintenanceMode']) {
- $this->getLogic()->setProducerContext($producer);
-
- $userProducer = UserProducer::find()
- ->where([
- 'id_user' => GlobalParam::getCurrentUserId(),
- 'id_producer' => $producer->id
- ])
- ->one() ;
-
-
-
- if(!$producer->active && (\Yii::$app->user->isGuest || \Yii::$app->user->identity->id_producer != $producer->id)) {
- $this->redirect(\Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer-offline','id' => $producer->id])) ;
- }
-
-
-
- if(strlen($producer->code)) {
-
-
-
- if(\Yii::$app->user->isGuest) {
- $this->redirect(\Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer','id' => $producer->id])) ;
- }
-
- else {
- if(!$userProducer || ($userProducer && !$userProducer->active)) {
- $this->redirect(\Yii::$app->urlManagerFrontend->createAbsoluteUrl(['site/producer-code','id' => $producer->id])) ;
- }
- }
- }
- }
-
- return parent::beforeAction($event);
- }
-
-
-
- public function getProducerCurrent() : ?Producer
- {
- if($this->producer) {
- return $this->producer ;
- }
- else {
- $slug = \Yii::$app->getRequest()->getQueryParam('slug_producer');
- $producer = Producer::find()
- ->with('contact')
- ->where(['slug' => $slug])
- ->one() ;
-
- if($producer) {
- $this->producer = $producer ;
- return $this->producer ;
- }
- else {
- if($this->action->id != 'maintenance') {
- throw new \yii\web\HttpException(404, 'Producteur introuvable ('.$slug.')');
- }
- else {
- return null;
- }
- }
- }
- }
- }
-
- ?>
|