SiteController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace app\controllers;
  3. use app\controllers\actions\PageAction;
  4. use app\models\News;
  5. use Yii;
  6. use yii\data\ActiveDataProvider;
  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\Page;
  13. use yii\filters\HttpCache;
  14. class SiteController extends Controller
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function behaviors()
  20. {
  21. return [
  22. 'access' => [
  23. 'class' => AccessControl::className(),
  24. 'only' => ['logout'],
  25. 'rules' => [
  26. [
  27. 'actions' => ['logout'],
  28. 'allow' => true,
  29. 'roles' => ['@'],
  30. ],
  31. ],
  32. ],
  33. 'verbs' => [
  34. 'class' => VerbFilter::className(),
  35. 'actions' => [
  36. 'logout' => ['post','get'],
  37. ],
  38. ],
  39. ];
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function actions()
  45. {
  46. // return []; ??
  47. $path = \Yii::$app->request->pathInfo;
  48. $pathExploded = explode('/', $path);
  49. // $key = array_filter($pathExploded)[1];
  50. $url = implode('/',array_filter($pathExploded));
  51. //$page = Yii::$app->cache->getOrSet("static_pagesx",function () use ($url){return Page::getPage($url);},60);
  52. $page = Page::getPage($url);
  53. if( $page && $page->status == 0 ){
  54. return [
  55. 'error' => [
  56. 'class' => 'yii\web\ErrorAction',
  57. ],
  58. $url => [
  59. 'class' => PageAction::className(),
  60. 'model' => $page,
  61. 'id' => $page->id
  62. ],
  63. ];
  64. }else{
  65. return [
  66. 'error' => [
  67. 'class' => 'yii\web\ErrorAction',
  68. ],
  69. 'captcha' => [
  70. 'class' => 'yii\captcha\CaptchaAction',
  71. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  72. ],
  73. ];
  74. }
  75. }
  76. public function actionSetfilters( Array $filters )
  77. {
  78. $f = array();
  79. foreach( $filters as $item ){
  80. $filter = (int) $item;
  81. if( $filter ) $f[] = $filter;
  82. }
  83. $session = Yii::$app->session;
  84. $cookies = Yii::$app->response->cookies;
  85. Yii::$app->request->enableCookieValidation = false;
  86. if( count( $f ) == 0 ){
  87. $session->remove('filterx');
  88. $cookies->remove('filterq');
  89. }else{
  90. $session->set('filterx', $f);
  91. $s = \app\models\front\News::keyFilter();
  92. $cookies->add(new \yii\web\Cookie(['name' => 'filterq', 'value' => $s,]));
  93. }
  94. return $this->asJson( ['status'=>'ok', 'ret'=>$f] );
  95. }
  96. public function actionPage( $id )
  97. {
  98. var_dump($id);
  99. }
  100. /**
  101. * Displays homepage.
  102. *
  103. * @return string
  104. */
  105. public function actionIndex()
  106. {
  107. /* $newsDataProvider = new ActiveDataProvider([
  108. "query"=>News::find(),
  109. 'sort'=> [
  110. 'defaultOrder' => ['dt_pub' => SORT_DESC]
  111. ],
  112. ]);
  113. return $this->render('index',['newsDataProvider'=>$newsDataProvider]);
  114. */
  115. $s = \app\models\front\News::keyFilter();
  116. if( $s != '' ){
  117. $this->redirect('/news', 301);
  118. return '';
  119. }
  120. return $this->render('index'); //главная страница
  121. }
  122. /**
  123. * Login action.
  124. *
  125. * @return Response|string
  126. */
  127. public function actionLogin()
  128. {
  129. if (!Yii::$app->user->isGuest) {
  130. return $this->goHome();
  131. }
  132. $model = new LoginForm();
  133. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  134. return $this->goBack();
  135. }
  136. $model->password = '';
  137. return $this->render('login', [
  138. 'model' => $model,
  139. ]);
  140. }
  141. /**
  142. * Logout action.
  143. *
  144. * @return Response
  145. */
  146. public function actionLogout()
  147. {
  148. Yii::$app->user->logout();
  149. return $this->goHome();
  150. }
  151. /**
  152. * Displays contact page.
  153. *
  154. * @return Response|string
  155. */
  156. /*
  157. public function actionContact()
  158. {
  159. $model = new ContactForm();
  160. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  161. Yii::$app->session->setFlash('contactFormSubmitted');
  162. return $this->refresh();
  163. }
  164. return $this->render('contact', [
  165. 'model' => $model,
  166. ]);
  167. }
  168. */
  169. /**
  170. * Displays about page.
  171. *
  172. * @return string
  173. */
  174. public function actionWeather()
  175. {
  176. return $this->render('weather');
  177. }
  178. }