159 lines
5.2 KiB
PHP
159 lines
5.2 KiB
PHP
<?php
|
|
|
|
class ImportController
|
|
{
|
|
private function getBuilders()
|
|
{
|
|
return [
|
|
XmlImportComponent::SECTION_APARTMENTS => new ApartmentBuilder(),
|
|
XmlImportComponent::SECTION_BANKS => new BaseBuilder(Bank::className()),
|
|
XmlImportComponent::SECTION_BLOCKS => new BlockBuilder(),
|
|
XmlImportComponent::SECTION_BLOCKSUBWAYS => new BlockSubwayBuilder(),
|
|
XmlImportComponent::SECTION_BUILDERS => new BaseBuilder(Builder::className()),
|
|
XmlImportComponent::SECTION_BUILDINGS => new BuildingBuilder(),
|
|
XmlImportComponent::SECTION_FLATTYPES => new FlatTypeBuilder(),
|
|
XmlImportComponent::SECTION_MORTGAGES => new MortgageBuilder(),
|
|
XmlImportComponent::SECTION_REGIONS => new RegionBuilder(),
|
|
XmlImportComponent::SECTION_ROOMTYPES => new RoomTypeBuilder(),
|
|
XmlImportComponent::SECTION_SUBWAYS => new BaseBuilder(Subway::className()),
|
|
XmlImportComponent::SECTION_BANK_PROGRAMS => new BankProgramBuilder(),
|
|
XmlImportComponent::SECTION_BANK_PROGRAM_RATES => new BankProgramRateBuilder(),
|
|
XmlImportComponent::SECTION_CLASSES => new ClassTypeBuilder()
|
|
];
|
|
}
|
|
|
|
private $isReport = false;
|
|
private $tmpReportFile;
|
|
private $reportFile;
|
|
|
|
private static function getDateStr()
|
|
{
|
|
return date('[d-m-Y H:i:s] ');
|
|
}
|
|
|
|
public function saveReport()
|
|
{
|
|
if (!$this->isReport) {
|
|
return ;
|
|
}
|
|
|
|
if (is_file($this->tmpReportFile)) {
|
|
rename($this->tmpReportFile, $this->reportFile);
|
|
}
|
|
}
|
|
|
|
private function out($string)
|
|
{
|
|
$str = self::getDateStr().$string.PHP_EOL;
|
|
|
|
if ($this->isReport) {
|
|
file_put_contents($this->tmpReportFile, $str, FILE_APPEND);
|
|
}
|
|
}
|
|
|
|
private function validateFile($file, $force)
|
|
{
|
|
if (is_file($file) && is_readable($file)) {
|
|
return $force || (new ImportStartStrategy())->isRunnable(new \DateTimeImmutable());
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param int $daysOfLife
|
|
**/
|
|
public function cleanLogs($daysOfLife=14)
|
|
{
|
|
$dir = $_SERVER['DOCUMENT_ROOT'].'/import/reports/';
|
|
$files = glob($dir . '*', GLOB_MARK);
|
|
foreach($files as $file) {
|
|
if (stripos(basename($file), ".log") !== false) {
|
|
$ts = filemtime($file);
|
|
if ($ts === false) {
|
|
continue;
|
|
}
|
|
|
|
$days = floor((time() - $ts) / (60 * 60 * 24));
|
|
if ($days > $daysOfLife) {
|
|
unlink($file);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public function doImport(
|
|
$xml= '/abc/SiteData.xml',
|
|
$report=1,
|
|
$force=0
|
|
) {
|
|
$this->isReport = (bool)$report;
|
|
$this->tmpReportFile = $_SERVER['DOCUMENT_ROOT'].'/import/reports/' . time();
|
|
$this->reportFile = $this->tmpReportFile.'.log';
|
|
|
|
if (!$this->validateFile($xml, $force)) {
|
|
return false;
|
|
}
|
|
|
|
try {
|
|
$this->out('Start import');
|
|
$importComponent = new XmlImportComponent($xml);
|
|
|
|
foreach ($this->getBuilders() as $sectionId => $builder) {
|
|
$importComponent->setBuilder($sectionId, $builder);
|
|
}
|
|
|
|
$importComponent->loadData(
|
|
function ($modelClass, $wastedTime, $newElementsCount, $delElementsCount, $count) {
|
|
$this->out($modelClass);
|
|
$this->out('Wasted time: '.$wastedTime);
|
|
$this->out('New: '.$newElementsCount);
|
|
$this->out('Updated: '.($count-$newElementsCount));
|
|
if ($delElementsCount !== NULL) {
|
|
$this->out('Deleted: '.$delElementsCount);
|
|
}
|
|
$this->out('Total: '.$count);
|
|
$this->out('');
|
|
}
|
|
);
|
|
$this->out('FINISH');
|
|
(new ImporterService())->setImportDate((new \DateTimeImmutable())->format('r'));
|
|
} catch (\Exception $e) {
|
|
$this->out(var_export($e, true));
|
|
} finally {
|
|
$this->saveReport();
|
|
}
|
|
|
|
$sql="delete from builders where id not in (select distinct builder_id from blocks)";
|
|
mysql_query($sql);
|
|
$this->cleanLogs();
|
|
return true;
|
|
}
|
|
|
|
/*public function actionImagelist()
|
|
{
|
|
$images = [];
|
|
$mapCallback = function($item) { return basename($item['image']); };
|
|
|
|
$img = Apartment::find()->asArray()
|
|
->distinct(true)
|
|
->select('image')
|
|
->all();
|
|
$images = array_merge($images, array_map($mapCallback, $img));
|
|
|
|
$img = Apartment::find()->asArray()
|
|
->distinct(true)
|
|
->select('image2 as image')
|
|
->all();
|
|
$images = array_merge($images, array_map($mapCallback, $img));
|
|
|
|
$img = Block::find()->asArray()
|
|
->distinct(true)
|
|
->select('image')
|
|
->all();
|
|
$images = array_merge($images, array_map($mapCallback, $img));
|
|
foreach (array_unique($images) as $img) {
|
|
echo $img . PHP_EOL;
|
|
}
|
|
}*/
|
|
} |