315 lines
11 KiB
PHP
315 lines
11 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
class PoliticalParties extends Base {
|
||
|
|
|
||
|
|
// Variables.
|
||
|
|
|
||
|
|
private $current_user_serial = 0;
|
||
|
|
private $current_user_id = "";
|
||
|
|
private $current_user_name = "";
|
||
|
|
private $current_user_email = "";
|
||
|
|
private $pagination_size = 20;
|
||
|
|
private $pagination_page = 1;
|
||
|
|
|
||
|
|
// =================
|
||
|
|
// Class Constructor
|
||
|
|
// =================
|
||
|
|
|
||
|
|
public function __construct() {
|
||
|
|
|
||
|
|
set_error_handler(array($this, "displayError"));
|
||
|
|
|
||
|
|
date_default_timezone_set(self::TIMEZONE);
|
||
|
|
|
||
|
|
$this->current_user_serial = $_SESSION[self::USER_SERIAL] ?? 0;
|
||
|
|
$this->current_user_id = $_SESSION[self::USER_ID] ?? "";
|
||
|
|
$this->current_user_name = $_SESSION[self::USER_NAME] ?? "";
|
||
|
|
$this->current_user_email = $_SESSION[self::USER_EMAIL] ?? "";
|
||
|
|
|
||
|
|
$this->pagination_page = $_SESSION[self::PAGINATION_PAGE] ?? 1;
|
||
|
|
}
|
||
|
|
|
||
|
|
// =======================
|
||
|
|
// Setup Political Parties
|
||
|
|
// =======================
|
||
|
|
|
||
|
|
public function setupPoliticalParties() {
|
||
|
|
|
||
|
|
$politicalparties = $this->getPoliticalPartiesPage();
|
||
|
|
$popovers = (new Popovers())->getPopovers();
|
||
|
|
|
||
|
|
$XML = $this->mergeXML($politicalparties, "<XML/>");
|
||
|
|
$XML = $this->mergeXML($popovers, $XML);
|
||
|
|
|
||
|
|
$content = $this->applyXSL($XML, $this->getXSL("setupPoliticalParties"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===================
|
||
|
|
// Add Political Party
|
||
|
|
// ===================
|
||
|
|
|
||
|
|
public function addPoliticalParty() {
|
||
|
|
|
||
|
|
$step = filter_input(INPUT_POST, "step") ?: "prompt";
|
||
|
|
|
||
|
|
switch ($step) {
|
||
|
|
|
||
|
|
case "prompt":
|
||
|
|
|
||
|
|
$content = $this->applyXSL("", $this->getXSL("addPoliticalParty"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
case "add":
|
||
|
|
|
||
|
|
$politicalparty_code = filter_input(INPUT_POST, "politicalparty_code") ?: "";
|
||
|
|
$politicalparty_name = filter_input(INPUT_POST, "politicalparty_name") ?: "";
|
||
|
|
|
||
|
|
$SQL = "insert into politicalparties
|
||
|
|
|
||
|
|
( politicalparty_code,
|
||
|
|
politicalparty_name,
|
||
|
|
politicalparty_active,
|
||
|
|
politicalparty_creator,
|
||
|
|
politicalparty_created )
|
||
|
|
|
||
|
|
VALUES ( :politicalparty_code,
|
||
|
|
:politicalparty_name,
|
||
|
|
:politicalparty_active,
|
||
|
|
:politicalparty_creator,
|
||
|
|
:politicalparty_created )";
|
||
|
|
|
||
|
|
$statement = $this->connect()->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":politicalparty_code", strtoupper($politicalparty_code));
|
||
|
|
$statement->bindValue(":politicalparty_name", $politicalparty_name);
|
||
|
|
$statement->bindValue(":politicalparty_active", true, PDO::PARAM_BOOL);
|
||
|
|
$statement->bindValue(":politicalparty_creator", $this->current_user_name);
|
||
|
|
$statement->bindValue(":politicalparty_created", $this->getTimestamp());
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
|
||
|
|
$this->displayHome();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ======================
|
||
|
|
// Edit a Political Party
|
||
|
|
// ======================
|
||
|
|
|
||
|
|
public function editPoliticalParty() {
|
||
|
|
|
||
|
|
$step = filter_input(INPUT_POST, "step") ?: "prompt";
|
||
|
|
|
||
|
|
switch ($step) {
|
||
|
|
|
||
|
|
case "prompt":
|
||
|
|
|
||
|
|
$politicalparty_serial = filter_input(INPUT_POST, "politicalparty_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
|
||
|
|
$politicalparty = $this->getPoliticalParty($politicalparty_serial);
|
||
|
|
|
||
|
|
if ($politicalparty->count() == 0) {
|
||
|
|
$this->logError("Invalid Political Party ({$politicalparty_serial}). " . __METHOD__);
|
||
|
|
}
|
||
|
|
|
||
|
|
$content = $this->applyXSL($politicalparty, $this->getXSL("editPoliticalParty"));
|
||
|
|
|
||
|
|
$this->displayContent($content);
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
case "update":
|
||
|
|
|
||
|
|
$politicalparty_serial = filter_input(INPUT_POST, "politicalparty_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
$politicalparty_code = filter_input(INPUT_POST, "politicalparty_code") ?: "";
|
||
|
|
$politicalparty_name = filter_input(INPUT_POST, "politicalparty_name") ?: "";
|
||
|
|
$politicalparty_active = filter_input(INPUT_POST, "politicalparty_active", FILTER_VALIDATE_BOOLEAN) ?: false;
|
||
|
|
|
||
|
|
// Verify the PoliticalParties
|
||
|
|
|
||
|
|
$politicalparty = $this->getPoliticalParty($politicalparty_serial);
|
||
|
|
|
||
|
|
if ($politicalparty->count() == 0) {
|
||
|
|
$this->logError("Invalid PoliticalParties ({$politicalparty_serial}). " . __METHOD__);
|
||
|
|
}
|
||
|
|
|
||
|
|
$SQL = "update politicalparties
|
||
|
|
set politicalparty_code = :politicalparty_code,
|
||
|
|
politicalparty_name = :politicalparty_name,
|
||
|
|
politicalparty_active = :politicalparty_active,
|
||
|
|
politicalparty_changer = :politicalparty_changer,
|
||
|
|
politicalparty_changed = :politicalparty_changed
|
||
|
|
where politicalparty_serial = :politicalparty_serial";
|
||
|
|
|
||
|
|
$statement = $this->connect()->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":politicalparty_serial", $politicalparty_serial);
|
||
|
|
$statement->bindValue(":politicalparty_code", strtoupper($politicalparty_code));
|
||
|
|
$statement->bindValue(":politicalparty_name", $politicalparty_name);
|
||
|
|
$statement->bindValue(":politicalparty_active", $politicalparty_active, PDO::PARAM_BOOL);
|
||
|
|
$statement->bindValue(":politicalparty_changer", $this->current_user_name);
|
||
|
|
$statement->bindValue(":politicalparty_changed", $this->getTimestamp());
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
break;
|
||
|
|
|
||
|
|
default:
|
||
|
|
|
||
|
|
$this->displayHome();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ======================
|
||
|
|
// Delete Political Party
|
||
|
|
// ======================
|
||
|
|
|
||
|
|
public function deletePoliticalParty() {
|
||
|
|
|
||
|
|
$politicalparty_serial = filter_input(INPUT_POST, "politicalparty_serial", FILTER_SANITIZE_NUMBER_INT) ?: 0;
|
||
|
|
|
||
|
|
$politicalparty = $this->getPoliticalParty($politicalparty_serial);
|
||
|
|
|
||
|
|
if ($politicalparty->count() == 0) {
|
||
|
|
$this->logError("Invalid Political Party ({$politicalparty_serial}). " . __METHOD__);
|
||
|
|
}
|
||
|
|
|
||
|
|
$SQL = "delete from politicalparties where politicalparty_serial = {$politicalparty_serial}";
|
||
|
|
|
||
|
|
$this->executeSQL($SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// =======================================
|
||
|
|
// Return an Active Political Party Object
|
||
|
|
// =======================================
|
||
|
|
|
||
|
|
public function getActivePoliticalParties() {
|
||
|
|
|
||
|
|
$SQL = "select view_politicalparties.*
|
||
|
|
from view_politicalparties
|
||
|
|
where politicalparty_active is true";
|
||
|
|
|
||
|
|
return $this->getTable("politicalparties", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ================================
|
||
|
|
// Return an Political Party Object
|
||
|
|
// ================================
|
||
|
|
|
||
|
|
public function getPoliticalParty($politicalparty_serial = 0) {
|
||
|
|
|
||
|
|
$SQL = "select view_politicalparties.*
|
||
|
|
from view_politicalparties
|
||
|
|
where politicalparty_serial = {$politicalparty_serial}";
|
||
|
|
|
||
|
|
return $this->getTable("politicalparties", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===============================
|
||
|
|
// Return a Political Party Object
|
||
|
|
// ===============================
|
||
|
|
|
||
|
|
public function getPoliticalParties() {
|
||
|
|
|
||
|
|
$SQL = "select view_politicalparties.*
|
||
|
|
from view_politicalparties";
|
||
|
|
|
||
|
|
return $this->getTable("politicalparties", $SQL);
|
||
|
|
}
|
||
|
|
|
||
|
|
// =====================================
|
||
|
|
// Return a Political Partys Page Object
|
||
|
|
// =====================================
|
||
|
|
|
||
|
|
public function getPoliticalPartiesPage() {
|
||
|
|
|
||
|
|
// Find Criteria
|
||
|
|
|
||
|
|
$find_politicalparty = filter_input(INPUT_POST, "find_politicalparty") ?: "";
|
||
|
|
$find_politicalparty = $this->sanitizeString($find_politicalparty);
|
||
|
|
|
||
|
|
$find_words = implode("|", explode(" ", $find_politicalparty));
|
||
|
|
|
||
|
|
// Record Count
|
||
|
|
|
||
|
|
$connection = $this->connect();
|
||
|
|
|
||
|
|
$SQL = "select count(*) as count
|
||
|
|
from view_politicalparties
|
||
|
|
where politicalparty_code regexp :politicalparty_code
|
||
|
|
or politicalparty_name regexp :politicalparty_name";
|
||
|
|
|
||
|
|
$statement = $connection->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":politicalparty_code", $find_words);
|
||
|
|
$statement->bindValue(":politicalparty_name", $find_words);
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
$recordset = $statement->fetch(PDO::FETCH_ASSOC);
|
||
|
|
|
||
|
|
$count = (count($recordset) > 0) ? (integer) $recordset["count"] : (integer) 0;
|
||
|
|
|
||
|
|
// Pagination
|
||
|
|
|
||
|
|
$pagination = filter_input(INPUT_POST, "pagination") ?: "";
|
||
|
|
|
||
|
|
switch ($pagination) {
|
||
|
|
|
||
|
|
case "first" : $this->pagination_page = 1;
|
||
|
|
break;
|
||
|
|
case "previous" : $this->pagination_page = max(($this->pagination_page - 1), 1);
|
||
|
|
break;
|
||
|
|
case "next" : $this->pagination_page = ($this->pagination_page + 1);
|
||
|
|
break;
|
||
|
|
case "last" : $this->pagination_page = ($count / $this->pagination_size);
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
|
||
|
|
$offset = (($this->pagination_page * $this->pagination_size) - $this->pagination_size);
|
||
|
|
$start = ($offset + 1);
|
||
|
|
$end = min($count, (($start + $this->pagination_size) - 1));
|
||
|
|
|
||
|
|
// Data
|
||
|
|
|
||
|
|
$SQL = "select view_politicalparties.*
|
||
|
|
from view_politicalparties
|
||
|
|
where politicalparty_code regexp :politicalparty_code
|
||
|
|
or politicalparty_name regexp :politicalparty_name
|
||
|
|
order by politicalparty_code
|
||
|
|
limit {$this->pagination_size}
|
||
|
|
offset {$offset}";
|
||
|
|
|
||
|
|
$statement = $connection->prepare($SQL);
|
||
|
|
|
||
|
|
$statement->bindValue(":politicalparty_code", $find_words);
|
||
|
|
$statement->bindValue(":politicalparty_name", $find_words);
|
||
|
|
|
||
|
|
$statement->execute();
|
||
|
|
|
||
|
|
$politicalparties = $this->getTableXML("politicalparties", $statement);
|
||
|
|
|
||
|
|
// Insert Pagination Attributes
|
||
|
|
|
||
|
|
$politicalparties->addAttribute("count", $count);
|
||
|
|
$politicalparties->addAttribute("start", $start);
|
||
|
|
$politicalparties->addAttribute("end", $end);
|
||
|
|
$politicalparties->addAttribute("page", $this->pagination_page);
|
||
|
|
|
||
|
|
$_SESSION[self::PAGINATION_PAGE] = $this->pagination_page;
|
||
|
|
|
||
|
|
return $politicalparties;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
?>
|