/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = ParseOtchet::find(); $query->joinWith(['parseKsk']); // 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, ParseKsk::tableName() . '.parse_region_id' => $this->region_id, 'parse_ksk_id' => $this->parse_ksk_id, 'posted_at' => $this->posted_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'url_otchet', $this->url_otchet]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getParseOtchets() { return $this->hasMany(ParseOtchet::className(), ['parse_ksk_id' => '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; }
/** * Finds the ParseOtchet model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return ParseOtchet the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = ParseOtchet::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getParseOtchet() { return $this->hasOne(ParseOtchet::className(), ['id' => 'parse_otchet_id']); }
public function actionUpdateReportFile() { return; $this->checkPath(); $count = 0; $files = 0; foreach (ParseOtchet::find()->limit(10)->all() as $model) { if (ParseOtchetFile::findOne(['parse_otchet_id' => $model->id]) === null && $model->url_otchet !== null && $model->text === null) { $saw = new NokogiriHelper(file_get_contents($model->url_otchet)); $text = []; foreach ($saw->get('div.post p') as $p) { if (isset($p['a'])) { foreach ($p['a'] as $a) { echo $a['href'] . "\n\n"; $file = file_get_contents($a['href']); $url_base = "C:/OpenServer/domains/yii2-papa/statics/web/ksk/otchet/{$model->id}"; $this->checkPath($url_base); $size = sizeof(scandir($url_base)); if (file_put_contents("{$url_base}/{$size}.jpg", $file) !== false) { $modelFile = new ParseOtchetFile(); $modelFile->file_name = $a['title']; $modelFile->path = "{$url_base}/{$size}.jpg"; $modelFile->parse_otchet_id = $model->id; if ($modelFile->save()) { $files++; echo str_pad("otchet-{$model->id}", 15) . str_pad("{$modelFile->path}", 50) . str_pad("SUCCESS", 15) . "\n"; } else { echo str_pad("otchet-{$model->id}", 15) . str_pad(Json::encode($modelFile->errors), 50) . str_pad("ERROR MODEL", 15) . "\n"; } } else { echo str_pad("otchet-{$model->id}", 15) . str_pad("{$url_base}/{$size}.jpg", 50) . str_pad("ERROR FILE", 15) . "\n"; } } } !isset($p['#text']) ?: ($text[] = $p['#text']); } $model = ParseOtchet::findOne($model->id); $model->text = $this->formatText($text); $model->save(); $count++; } } echo "updated - {$count}\ncreated files - {$files}\n"; }