180 lines
5.9 KiB
PHP
180 lines
5.9 KiB
PHP
|
|
<?php
|
||
|
|
set_time_limit(0);
|
||
|
|
|
||
|
|
require_once(__DIR__."/../config.php");
|
||
|
|
require_once(__DIR__."/../engine/classes/phpexcel/Classes/PHPExcel.php");
|
||
|
|
|
||
|
|
error_reporting(E_ALL);
|
||
|
|
ini_set('display_errors', 1);
|
||
|
|
|
||
|
|
$arguments = $argv;
|
||
|
|
|
||
|
|
if (isset($arguments[1])) {
|
||
|
|
|
||
|
|
$import_id = $arguments[1];
|
||
|
|
// Logger("START with import_id: $import_id");
|
||
|
|
|
||
|
|
$query = mysql_query("UPDATE complex_house_import SET status = 'get-houses:in_progress' WHERE id={$import_id}");
|
||
|
|
$selectSql = mysql_query("SELECT * FROM complex_house_import WHERE id = $import_id");
|
||
|
|
$import = mysql_fetch_assoc($selectSql);
|
||
|
|
|
||
|
|
if (isset($import['document'])) {
|
||
|
|
$document = file_get_contents($import['document']);
|
||
|
|
if (isset($import['mapping_fields'])) {
|
||
|
|
$mapping_fields = json_decode($import['mapping_fields'], true);
|
||
|
|
if (isset($mapping_fields['house_name'])) {
|
||
|
|
// $document
|
||
|
|
if (endsWith($import['document'], 'csv')) {
|
||
|
|
// Logger("Format: CSV");
|
||
|
|
$data = parseCsv(mb_convert_encoding($document, 'utf-8'));
|
||
|
|
// Logger(json_encode($data, JSON_UNESCAPED_UNICODE));
|
||
|
|
} else if (endsWith($import['document'], 'xlsx')) {
|
||
|
|
// Logger("Format: xlsx");
|
||
|
|
$temp_name = tempnam(sys_get_temp_dir(), 'tmp-xlsx');
|
||
|
|
|
||
|
|
file_put_contents($temp_name, $document);
|
||
|
|
|
||
|
|
$type = PHPExcel_IOFactory::identify($temp_name);
|
||
|
|
$objReader = PHPExcel_IOFactory::createReader($type);
|
||
|
|
$objPHPExcel = $objReader->load($temp_name);
|
||
|
|
// $data = [];
|
||
|
|
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
|
||
|
|
// $worksheets[$worksheet->getTitle()] = $worksheet->toArray();
|
||
|
|
$data = $worksheet->toArray('');
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$document_index = 0;
|
||
|
|
$number_index = 0;
|
||
|
|
foreach ($data[0] as $index => $header) {
|
||
|
|
if ($header == $mapping_fields['house_name']) {
|
||
|
|
$document_index = $index;
|
||
|
|
}
|
||
|
|
if ($header == $mapping_fields['number']) {
|
||
|
|
$number_index = $index;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Logger("document_index: $document_index");
|
||
|
|
|
||
|
|
unset($data[0]);
|
||
|
|
|
||
|
|
$house_names = [];
|
||
|
|
|
||
|
|
foreach ($data as $index => $bodyValues) {
|
||
|
|
if (
|
||
|
|
isset($bodyValues[$document_index])
|
||
|
|
&& isset($bodyValues[$number_index])
|
||
|
|
&& !empty($bodyValues[$number_index])
|
||
|
|
) {
|
||
|
|
$house_names[] = trim($bodyValues[$document_index]);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Logger(json_encode($house_names, JSON_UNESCAPED_UNICODE));
|
||
|
|
|
||
|
|
$house_names = array_values(array_unique($house_names));
|
||
|
|
$house_name_to_ids = [];
|
||
|
|
foreach ($house_names as $key => $house_name) {
|
||
|
|
$query = mysql_query("SELECT complex_houses.id FROM complex_houses
|
||
|
|
JOIN complex_list on complex_list.id = complex_houses.complex_id
|
||
|
|
WHERE complex_houses.name = '{$house_name}' AND complex_list.user_id = $import[user_id] ");
|
||
|
|
if ($query) {
|
||
|
|
$result = mysql_fetch_assoc($query);
|
||
|
|
$house_name_to_ids[$house_name] = $result['id'];
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
updateImportHouse([
|
||
|
|
'house_name_to_id' => json_encode($house_name_to_ids, JSON_UNESCAPED_UNICODE),
|
||
|
|
'status' => 'get-houses:done',
|
||
|
|
'step' => 3,
|
||
|
|
], $import_id);
|
||
|
|
// $house_name_to_ids
|
||
|
|
// Logger(json_encode($house_names, JSON_UNESCAPED_UNICODE));
|
||
|
|
// Logger(json_encode($house_name_to_ids, JSON_UNESCAPED_UNICODE));
|
||
|
|
} else {
|
||
|
|
updateImportHouse([
|
||
|
|
'status' => 'get-houses:done',
|
||
|
|
'step' => 3,
|
||
|
|
], $import_id);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Logger("END with import_id: $import[id]");
|
||
|
|
} else {
|
||
|
|
// Logger("Parameter import_id is missing. (complex_house_import - id)");
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
function parseCsv($input)
|
||
|
|
{
|
||
|
|
$possibleSeparators = [',', ';', "\t", '|'];
|
||
|
|
$separatorCounts = [];
|
||
|
|
$for_first_line = fopen('php://memory', 'rw');
|
||
|
|
fwrite($for_first_line, $input);
|
||
|
|
fseek($for_first_line, 0);
|
||
|
|
$firstLine = fgets($for_first_line);
|
||
|
|
fclose($for_first_line);
|
||
|
|
|
||
|
|
$temp = fopen('php://memory', 'rw');
|
||
|
|
|
||
|
|
fwrite($temp, $input);
|
||
|
|
fseek($temp, 0);
|
||
|
|
$res = array();
|
||
|
|
|
||
|
|
|
||
|
|
foreach ($possibleSeparators as $separator) {
|
||
|
|
$columns = str_getcsv($firstLine, $separator);
|
||
|
|
$separatorCounts[$separator] = count($columns);
|
||
|
|
}
|
||
|
|
|
||
|
|
$mostLikelySeparator = array_search(max($separatorCounts), $separatorCounts);
|
||
|
|
|
||
|
|
while (($data = fgetcsv($temp, null, $mostLikelySeparator)) !== false) {
|
||
|
|
$res[] = $data;
|
||
|
|
}
|
||
|
|
fclose($temp);
|
||
|
|
|
||
|
|
return $res;
|
||
|
|
}
|
||
|
|
|
||
|
|
function Logger($message)
|
||
|
|
{
|
||
|
|
$timestamp = date('Y-m-d H:i:s');
|
||
|
|
print("[$timestamp] $message \n");
|
||
|
|
}
|
||
|
|
|
||
|
|
function endsWith($haystack, $needle)
|
||
|
|
{
|
||
|
|
$length = strlen($needle);
|
||
|
|
return $length > 0 ? substr($haystack, -$length) === $needle : true;
|
||
|
|
}
|
||
|
|
|
||
|
|
function updateImportHouse($data, $import_id)
|
||
|
|
{
|
||
|
|
// complex_house_import
|
||
|
|
|
||
|
|
if ($data && $import_id) {
|
||
|
|
$sql = [];
|
||
|
|
foreach ($data as $key => $value) {
|
||
|
|
if (is_string($value)) {
|
||
|
|
$sql[] = "$key='$value'";
|
||
|
|
} else {
|
||
|
|
$sql[] = "$key=$value";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
$sql = implode(' ,', $sql);
|
||
|
|
|
||
|
|
$sql = "UPDATE complex_house_import SET $sql WHERE id={$import_id}";
|
||
|
|
return mysql_query($sql);
|
||
|
|
}
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
exit(0);
|