[ 'class' => AccessControl::class, 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'actions' => ['logout', 'index'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserManager()->hasAccessBackend(); } ], [ 'actions' => ['change-producer'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return $this->getUserManager()->getCurrentStatus() == User::STATUS_ADMIN; } ], ], ], 'verbs' => [ 'class' => VerbFilter::class, 'actions' => [ ], ], ]; } /** * @inheritdoc */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], ]; } /** * Affiche le tableau de bord du backend avec les dernières commandes * réalisées, les dernières inscriptions, la liste des clients ayant un crédit * négatif etc. * * @return mixed */ public function actionIndex() { $userManager = $this->getUserManager(); $producerManager = $this->getProducerManager(); $optionDashboardNumberDistributions = $producerManager->getConfig('option_dashboard_number_distributions'); $dashboardNumberDistributions = $optionDashboardNumberDistributions ? $optionDashboardNumberDistributions : 3; $optionDashboardDateStart = $producerManager->getConfig('option_dashboard_date_start'); $optionDashboardDateEnd = $producerManager->getConfig('option_dashboard_date_end'); $queryDistributions = Distribution::find()->with('order'); if ($optionDashboardDateStart || $optionDashboardDateEnd) { if ($optionDashboardDateStart) { $queryDistributions->andWhere(['>=', 'distribution.date', $optionDashboardDateStart]); } if ($optionDashboardDateEnd) { $queryDistributions->andWhere(['<=', 'distribution.date', $optionDashboardDateEnd]); } } else { $queryDistributions->andWhere(['>=', 'distribution.date', date('Y-m-d')]); } $distributionsArray = $queryDistributions->andWhere([ 'distribution.id_producer' => GlobalParam::getCurrentProducerId(), 'distribution.active' => 1 ]) ->orderBy('date ASC') ->limit($dashboardNumberDistributions) ->all(); // dernières commandes $paramsOrders = []; if ($optionDashboardDateStart || $optionDashboardDateEnd) { $conditionsOrders = ''; if ($optionDashboardDateStart) { $conditionsOrders .= 'distribution.date >= :date_start'; $paramsOrders[':date_start'] = $optionDashboardDateStart; } if ($optionDashboardDateEnd) { if ($optionDashboardDateStart) { $conditionsOrders .= ' AND '; } $conditionsOrders .= 'distribution.date <= :date_end'; $paramsOrders[':date_end'] = $optionDashboardDateEnd; } } else { $conditionsOrders = 'distribution.date >= :date_start'; $paramsOrders[':date_start'] = date('Y-m-d 00:00:00'); } $ordersArray = Order::searchAll([], [ 'orderby' => 'date DESC', 'conditions' => $conditionsOrders . ' AND (origin = \'' . Order::ORIGIN_USER . '\' OR origin = \'' . Order::ORIGIN_ADMIN . '\' OR (origin = \'' . Order::ORIGIN_AUTO . '\' AND (date_update IS NOT NULL OR date_delete IS NOT NULL)))', 'params' => $paramsOrders, ]); // clients $usersArray = $userManager->queryUsersBy() ->orderBy('created_at DESC') ->limit(5) ->all(); $usersNegativeCredit = $userManager->queryUsersBy(['id_producer' => GlobalParam::getCurrentProducerId()]) ->andWhere('user_producer.credit < 0') ->all(); $producerCurrent = GlobalParam::getCurrentProducer(); $productsCount = Product::searchCount(); $pointsSaleCount = PointSale::searchCount(); return $this->render('index', [ 'distributionsArray' => $distributionsArray, 'ordersArray' => $ordersArray, 'usersArray' => $usersArray, 'usersNegativeCredit' => $usersNegativeCredit, 'producer' => $producerCurrent, 'productsCount' => $productsCount, 'pointsSaleCount' => $pointsSaleCount ]); } /** * Affiche la page de connexion. */ public function actionLogin() { if (!\Yii::$app->user->isGuest) { return $this->goHome(); } $model = new LoginForm(); if ($model->load(\Yii::$app->request->post()) && $model->login()) { return $this->goBack(); } else { return $this->render('login', [ 'model' => $model, ]); } } /** * Déconnecte l'utilisateur et le redirige à la page d'accueil. */ public function actionLogout() { Yii::$app->user->logout(); return $this->goHome(); } /** * Change le producteur courant de l'utilisateur connecté. * Permet de passer d'un producteur à un autre en tant qu'administrateur. */ public function actionChangeProducer(int $id) { Yii::$app->user->identity->id_producer = $id; Yii::$app->user->identity->save(); $this->redirect(['site/index']); } }