public function search_view($params)
 {
     //->where(['id_project' => $params['id']] // выборка по проэкту
     /*Campaigns::find()->with('author')->where(['id_project' => $params['id']])*/
     $query = Campaigns::find();
     $query->joinWith(['author']);
     $query->joinWith(['spheres']);
     $query->where(['id_project' => $params['id']]);
     $query->orderBy("date_post DESC");
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     // Important: here is how we set up the sorting
     // The key is the attribute name on our "TourSearch" instance
     $dataProvider->sort->attributes['author'] = ['asc' => ['user.username' => SORT_ASC], 'desc' => ['user.username' => SORT_DESC]];
     // Important: here is how we set up the sorting
     // The key is the attribute name on our "TourSearch" instance
     $dataProvider->sort->attributes['spheres'] = ['asc' => ['spheres.name' => SORT_ASC], 'desc' => ['spheres.name' => SORT_DESC]];
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'date_post' => $this->date_post, 'date_end' => $this->date_end, 'date_begin' => $this->date_begin, 'date_update' => $this->date_update, 'id_project' => $this->id_project, 'id_user' => $this->id_user, 'id_sphere' => $this->id_sphere]);
     $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'about', $this->about])->andFilterWhere(['like', 'link', $this->link])->andFilterWhere(['like', 'user.username', $this->author])->andFilterWhere(['like', 'spheres.name', $this->spheres]);
     return $dataProvider;
 }
 /**
  * Get data form Ajax request and create or update Analytics model.
  * @return mixed
  */
 public function actionAjax_request()
 {
     if (Yii::$app->request->isAjax) {
         $data = Yii::$app->request->post();
         function find_($value1)
         {
             // проверка на нужную ссылку
             $value = '/campaigns?id=';
             $result = strpos($value1, $value);
             return $result;
         }
         function find_id($value1)
         {
             // получение id
             $value = '/campaigns?id=';
             $id = str_replace($value, '', $value1);
             return $id;
         }
         if (isset($data['analytics'])) {
             // проверка на пустоту массива
             $mass = $data['analytics'];
             for ($i = 0; $i < count($mass); $i++) {
                 $result = find_($mass[$i][0]);
                 if (!($result === false)) {
                     $id = find_id($mass[$i][0]);
                     if ($id != '') {
                         $models = Campaigns::Get_info($id);
                         $mass[$i][] = (int) $id;
                         $mass[$i][] = $models->id_user;
                     }
                     $new[] = $mass[$i];
                 }
             }
         }
         // подготовили массив
         for ($i = 0; $i < count($new); $i++) {
             $models = Analytics::Get_info($new[$i][1], $new[$i][5]);
             if ($models == '') {
                 $create_ = Analytics::Create_($new[$i]);
                 if ($create_ != false) {
                     $create_detail_ = Detail::Create_($new[$i], $create_);
                 }
             } else {
                 $check_detail = Detail::Get_info($models->id, $new[$i][2]);
                 if ($check_detail == '') {
                     $create_detail_ = Detail::Create_($new[$i], $models->id);
                 } else {
                     $update_detail_ = Detail::Update_($new[$i], $models->id);
                 }
             }
         }
         \Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
         return ['data' => 'Данные успешно загружены'];
     }
 }
 public function actionCron()
 {
     $client_id = '746431818850-sddh6at9g6o18hbmi82rh13tu2mi3pbg.apps.googleusercontent.com';
     $client_secret = 'mz1lOoqB9oslvGZVhoT6KS-V';
     $redirect_uri = 'http://insol.madeforpets.com.ua/cron';
     $account_id = 'ga:114815562';
     session_start();
     $_SESSION['oauth_access_token'] = 'ya29.jAK-QzSp8fb_ou3CxnTp2SY_WEZ5aQF5JjxNmu9kpan_C0roaG6I1nFVGlOQJXL94MAU';
     $ga = new GoogleAnalyticsAPI();
     $ga->auth->setClientId($client_id);
     $ga->auth->setClientSecret($client_secret);
     $ga->auth->setRedirectUri($redirect_uri);
     if (isset($_GET['force_oauth'])) {
         $_SESSION['oauth_access_token'] = null;
     }
     if (!isset($_SESSION['oauth_access_token']) && !isset($_GET['code'])) {
         // Go get the url of the authentication page, redirect the client and go get that token!
         $url = $ga->auth->buildAuthUrl();
         header("Location: " . $url);
     }
     if (!isset($_SESSION['oauth_access_token']) && isset($_GET['code'])) {
         $auth = $ga->auth->getAccessToken($_GET['code']);
         if ($auth['http_code'] == 200) {
             $accessToken = $auth['access_token'];
             $refreshToken = $auth['refresh_token'];
             $tokenExpires = $auth['expires_in'];
             $tokenCreated = time();
             // For simplicity of the example we only store the accessToken
             // If it expires use the refreshToken to get a fresh one
             $_SESSION['oauth_access_token'] = $accessToken;
         } else {
             die("Sorry, something wend wrong retrieving the oAuth tokens");
         }
     }
     if (!empty($_SESSION['oauth_access_token'])) {
         $ga->setAccessToken($_SESSION['oauth_access_token']);
         $ga->setAccountId($account_id);
         // Set the default params. For example the start/end dates and max-results
         $defaults = array('start-date' => '2016-01-01', 'end-date' => 'today');
         $ga->setDefaultQueryParams($defaults);
         $params = array('metrics' => 'ga:pageviews,ga:sessionDuration', 'dimensions' => 'ga:pagePath,ga:date,ga:fullReferrer', 'sort' => '-ga:date');
         $visits = $ga->query($params);
     }
     $data = $visits['rows'];
     if (isset($data)) {
         // проверка на пустоту массива
         $mass = $data;
         for ($i = 0; $i < count($mass); $i++) {
             $result = $this->find_($mass[$i][0]);
             if (!($result === false)) {
                 $id = $this->find_id($mass[$i][0]);
                 if ($id != '') {
                     $models = Campaigns::Get_info($id);
                     $mass[$i][] = (int) $id;
                     $mass[$i][] = $models->id_user;
                 }
                 $new[] = $mass[$i];
             }
         }
     }
     for ($i = 0; $i < count($new); $i++) {
         $models = Analytics::Get_info($new[$i][1], $new[$i][5]);
         if ($models == '') {
             $create_ = Analytics::Create_($new[$i]);
             if ($create_ != false) {
                 $create_detail_ = Detail::Create_($new[$i], $create_);
             }
         } else {
             $check_detail = Detail::Get_info($models->id, $new[$i][2]);
             if ($check_detail == '') {
                 $create_detail_ = Detail::Create_($new[$i], $models->id);
             } else {
                 $update_detail_ = Detail::Update_($new[$i], $models->id);
             }
         }
     }
     return $this->render('cron', ['data' => $data]);
 }
 /**
  * Finds the Campaigns model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Campaigns the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Campaigns::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Пример #5
0
 public function afterDelete()
 {
     parent::afterDelete();
     Campaigns::deleteAll('id_project = :id_project', [':id_project' => $this->id]);
 }