<?php
/**
 * Demo Oracle — exports, imports et tables externes (section « dash/exports »).
 *
 * Portage de modules/oracle/_report_dash_exports.php, sans retrait :
 * ce rapport est une lecture pure, et le module complet n'y propose aucune
 * action. La demonstration le sert donc a l'identique.
 *
 * Inclus par report.php. Variables : $oraDb, $db, $currentUser, $settings.
 */

require_once __DIR__ . '/lib/DemoOracleOciConnect.php';
require_once __DIR__ . '/lib/DemoOracleDashExportsRepository.php';

/** @var array      $oraDb       */
/** @var Database   $db          */
/** @var array|null $currentUser */

$ociError   = null;
$fetchError = null;
$conn       = demo_oracle_oci_open($oraDb, $db, $ociError, '', 'dash/exports');
$dpJobs     = [];
$dpLongOps  = [];
$dirs       = [];
$extTables  = [];
$extLocs    = [];

if (!$conn) {
    $fetchError = $ociError
        ? ('ORA-' . $ociError['code'] . ': ' . $ociError['message'])
        : t('demo_oracle.connect_error');
} else {
    $dpJobs    = DemoOracleDashExportsRepository::fetchDataPumpJobs($conn);
    $dpLongOps = DemoOracleDashExportsRepository::fetchDataPumpLongOps($conn);
    $dirs      = DemoOracleDashExportsRepository::fetchDirectories($conn);
    $extTables = DemoOracleDashExportsRepository::fetchExternalTables($conn);
    $extLocs   = DemoOracleDashExportsRepository::fetchExternalLocations($conn);

    oci_close($conn);
}

/* Indexe les locations par (owner, table_name) pour les afficher en sous-ligne */
$locsByTable = [];
foreach ($extLocs as $l) {
    $k = ($l['OWNER'] ?? '') . '|' . ($l['TABLE_NAME'] ?? '');
    $locsByTable[$k][] = $l;
}

View::display(__DIR__ . '/views/report/dash_exports.php', [
    'fetchError'  => $fetchError,
    'dpJobs'      => $dpJobs,
    'dpLongOps'   => $dpLongOps,
    'dirs'        => $dirs,
    'extTables'   => $extTables,
    'locsByTable' => $locsByTable,
]);