Files
thetool/application/ConstructionConsent/ConstructionConsentController.php
2024-12-18 19:49:18 +01:00

329 lines
12 KiB
PHP

<?php
class ConstructionConsentController extends mfBaseController {
protected function init() : void
{
$this->needlogin = true;
$me = new User();
$me->loadMe();
$this->me = $me;
$this->layout()->set("me", $me);
if (!$me->is(["Admin"])) {
$this->redirect("Dashboard");
}
}
protected function indexAction() : void {
$this->layout()->setTemplate("ConstructionConsent/Index");
if($this->request->resetFilter) {
unset($_SESSION[MFAPPNAME . '-ConstructionConsent-filter']);
foreach($_SESSION as $key => $s) {
if(preg_match('/^' . MFAPPNAME . '-ConstructionConsent-filter-project-\d+$/', $key)) {
unset($_SESSION[$key]);
}
}
}
$filter = [];
if(is_array($this->request->filter)) {
$filter = $this->request->filter;
if(isset($this->request->filter["project_id"]) && $this->request->filter["project_id"]) {
$filter_p_id = $filter["project_id"];
if(count($this->request->filter) == 1 && isset($_SESSION[MFAPPNAME . '-ConstructionConsent-filter-project-' . $filter_p_id])) {
$filter = $_SESSION[MFAPPNAME . '-ConstructionConsent-filter-project-' . $filter_p_id];
} else {
$_SESSION[MFAPPNAME . '-ConstructionConsent-filter-project-' . $filter["project_id"]] = $filter;
}
} else {
$_SESSION[MFAPPNAME . '-ConstructionConsent-filter'] = $filter;
}
} else {
if(array_key_exists(MFAPPNAME . '-ConstructionConsent-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-ConstructionConsent-filter'])) {
$filter = $_SESSION[MFAPPNAME . '-ConstructionConsent-filter'];
if(isset($filter['project_id'])) {
$filter_p_id = $filter['project_id'];
if(isset($_SESSION[MFAPPNAME . '-ConstructionConsent-filter-project-' . $filter_p_id])) {
$filter = $_SESSION[MFAPPNAME . '-ConstructionConsent-filter-project-' . $filter_p_id];
}
}
}
}
//var_dump($_SESSION, $filter);exit;
$this->layout->set("filter", $filter);
$filter = $this->getPreparedFilter($filter);
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 20;
$pagination['maxItems'] = 0;
if (is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
//var_dump($filter);exit;
$pagination['maxItems'] = ConstructionConsent::count($filter);
$this->layout()->set("pagination", $pagination);
$items = ConstructionConsent::search($filter);
$this->layout->set("items", $items);
}
private function getPreparedFilter($filter) : array
{
$new_filter = [];
if (is_array($filter) && count($filter)) {
foreach ($filter as $name => $value) {
$new_filter[$name] = $value;
}
}
return $new_filter;
}
protected function addAction() : void {
$this->layout()->setTemplate("ConstructionConsent/Form");
}
protected function editAction() : void {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
$item = new ConstructionConsent($id);
if(!$item || !$item->id) {
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
$this->layout()->set("item", $item);
$this->addAction();
}
protected function saveAction() {
$r = $this->request;
//var_dump($r->get());exit;
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$item = new ConstructionConsent($id);
if(!$item->id) {
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
} else {
$id = false;
$mode = "add";
}
$data = [];
$data["constructionconsentproject_id"] = $r->constructionconsentproject_id;
$data["object_type"] = $r->object_type;
$data["name"] = $r->name;
$data["adb_street_id"] = $r->adb_street_id;
$data["ez"] = $r->ez;
$data["kg"] = $r->kg;
$data["gst"] = $r->gst;
$data["gstnr"] = $r->gstnr;
$data["usage_length"] = $r->usage_length;
$data["usage_pipe_on_plot"] = $r->usage_pipe_on_plot;
$data["usage_pipe_in_building"] = $r->usage_pipe_in_building;
$data["usage_manhole"] = $r->usage_manhole;
$data["usage_owner"] = $r->usage_owner;
if($mode == "add") {
$item = ConstructionConsent::create($data);
} else {
$item->update($data);
}
$this->layout()->set("item", $item);
$project = new ConstructionConsentProject($data["constructionconsentproject_id"]);
if(!$project->id) {
$this->layout()->setFlash("Projekt nicht gefunden", "error");
return $this->addAction();
}
if(!$data["object_type"] || !$data["adb_strasse_id"]) {
$this->layout()->setFlash("Bitte alle benötigten Felder ausfüllen", "error");
return $this->addAction();
}
if(!$item->save()) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
return $this->addAction();
}
$this->layout()->setFlash("Zustimmungserklärung erfolgreich gespeichert", "success");
$this->redirect("ConstructionConsent");
}
protected function apiAction() {
if(!$this->me->is(["Admin","netowner"]) && !$this->me->can("Preorder")) {
$this->redirect("Dashboard");
}
$do = $this->request->do;
$data = [];
switch($do) {
case "findStreet":
$return = $this->findStreetApi();
break;
default:
$this->log->warn(__METHOD__ . ": Called API function '$do' does not exist");
$return = false;
}
if(!is_array($return) || !count($return)) {
$data = ["status" => "error"];
$this->returnJson($data);
}
$data['status'] = "OK";
$data['result'] = $return;
$this->returnJson($data);
}
private function findStreetApi() {
$addresses = [];
$search = trim($this->request->q);
$project_id = $this->request->project_id;
$scluster_ids = [];
if($project_id) {
$project = new ConstructionConsentProject($project_id);
if(!$project->id) {
header("Content-Type: application/json");
echo json_encode(["results" => []]);
exit;
}
foreach($project->adb_networks as $network) {
$scluster_ids[] = $network->id;
}
} else {
// get all salesclusters
foreach(ADBNetzgebietModel::getAll() as $network) {
$scluster_ids[] = $network->id;
}
}
$results = [];
$search_parts = explode(" ", $search);
$ort_search = $strasse_search = $plz_search = $hausnummer_search = $gst_search = [];
foreach($search_parts as $p) {
$p = $this->db->escape(trim($p));
if(!$p) continue;
$ort_search[] = "ortschaft like '$p%'";
$strasse_search[] = "strasse like '$p%'";
$plz_search[] = "plz like '%$p%'";
$hausnummer_search[] = "hausnummer like '%$p%'";
$gst_search[] = "grund_nr like '%$p%'";
}
$where = "1=1";
if(count($scluster_ids)) {
$where .= " AND netzgebiet_id IN (".implode(', ',$scluster_ids).")";
}
/*if($include_gst) {
$sql = "SELECT * FROM view_hausnummer WHERE $where AND ((".implode(" OR ", $ort_search).") OR (".implode(" OR ", $strasse_search).") OR (".implode(" OR ", $plz_search).") OR (".implode(" OR ", $hausnummer_search).") OR (".implode(" OR ", $gst_search).") ) ORDER BY strasse, LENGTH(hausnummer), hausnummer";
} else {
$sql = "SELECT * FROM view_hausnummer WHERE $where AND ((".implode(" OR ", $ort_search).") OR (".implode(" OR ", $strasse_search).") OR (".implode(" OR ", $plz_search).") OR (".implode(" OR ", $hausnummer_search).")) ORDER BY strasse, LENGTH(hausnummer), hausnummer";
}*/
$sql = "SELECT strasse_id,plz,ortschaft,strasse FROM view_hausnummer
WHERE $where
AND ((".implode(" OR ", $ort_search).") OR (".implode(" OR ", $strasse_search).") OR (".implode(" OR ", $plz_search).") OR (".implode(" OR ", $hausnummer_search)."))
GROUP BY plz,ortschaft,strasse,strasse_id
ORDER BY strasse
";
$this->log->debug($sql);
$adb = FronkDB::singleton(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$res = $adb->query($sql);
$this->log->debug("done");
if(!$adb->num_rows($res)) {
header("Content-Type: application/json");
echo json_encode(["results" => []]);
exit;
}
while($data = $adb->fetch_object($res)) {
//$address_string = $data->plz." ".$data->ortschaft.", ".$data->strasse." ".$data->hausnummer;
$address_string = $data->plz." ".$data->ortschaft.", ".$data->strasse;
/*if($include_gst) {
$address_string .= " | GST: ".$data->grund_nr;
}*/
$sort_key = $data->plz." ".$data->ortschaft." ".$data->strasse;
$address = [];
$address['id'] = $data->strasse_id;
$address["text"] = $address_string;
$address['sort_key'] = $sort_key;
$addresses[] = $address;
}
// sort results by most occurences of search strings
$sort = [];
foreach($addresses as $key => $address) {
$includes_int = false;
$count = 0;
foreach($search_parts as $p) {
$p = $this->db->escape(trim($p));
if(!$p) continue;
if(is_numeric(($p))) {
$includes_int = true;
if(substr_count(strtolower($address['text']), strtolower($p))) {
$count++;
}
} else {
$count += substr_count(strtolower($address['text']), strtolower($p));
}
}
unset($address['sort_key']);
//echo $address['text']." $p $count<br />\n";
if($includes_int && (($count + 1) - count($search_parts) ) < 1) {
continue;
}
if(!array_key_exists($count, $sort)) {
$sort[$count] = [];
}
$sort[$count][] = $address;
}
ksort($sort, SORT_NUMERIC);
$sort = array_reverse($sort, true);
//var_dump($sort);exit;
foreach($sort as $res) {
foreach($res as $a) {
$results[] = $a;
}
}
header("Content-Type: application/json");
echo json_encode(["results" => $results]);
exit;
}
}