|
- <?php
-
-
-
- namespace backend\controllers;
-
- use common\helpers\GlobalParam;
- use Yii;
- use yii\filters\AccessControl;
- use yii\web\Controller;
- use common\models\LoginForm;
- use common\models\User;
- use yii\filters\VerbFilter;
- use common\models\Product;
- use common\models\PointSale;
- use common\models\Producer;
- use common\models\Distribution;
- use common\models\Order;
-
-
- class SiteController extends BackendController
- {
-
-
-
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'rules' => [
- [
- 'actions' => ['login', 'error'],
- 'allow' => true,
- ],
- [
- 'actions' => ['logout', 'index'],
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::hasAccessBackend() ;
- }
- ],
- [
- 'actions' => ['change-producer'],
- 'allow' => true,
- 'roles' => ['@'],
- 'matchCallback' => function ($rule, $action) {
- return User::getCurrentStatus() == User::STATUS_ADMIN ;
- }
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- ],
- ],
- ];
- }
-
-
-
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- ];
- }
-
-
-
- public function actionIndex()
- {
-
- $distributionsArray = Distribution::find()
- ->with('order')
- ->where(['>=', 'distribution.date', date('Y-m-d')])
- ->andWhere([
- 'distribution.id_producer' => GlobalParam::getCurrentProducerId(),
- 'distribution.active' => 1
- ])
- ->orderBy('date ASC')
- ->limit(3)
- ->all();
-
-
- $ordersArray = Order::searchAll([],[
- 'orderby' => 'date DESC',
- 'conditions' => 'distribution.date > :date 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' => [':date' => date('Y-m-d 00:00:00')],
- ]);
-
-
- $nbUsers = User::searchCount();
-
- $usersArray = User::findBy()
- ->orderBy('created_at DESC')
- ->limit(5)
- ->all();
-
- $usersNegativeCredit = User::findBy(['id_producer' => GlobalParam::getCurrentProducerId()])
- ->andWhere('user_producer.credit < 0')
- ->all();
-
-
- $producer = GlobalParam::getCurrentProducer();
-
- $productsCount = Product::searchCount() ;
- $pointsSaleCount = PointSale::searchCount() ;
-
- return $this->render('index', [
- 'distributionsArray' => $distributionsArray,
- 'ordersArray' => $ordersArray,
- 'usersArray' => $usersArray,
- 'nbUsers' => $nbUsers,
- 'usersNegativeCredit' => $usersNegativeCredit,
- 'producer' => $producer,
- 'productsCount' => $productsCount,
- 'pointsSaleCount' => $pointsSaleCount
- ]);
- }
-
-
-
- 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,
- ]);
- }
- }
-
-
-
- public function actionLogout()
- {
- Yii::$app->user->logout();
-
- return $this->goHome();
- }
-
-
-
- public function actionChangeProducer($id)
- {
- Yii::$app->user->identity->id_producer = $id;
- Yii::$app->user->identity->save();
- $this->redirect(['site/index']);
- }
-
- }
|