428 lines
15 KiB
PHP
428 lines
15 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);
|
|
|
|
if(array_key_exists("project_id", $filter) && $filter["project_id"]) {
|
|
$project = new ConstructionConsentProject($filter["project_id"]);
|
|
if($project->id) {
|
|
$this->layout()->set("project", $project);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
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 viewAction() : void {
|
|
$this->layout()->setTemplate("ConstructionConsent/View");
|
|
|
|
$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);
|
|
}
|
|
|
|
protected function addAction() : void {
|
|
$this->layout()->setTemplate("ConstructionConsent/Form");
|
|
if($this->request->project_id) {
|
|
$this->layout()->set("project_id", $this->request->project_id);
|
|
}
|
|
}
|
|
|
|
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 download() {
|
|
//var_dump($r->get());exit;
|
|
$owner_id = $this->request->owner_id;
|
|
if (!is_numeric($owner_id) || $owner_id < 1) {
|
|
$this->layout()->setFlash("Besitzer nicht gefunden", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$owner = new ConstructionConsentOwner($owner_id);
|
|
if (!$owner->id) {
|
|
$this->layout()->setFlash("Besitzer nicht gefunden", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$cc = $owner->consent;
|
|
if(!$cc) {
|
|
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden.", "error");
|
|
$this->redirect("ConstructionConsent");
|
|
}
|
|
|
|
$this->layout()->setTemplate("ConstructionConsent/Consentform.pdf");
|
|
$this->layout()->set("ressourcePathPrefix", MFFANCYBASEURL);
|
|
$this->layout()->set("owners", [$owner]);
|
|
$this->layout()->set("consent", $cc);
|
|
//return true;
|
|
|
|
$filename = $cc->createConsentFormPdf($owner);
|
|
if(!$filename) {
|
|
$this->layout()->setFlash("Beim Erstellen des PDFs ist ein Fehler aufgetreten. Bitte versuchen Sie es erneut.", "error");
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $cc->id]);
|
|
}
|
|
|
|
header('Content-Type: application/octet-stream');
|
|
header('Content-disposition: attachment; filename="Zustimmungserklärung-'.$cc->id.'-'.$owner_id.'.pdf"');
|
|
header('Content-Transfer-Encoding: binary');
|
|
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
|
|
header('Content-Type: ' . mime_content_type($filename));
|
|
header("Content-Length: " . filesize($filename));
|
|
|
|
readfile($filename);
|
|
exit;
|
|
}
|
|
|
|
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_strasse_id"] = $r->adb_strasse_id;
|
|
$data["ez"] = $r->ez;
|
|
$data["kg"] = $r->kg;
|
|
$data["gst"] = $r->gst;
|
|
$data["gstnr"] = $r->gstnr;
|
|
$data["usage_length"] = $r->usage_length ?: null;
|
|
$data["usage_pipe_on_plot"] = $r->usage_pipe_on_plot ? 1 : 0;
|
|
$data["usage_pipe_in_building"] = $r->usage_pipe_in_building ? 1 : 0;
|
|
$data["usage_manhole"] = $r->usage_manhole ? 1 : 0;
|
|
$data["usage_owner"] = $r->usage_owner ? 1 : 0;
|
|
|
|
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();
|
|
}
|
|
|
|
if(is_array($_FILES) && array_key_exists("consent_plan_image", $_FILES) && !$_FILES['consent_plan_image']['error']) {
|
|
try {
|
|
// returns File object or throws Exception on error
|
|
$file = mfUpload::handleFormUpload("consent_plan_image", false, TT_CONSTRUCTIONCONSENT_FILE_UPLOAD_SUBFOLDER);
|
|
} catch (Exception $ex) {
|
|
$this->layout()->setFlash("Fehler beim Hochladen: " . $ex->getMessage(), "warning");
|
|
return $this->editAction();
|
|
}
|
|
|
|
$ccf = ConstructionConsentFile::create([
|
|
'constructionconsent_id' => $id,
|
|
'file_id' => $file->id,
|
|
'filename' => "zustimmungserklärung-".$item->id."-plan.png",
|
|
]);
|
|
|
|
// delete previous image
|
|
$img = ConstructionConsentFile::getFirst(["constructionconsent_id" => $id]);
|
|
if($img) {
|
|
$img->file->delete();
|
|
$img->delete();
|
|
}
|
|
|
|
if(!$ccf->save()) {
|
|
$this->layout()->setFlash("Fehler beim Hochladen", "warning");
|
|
}
|
|
}
|
|
|
|
$this->layout()->setFlash("Zustimmungserklärung erfolgreich gespeichert", "success");
|
|
$this->redirect("ConstructionConsent", "View", ["id" => $id]);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
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;
|
|
|
|
}
|
|
} |