/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ParseKsk::find(); // add conditions that should always apply here $dataProvider = new ActiveDataProvider(['query' => $query]); $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; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'parse_region_id' => $this->parse_region_id, 'color' => $this->color, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'url_ksk', $this->url_ksk])->andFilterWhere(['like', 'url_otchet', $this->url_otchet]); return $dataProvider; }
/** * Finds the ParseKsk model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ParseKsk the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ParseKsk::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getParseKsk() { return $this->hasOne(ParseKsk::className(), ['id' => 'parse_ksk_id']); }
/** * парсим "кскфорум" * обновляем отчеты */ public function importReport($offset, $limit) { $saved = 0; $errors = []; $models = ParseKsk::find()->where(['parse_region_id' => $this->id])->andWhere(['IS NOT', 'url_otchet', null])->offset($offset)->limit($limit)->all(); foreach ($models as $model) { /* @var $model ParseKsk */ $saw = new NokogiriHelper(file_get_contents($model->url_otchet)); foreach ($saw->get('.col_f_content') as $forum_name) { $h4s = $forum_name['h4']; if (isset($forum_name['span'][0]['span'])) { $posted_at = $forum_name['span'][0]['span'][0]['#text'][0]; } else { $posted_at = $forum_name['span'][1]['span'][0]['#text'][0]; } foreach ($h4s as $h4) { $a = $h4['a'][0]; $name = trim($a['span'][0]['#text'][0]); $url = $a['href']; $model_otchet = ParseOtchet::find()->where(['name' => $name])->one(); if (is_null($model_otchet)) { $model_otchet = new ParseOtchet(); $model_otchet->parse_ksk_id = $model->id; $model_otchet->name = $name; $model_otchet->url_otchet = $url; $model_otchet->posted_at = date('Y-m-d H:i:s', strtotime($posted_at)); $model_otchet->updated_at = new \yii\db\Expression('utc_timestamp()'); $model_otchet->save() ? $saved++ : ($errors[] = $model_otchet->errors); } } } } if ($saved > 0) { // Yii::$app->session->setFlash('info', "Создано записей - {$saved}<br>"); $this->updated_at = new \yii\db\Expression('utc_timestamp()'); $this->save(); } if (sizeof($errors) > 0) { // Yii::$app->session->setFlash('warning', Json::encode($errors)); } return sizeof($models) > 0; }
<?php use yii\helpers\Html; use yii\grid\GridView; /* @var $this yii\web\View */ /* @var $searchModel backend\modules\parse\models\ParseOtchetSearch */ /* @var $dataProvider yii\data\ActiveDataProvider */ $this->title = 'Parse Otchets'; $this->params['breadcrumbs'][] = $this->title; ?> <div class="parse-otchet-index"> <h1><?php echo Html::encode($this->title); ?> </h1> <?php // echo $this->render('_search', ['model' => $searchModel]); ?> <p> <?php echo Html::a('Create Parse Otchet', ['create'], ['class' => 'btn btn-success']); ?> </p> <?php echo GridView::widget(['dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'columns' => [['class' => 'yii\\grid\\SerialColumn'], 'id', ['attribute' => 'region_id', 'value' => 'parseKsk.parseRegion.name', 'filter' => \yii\helpers\ArrayHelper::map(\common\models\ParseRegion::find()->all(), 'id', 'name')], ['attribute' => 'parse_ksk_id', 'value' => 'parseKsk.name', 'filter' => \yii\helpers\ArrayHelper::map(\common\models\ParseKsk::find()->all(), 'id', 'name')], 'name', 'url_otchet:url', 'posted_at', ['class' => 'yii\\grid\\ActionColumn']]]); ?> </div>
<th>Дата</th> </tr> <?php foreach ($parse_regions as $region) { ?> <tr> <td><?php echo $region->name; ?> </td> <td><?php echo \common\models\ParseKsk::find()->where(['parse_region_id' => $region->id])->count() . ' '; ?> </td> <td><?php echo \common\models\ParseKsk::find()->where(['parse_region_id' => $region->id])->andWhere(['IS NOT', 'url_otchet', null])->count() . ' '; ?> </td> <td><?php echo sizeof($region->parseOtchets) . ' '; ?> </td> <td><?php echo \backend\helpers\DateHelper::format(strtotime($region->updated_at . ' +6 hours')); ?> </td> <!-- <td>11-7-2014</td>--> <!-- <td><span class="label label-success">Approved</span></td>--> <!-- <td>Bacon ipsum dolor sit amet salami venison chicken flank fatback doner.</td>--> </tr> <?php
public function actionUpdateReport() { foreach (ParseRegion::find()->all() as $region) { /* @var $region ParseRegion*/ echo $region->url . "\n"; $saved = 0; $errors = []; $models = ParseKsk::find()->where(['parse_region_id' => $region->id])->andWhere(['IS NOT', 'url_otchet', null])->all(); foreach ($models as $model) { /* @var $model ParseKsk */ echo ' ' . $model->url_otchet . "\n"; $saw = new NokogiriHelper(file_get_contents($model->url_otchet)); $ok = $saved; foreach ($saw->get('.col_f_content') as $forum_name) { $h4s = $forum_name['h4']; if (isset($forum_name['span'][0]['span'])) { $posted_at = $forum_name['span'][0]['span'][0]['#text'][0]; } else { $posted_at = $forum_name['span'][1]['span'][0]['#text'][0]; } foreach ($h4s as $h4) { $a = $h4['a'][0]; $name = trim($a['span'][0]['#text'][0]); $url = $a['href']; $model_otchet = ParseOtchet::find()->where(['name' => $name])->one(); if (is_null($model_otchet)) { $model_otchet = new ParseOtchet(); $model_otchet->parse_ksk_id = $model->id; $model_otchet->name = $name; $model_otchet->url_otchet = $url; $model_otchet->posted_at = date('Y-m-d H:i:s', strtotime($posted_at)); $model_otchet->updated_at = new \yii\db\Expression('utc_timestamp()'); $model_otchet->save() ? $saved++ : ($errors[] = $model_otchet->errors); } } } if ($ok < $saved) { $model = ParseKsk::findOne($model->id); $model->updated_at = new \yii\db\Expression('utc_timestamp()'); $model->save(); } } if ($saved > 0) { $region = ParseRegion::findOne($region->id); $region->updated_at = new \yii\db\Expression('utc_timestamp()'); $region->save(); } echo "saved - {$saved}\n"; } }