230 lines
7.4 KiB
PHP
230 lines
7.4 KiB
PHP
<?php
|
|
|
|
require_once("/var/www/thetool/config/config.php");
|
|
|
|
define('FRONKDB_SQLDEBUG', false);
|
|
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
|
|
|
|
require_once(LIBDIR . "/mvcfronk/mfRouter/mfRouter.php");
|
|
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseModel.php");
|
|
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseController.php");
|
|
|
|
$pgHost = QGIS_DBHOST;
|
|
$pgPort = '5432';
|
|
$pgDb = QGIS_DBNAME;
|
|
$pgUser = QGIS_DBUSER;
|
|
$pgPass = QGIS_DBPASS;
|
|
|
|
$targetTable = 'Preorders';
|
|
|
|
|
|
$campaigns = [
|
|
[
|
|
'targetSchema' => '"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 = <<<SQL
|
|
CREATE TABLE IF NOT EXISTS $targetSchema."$targetTable" (
|
|
id INTEGER PRIMARY KEY,
|
|
type VARCHAR(50),
|
|
type_label VARCHAR(100),
|
|
strasse VARCHAR(255),
|
|
hausnummer VARCHAR(50),
|
|
plz VARCHAR(10),
|
|
ort VARCHAR(100),
|
|
geom geometry(Point, 4326),
|
|
company VARCHAR(255),
|
|
firstname VARCHAR(255),
|
|
lastname VARCHAR(255),
|
|
phone VARCHAR(100),
|
|
email VARCHAR(255),
|
|
status_code INTEGER,
|
|
status_id INTEGER,
|
|
oaid VARCHAR(255),
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_preorders_geom ON $targetSchema."$targetTable" USING GIST (geom);
|
|
SQL;
|
|
|
|
$pdo->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 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)");
|
|
|
|
$sqlUpsert = <<<SQL
|
|
INSERT INTO $targetSchema."$targetTable"
|
|
(id, type, type_label, strasse, hausnummer, plz, ort, geom, company, firstname, lastname, phone, email, status_code, status_id, oaid, updated_at)
|
|
VALUES
|
|
(:id, :type, :type_label, :strasse, :hausnummer, :plz, :ort, ST_SetSRID(ST_MakePoint(:lon, :lat), 4326), :company, :firstname, :lastname, :phone, :email, :status_code, :status_id, :oaid, NOW())
|
|
ON CONFLICT (id) DO UPDATE SET
|
|
type = EXCLUDED.type,
|
|
type_label = EXCLUDED.type_label,
|
|
strasse = EXCLUDED.strasse,
|
|
hausnummer = EXCLUDED.hausnummer,
|
|
plz = EXCLUDED.plz,
|
|
ort = EXCLUDED.ort,
|
|
geom = EXCLUDED.geom,
|
|
company = EXCLUDED.company,
|
|
firstname = EXCLUDED.firstname,
|
|
lastname = EXCLUDED.lastname,
|
|
phone = EXCLUDED.phone,
|
|
email = EXCLUDED.email,
|
|
status_code = EXCLUDED.status_code,
|
|
status_id = EXCLUDED.status_id,
|
|
oaid = EXCLUDED.oaid,
|
|
updated_at = NOW()
|
|
WHERE
|
|
"$targetTable".type IS DISTINCT FROM EXCLUDED.type OR
|
|
"$targetTable".type_label IS DISTINCT FROM EXCLUDED.type_label OR
|
|
"$targetTable".strasse IS DISTINCT FROM EXCLUDED.strasse OR
|
|
"$targetTable".hausnummer IS DISTINCT FROM EXCLUDED.hausnummer OR
|
|
"$targetTable".plz IS DISTINCT FROM EXCLUDED.plz OR
|
|
"$targetTable".ort IS DISTINCT FROM EXCLUDED.ort OR
|
|
"$targetTable".geom IS DISTINCT FROM EXCLUDED.geom OR
|
|
"$targetTable".company IS DISTINCT FROM EXCLUDED.company OR
|
|
"$targetTable".firstname IS DISTINCT FROM EXCLUDED.firstname OR
|
|
"$targetTable".lastname IS DISTINCT FROM EXCLUDED.lastname OR
|
|
"$targetTable".phone IS DISTINCT FROM EXCLUDED.phone OR
|
|
"$targetTable".email IS DISTINCT FROM EXCLUDED.email OR
|
|
"$targetTable".status_code IS DISTINCT FROM EXCLUDED.status_code OR
|
|
"$targetTable".status_id IS DISTINCT FROM EXCLUDED.status_id OR
|
|
"$targetTable".oaid IS DISTINCT FROM EXCLUDED.oaid;
|
|
SQL;
|
|
|
|
$stmt = $pdo->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;
|
|
}
|
|
|
|
$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,
|
|
':plz' => $po->adb_plz,
|
|
':ort' => $po->adb_ort,
|
|
':company' => $po->company,
|
|
':firstname' => $po->firstname,
|
|
':lastname' => $po->lastname,
|
|
':phone' => $po->phone,
|
|
':email' => $po->email,
|
|
':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();
|
|
}
|