Ejemplo n.º 1
0
 public function actionUpdateKsk()
 {
     foreach (ParseRegion::find()->all() as $region) {
         /* @var $region ParseRegion*/
         echo $region->url . "\n";
         $changes = [];
         $news = [];
         $errors = [];
         $updated = 0;
         $created = 0;
         $saw = new NokogiriHelper(file_get_contents($region->url));
         foreach ($saw->get('ol.subforums li a') as $a) {
             $color = 0;
             $name = $a['title'];
             if (isset($a['font'])) {
                 $name = $a['font'][0]['#text'][0];
                 $color = 1;
             }
             $name = trim($name);
             $model = ParseKsk::find()->where(['name' => $name, 'parse_region_id' => $region->id])->one();
             if ($model === null) {
                 $model = new ParseKsk();
                 $model->name = $name;
                 $model->parse_region_id = $region->id;
             }
             $update = false;
             if (!$model->isNewRecord && trim($a['href']) != '' && $model->url_ksk != $a['href']) {
                 $changes[] = "({$model->name}) {$model->url_ksk} -> " . $a['href'];
                 $model->url_ksk = $a['href'];
                 $update = true;
             }
             if (!$model->isNewRecord && $model->color != $color) {
                 $changes[] = "({$model->name}) {$model->color} -> " . $color;
                 $model->color = $color;
                 $update = true;
             }
             if ($update || $model->isNewRecord) {
                 $model->updated_at = new \yii\db\Expression('utc_timestamp()');
                 if ($model->isNewRecord) {
                     $news[] = $model->name;
                     $created += $model->save() ? 1 : 0;
                 } else {
                     $updated += $model->save() ? 1 : 0;
                 }
                 if ($model->hasErrors()) {
                     $errors[] = $model->errors;
                 }
             }
         }
         if ($created + $updated > 0) {
             $region->updated_at = new \yii\db\Expression('utc_timestamp()');
             $region->save();
         }
         echo "created - {$created}, updated - {$updated}\n";
     }
 }
Ejemplo n.º 2
0
 /**
  * Creates a new ParseKsk model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new ParseKsk();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Ejemplo n.º 3
0
 /**
  * парсим "кскфорум"
  * выкачиваем всю информация по текущему району кроме ссылки на отчет
  */
 public function importKsk()
 {
     $changes = [];
     $news = [];
     $errors = [];
     $updated = 0;
     $created = 0;
     $saw = new NokogiriHelper(file_get_contents($this->url));
     foreach ($saw->get('ol.subforums li a') as $a) {
         $color = 0;
         $name = trim($a['title']);
         if (isset($a['font'])) {
             $name = trim($a['font'][0]['#text'][0]);
             $color = 1;
         }
         $model = ParseKsk::find()->where(['name' => $name, 'parse_region_id' => $this->id])->one();
         if ($model === null) {
             $model = new ParseKsk();
             $model->name = $name;
             $model->parse_region_id = $this->id;
         }
         $update = false;
         if (!$model->isNewRecord && trim($a['href']) != '' && $model->url_ksk != $a['href']) {
             $changes[] = "({$model->name}) {$model->url_ksk} -> " . $a['href'];
             $model->url_ksk = $a['href'];
             $update = true;
         }
         if (!$model->isNewRecord && $model->color != $color) {
             $changes[] = "({$model->name}) {$model->color} -> " . $color;
             $model->color = $color;
             $update = true;
         }
         if ($update || $model->isNewRecord) {
             $model->updated_at = new \yii\db\Expression('utc_timestamp()');
             if ($model->isNewRecord) {
                 $news[] = $model->name;
                 $created += $model->save() ? 1 : 0;
             } else {
                 $updated += $model->save() ? 1 : 0;
             }
             if ($model->hasErrors()) {
                 $errors[] = $model->errors;
             }
         }
     }
     if ($created + $updated > 0) {
         $this->updated_at = new \yii\db\Expression('utc_timestamp()');
         $message = '';
         $message .= $created > 0 ? "Новых записей - {$created}<br>" : '';
         $message .= $updated > 0 ? "Обновлено записей - {$updated}<br>" : '';
         $message .= sizeof($news) > 0 ? "Новые - [<br>" : '';
         foreach ($news as $new) {
             $message .= "<pre>{$new}</pre><br>";
         }
         $message .= sizeof($news) > 0 ? "]<br>" : '';
         $message .= sizeof($changes) > 0 ? "Обновление - [<br>" : '';
         foreach ($changes as $change) {
             $message .= "<pre>{$change}</pre><br>";
         }
         $message .= sizeof($changes) > 0 ? "]<br>" : '';
         Yii::$app->session->setFlash('info', $message);
         $this->save();
     }
     if (sizeof($errors) > 0) {
         Yii::$app->session->setFlash('warning', Json::encode($errors));
     }
 }