Current directory: /home/klas4s23/domains/585455.klas4s23.mid-ica.nl/public_html/Gastenboek/uploads
<?php
class Party {
private $id;
private $name;
private $abbreviation;
private $description;
private $leader;
private $founded_date;
private $logo_path;
private $ideology; // 'left' | 'right' | null
private $ideology_x; // -100..100 | null
private $ideology_y; // -100..100 | null
private $created_at;
private static $hasIdeologyCache = null;
private static $hasIdeologyXCache = null;
private static $hasIdeologyYCache = null;
private static $schemaEnsured = false;
public function __construct(array $data = []) {
if (!empty($data)) {
$this->id = $data['id'] ?? null;
$this->name = $data['name'] ?? '';
$this->abbreviation = $data['abbreviation'] ?? '';
$this->description = $data['description'] ?? '';
$this->leader = $data['leader'] ?? '';
$this->founded_date = $data['founded_date'] ?? null;
$this->logo_path = $data['logo_path'] ?? null;
$this->ideology = $data['ideology'] ?? null;
$this->ideology_x = isset($data['ideology_x']) ? (int)$data['ideology_x'] : null;
$this->ideology_y = isset($data['ideology_y']) ? (int)$data['ideology_y'] : null;
$this->created_at = $data['created_at'] ?? null;
}
}
// Getters
public function getId(): ?int { return $this->id; }
public function getName(): string { return $this->name; }
public function getAbbreviation(): string { return $this->abbreviation; }
public function getDescription(): ?string { return $this->description; }
public function getLeader(): ?string { return $this->leader; }
public function getFoundedDate(): ?string { return $this->founded_date; }
public function getLogoPath(): ?string { return $this->logo_path; }
public function getIdeology(): ?string { return $this->ideology; }
public function getIdeologyX(): ?int { return $this->ideology_x; }
public function getIdeologyY(): ?int { return $this->ideology_y; }
public function getCreatedAt(): ?string { return $this->created_at; }
public function setIdeology(?string $value): void { $this->ideology = $value; }
public function setIdeologyX(?int $v): void { $this->ideology_x = $v; }
public function setIdeologyY(?int $v): void { $this->ideology_y = $v; }
// CRUD operations
public static function find(int $id): ?self {
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->prepare("SELECT * FROM parties WHERE id = :id");
$stmt->execute([':id' => $id]);
$data = $stmt->fetch();
return $data ? new self($data) : null;
}
public static function getAll(): array {
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->query("SELECT * FROM parties ORDER BY name");
$parties = [];
while ($data = $stmt->fetch()) {
$parties[] = new self($data);
}
return $parties;
}
public function save(): bool {
$pdo = Database::getInstance()->getConnection();
// Best-effort: ensure columns exist so X/Y are persisted
self::ensureIdeologySchema($pdo);
$hasIdeology = self::hasIdeologyColumn();
$hasIdeologyX = self::hasIdeologyXColumn();
$hasIdeologyY = self::hasIdeologyYColumn();
if ($this->id) {
$sql = "UPDATE parties SET
name = :name, abbreviation = :abbreviation, description = :description,
leader = :leader, founded_date = :founded_date, logo_path = :logo_path";
if ($hasIdeology) {
$sql .= ", ideology = :ideology";
}
if ($hasIdeologyX) { $sql .= ", ideology_x = :ideology_x"; }
if ($hasIdeologyY) { $sql .= ", ideology_y = :ideology_y"; }
$sql .= " WHERE id = :id";
$stmt = $pdo->prepare($sql);
$params = [
':name' => $this->name,
':abbreviation' => $this->abbreviation,
':description' => $this->description,
':leader' => $this->leader,
':founded_date' => $this->founded_date,
':logo_path' => $this->logo_path,
':id' => $this->id
];
if ($hasIdeology) {
$params[':ideology'] = $this->ideology ?: null;
}
if ($hasIdeologyX) { $params[':ideology_x'] = $this->ideology_x; }
if ($hasIdeologyY) { $params[':ideology_y'] = $this->ideology_y; }
return $stmt->execute($params);
} else {
$cols = ["name", "abbreviation", "description", "leader", "founded_date", "logo_path"];
$vals = [":name", ":abbreviation", ":description", ":leader", ":founded_date", ":logo_path"];
if ($hasIdeology) {
$cols[] = "ideology";
$vals[] = ":ideology";
}
if ($hasIdeologyX) { $cols[] = "ideology_x"; $vals[] = ":ideology_x"; }
if ($hasIdeologyY) { $cols[] = "ideology_y"; $vals[] = ":ideology_y"; }
$sql = "INSERT INTO parties (" . implode(", ", $cols) . ") VALUES (" . implode(", ", $vals) . ")";
$stmt = $pdo->prepare($sql);
$params = [
':name' => $this->name,
':abbreviation' => $this->abbreviation,
':description' => $this->description,
':leader' => $this->leader,
':founded_date' => $this->founded_date,
':logo_path' => $this->logo_path
];
if ($hasIdeology) {
$params[':ideology'] = $this->ideology ?: null;
}
if ($hasIdeologyX) { $params[':ideology_x'] = $this->ideology_x; }
if ($hasIdeologyY) { $params[':ideology_y'] = $this->ideology_y; }
$result = $stmt->execute($params);
if ($result) {
$this->id = (int)$pdo->lastInsertId();
}
return $result;
}
}
public function delete(): bool {
if (!$this->id) return false;
$pdo = Database::getInstance()->getConnection();
// Delete party positions first
$stmt = $pdo->prepare("DELETE FROM party_positions WHERE party_id = :id");
$stmt->execute([':id' => $this->id]);
// Delete the party
$stmt = $pdo->prepare("DELETE FROM parties WHERE id = :id");
return $stmt->execute([':id' => $this->id]);
}
public static function create(array $data): bool {
$party = new self($data);
return $party->save();
}
public function setLogoPath(string $path): void {
$this->logo_path = $path;
}
private static function hasIdeologyColumn(): bool {
if (self::$hasIdeologyCache !== null) return self::$hasIdeologyCache;
try {
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->prepare("SHOW COLUMNS FROM parties LIKE 'ideology'");
$stmt->execute();
self::$hasIdeologyCache = $stmt->fetch() ? true : false;
} catch (Throwable $e) {
self::$hasIdeologyCache = false;
}
return self::$hasIdeologyCache;
}
private static function hasIdeologyXColumn(): bool {
if (self::$hasIdeologyXCache !== null) return self::$hasIdeologyXCache;
try {
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->prepare("SHOW COLUMNS FROM parties LIKE 'ideology_x'");
$stmt->execute();
self::$hasIdeologyXCache = $stmt->fetch() ? true : false;
} catch (Throwable $e) {
self::$hasIdeologyXCache = false;
}
return self::$hasIdeologyXCache;
}
private static function hasIdeologyYColumn(): bool {
if (self::$hasIdeologyYCache !== null) return self::$hasIdeologyYCache;
try {
$pdo = Database::getInstance()->getConnection();
$stmt = $pdo->prepare("SHOW COLUMNS FROM parties LIKE 'ideology_y'");
$stmt->execute();
self::$hasIdeologyYCache = $stmt->fetch() ? true : false;
} catch (Throwable $e) {
self::$hasIdeologyYCache = false;
}
return self::$hasIdeologyYCache;
}
/**
* Attempt to add ideology/coordinate columns when missing.
* Safe to call repeatedly; guarded by caches and try/catch.
*/
private static function ensureIdeologySchema(PDO $pdo): void {
if (self::$schemaEnsured) return;
try {
$needIdeology = !self::hasIdeologyColumn();
$needX = !self::hasIdeologyXColumn();
$needY = !self::hasIdeologyYColumn();
if (!$needIdeology && !$needX && !$needY) { self::$schemaEnsured = true; return; }
// Build ALTER TABLE statement(s) per missing column to avoid IF NOT EXISTS compatibility issues
if ($needIdeology) {
try {
$pdo->exec("ALTER TABLE parties ADD COLUMN ideology ENUM('left','right') NULL DEFAULT NULL");
self::$hasIdeologyCache = true;
} catch (Throwable $e) { /* ignore if already exists */ }
}
if ($needX) {
try {
$pdo->exec("ALTER TABLE parties ADD COLUMN ideology_x INT NULL DEFAULT NULL");
self::$hasIdeologyXCache = true;
} catch (Throwable $e) { /* ignore */ }
}
if ($needY) {
try {
$pdo->exec("ALTER TABLE parties ADD COLUMN ideology_y INT NULL DEFAULT NULL");
self::$hasIdeologyYCache = true;
} catch (Throwable $e) { /* ignore */ }
}
} catch (Throwable $e) {
// Swallow errors; app will continue using fallback rendering
} finally {
self::$schemaEnsured = true;
}
}
}