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' => 'Файлы не найдены' ]; } $xmlPlaces = XmlToArray::convert(file_get_contents($places_file)); Schema::disableForeignKeyConstraints(); DB::table('place_tables')->truncate(); DB::table('places')->truncate(); Schema::enableForeignKeyConstraints(); if (count($xmlPlaces['Place']) > 1) { $places = $xmlPlaces['Place']; foreach ($places as $place) { $newPlace = new Places; $newPlace->name = $place['@attributes']['name']; if ($place['@attributes']['image'] != "false") { $newPlace->image = base64_encode(Base::customBase64Decode($place['@attributes']['image'])); $newPlace->save(); $image = $newPlace->image; $imageName = "place-" . $newPlace->id . ".png"; file_put_contents($dirname . $imageName, base64_decode($image)); } else { $newPlace->save(); } $tables = $place['Table']; if (count($tables) > 1) { foreach ($tables 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']; if (isset($table['@attributes']['type'])) { $newTable->type = intval($table['@attributes']['type']); } else { $newTable->type = 0; } $newTable->save(); } } else { $table = $place['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']; if (isset($table['@attributes']['type'])) { $newTable->type = intval($table['@attributes']['type']); } else { $newTable->type = 0; } $newTable->save(); } } } else { $place = $xmlPlaces['Place']; $newPlace = new Places; $newPlace->name = $place['@attributes']['name']; if ($place['@attributes']['image'] != "false") { $newPlace->image = $place['@attributes']['image']; $newPlace->save(); $image = $newPlace->image; $image = str_replace(array('\r\n', '\r', '\n'), '', $image); $image = str_replace(' ', '', $image); $imageName = "place-" . $newPlace->id . ".png"; file_put_contents($dirname . $imageName, base64_decode($image)); } else { $newPlace->save(); } $tables = $place['Table']; if (count($tables) > 1) { foreach ($tables 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']; if (isset($table['@attributes']['type'])) { $newTable->type = intval($table['@attributes']['type']); } else { $newTable->type = 0; } $newTable->save(); } } else { $table = $place['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']; if (isset($table['@attributes']['type'])) { $newTable->type = intval($table['@attributes']['type']); } else { $newTable->type = 0; } $newTable->save(); } } return [ 'status' => 'success', 'places' => $xmlPlaces ]; } }