SiteController.php 4.9 KB

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