Files
admin-php-module/commands/GETRoomMapFile.php
miroman-afk dcd0e72d1f v.2.22
Редактор карты зала
Электронные заказы
Отчет по удалениям
2022-09-22 16:34:02 +03:00

101 lines
4.2 KiB
PHP

<?php
namespace App\Commands;
use App\Component\Models\Places;
use App\Component\Models\Tables;
use App\Component\Models\Terminal;
use App\Console\Commands\HRCCommand;
use App\Console\Commands\HRCCommandInterface;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
class GETRoomMapFile extends HRCCommand implements HRCCommandInterface {
protected $signature = 'getroommapfile';
public function command($input, $output = null) {
$terminal = Terminal::where('soft', '=', 1)->where('is_active', '=', 1)->first();
$files = [];
$dirname = __DIR__ . "\\..\\..\\..\\Exchange\\" . $terminal['key'] . "\\places\\";
Http::post('https://portal.hrc.by/api/cloud/folder', [
'code' => $terminal['key'],
'folder' => 'places',
'project_code' => 'hrc',
'api' => '2.0',
]);
$response_files = Http::get('https://portal.hrc.by/api/cloud/list?api=2.0&project_code=hrc&code=' . $terminal['key'] . '&folder=places');
if (count($response_files->json()['files']) > 0) {
foreach ($response_files->json()['files'] as $response) {
$filename = $response['filename'];
$basename = $response['basename'];
$files[$response['timestamp']] = array('filename' => $filename, 'basename' => $basename);
}
if (count($files) == 0) {
return ['status' => 'success',
'message' => 'Файлы не найдены'
];
}
$iMaxArrayIndex = max(array_keys($files));
$basename = $files[$iMaxArrayIndex]['basename'];
$download_files = Http::get('https://portal.hrc.by/api/cloud/download?api=2.0&project_code=hrc&code=' . $terminal['key'] . '&path=/places/' . $basename);
$file = base64_decode($download_files->json()['content']);
if (!is_dir($dirname)) {
mkdir($dirname, 0755, 'w+');
}
if (file_exists($dirname . $basename)) {
unlink($dirname . $basename);
}
$handle = fopen($dirname . $basename, 'w+');
fputs($handle, chr(0xEF) . chr(0xBB) . chr(0xBF)); // BOM
file_put_contents($dirname . $basename, $file);
fclose($handle);
foreach ($files as $file) {
Http::delete('https://portal.hrc.by/api/cloud/file', [
'code' => $terminal['key'],
'name' => $file['basename'],
'folder' => 'places',
'project_code' => 'hrc',
'api' => '2.0',
]);
}
$places_file = $dirname . $basename;
} else {
return ['status' => 'success',
'message' => 'Файлы не найдены'
];
}
$objXmlDocument = simplexml_load_file($places_file);
$objJsonDocument = json_encode($objXmlDocument);
$xmlPlaces = json_decode($objJsonDocument, TRUE);
Schema::disableForeignKeyConstraints();
DB::table('place_tables')->truncate();
DB::table('places')->truncate();
Schema::enableForeignKeyConstraints();
foreach ($xmlPlaces['Place'] as $xmlPlace) {
$place_name = $xmlPlace['@attributes']['name'];
$newPlace = new Places;
$newPlace->name = $place_name;
$newPlace->save();
foreach ($xmlPlace['Table'] as $table) {
$newTable = new Tables;
$newTable->name = $table['@attributes']['text'];
$newTable->table_id = intval($table['@attributes']['number']);
$newTable->place_id = $newPlace['id'];
$newTable->width = $table['@attributes']['width'];
$newTable->height = $table['@attributes']['height'];
$newTable->x = $table['@attributes']['y'];
$newTable->y = $table['@attributes']['x'];
$newTable->save();
}
}
return [
'status' => 'success',
'places' => $xmlPlaces
];
}
}