Blame view

app/controllers/_SiteController.php 3.48 KB
bf807468   Alex Savenko   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  <?php
  
  namespace app\controllers;
  
  use Yii;
  use yii\filters\AccessControl;
  use yii\web\Controller;
  use yii\filters\VerbFilter;
  use app\models\LoginForm;
  use app\models\ContactForm;
  use yii\rbac\DbManager;
  use yii\web\Response;
  
  class SiteController extends Controller
  {
      public function behaviors()
      {
          return [
              'access' => [
                  'class' => AccessControl::className(),
                  'only' => ['about','contact'],
                  'rules' => [
                      [
                          'actions' => ['about'],
                          'allow' => true,
                          'roles' => ['admin'],
                      ],
                      [
                          'actions' => ['contact'],
                          'allow' => true,
                          'roles' => ['admin1'],
                      ],						
                  ],
              ],
              'verbs' => [
                  'class' => VerbFilter::className(),
                  'actions' => [
                      'logout' => ['post'],
                  ],
              ],
          ];
      }
  
      public function actions()
      {
          return [
              'error' => [
                  'class' => 'yii\web\ErrorAction',
              ],
              'captcha' => [
                  'class' => 'yii\captcha\CaptchaAction',
                  'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
              ],
          ];
      }
  
      public function actionIndex()
      {
          
  //$userRole = Yii::$app->authManager->getRole('admin');
  //Yii::$app->authManager->assign($userRole, 1);
  /*
  $auth = new DbManager;
  $auth->init();
  $role = $auth->createRole('admin');
  $auth->add($role);
  
  $auth->assign($role, 1);
  */
  
  		$model = new LoginForm();
  		return $this->render('index', [
                  'model' => $model,
              ]);
      }
  
      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();
          } else {
              return $this->render('login', [
                  'model' => $model,
              ]);
          }
      }
  
      public function actionLogout()
      {
          Yii::$app->user->logout();
  
          return $this->goHome();
      }
  
      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();
          } else {
              return $this->render('contact', [
                  'model' => $model,
              ]);
          }
      }
  
      public function actionAbout()
      {
          return $this->render('about');
      }
  	
      public function actionTerm()
      {
          Yii::$app->response->format = Response::FORMAT_JSON;
  		$ch = curl_init();
  		curl_setopt($ch, CURLOPT_URL, 'http://places.aviasales.ru/?term='.$_GET['term'].'&locale=ru');
  		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  		$result = curl_exec($ch);
  		$obj = json_decode($result);
  		//print_r($obj);
  		$r = [];
  		foreach($obj as $o)
  		{ //print_r($o);
  			//foreach($o->airport_name as $airport_name)
  			//{
  				$r[] = ((!empty($o->airport_name)) ? $o->airport_name . ', ' : '') . $o->name . '(' . $o->iata . ')';
  			//}
  			
  		}
  		//print_r($r);
  		//$r = ($obj[0]->index_strings); airport_name
  		curl_close($ch);		
  		return $r;
      }
  	
  }