[ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['login', 'error'], 'allow' => true, ], [ 'actions' => ['logout', 'index'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return Yii::$app->user->identity->status == USER::STATUS_ADMIN || Yii::$app->user->identity->status == USER::STATUS_BOULANGER; } ], [ 'actions' => ['change-etablissement'], 'allow' => true, 'roles' => ['@'], 'matchCallback' => function ($rule, $action) { return Yii::$app->user->identity->status == USER::STATUS_ADMIN; } ], ], ], 'verbs' => [ 'class' => VerbFilter::className(), 'actions' => [ 'logout' => ['post'], ], ], ]; } /** * @inheritdoc */ public function actions() { return [ 'error' => [ 'class' => 'yii\web\ErrorAction', ], ]; } /** * Affiche le tableau de bord du backend avec les dernières commandes * réalisée, les dernières inscriptions, la liste des clients ayant un crédit * négatif etc. * * @return mixed */ public function actionIndex() { // commandes $productions = Production::find() ->with('commande') ->where(['>=', 'production.date', date('Y-m-d')]) ->andWhere([ 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement, 'production.actif' => 1 ]) ->orderBy('date ASC') ->limit(5) ->all(); // dernières commandes $commandes = Commande::findBy([ 'orderby' => 'date DESC', 'condition' => 'production.date > \'' . date('Y-m-d 00:00:00') . '\' AND (type = \'' . Commande::TYPE_USER . '\' OR type = \'' . Commande::TYPE_ADMIN . '\' OR (type = \'' . Commande::TYPE_AUTO . '\' AND (date_update IS NOT NULL OR date_delete IS NOT NULL)))' ]); foreach ($commandes as $c) $c->init(); // clients $nb_clients = User::findBy()->count(); $clients = User::findBy() ->orderBy('created_at DESC') ->limit(5) ->all(); $clients_credit_pain_negatif = User::findBy(['id_etablissement' => Yii::$app->user->identity->id_etablissement]) ->andWhere('user_etablissement.credit < 0') ->all(); // paramètres $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement); return $this->render('index', [ 'productions' => $productions, 'commandes' => $commandes, 'clients' => $clients, 'nb_clients' => $nb_clients, 'clients_credit_pain_negatif' => $clients_credit_pain_negatif, 'etablissement' => $etablissement, ]); } /** * Affiche la page de connexion. * * @return mixed */ 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. * * @return mixed */ 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. * * @param integer $id */ public function actionChangeEtablissement($id) { Yii::$app->user->identity->id_etablissement = $id; Yii::$app->user->identity->save(); $this->redirect(['site/index']); } }