Non puoi selezionare più di 25 argomenti
Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
294 righe
11 KiB
294 righe
11 KiB
<?php |
|
|
|
namespace App\Console\Commands\Export; |
|
|
|
use App\Models\Main\Tenant; |
|
use Illuminate\Support\Str; |
|
use Illuminate\Console\Command; |
|
use App\Models\Tenant\Gare\Gara; |
|
use Illuminate\Support\Facades\Mail; |
|
use Spatie\DbDumper\Databases\MySql; |
|
use App\Jobs\Preservation\GetFilesJob; |
|
use App\Models\Tenant\Contratti\Contratti; |
|
use App\Jobs\Preservation\MakeZipArchiveJob; |
|
use Facades\Illuminate\Filesystem\Filesystem; |
|
use App\Models\Tenant\Conservazione\Conservazione; |
|
use Symfony\Component\Console\Output\BufferedOutput; |
|
|
|
$path = dirname(base_path()); |
|
include_once "{$path}/config.php"; |
|
|
|
class TenantDataExportCommand extends Command |
|
{ |
|
/** |
|
* Tenant |
|
* |
|
* @var App\Models\Main\Tenant |
|
*/ |
|
private $tenant; |
|
|
|
/** |
|
* The name and signature of the console command. |
|
* |
|
* @var string |
|
*/ |
|
protected $signature = 'export:tenant-data |
|
{--T|tenant= : Tenant Identifier}'; |
|
|
|
/** |
|
* The console command description. |
|
* |
|
* @var string |
|
*/ |
|
protected $description = 'Exports all data for a specified tenant. The tenant identifier must be provided with the --tenant (or -T) option.'; |
|
|
|
/** |
|
* Execute the console command. |
|
* |
|
* @return int |
|
*/ |
|
public function handle() |
|
{ |
|
global $config, $pdo; |
|
|
|
do { |
|
|
|
$tenant = $this->option('tenant') ?? $this->ask('What is Tenant id?'); |
|
$this->tenant = Tenant::find($tenant); |
|
|
|
if (! $this->tenant) { |
|
$this->error('Invalid Tenant ID. Please try again.'); |
|
} |
|
|
|
} while (! $this->tenant); |
|
|
|
setup_db_connection($this->tenant->codice); |
|
|
|
// Rimuovo tutti i pacchetti generati per una eventuale esportazione già eseguita |
|
$this->line("Remove all packages generated for any previous export"); |
|
Conservazione::where('tipologia', 'esportazione')->delete(); |
|
|
|
// Elimino tutti i documenti delle eventuali precedenti estrazioni |
|
$export_folder = "{$config["mediaFolder"]}/{$this->tenant->codice}/esportazione"; |
|
$this->line("Delete all documents from any previous extractions from {$export_folder}"); |
|
|
|
if (!defined("__CMDS_JOB_EXEC")) { define("__CMDS_JOB_EXEC", TRUE); } |
|
$pdo->setConnessioneEnte($this->tenant->codice); |
|
|
|
Filesystem::deleteDirectory($export_folder); |
|
mkdir($export_folder, 0775, TRUE); |
|
|
|
// Creo un archivio per ciascuna procedura di gara |
|
if(Gara::count() > 0) { |
|
|
|
foreach(Gara::select("codice")->cursor() as $gara) { |
|
|
|
$item = Conservazione::firstOrNew([ |
|
'modulo' => 'gare', |
|
'tipologia' => 'esportazione', |
|
'codice_elemento' => $gara["codice"], |
|
'codice_ente' => $this->tenant->codice |
|
]); |
|
|
|
if(empty($item->codice) || ! file_exists("{$export_folder}/ZIPARCH-GARA-{$item->codice_elemento}.zip")) { |
|
|
|
$item->stato = 0; |
|
$item->archivio = 'N'; |
|
$item->titolo = "EXPORT FASCICOLO GARA #{$gara["codice"]} " . date('Ymd'); |
|
$item->timestamp_esecuzione = date(_MYSQL_DT); |
|
$item->timestamp_creazione = date(_MYSQL_DT); |
|
$item->timestamp = date(_MYSQL_DT); |
|
$item->utente_modifica = 0; |
|
$item->save(); |
|
|
|
GetFilesJob::dispatchSync($item->codice_ente, $item->codice, true, false); |
|
$item->refresh(); |
|
if($item->stato == 5) { |
|
|
|
MakeZipArchiveJob::dispatchSync($item->codice_ente, $item->codice); |
|
$item->refresh(); |
|
if($item->archivio == "S") { |
|
|
|
if(rename("{$config["mediaFolder"]}/{$this->tenant->codice}/conservazione/ZIPARCH-{$item->codice}.zip", "{$export_folder}/ZIPARCH-GARA-{$item->codice_elemento}.zip")) { |
|
|
|
$this->line("Processed tender #{$item->codice_elemento}"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
} |
|
|
|
if(Contratti::count() > 0) { |
|
|
|
foreach(Contratti::select("codice")->cursor() as $contratto) { |
|
|
|
$this->line("Processing contract #{$contratto["codice"]}"); |
|
$item = Conservazione::firstOrNew([ |
|
'modulo' => 'contratti', |
|
'tipologia' => 'esportazione', |
|
'codice_elemento' => $contratto["codice"], |
|
'codice_ente' => $this->tenant->codice |
|
]); |
|
|
|
if(empty($item->codice) || ! file_exists("{$export_folder}/ZIPARCH-CONTRATTO-{$item->codice_elemento}.zip")) { |
|
|
|
$item->stato = 0; |
|
$item->archivio = 'N'; |
|
$item->titolo = "EXPORT FASCICOLO CONTRATTO #{$contratto["codice"]} " . date('Ymd'); |
|
$item->timestamp_esecuzione = date(_MYSQL_DT); |
|
$item->timestamp_creazione = date(_MYSQL_DT); |
|
$item->timestamp = date(_MYSQL_DT); |
|
$item->utente_modifica = 0; |
|
$item->save(); |
|
|
|
GetFilesJob::dispatchSync($item->codice_ente, $item->codice, true, false); |
|
$item->refresh(); |
|
if($item->stato == 5) { |
|
|
|
MakeZipArchiveJob::dispatchSync($item->codice_ente, $item->codice); |
|
$item->refresh(); |
|
if($item->archivio == "S") { |
|
|
|
if(rename("{$config["mediaFolder"]}/{$this->tenant->codice}/conservazione/ZIPARCH-{$item->codice}.zip", "{$export_folder}/ZIPARCH-CONTRATTO-{$item->codice_elemento}.zip")) { |
|
|
|
$this->line("Processed contract #{$item->codice_elemento}"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
// Ettettuo il dump del database |
|
$this->line("Dumping database"); |
|
if(! file_exists("{$export_folder}/DATABASE-FULL-DUMP.sql")) { |
|
|
|
MySql::create() |
|
->setDbName(config('database.connections.mysql-tenant.database')) |
|
->setUserName(config('database.connections.mysql-tenant.username')) |
|
->setPassword(config('database.connections.mysql-tenant.password')) |
|
->setHost(config('database.connections.mysql-tenant.host')) |
|
->setPort((int) config('database.connections.mysql-tenant.port')) |
|
->dumpToFile("{$export_folder}/DATABASE-FULL-DUMP.sql"); |
|
|
|
} |
|
|
|
// Esporto tutto l'indirizzario |
|
if(! file_exists("{$export_folder}/INDIRIZZARIO-OO.EE.zip")) { |
|
|
|
$this->line("Dumping indirizzario"); |
|
|
|
$manager = new \oeManager; |
|
$manager->countInviti = true; |
|
$manager->countAggiudicato = true; |
|
|
|
$operatori = $manager->getOERelations($this->tenant->codice); |
|
if(! empty($operatori)) { |
|
|
|
$operatori = array_column($operatori, "codice_operatore"); |
|
$export = $manager->exportInfo($operatori, $this->tenant->codice); |
|
if (! empty($export)) { |
|
|
|
$path = "{$export_folder}/INDIRIZZARIO-OO.EE"; |
|
if(! is_dir($path)) { mkdir($path, 0775, 1); } |
|
if(file_put_contents("{$path}/data.json", json_encode($export["data"], JSON_PRETTY_PRINT))) { |
|
|
|
if (! empty($export["allegati"])) { |
|
|
|
foreach ($export["allegati"] as $allegato) { |
|
|
|
// Rimuovo eventuali sequenze di "/" nel percorso di salvataggio dell'allegato |
|
$attachment_path = preg_replace('#/+#', '/', "{$path}/{$allegato["path"]}"); |
|
|
|
// Cro le cartelle ricorsivamente se non esistono |
|
$cartella = dirname($attachment_path); |
|
if (!is_dir($cartella)) { mkdir($cartella, 0775, true); } |
|
file_put_contents($attachment_path, $allegato["content"]); |
|
|
|
} |
|
|
|
} |
|
|
|
$command = sprintf('cd %s && zip -r %s %s', escapeshellarg($export_folder), escapeshellarg("INDIRIZZARIO-OO.EE.zip"), escapeshellarg("INDIRIZZARIO-OO.EE")); |
|
$output = $result_code = null; |
|
exec($command, $output, $result_code); |
|
if($result_code === 0) { |
|
|
|
Filesystem::deleteDirectory($path); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
$export = null; |
|
|
|
} |
|
|
|
} |
|
|
|
$this->line("Making zip archive"); |
|
|
|
$folderToZip = "esportazione"; |
|
$export_folder = dirname($export_folder); |
|
$zipFile = "esportazione.zip"; |
|
$command = sprintf('cd %s && zip -r %s %s', escapeshellarg($export_folder), escapeshellarg($zipFile), escapeshellarg($folderToZip)); |
|
$output = $result_code = null; |
|
exec($command, $output, $result_code); |
|
if($result_code === 0) { |
|
|
|
$password = generateStrongPassword(15, true); |
|
|
|
$this->tenant->esportazione = 'completa'; |
|
$this->tenant->password_esportazione = password_hash($password, PASSWORD_BCRYPT); |
|
$this->tenant->save(); |
|
|
|
$this->info("Export succeded"); |
|
// Filesystem::deleteDirectory("{$export_folder}/esportazione"); |
|
|
|
$config = config("json", null); |
|
$alerts = $config["alerts"]; |
|
|
|
config([ |
|
"mail.mailers.smtp.host" => $alerts["config"]["smtp"], |
|
"mail.mailers.smtp.port" => $alerts["config"]["port"], |
|
"mail.mailers.smtp.encryption" => "ssl", |
|
"mail.mailers.smtp.username" => $alerts["config"]["username"], |
|
"mail.mailers.smtp.password" => $alerts["config"]["password"], |
|
]); |
|
|
|
$message = "Esportazione piattaforma #{$this->tenant->codice} completata con successo (TenantDataExportCommand)" . PHP_EOL; |
|
$message .= "Password: {$password}"; |
|
|
|
Mail::raw($message, function ($message) use ($alerts, $config) { |
|
|
|
$message->priority(3); |
|
$message->subject("{$config["nome_sito"]}: esportazione piattaforma."); |
|
if(! empty($alerts["from"]["email"])) { $message->from($alerts["from"]["email"], $alerts["from"]["name"]); } |
|
if(! empty($alerts["reply"]["email"])) { $message->replyTo($alerts["reply"]["email"], $alerts["reply"]["name"]); } |
|
|
|
foreach ($alerts["to"] as $address) { |
|
$message->to($address["email"], $address["name"]); |
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
return 0; |
|
|
|
} |
|
|
|
}
|
|
|