public function actionAristo($url, $lang) { Lang::setCurrent($lang); $type_list = ParserAlias::type(); $view_list = ParserAlias::view(); $region_list = ParserAlias::region(); $district_list = ParserAlias::district(); $vat_list = ParserAlias::vat(); $stage_list = ParserAlias::stage(); //$file = '../../upload/xml'; $file = $url; $ids = Parse::saveXml($file, Parse::SCENARIO_ARISTO); $temp = Parse::find()->with('content')->where(['id' => $ids])->all(); $items = []; foreach ($temp as $t) { $data = Json::decode($t->content->data); if ($t->sale_id) { $sale = Sale::findOne(['id' => $t->sale_id]); $content = SaleLang::findOne(['id' => $t->sale_id, 'lang_id' => Lang::getCurrent()->id]); } else { $sale = new Sale(); $sale->user_id = $t->user_id; $content = new SaleLang(); $content->lang_id = Lang::getCurrent()->id; } $sale->covered = round($data['propertyTotalCoveredArea']); $sale->gps = $data['gpsLat'] . ', ' . $data['gpsLong']; $sale->bedroom = $data['propertyBedrooms']; $sale->price = round($data['propertyPrice']); $origin = []; if (isset($type_list[$data['propertyType']])) { $sale->type_id = $type_list[$data['propertyType']]; } else { $origin['type'] = $data['propertyType']; } if (isset($region_list[$data['area']])) { $sale->region_id = $region_list[$data['area']]; } else { $origin['region'] = $data['area']; } if (isset($district_list[$data['city']])) { $sale->district_id = $district_list[$data['city']]; } else { $origin['district'] = $data['city']; } if (!empty($data['views'])) { $view_ids = []; $views = explode(',', $data['views']); foreach ($views as $view) { $view = trim($view); if (isset($view_list[$view])) { $view_ids[] = $view_list[$view]; } } $sale->view_ids = $view_ids; } if (!empty($data['constructionStatus'])) { $stage_ids = []; $stages = explode(',', $data['constructionStatus']); foreach ($stages as $stage) { $stage = trim($stage); if (isset($stage_list[$stage])) { $stage_ids[] = $stage_list[$stage]; } } $sale->stage_ids = $stage_ids; } if (isset($vat_list[$data['vat']])) { $sale->vat = $vat_list[$data['vat']]; } else { $origin['vat'] = $data['vat']; } $content->description = $data['longDesc']; $content->name = $data['propertyName']; $image_array = []; if (isset($data['ImagesGallery']['mainImage'])) { $image_array[] = $data['ImagesGallery']['mainImage']; } foreach ($data['ImagesGallery']['image'] as $img) { $image_array[] = $img; } $image = ParseImage::find()->where(['parse_id' => $t->id])->indexBy('url')->all(); foreach ($image as $key => $value) { $image[$key]->url = ''; } foreach ($image_array as $key) { if (!isset($image[$key])) { $image[$key] = new ParseImage(); $image[$key]->parse_id = $t->id; } $image[$key]->url = $key; } $sale->validate(); $content->validate(); $items[] = ['sale' => $sale, 'content' => $content, 'image' => $image, 'origin' => $origin, 'parse' => $t]; } return $this->render('prepare', ['items' => $items]); }
/** * Load XML file and saves the modified data in the database. * Returns the IDs of rows affected. * * @param $file * @param $scenario * @param bool $force * @return array $ids */ public static function saveXml($file, $user_id, $lang_id = null, $force = null) { $lang_id = $lang_id === null ? Lang::getCurrent()->id : $lang_id; $properties = simplexml_load_file($file); $property_ids = []; if ($user_id == Parse::SCENARIO_ARISTO) { foreach ($properties as $property) { $property_ids[] = (string) $property->propertyID; } } if ($user_id == Parse::SCENARIO_PAFILIA) { $properties = $properties->properties->property; foreach ($properties as $property) { $property_ids[] = str_replace('property-', '', (string) $property->attributes()->id); } } $parses = Parse::find()->where(['user_id' => $user_id, 'remote_id' => $property_ids])->indexBy('remote_id')->all(); $parse_ids = []; foreach ($parses as $p) { $parse_ids[] = $p->id; } $contents = ParseLang::find()->where(['id' => $parse_ids, 'lang_id' => $lang_id])->indexBy('id')->all(); $ids = []; foreach ($properties as $property) { $property_id = 0; if ($user_id == Parse::SCENARIO_ARISTO) { $property_id = (string) $property->propertyID; } if ($user_id == Parse::SCENARIO_PAFILIA) { $property_id = str_replace('property-', '', (string) $property->attributes()->id); } $content = false; if (!isset($parses[$property_id])) { $parse = new Parse(); $parse->user_id = $user_id; $parse->remote_id = $property_id; } else { $parse = $parses[$property_id]; if (isset($contents[$parse->id])) { $content = $contents[$parse->id]; } } if (empty($content)) { $content = new ParseLang(); $content->lang_id = $lang_id; } $content->data = Json::encode($property); $hash = md5($content->data); if ($hash != $content->hash || $force) { if ($parse->save()) { $content->hash = $hash; $content->id = $parse->id; if ($content->save()) { $ids[] = $parse->id; } } } elseif (!$parse->sale_id) { $ids[] = $parse->id; } } return $ids; }