Files
realestatedatainc/classes/PermitFacts.php
T
2026-05-29 14:52:16 -04:00

632 lines
26 KiB
PHP

<?php
class PermitFacts 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;
}
// ==============
// Search Permits
// ==============
public function searchPermitFacts() {
$subscription_serial = $_SESSION['subscription_serial'];
$usersubscription_serial = $_SESSION['usersubscription_serial'];
$subscription = (new Subscriptions())->getSubscription($subscription_serial);
if ($subscription->count() === 0) {
$this->logError("Invalid Subscription ({$subscription_serial}): " . __METHOD__);
}
$userssubscription = (new Users())->getUserSubscription($usersubscription_serial);
$user_subscription_start_date = $userssubscription->record->usersubscription_start_date;
$user_subscription_end_date = $userssubscription->record->usersubscription_end_date;
$searchhistory = (new SearchHistory())->getUsersPreviousSearchParameters($this->current_user_serial, $subscription_serial);
// Update Search History Record
if (empty($searchhistory)) {
(new SearchHistory())->saveUserSearchParameters($_POST);
} else {
(new SearchHistory())->updateUserSearchParameters($_POST);
}
$permits = $this->getPermitFactsPage($user_subscription_start_date, $user_subscription_end_date);
$searchhistory = (new SearchHistory())->getUsersPreviousSearchParameters($this->current_user_serial, $subscription_serial);
// Populate Entry Dates with what was searched for if provided or users entry dates if no entry dates were provided
$searchhistory->record->searchhistory_parameters->permitfact_entry_date_from = $permits->record->permitfact_entry_date_from;
$searchhistory->record->searchhistory_parameters->permitfact_entry_date_to = $permits->record->permitfact_entry_date_to;
$popovers = (new Popovers())->getPopovers();
$dropdowns = (new Dropdowns())->getDropdowns();
$cities = (new Cities())->getSubscriptionCities($subscription_serial);
$counties = (new Counties())->getSubscriptionCounties($subscription_serial);
$availablerecordcount = $this->userTotalAvailableRecordCount($user_subscription_start_date, $user_subscription_end_date);
$XML = $this->mergeXML($permits, "<XML/>");
$XML = $this->mergeXML($subscription, $XML);
$XML = $this->mergeXML($popovers, $XML);
$XML = $this->mergeXML($dropdowns, $XML);
$XML = $this->mergeXML($cities, $XML);
$XML = $this->mergeXML($counties, $XML);
$XML = $this->mergeXML($searchhistory, $XML);
$XML = $this->mergeXML($availablerecordcount, $XML);
$content = $this->applyXSL($XML, $this->getXSL("searchPermitFacts"));
$this->displayContent($content);
}
// =================================
// Return a Permit Facts Page Object
// =================================
public function getPermitFactsPage($user_subscription_start_date, $user_subscription_end_date) {
set_time_limit(0);
// Sanitize inputs
$permitfact_company = filter_input(INPUT_POST, "permitfact_company") ?: "";
$permitfact_project_name = filter_input(INPUT_POST, "permitfact_project_name") ?: "";
$permitfact_project_zip = filter_input(INPUT_POST, "permitfact_project_zip") ?: "";
$permitfact_project_city = filter_input(INPUT_POST, "permitfact_project_city") ?: "";
$permitfact_county = filter_input(INPUT_POST, "permitfact_county") ?: "";
$permitfact_project_type = filter_input(INPUT_POST, "permitfact_project_type") ?: "";
$permitfact_work_type = filter_input(INPUT_POST, "permitfact_work_type") ?: "";
$permitfact_building_type = filter_input(INPUT_POST, "permitfact_building_type") ?: "";
$permitfact_size_from = filter_input(INPUT_POST, "permitfact_size_from") ?: "";
$permitfact_size_to = filter_input(INPUT_POST, "permitfact_size_to") ?: "";
$permitfact_value_from = filter_input(INPUT_POST, "permitfact_value_from") ?: "";
$permitfact_value_to = filter_input(INPUT_POST, "permitfact_value_to") ?: "";
$permitfact_permit_date_from = filter_input(INPUT_POST, "permitfact_permit_date_from") ?: "";
$permitfact_permit_date_to = filter_input(INPUT_POST, "permitfact_permit_date_to") ?: "";
$permitfact_entry_date_from = filter_input(INPUT_POST, "permitfact_entry_date_from") ?: "";
$permitfact_entry_date_to = filter_input(INPUT_POST, "permitfact_entry_date_to") ?: "";
// Determine real date range based upon users subscription start date
if ($user_subscription_start_date <= date('Y-m-d', strtotime('-1 year'))) {
$user_subscription_start_date = date('Y-m-d', strtotime('-1 year'));
}
$permitfact_permit_date_from = ($permitfact_permit_date_from == "") ? null : date("Y-m-d", strtotime($permitfact_permit_date_from));
$permitfact_permit_date_to = ($permitfact_permit_date_to == "") ? null : date("Y-m-d", strtotime($permitfact_permit_date_to));
$permitfact_entry_date_from = ($permitfact_entry_date_from == "") ? null : date("Y-m-d", strtotime($permitfact_entry_date_from));
$permitfact_entry_date_to = ($permitfact_entry_date_to == "") ? null : date("Y-m-d", strtotime($permitfact_entry_date_to));
// Pagination settings
$pagination = filter_input(INPUT_POST, "pagination") ?: "";
$this->pagination_size = filter_input(INPUT_POST, "permitfacts_per_page", FILTER_SANITIZE_NUMBER_INT) ?: $this->pagination_size;
// Connect to DB
$connection = $this->connect();
// Record Count
$SQL = "select count(*) as count
from view_permitfacts
where view_permitfacts.permitfact_entry_date between :user_subscription_start_date and :user_subscription_end_date ";
if ($permitfact_company !== '') {
$SQL .= " and permitfact_company regexp :permitfact_company ";
}
if ($permitfact_project_name !== '') {
$SQL .= " and view_permitfacts.permitfact_project_name regexp :permitfact_project_name ";
}
if ($permitfact_project_zip !== '') {
$SQL .= " and view_permitfacts.permitfact_project_zip = :permitfact_project_zip ";
}
if ($permitfact_project_city !== '') {
$SQL .= " and view_permitfacts.permitfact_project_city in ($permitfact_project_city) ";
}
// if ($permitfact_project_city !== '') {
// $SQL .= " and view_permitfacts.permitfact_city in ($permitfact_project_city) ";
// } else {
// $cities = (new Cities())->getSubscriptionCities($_SESSION['subscription_serial']);
//
// $cityNames = [];
//
// foreach ($cities as $city) {
// $cityNames[] = " " . (string) $city->subscriptioncity_city . " ";
// }
//
// $permitfact_project_city = ' ' . implode(', ', $cityNames) . ' ';
//
// $SQL .= " and view_permitfacts.permitfact_city in ($permitfact_project_city) ";
// }
if ($permitfact_county !== '') {
$SQL .= " and view_permitfacts.permitfact_county in ($permitfact_county) ";
} else {
$counties = (new Counties())->getSubscriptionCounties($_SESSION['subscription_serial']);
$countyNames = [];
foreach ($counties as $county) {
$countyNames[] = " " . (string) $county->subscriptioncounty_county . " ";
}
$permitfact_county = ' ' . implode(', ', $countyNames) . ' ';
$SQL .= " and view_permitfacts.permitfact_county in ($permitfact_county) ";
}
if ($permitfact_project_type !== '') {
$SQL .= " and view_permitfacts.permitfact_project_type in ($permitfact_project_type) ";
}
if ($permitfact_work_type !== '') {
$SQL .= " and view_permitfacts.permitfact_work_type in ($permitfact_work_type) ";
}
if ($permitfact_building_type !== '') {
$SQL .= " and view_permitfacts.permitfact_building_type in ($permitfact_building_type) ";
}
if ($permitfact_size_from !== '' && $permitfact_size_to !== '') {
$SQL .= " and view_permitfacts.permitfact_size between '{$permitfact_size_from}' and '{$permitfact_size_to}' ";
} elseif ($permitfact_size_from != '') {
$SQL .= " and view_permitfacts.permitfact_size = '{$permitfact_size_from}' ";
} elseif ($permitfact_size_to != '') {
$SQL .= " and view_permitfacts.permitfact_size = '{$permitfact_size_to}' ";
}
if ($permitfact_value_from !== '' && $permitfact_value_to !== '') {
$SQL .= " and view_permitfacts.permitfact_value between '{$permitfact_value_from}' and '{$permitfact_value_to}' ";
} elseif ($permitfact_value_from != '') {
$SQL .= " and view_permitfacts.permitfact_value = '{$permitfact_value_from}' ";
} elseif ($permitfact_value_to != '') {
$SQL .= " and view_permitfacts.permitfact_value = '{$permitfact_value_to}' ";
}
// =======================
// Permit Date Logic Start
// =======================
$permitFrom = $permitfact_permit_date_from; // null or Y-m-d
$permitTo = $permitfact_permit_date_to; // null or Y-m-d
$subStart = $user_subscription_start_date; // Y-m-d
$subEnd = $user_subscription_end_date; // Y-m-d
// ---- Enforce subscription window for permit dates (only if user provided them) ----
if ($permitFrom !== null) {
if ($permitFrom < $subStart || $permitFrom > $subEnd) {
$this->displayNotice("The Permit Issue 'from' date or date range {$this->current_user_name} selected is outside your authorized access period.");
return;
}
}
if ($permitTo !== null) {
if ($permitTo < $subStart || $permitTo > $subEnd) {
$this->displayNotice("The Permit Issue 'to' date or date range {$this->current_user_name} selected is outside your authorized access period.");
return;
}
}
// ---- Build the permit date filter logic ----
if ($permitFrom !== null && $permitTo !== null) {
// If user reversed the dates, swap them
if ($permitFrom > $permitTo) {
$tmp = $permitFrom;
$permitFrom = $permitTo;
$permitTo = $tmp;
}
$SQL .= " AND view_permitfacts.permitfact_permit_date BETWEEN '{$permitFrom}' AND '{$permitTo}'";
} elseif ($permitFrom !== null) {
// User entered only "From" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_permit_date = '{$permitFrom}'";
} elseif ($permitTo !== null) {
// User entered only "To" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_permit_date = '{$permitTo}'";
}
// =====================
// Permit Date Logic End
// =====================
// ======================
// Entry Date Logic Start
// ======================
$entryFrom = $permitfact_entry_date_from; // null or Y-m-d
$entryTo = $permitfact_entry_date_to; // null or Y-m-d
$subStart = $user_subscription_start_date; // Y-m-d
$subEnd = $user_subscription_end_date; // Y-m-d
// ---- Enforce subscription window for entry dates (only if user provided them) ----
if ($entryFrom !== null) {
if ($entryFrom < $subStart || $entryFrom > $subEnd) {
$this->displayNotice("The Entry 'from' date or date range {$this->current_user_name} selected is outside your authorized access period.");
return;
}
}
if ($entryTo !== null) {
if ($entryTo < $subStart || $entryTo > $subEnd) {
$this->displayNotice("The Entry 'to' date or date range {$this->current_user_name} selected is outside your authorized access period.");
return;
}
}
// ---- Build the entry date filter logic ----
if ($entryFrom !== null && $entryTo !== null) {
// If user reversed the dates, swap them
if ($entryFrom > $entryTo) {
$tmp = $entryFrom;
$entryFrom = $entryTo;
$entryTo = $tmp;
}
$SQL .= " AND view_permitfacts.permitfact_entry_date BETWEEN '{$entryFrom}' AND '{$entryTo}'";
} elseif ($entryFrom !== null) {
// User entered only "From" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_entry_date = '{$entryFrom}'";
} elseif ($entryTo !== null) {
// User entered only "To" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_entry_date = '{$entryTo}'";
}
// ====================
// Entry Date Logic End
// ====================
$statement = $connection->prepare($SQL);
if ($permitfact_company !== '') {
$statement->bindValue(":permitfact_company", $permitfact_company, PDO::PARAM_STR);
}
if ($permitfact_project_name !== '') {
$statement->bindValue(":permitfact_project_name", $permitfact_project_name, PDO::PARAM_STR);
}
if ($permitfact_project_zip !== '') {
$statement->bindValue(":permitfact_project_zip", $permitfact_project_zip, PDO::PARAM_STR);
}
$statement->bindValue(":user_subscription_start_date", $user_subscription_start_date, PDO::PARAM_STR);
$statement->bindValue(":user_subscription_end_date", $user_subscription_end_date, PDO::PARAM_STR);
$statement->execute();
$recordset = $statement->fetch(PDO::FETCH_ASSOC);
$count = $recordset ? (int) $recordset["count"] : 0;
// === PAGINATION LOGIC ===
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 = min($this->pagination_page + 1, ceil($count / $this->pagination_size));
break;
case "last": $this->pagination_page = (int) ceil($count / $this->pagination_size);
break;
}
$offset = max(0, ($this->pagination_page - 1) * $this->pagination_size);
$start = $offset + 1;
$end = min($count, $start + $this->pagination_size - 1);
// === DATA QUERY ===
$SQL = "select view_permitfacts.*
from view_permitfacts
where view_permitfacts.permitfact_entry_date between :user_subscription_start_date and :user_subscription_end_date ";
if ($permitfact_company !== '') {
$SQL .= " and view_permitfacts.permitfact_company regexp :permitfact_company ";
}
if ($permitfact_project_name !== '') {
$SQL .= " and view_permitfacts.permitfact_project_name regexp :permitfact_project_name ";
}
if ($permitfact_project_zip !== '') {
$SQL .= " and view_permitfacts.permitfact_project_zip = :permitfact_project_zip ";
}
if ($permitfact_project_city !== '') {
$SQL .= " and view_permitfacts.permitfact_project_city in ($permitfact_project_city) ";
}
// if ($permitfact_project_city !== '') {
// $SQL .= " and view_permitfacts.permitfact_city in ($permitfact_project_city) ";
// } else {
// $cities = (new Cities())->getSubscriptionCities($_SESSION['subscription_serial']);
//
// $cityNames = [];
//
// foreach ($cities as $city) {
// $cityNames[] = " " . (string) $city->subscriptioncity_city . " ";
// }
//
// $permitfact_project_city = ' ' . implode(', ', $cityNames) . ' ';
//
// $SQL .= " and view_permitfacts.permitfact_city in ($permitfact_project_city) ";
// }
if ($permitfact_county !== '') {
$SQL .= " and view_permitfacts.permitfact_county in ($permitfact_county) ";
} else {
$counties = (new Counties())->getSubscriptionCounties($_SESSION['subscription_serial']);
$countyNames = [];
foreach ($counties as $county) {
$countyNames[] = " " . (string) $county->subscriptioncounty_county . " ";
}
$permitfact_county = ' ' . implode(', ', $countyNames) . ' ';
$SQL .= " and view_permitfacts.permitfact_county in ($permitfact_county) ";
}
if ($permitfact_project_type !== '') {
$SQL .= " and view_permitfacts.permitfact_project_type in ($permitfact_project_type) ";
}
if ($permitfact_work_type !== '') {
$SQL .= " and view_permitfacts.permitfact_work_type in ($permitfact_work_type) ";
}
if ($permitfact_building_type !== '') {
$SQL .= " and view_permitfacts.permitfact_building_type in ($permitfact_building_type) ";
}
if ($permitfact_size_from !== '' && $permitfact_size_to !== '') {
$SQL .= " and view_permitfacts.permitfact_size between '{$permitfact_size_from}' and '{$permitfact_size_from}' ";
} elseif ($permitfact_size_from != '') {
$SQL .= " and view_permitfacts.permitfact_size = '{$permitfact_size_from}' ";
} elseif ($permitfact_size_to != '') {
$SQL .= " and view_permitfacts.permitfact_size = '{$permitfact_size_from}' ";
}
if ($permitfact_value_from !== '' && $permitfact_value_to !== '') {
$SQL .= " and view_permitfacts.permitfact_value between '{$permitfact_value_from}' and '{$permitfact_value_to}' ";
} elseif ($permitfact_value_from !== '') {
$SQL .= " and view_permitfacts.permitfact_value = '{$permitfact_value_to}' ";
} elseif ($permitfact_value_to !== '') {
$SQL .= " and view_permitfacts.permitfact_value = '{$permitfact_value_to}' ";
}
// =======================
// Permit Date Logic Start
// =======================
$permitFrom = $permitfact_permit_date_from; // null or Y-m-d
$permitTo = $permitfact_permit_date_to; // null or Y-m-d
$subStart = $user_subscription_start_date; // Y-m-d
$subEnd = $user_subscription_end_date; // Y-m-d
// ---- Enforce subscription window for permit dates (only if user provided them) ----
if ($permitFrom !== null) {
if ($permitFrom < $subStart || $permitFrom > $subEnd) {
return;
}
}
if ($permitTo !== null) {
if ($permitTo < $subStart || $permitTo > $subEnd) {
return;
}
}
// ---- Build the permit date filter logic ----
if ($permitFrom !== null && $permitTo !== null) {
// If user reversed the dates, swap them
if ($permitFrom > $permitTo) {
$tmp = $permitFrom;
$permitFrom = $permitTo;
$permitTo = $tmp;
}
$SQL .= " AND view_permitfacts.permitfact_permit_date BETWEEN '{$permitFrom}' AND '{$permitTo}'";
} elseif ($permitFrom !== null) {
// User entered only "From" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_permit_date = '{$permitFrom}'";
} elseif ($permitTo !== null) {
// User entered only "To" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_permit_date = '{$permitTo}'";
}
// ======================
// Permit Date Logic End
// ======================
// ======================
// Entry Date Logic Start
// ======================
$entryFrom = $permitfact_entry_date_from; // null or Y-m-d
$entryTo = $permitfact_entry_date_to; // null or Y-m-d
$subStart = $user_subscription_start_date; // Y-m-d
$subEnd = $user_subscription_end_date; // Y-m-d
// ---- Enforce subscription window for entry dates (only if user provided them) ----
if ($entryFrom !== null) {
if ($entryFrom < $subStart || $entryFrom > $subEnd) {
return;
}
}
if ($entryTo !== null) {
if ($entryTo < $subStart || $entryTo > $subEnd) {
return;
}
}
// ---- Build the entry date filter logic ----
if ($entryFrom !== null && $entryTo !== null) {
// If user reversed the dates, swap them
if ($entryFrom > $entryTo) {
$tmp = $entryFrom;
$entryFrom = $entryTo;
$entryTo = $tmp;
}
$SQL .= " AND view_permitfacts.permitfact_entry_date BETWEEN '{$entryFrom}' AND '{$entryTo}'";
} elseif ($entryFrom !== null) {
// User entered only "From" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_entry_date = '{$entryFrom}'";
} elseif ($entryTo !== null) {
// User entered only "To" => treat as a specific date
$SQL .= " AND view_permitfacts.permitfact_entry_date = '{$entryTo}'";
}
// ====================
// Entry Date Logic End
// ====================
if ($pagination !== 'off') {
$SQL .= " limit :limit
offset :offset";
}
$statement = $connection->prepare($SQL);
if ($permitfact_company !== '') {
$statement->bindValue(":permitfact_company", $permitfact_company, PDO::PARAM_STR);
}
if ($permitfact_project_name !== '') {
$statement->bindValue(":permitfact_project_name", $permitfact_project_name, PDO::PARAM_STR);
}
if ($permitfact_project_zip !== '') {
$statement->bindValue(":permitfact_project_zip", $permitfact_project_zip, PDO::PARAM_STR);
}
$statement->bindValue(":user_subscription_start_date", $user_subscription_start_date, PDO::PARAM_STR);
$statement->bindValue(":user_subscription_end_date", $user_subscription_end_date, PDO::PARAM_STR);
if ($pagination !== 'off') {
$statement->bindValue(":limit", $this->pagination_size, PDO::PARAM_INT);
$statement->bindValue(":offset", $offset, PDO::PARAM_INT);
}
$statement->execute();
$permitfacts = $this->getTableXML("permitfacts", $statement);
// Add searched entry_dates for search history
$permitfacts->record->permitfact_entry_date_from = !empty($entryFrom) ? date('m/d/Y', strtotime($entryFrom)) : date('m/d/Y', strtotime($user_subscription_start_date));
$permitfacts->record->permitfact_entry_date_to = !empty($entryTo) ? date('m/d/Y', strtotime($entryTo)) : date('m/d/Y', strtotime($user_subscription_end_date));
// Insert Pagination Attributes
$permitfacts->addAttribute("count", $count);
$permitfacts->addAttribute("start", $start);
$permitfacts->addAttribute("end", $end);
$permitfacts->addAttribute("page", $this->pagination_page);
$_SESSION[self::PAGINATION_PAGE] = $this->pagination_page;
return $permitfacts;
}
// ==================================
// Users Total Available Record Count
// ==================================
public function userTotalAvailableRecordCount($user_subscription_start_date, $user_subscription_end_date) {
// determine real date range based upon users subscription start date
if ($user_subscription_start_date <= date('Y-m-d', strtotime('-1 year'))) {
$user_subscription_start_date = date('Y-m-d', strtotime('-1 year'));
}
$SQL = "select count(*) as count
from view_permitfacts
where view_permitfacts.permitfact_entry_date between '{$user_subscription_start_date}' and '{$user_subscription_end_date}' ";
$counties = (new Counties())->getSubscriptionCounties($_SESSION['subscription_serial']);
$countyNames = [];
foreach ($counties as $county) {
$countyNames[] = " " . (string) $county->subscriptioncounty_county . " ";
}
$permitfact_county = ' ' . implode(', ', $countyNames) . ' ';
$SQL .= " and view_permitfacts.permitfact_county in ($permitfact_county) ";
return $this->getTable("userstotalavailablerecords", $SQL);
}
// ======================
// Get Users Full Dataset
// ======================
public function getPermitFactsFullDataset($user_subscription_start_date, $user_subscription_end_date) {
set_time_limit(0);
$connection = $this->connect();
$SQL = "select view_permitfacts.*
from view_permitfacts
where view_permitfacts.permitfact_entry_date between :user_subscription_start_date and :user_subscription_end_date";
$statement = $connection->prepare($SQL);
$statement->bindValue(":user_subscription_start_date", $user_subscription_start_date, PDO::PARAM_STR);
$statement->bindValue(":user_subscription_end_date", $user_subscription_end_date, PDO::PARAM_STR);
$statement->execute();
$permitfacts = $this->getTableXML("permitfacts", $statement);
return $permitfacts;
}
}
?>