Advertisement
Guest User

Untitled

a guest
Dec 6th, 2018
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.42 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use app\components\AppHelper;
  6. use app\components\UrlHelper;
  7. use app\models\SubIds;
  8. use Yii;
  9. use app\components\AppController;
  10. use app\components\Installer;
  11. use app\models\InstallForm;
  12. use yii\filters\AccessControl;
  13. use yii\filters\VerbFilter;
  14. use app\models\LoginForm;
  15. use app\models\Conversions;
  16. use app\models\Clicks;
  17. use app\models\SxgeoCountry;
  18. use app\models\ContactForm;
  19. use yii\web\NotFoundHttpException;
  20. use app\components\Migrations;
  21.  
  22. class SiteController extends AppController
  23. {
  24.     /**
  25.      * @inheritdoc
  26.      */
  27.     public function behaviors()
  28.     {
  29.         return [
  30.             'access' => [
  31.                 'class' => AccessControl::className(),
  32.                 'only' => ['logout'],
  33.                 'rules' => [
  34.                     [
  35.                         'actions' => ['logout'],
  36.                         'allow' => true,
  37.                         'roles' => ['@'],
  38.                     ],
  39.                 ],
  40.             ],
  41.             /*'verbs' => [
  42.                 'class' => VerbFilter::className(),
  43.                 'actions' => [
  44.                     'logout' => ['post'],
  45.                 ],
  46.             ],*/
  47.         ];
  48.     }
  49.  
  50.     /**
  51.      * @inheritdoc
  52.      */
  53.     public function actions()
  54.     {
  55.         return [
  56.             'error' => [
  57.                 'class' => 'yii\web\ErrorAction',
  58.             ],
  59.             'captcha' => [
  60.                 'class' => 'yii\captcha\CaptchaAction',
  61.                 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  62.             ],
  63.         ];
  64.     }
  65.  
  66.     /**
  67.      * Displays homepage.
  68.      *
  69.      * @return string
  70.      */
  71.     public function actionIndex()
  72.     {
  73.         $conv_out=0;
  74.         $clicks_out=0;
  75.         $leed_click_list=0;
  76.         $textClicksArr="";
  77.         $countryConversions=0;
  78.         $countryClicks=0;
  79.         if(!Yii::$app->user->isGuest and !Installer::isSetupMode()){
  80.  
  81.             $week = 86400*7;
  82.             $month = 86400*31*3;
  83.             $days = array(
  84.                 'Воскресенье', 'Понедельник', 'Вторник', 'Среда',
  85.                 'Четверг', 'Пятница', 'Суббота'
  86.             );
  87.             $conv_out = array();
  88.             $clicks_out = array();
  89.             $conversions = Conversions::find()->where(['>=', 'day_timestamp', time()-$week])
  90.                 ->select('count(id) as sum, wday')
  91.                 ->groupBy('wday')
  92.                 ->orderBy('wday ASC')
  93.                 ->asArray()
  94.                 ->all();
  95.             $clicks = Clicks::find()->where(['>=', 'day_timestamp', time()-$week])
  96.                 ->select('count(id) as sum,wday')
  97.                 ->groupBy('wday')
  98.                 ->orderBy('wday ASC')
  99.                 ->asArray()
  100.                 ->all();
  101.             $countryConversions = Conversions::find()->where(['>=', 'day_timestamp', time()-$month])
  102.                 ->select('count(id) as sum,country')
  103.                 ->groupBy('country')
  104.                 ->orderBy('country ASC')
  105.                 ->indexBy('country')
  106.                 ->asArray()
  107.                 ->all();
  108.             $countryClicks = Clicks::find()->where(['>=', 'day_timestamp', time()-$month])
  109.                 ->select('count(id) as sum,country')
  110.                 ->groupBy('country')
  111.                 ->orderBy('country ASC')
  112.                 ->indexBy('country')
  113.                 ->asArray()
  114.                 ->all();
  115.             $textClicksArr="";
  116.  
  117.  
  118.             $leed_click_list = [];
  119.             foreach ($countryConversions as $key=>$val){
  120.                 //здесь мы прогоняем все найденные страны в конверсиях и делаем их массив с кликами
  121.                 $country = "ru";
  122.                 $add=0;
  123.                 if(isset($countryClicks[$key])){
  124.                     $add = $countryClicks[$key]['sum'];
  125.                     unset($countryClicks[$key]);
  126.                 }
  127.                 $countrymodel = SxgeoCountry::find()->where(["iso"=>$key])->one();
  128.                 if($countrymodel)
  129.                     $country = $countrymodel->name_ru;
  130.                 $leed_click_list[$country]=array("click_sum"=>$add,"leed_sum"=>$val['sum']);
  131.                 $textClicksArr.= '"'.strtoupper($key).'":"'.$val['sum'].'\,'.$add.'",';
  132.  
  133.             }
  134.             if (count($countryClicks)>0)
  135.             {
  136.                 //всё, что осталось в массиве countryClick прогоняется и добавляется в общий массив, но без лидов
  137.                 foreach ($countryClicks as $key=>$val) {
  138.                     $country = "ru";
  139.                     $countrymodel = SxgeoCountry::find()->where(["iso"=>$key])->one();
  140.                     if($countrymodel)
  141.                         $country = $countrymodel->name_ru;
  142.                     $leed_click_list[$country]=array("click_sum"=>$val['sum'],"leed_sum"=>0);
  143.  
  144.                     $textClicksArr .= '"' . strtoupper($key) . '":"0\,' . $val['sum'] . '",';
  145.                 }
  146.             }
  147.  
  148.             foreach ($conversions as $conversion) {
  149.  
  150.                 $conv_out[$days[$conversion['wday']]]=$conversion['sum'];
  151.             }
  152.             foreach ($clicks as $click) {
  153.  
  154.                 $clicks_out[$days[$click['wday']]]=$click['sum'];
  155.             }
  156.         }
  157.         if (Installer::isSetupMode() && $this->action->uniqueId != 'site/install') {
  158.             return $this->redirect(UrlHelper::getRoot('/install'));
  159.         }
  160.  
  161.         $model = new LoginForm();
  162.         if ($model->load(Yii::$app->request->post()) && $model->login()) {
  163.  
  164.             return $this->goBack();
  165.         }
  166.         return $this->render('index',[
  167.             'countryConversions'=>$leed_click_list,
  168.             'textClicksArr'=>$textClicksArr,
  169.             'conversions'=>$conv_out,
  170.             'clicks'=>$clicks_out,
  171.             'model'=>$model,
  172.         ]);
  173.     }
  174.  
  175.     /**
  176.      * Login action.
  177.      *
  178.      * @return string
  179.      */
  180.     public function actionLogin()
  181.     {
  182.         if (Installer::isSetupMode() && $this->action->uniqueId != 'site/install') {
  183.             return $this->redirect(UrlHelper::getRoot('/install'));
  184.         }
  185.         if (!Yii::$app->user->isGuest) {
  186.             return $this->goHome();
  187.         }
  188.  
  189.         $model = new LoginForm();
  190.         if ($model->load(Yii::$app->request->post()) && $model->login()) {
  191.             return $this->goBack();
  192.         }
  193.         return $this->render('login', [
  194.             'model' => $model,
  195.         ]);
  196.     }
  197.  
  198.     /**
  199.      * Logout action.
  200.      *
  201.      * @return string
  202.      */
  203.     public function actionLogout()
  204.     {
  205.         Yii::$app->user->logout();
  206.  
  207.         return $this->goHome();
  208.     }
  209.  
  210.     public function actionInstall()
  211.     {
  212.         if (!Installer::isSetupMode()) {
  213.             throw new NotFoundHttpException();
  214.         }
  215.         $model = new InstallForm();
  216.         $model->dbhost = 'localhost';
  217.         if (\Yii::$app->request->isPost) {
  218.             $model->load(\Yii::$app->request->post());
  219.             if ($model->validate()) {
  220.                 if ($model->setup()) {
  221.                     return $this->redirect('migration/apply');
  222.                 } else {
  223.                     die('Setup error: ' . __METHOD__);
  224.                 }
  225.             } else {
  226.                 var_dump($model->errors);
  227.                 die(__METHOD__);
  228.             }
  229.         }
  230.  
  231.         $checkFiles = Installer::checkFiles();
  232.         return $this->render('install', ['model' => $model, 'checkFiles' => $checkFiles]);
  233.     }
  234.  
  235.     public function actionGetSubIds($q = null, $id = null)
  236.     {
  237.         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
  238.         $name = Yii::$app->request->get('name');
  239.         $out = ['results' => ['id' => '', 'text' => '']];
  240.         $query = SubIds::find();
  241.         if ($name) {
  242.             $query->andWhere(['name' => $name]);
  243.         }
  244.         $data = $query
  245.             ->select('id, value AS text')
  246.             ->andWhere(['like', 'value', $q])
  247.             ->limit(20)
  248.             ->asArray()
  249.             ->all();
  250.         $out['results'] = array_values($data);
  251.         return $out;
  252.     }
  253.  
  254.     /**
  255.      * Displays about page.
  256.      *
  257.      * @return string
  258.      */
  259.     public function actionAbout()
  260.     {
  261.         return $this->render('about');
  262.     }
  263.  
  264.     public function actionSuccess()
  265.     {
  266.         $this->layout = 'blank';
  267.         return $this->render('success');
  268.     }
  269. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement