'"ON Leibnitz"', 'campaignId' => 99 ], [ 'targetSchema' => '"ON Semriach"', 'campaignId' => 101 ], [ 'targetSchema' => '"ON Bad Gleichenberg"', 'campaignId' => 108 ], [ 'targetSchema' => '"ON Straden"', 'campaignId' => 107 ], [ 'targetSchema' => '"ON St.Anna am Aigen"', 'campaignId' => 106 ] ]; define("INTERNAL_USER_ID", 154); class PreorderSyncWrapper extends PreorderController { public static $capturedResult = null; protected function init() { $this->me = new User(INTERNAL_USER_ID); $this->layout()->setTemplate(null); } public static function returnJson($data) { self::$capturedResult = $data; } } try { $dsn = "pgsql:host=$pgHost;port=$pgPort;dbname=$pgDb"; $pdo = new PDO($dsn, $pgUser, $pgPass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); } catch (PDOException $e) { die("Verbindung zu PostgreSQL fehlgeschlagen: " . $e->getMessage() . "\n"); } foreach ($campaigns as $campaign) { $targetSchema = $campaign['targetSchema']; $campaignId = $campaign['campaignId']; $apiParams = [ 'mod' => 'Preorder', 'action' => 'api', 'do' => 'getFilteredPreorders', 'filter' => [ 'preordercampaign_id' => $campaignId ] ]; PreorderSyncWrapper::$capturedResult = null; new PreorderSyncWrapper($apiParams); $response = PreorderSyncWrapper::$capturedResult; if (!$response || !isset($response['status']) || $response['status'] !== 'OK') { echo "Fehler beim Abrufen der Daten oder keine Daten erhalten fuer Schema $targetSchema (ID: $campaignId).\n"; continue; } $preorders = $response['result']['preorders'] ?? []; $pdo->exec("CREATE SCHEMA IF NOT EXISTS $targetSchema"); $createTableSql = <<exec($createTableSql); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS company VARCHAR(255)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS firstname VARCHAR(255)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS lastname VARCHAR(255)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS phone VARCHAR(100)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS email VARCHAR(255)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS note TEXT"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS status_code INTEGER"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS status_id INTEGER"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS oaid VARCHAR(255)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS block VARCHAR(50)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS stiege VARCHAR(50)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS stock VARCHAR(50)"); $pdo->exec("ALTER TABLE $targetSchema.\"$targetTable\" ADD COLUMN IF NOT EXISTS tuer VARCHAR(50)"); $sqlUpsert = <<prepare($sqlUpsert); $processedIds = []; $countUpsert = 0; $countUnchanged = 0; $countSkipped = 0; $pdo->beginTransaction(); foreach ($preorders as $po) { $id = $po->id; $gps_lat = $po->gps_lat; $gps_long = $po->gps_long; if (empty($gps_lat) || empty($gps_long)) { $countSkipped++; continue; } $block = $po->adb_block ?? null; $stiege = $po->adb_stiege ?? null; $stock = $po->adb_stock ?? null; $tuer = $po->adb_tuer ?? null; if (!empty($po->adb_wohneinheit_id)) { $wohneinheit = new ADBWohneinheit($po->adb_wohneinheit_id); if ($wohneinheit->id) { $block = $wohneinheit->block; $stiege = $wohneinheit->stiege; $stock = $wohneinheit->stock; $tuer = $wohneinheit->tuer; } } $latVal = str_replace(',', '.', $gps_lat); $lonVal = str_replace(',', '.', $gps_long); $params = [ ':id' => $id, ':type' => $po->type, ':type_label' => $po->type_label, ':strasse' => $po->adb_strasse, ':hausnummer' => $po->adb_hausnummer, ':block' => $block, ':stiege' => $stiege, ':stock' => $stock, ':tuer' => $tuer, ':plz' => $po->adb_plz, ':ort' => $po->adb_ort, ':company' => $po->company, ':firstname' => $po->firstname, ':lastname' => $po->lastname, ':phone' => $po->phone, ':email' => $po->email, ':note' => $po->note, ':status_code' => $po->status_code, ':status_id' => $po->status_id, ':oaid' => $po->oaid, ':lat' => $latVal, ':lon' => $lonVal ]; $stmt->execute($params); $processedIds[] = $id; if ($stmt->rowCount() > 0) { $countUpsert++; } else { $countUnchanged++; } } $deletedCount = 0; if (!empty($processedIds)) { $inQuery = implode(',', array_map('intval', $processedIds)); $deleteSql = "DELETE FROM $targetSchema.\"$targetTable\" WHERE id NOT IN ($inQuery)"; $deletedCount = $pdo->exec($deleteSql); } else { if (count($preorders) == 0) { } } $pdo->commit(); }