Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

152 lines
4.0KB

  1. <?php
  2. namespace backend\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use common\models\LoginForm;
  7. use common\models\User;
  8. use yii\filters\VerbFilter;
  9. use common\models\Produit;
  10. use common\models\PointVente;
  11. use common\models\Etablissement;
  12. use common\models\Production;
  13. use common\models\Commande;
  14. /**
  15. * Site controller
  16. */
  17. class SiteController extends BackendController
  18. {
  19. /**
  20. * @inheritdoc
  21. */
  22. public function behaviors()
  23. {
  24. return [
  25. 'access' => [
  26. 'class' => AccessControl::className(),
  27. 'rules' => [
  28. [
  29. 'actions' => ['login', 'error'],
  30. 'allow' => true,
  31. ],
  32. [
  33. 'actions' => ['logout', 'index'],
  34. 'allow' => true,
  35. 'roles' => ['@'],
  36. 'matchCallback' => function ($rule, $action) {
  37. return Yii::$app->user->identity->status == USER::STATUS_ADMIN
  38. || Yii::$app->user->identity->status == USER::STATUS_BOULANGER;
  39. }
  40. ],
  41. [
  42. 'actions' => ['change-etablissement'],
  43. 'allow' => true,
  44. 'roles' => ['@'],
  45. 'matchCallback' => function ($rule, $action) {
  46. return Yii::$app->user->identity->status == USER::STATUS_ADMIN ;
  47. }
  48. ],
  49. ],
  50. ],
  51. 'verbs' => [
  52. 'class' => VerbFilter::className(),
  53. 'actions' => [
  54. 'logout' => ['post'],
  55. ],
  56. ],
  57. ];
  58. }
  59. /**
  60. * @inheritdoc
  61. */
  62. public function actions()
  63. {
  64. return [
  65. 'error' => [
  66. 'class' => 'yii\web\ErrorAction',
  67. ],
  68. ];
  69. }
  70. public function actionIndex()
  71. {
  72. // commandes
  73. $productions = Production::find()
  74. ->with('commande')
  75. ->where(['>=','production.date',date('Y-m-d')])
  76. ->andWhere([
  77. 'production.id_etablissement' => Yii::$app->user->identity->id_etablissement,
  78. 'production.actif' => 1
  79. ])
  80. ->orderBy('date ASC')
  81. ->limit(5)
  82. ->all() ;
  83. // dernières commandes
  84. $commandes = Commande::findBy([
  85. 'order' => 'DESC',
  86. 'limit' => 15
  87. ]) ;
  88. foreach($commandes as $c)
  89. $c->init() ;
  90. // clients
  91. $nb_clients = User::findBy()->count();
  92. $clients = User::findBy()
  93. ->orderBy('created_at DESC')
  94. ->limit(5)
  95. ->all();
  96. // paramètres
  97. $etablissement = Etablissement::findOne(Yii::$app->user->identity->id_etablissement) ;
  98. return $this->render('index',[
  99. 'productions' => $productions,
  100. 'commandes' => $commandes,
  101. 'clients' => $clients,
  102. 'nb_clients' => $nb_clients,
  103. 'etablissement' => $etablissement,
  104. ]);
  105. }
  106. public function actionLogin()
  107. {
  108. if (!\Yii::$app->user->isGuest) {
  109. return $this->goHome();
  110. }
  111. $model = new LoginForm();
  112. if ($model->load(Yii::$app->request->post()) && $model->login())
  113. {
  114. return $this->goBack();
  115. }
  116. else {
  117. return $this->render('login', [
  118. 'model' => $model,
  119. ]);
  120. }
  121. }
  122. public function actionLogout()
  123. {
  124. Yii::$app->user->logout();
  125. return $this->goHome();
  126. }
  127. public function actionChangeEtablissement($id)
  128. {
  129. Yii::$app->user->identity->id_etablissement = $id ;
  130. Yii::$app->user->identity->save() ;
  131. $this->redirect(['site/index']) ;
  132. }
  133. }