Files
thetool/application/ConstructionConsentProject/ConstructionConsentProject.php
2025-06-03 11:24:39 +02:00

471 lines
15 KiB
PHP

<?php
class ConstructionConsentProject extends mfBaseModel {
private $consents;
private $networks;
private $adb_networks;
private $addresses;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "addresses") {
$addresses = ConstructionConsentProjectAddress::search(["constructionconsentproject_id" => $this->id]);
if(!count($addresses)) {
return [];
}
foreach($addresses as $address) {
$this->addresses[$address->address->id] = $address;
}
return $this->addresses;
}
if($name == "consents") {
$consents = ConstructionConsent::search(["constructionconsentproject_id" => $this->id]);
if(!$consents) {
return [];
}
$this->consents = $consents;
return $this->consents;
}
if($name == "networks") {
$networks = ConstructionConsentNetwork::search(["constructionconsentproject_id" => $this->id]);
if(!$networks) {
return [];
}
foreach($networks as $network) {
$this->networks[$network->id] = $network;
}
return $this->networks;
}
if($name == "adb_networks") {
$networks = ConstructionConsentNetwork::search(["constructionconsentproject_id" => $this->id]);
if(!$networks) {
return [];
}
foreach($networks as $network) {
$this->networks[$network->adb_netzgebiet->id] = $network->adb_netzgebiet;
}
return $this->networks;
}
$classname = ucfirst($name);
$idfield = $name."_id";
$this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield);
if(!$this->$name) {
$this->$name = new $classname($this->$idfield);
}
if($this->$name->id) {
mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name);
return $this->$name;
} else {
return null;
}
}
return $this->$name;
}
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new ConstructionConsentProject();
$table_fields = [
"name", "sender_name", "sender_email", "sender_reply_to", "email", "phone", "note",
"create_by","edit_by","create","edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("ConstructionConsentProject", "*", "1 = 1 ORDER BY name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentProject($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentProject.* FROM ConstructionConsentProject
LEFT JOIN ConstructionConsentNetwork ON (ConstructionConsentNetwork.constructionconsentproject_id = ConstructionConsentNetwork.id)
WHERE $where
GROUP BY ConstructionConsentProject.id
ORDER BY name
LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentProject($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) AS cnt FROM ConstructionConsentProject WHERE $where";
$result = $db->query($sql);
if ($result && $db->num_rows($result) > 0) {
$data = $db->fetch_object($result);
return (int)$data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "name ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentProject.* FROM ConstructionConsentProject
LEFT JOIN ConstructionConsentNetwork ON (ConstructionConsentNetwork.constructionconsentproject_id = ConstructionConsentNetwork.id)
WHERE $where
GROUP BY ConstructionConsentProject.id
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new ConstructionConsentProject($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("name", $filter)) {
$name = FronkDB::singleton()->escape($filter["name"]);
if($name) {
$where .= " AND name='$name'";
}
}
if(array_key_exists("id", $filter)) {
if(is_numeric($filter["id"])) {
$where .= " AND ConstructionConsentProject.id = ".$filter["id"];
} elseif(is_array($filter["id"])) {
$ids = [];
foreach($filter["id"] as $id) {
if(is_numeric($id)) {
$ids[] = $id;
}
}
if(count($ids)) {
$where .= " AND ConstructionConsentProject.id IN (".implode(",", $ids).")";
}
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
public static function hasFaultyOwnerEntries(int $projectId): bool {
if (empty($projectId)) return false;
$db = FronkDB::singleton();
$sql = "SELECT 1
FROM ConstructionConsentOwner cco
JOIN ConstructionConsent cc ON cc.id = cco.constructionconsent_id
WHERE cc.constructionconsentproject_id = $projectId
AND (
(
(
(cco.company IS NOT NULL AND TRIM(cco.company) <> '') OR
(
(cco.firstname IS NOT NULL AND TRIM(cco.firstname) <> '') AND
(cco.lastname IS NOT NULL AND TRIM(cco.lastname) <> '')
)
) OR
(cco.city IS NULL OR TRIM(cco.city) = '')
)
AND
(
/* Country must not contain any digits */
(cco.country REGEXP '[0-9]')
OR
/* For Austria or empty country: ZIP must be exactly 4 digits */
(
(
cco.country IS NULL OR
TRIM(cco.country) = '' OR
LOWER(cco.country) = 'österreich' OR
LOWER(cco.country) = 'austria'
) AND
(cco.zip IS NULL OR cco.zip NOT REGEXP '^[0-9]{4}$')
)
OR
/* street lowercase should not be oesterreich, österreich, austria, germany, deutschland, schweiz, switzerland */
(LOWER(cco.street) REGEXP '^(österreich|austria|germany|deutschland|schweiz|switzerland)$')
OR
/* For Germany: ZIP must be exactly 5 digits */
(
(LOWER(cco.country) = 'deutschland' OR LOWER(cco.country) = 'germany') AND
(cco.zip IS NULL OR cco.zip NOT REGEXP '^[0-9]{5}$')
)
OR
/* For other countries: ZIP must not be empty */
(
(
LOWER(cco.country) NOT IN ('österreich', 'austria', 'deutschland', 'germany') AND
cco.country IS NOT NULL AND
TRIM(cco.country) <> ''
) AND
(cco.zip IS NULL OR TRIM(cco.zip) = '')
)
)
)
LIMIT 1";
$res = $db->query($sql);
return ($res->num_rows > 0);
}
public static function getFaultyOwnerEntries($projectId = null) {
$faultyEntries = [];
$db = FronkDB::singleton();
$whereClause = "";
if (!empty($projectId)) {
$projectId = (int)$projectId;
$whereClause = "WHERE cc.constructionconsentproject_id = $projectId";
}
$sql = "
SELECT
cco.id as owner_id,
cco.firstname,
cco.lastname,
cco.company,
cco.title,
cco.street,
cco.zip,
cco.city,
cco.country,
cco.email,
cco.phone,
cco.status,
cco.result,
cc.id as consent_id,
cc.name as building_name,
cc.object_type,
ccp.id as project_id,
ccp.name as project_name,
cco.create,
cco.edit
FROM
ConstructionConsentOwner cco
JOIN
ConstructionConsent cc ON cc.id = cco.constructionconsent_id
JOIN
ConstructionConsentProject ccp ON ccp.id = cc.constructionconsentproject_id
$whereClause
AND (
(
(cco.company IS NOT NULL AND TRIM(cco.company) <> '') OR
(
(cco.firstname IS NOT NULL AND TRIM(cco.firstname) <> '') AND
(cco.lastname IS NOT NULL AND TRIM(cco.lastname) <> '')
)
) OR
(cco.city IS NULL OR TRIM(cco.city) = '')
)
AND (
/* Country must not contain any digits */
(cco.country REGEXP '[0-9]')
OR
/* For Austria or empty country: ZIP must be exactly 4 digits */
(
(
cco.country IS NULL OR
TRIM(cco.country) = '' OR
LOWER(cco.country) = 'österreich' OR
LOWER(cco.country) = 'austria'
) AND
(cco.zip IS NULL OR cco.zip NOT REGEXP '^[0-9]{4}$')
)
OR
/* street lowercase should not be oesterreich, österreich, austria, germany, deutschland, schweiz, switzerland */
(LOWER(cco.street) REGEXP '^(österreich|austria|germany|deutschland|schweiz|switzerland)$')
OR
/* For Germany: ZIP must be exactly 5 digits */
(
(LOWER(cco.country) = 'deutschland' OR LOWER(cco.country) = 'germany') AND
(cco.zip IS NULL OR cco.zip NOT REGEXP '^[0-9]{5}$')
)
OR
/* For other countries: ZIP must not be empty */
(
(
LOWER(cco.country) NOT IN ('österreich', 'austria', 'deutschland', 'germany') AND
cco.country IS NOT NULL AND
TRIM(cco.country) <> ''
) AND
(cco.zip IS NULL OR TRIM(cco.zip) = '')
)
)
ORDER BY
ccp.name, cc.name, cco.lastname, cco.firstname
";
$res = $db->query($sql);
while($data = $res->fetch_assoc()) {
$errors = [];
// Check if company is empty, OR if both firstname and lastname are empty
if (empty(trim($data['company']))) {
if (empty(trim($data['firstname']))) {
$errors[] = 'firstname';
}
if (empty(trim($data['lastname']))) {
$errors[] = 'lastname';
}
}
// Existing city check
if (empty(trim($data['city']))) {
$errors[] = 'city';
}
// Check country for numeric characters
if (isset($data['country']) && preg_match('/[0-9]/', $data['country'])) {
$errors[] = 'country';
}
// Check street for specific lowercase values
$streetLower = strtolower(trim($data['street'] ?? ''));
var_dump($streetLower);
if (in_array($streetLower, ['österreich', 'Österreich', 'austria', 'germany', 'deutschland', 'schweiz', 'switzerland'])) {
$errors[] = 'street';
}
// Check ZIP based on country
$country = strtolower($data['country'] ?? '');
// Austria or empty country check - must have exactly 4 digits
if ($country === '' || $country === 'österreich' || $country === 'austria') {
if (!isset($data['zip']) || !preg_match('/^[0-9]{4}$/', $data['zip'])) {
$errors[] = 'zip';
}
}
// Germany check - must have exactly 5 digits
else if ($country === 'deutschland' || $country === 'germany') {
if (!isset($data['zip']) || !preg_match('/^[0-9]{5}$/', $data['zip'])) {
$errors[] = 'zip';
}
}
// Other countries - ZIP must not be empty
else if (empty(trim($data['zip']))) {
$errors[] = 'zip';
}
$faultyEntries[] = [
'owner_id' => $data['owner_id'],
'consent_id' => $data['consent_id'],
'project_id' => $data['project_id'],
'project_name' => $data['project_name'],
'building_name' => $data['building_name'],
'object_type' => $data['object_type'],
'title' => $data['title'],
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'company' => $data['company'],
'street' => $data['street'],
'zip' => $data['zip'],
'city' => $data['city'],
'country' => $data['country'],
'email' => $data['email'],
'phone' => $data['phone'],
'status' => $data['status'],
'result' => $data['result'],
'create' => $data['create'],
'edit' => $data['edit'],
'errors' => $errors
];
}
return $faultyEntries;
}
}