123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php
- namespace app\controllers;
- use app\controllers\actions\PageAction;
- use app\models\News;
- use Yii;
- use yii\data\ActiveDataProvider;
- use yii\filters\AccessControl;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use app\models\LoginForm;
- use app\models\ContactForm;
- use app\models\Page;
- use yii\filters\HttpCache;
- class SiteController extends Controller
- {
- /**
- * {@inheritdoc}
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'only' => ['logout'],
- 'rules' => [
- [
- 'actions' => ['logout'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- 'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post','get'],
- ],
- ],
- [
- 'class' => 'yii\filters\HttpCache',
- // 'only' => ['index'],
- 'enabled' => true,
- 'cacheControlHeader' => 'public, max-age=60',
- 'lastModified' => function ($action, $params) {
- return time();
- },
- ],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function actions()
- {
- // return []; ??
- $path = \Yii::$app->request->pathInfo;
- $pathExploded = explode('/', $path);
- // $key = array_filter($pathExploded)[1];
- $url = implode('/',array_filter($pathExploded));
- //$page = Yii::$app->cache->getOrSet("static_pagesx",function () use ($url){return Page::getPage($url);},60);
- $page = Page::getPage($url);
- if( $page && $page->status == 0 ){
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- $url => [
- 'class' => PageAction::className(),
- 'model' => $page,
- 'id' => $page->id
- ],
- ];
- }else{
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
- }
- public function actionSetfilters( Array $filters )
- {
- $f = array();
- foreach( $filters as $item ){
- $filter = (int) $item;
- if( $filter ) $f[] = $filter;
- }
- $session = Yii::$app->session;
- $cookies = Yii::$app->response->cookies;
- Yii::$app->request->enableCookieValidation = false;
- if( count( $f ) == 0 ){
- $session->remove('filterx');
- $cookies->remove('filterq');
- }else{
- $session->set('filterx', $f);
- $s = \app\models\front\News::keyFilter();
- $cookies->add(new \yii\web\Cookie(['name' => 'filterq', 'value' => $s,]));
- }
- return $this->asJson( ['status'=>'ok', 'ret'=>$f] );
- }
- public function actionPage( $id )
- {
- var_dump($id);
- }
- /**
- * Displays homepage.
- *
- * @return string
- */
- public function actionIndex()
- {
- /* $newsDataProvider = new ActiveDataProvider([
- "query"=>News::find(),
- 'sort'=> [
- 'defaultOrder' => ['dt_pub' => SORT_DESC]
- ],
- ]);
- return $this->render('index',['newsDataProvider'=>$newsDataProvider]);
- */
- $s = \app\models\front\News::keyFilter();
- if( $s != '' ){
- $this->redirect('/news', 301);
- return '';
- }
- return $this->render('index'); //главная страница
- }
- /**
- * Login action.
- *
- * @return Response|string
- */
- public function actionLogin()
- {
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- return $this->goBack();
- }
- $model->password = '';
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- /**
- * Logout action.
- *
- * @return Response
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
- /**
- * Displays contact page.
- *
- * @return Response|string
- */
- /*
- public function actionContact()
- {
- $model = new ContactForm();
- if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
- Yii::$app->session->setFlash('contactFormSubmitted');
- return $this->refresh();
- }
- return $this->render('contact', [
- 'model' => $model,
- ]);
- }
- */
- /**
- * Displays about page.
- *
- * @return string
- */
- public function actionWeather()
- {
- return $this->render('weather');
- }
- }
|