Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace app\controllers;
- use app\components\AppHelper;
- use app\components\UrlHelper;
- use app\models\SubIds;
- use Yii;
- use app\components\AppController;
- use app\components\Installer;
- use app\models\InstallForm;
- use yii\filters\AccessControl;
- use yii\filters\VerbFilter;
- use app\models\LoginForm;
- use app\models\Conversions;
- use app\models\Clicks;
- use app\models\SxgeoCountry;
- use app\models\ContactForm;
- use yii\web\NotFoundHttpException;
- use app\components\Migrations;
- class SiteController extends AppController
- {
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'access' => [
- 'class' => AccessControl::className(),
- 'only' => ['logout'],
- 'rules' => [
- [
- 'actions' => ['logout'],
- 'allow' => true,
- 'roles' => ['@'],
- ],
- ],
- ],
- /*'verbs' => [
- 'class' => VerbFilter::className(),
- 'actions' => [
- 'logout' => ['post'],
- ],
- ],*/
- ];
- }
- /**
- * @inheritdoc
- */
- public function actions()
- {
- return [
- 'error' => [
- 'class' => 'yii\web\ErrorAction',
- ],
- 'captcha' => [
- 'class' => 'yii\captcha\CaptchaAction',
- 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
- ],
- ];
- }
- /**
- * Displays homepage.
- *
- * @return string
- */
- public function actionIndex()
- {
- $conv_out=0;
- $clicks_out=0;
- $leed_click_list=0;
- $textClicksArr="";
- $countryConversions=0;
- $countryClicks=0;
- if(!Yii::$app->user->isGuest and !Installer::isSetupMode()){
- $week = 86400*7;
- $month = 86400*31*3;
- $days = array(
- 'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
- 'Четверг', 'Пятница', 'Суббота'
- );
- $conv_out = array();
- $clicks_out = array();
- $conversions = Conversions::find()->where(['>=', 'day_timestamp', time()-$week])
- ->select('count(id) as sum, wday')
- ->groupBy('wday')
- ->orderBy('wday ASC')
- ->asArray()
- ->all();
- $clicks = Clicks::find()->where(['>=', 'day_timestamp', time()-$week])
- ->select('count(id) as sum,wday')
- ->groupBy('wday')
- ->orderBy('wday ASC')
- ->asArray()
- ->all();
- $countryConversions = Conversions::find()->where(['>=', 'day_timestamp', time()-$month])
- ->select('count(id) as sum,country')
- ->groupBy('country')
- ->orderBy('country ASC')
- ->indexBy('country')
- ->asArray()
- ->all();
- $countryClicks = Clicks::find()->where(['>=', 'day_timestamp', time()-$month])
- ->select('count(id) as sum,country')
- ->groupBy('country')
- ->orderBy('country ASC')
- ->indexBy('country')
- ->asArray()
- ->all();
- $textClicksArr="";
- $leed_click_list = [];
- foreach ($countryConversions as $key=>$val){
- //здесь мы прогоняем все найденные страны в конверсиях и делаем их массив с кликами
- $country = "ru";
- $add=0;
- if(isset($countryClicks[$key])){
- $add = $countryClicks[$key]['sum'];
- unset($countryClicks[$key]);
- }
- $countrymodel = SxgeoCountry::find()->where(["iso"=>$key])->one();
- if($countrymodel)
- $country = $countrymodel->name_ru;
- $leed_click_list[$country]=array("click_sum"=>$add,"leed_sum"=>$val['sum']);
- $textClicksArr.= '"'.strtoupper($key).'":"'.$val['sum'].'\,'.$add.'",';
- }
- if (count($countryClicks)>0)
- {
- //всё, что осталось в массиве countryClick прогоняется и добавляется в общий массив, но без лидов
- foreach ($countryClicks as $key=>$val) {
- $country = "ru";
- $countrymodel = SxgeoCountry::find()->where(["iso"=>$key])->one();
- if($countrymodel)
- $country = $countrymodel->name_ru;
- $leed_click_list[$country]=array("click_sum"=>$val['sum'],"leed_sum"=>0);
- $textClicksArr .= '"' . strtoupper($key) . '":"0\,' . $val['sum'] . '",';
- }
- }
- foreach ($conversions as $conversion) {
- $conv_out[$days[$conversion['wday']]]=$conversion['sum'];
- }
- foreach ($clicks as $click) {
- $clicks_out[$days[$click['wday']]]=$click['sum'];
- }
- }
- if (Installer::isSetupMode() && $this->action->uniqueId != 'site/install') {
- return $this->redirect(UrlHelper::getRoot('/install'));
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- return $this->goBack();
- }
- return $this->render('index',[
- 'countryConversions'=>$leed_click_list,
- 'textClicksArr'=>$textClicksArr,
- 'conversions'=>$conv_out,
- 'clicks'=>$clicks_out,
- 'model'=>$model,
- ]);
- }
- /**
- * Login action.
- *
- * @return string
- */
- public function actionLogin()
- {
- if (Installer::isSetupMode() && $this->action->uniqueId != 'site/install') {
- return $this->redirect(UrlHelper::getRoot('/install'));
- }
- if (!Yii::$app->user->isGuest) {
- return $this->goHome();
- }
- $model = new LoginForm();
- if ($model->load(Yii::$app->request->post()) && $model->login()) {
- return $this->goBack();
- }
- return $this->render('login', [
- 'model' => $model,
- ]);
- }
- /**
- * Logout action.
- *
- * @return string
- */
- public function actionLogout()
- {
- Yii::$app->user->logout();
- return $this->goHome();
- }
- public function actionInstall()
- {
- if (!Installer::isSetupMode()) {
- throw new NotFoundHttpException();
- }
- $model = new InstallForm();
- $model->dbhost = 'localhost';
- if (\Yii::$app->request->isPost) {
- $model->load(\Yii::$app->request->post());
- if ($model->validate()) {
- if ($model->setup()) {
- return $this->redirect('migration/apply');
- } else {
- die('Setup error: ' . __METHOD__);
- }
- } else {
- var_dump($model->errors);
- die(__METHOD__);
- }
- }
- $checkFiles = Installer::checkFiles();
- return $this->render('install', ['model' => $model, 'checkFiles' => $checkFiles]);
- }
- public function actionGetSubIds($q = null, $id = null)
- {
- \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
- $name = Yii::$app->request->get('name');
- $out = ['results' => ['id' => '', 'text' => '']];
- $query = SubIds::find();
- if ($name) {
- $query->andWhere(['name' => $name]);
- }
- $data = $query
- ->select('id, value AS text')
- ->andWhere(['like', 'value', $q])
- ->limit(20)
- ->asArray()
- ->all();
- $out['results'] = array_values($data);
- return $out;
- }
- /**
- * Displays about page.
- *
- * @return string
- */
- public function actionAbout()
- {
- return $this->render('about');
- }
- public function actionSuccess()
- {
- $this->layout = 'blank';
- return $this->render('success');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement