Files
thetool/application/ConstructionConsent/ConstructionConsentController.php
2025-02-14 16:19:05 +01:00

928 lines
34 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","netowner","salespartner"]) && in_array($me->address_id, [1,209,5908,2187]))) $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, $pagination);
$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;
$id = $r->id;
if(is_numeric($id) && $id > 0) {
//var_dump($id);exit;
$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";
}
//var_dump($r->get());exit;
$data = [];
$data["constructionconsentproject_id"] = $r->constructionconsentproject_id;
$data["object_type"] = $r->object_type;
$data["name"] = $r->name;
$data["adb_hausnummer_id"] = $r->adb_hausnummer_id;
$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($r->object_type == "building") {
$data["usage_length"] = 1;
$data["usage_pipe_on_plot"] = 1;
$data["usage_pipe_in_building"] = 1;
$data["usage_manhole"] = 1;
$data["usage_owner"] = 1;
}
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"]) {
$this->layout()->setFlash("Bitte alle benötigten Felder ausfüllen", "error");
return $this->addAction();
}
if($data["object_type"] == "address") {
if(!$data["adb_hausnummer_id"]) {
$this->layout()->setFlash("Kein Gebäude ausgewählt", "error");
return $this->addAction();
}
unset($data["adb_strasse_id"]);
}
if($data["object_type"] == "street") {
if(!$data["adb_strasse_id"]) {
$this->layout()->setFlash("Keine Straße ausgewählt", "error");
return $this->addAction();
}
unset($data["adb_hausnummer_id"]);
}
//var_dump($data, $item);exit;
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();
}
$history = ConstructionConsentHistory::create([
"constructionconsent_id" => $item->id,
"key" => "plan_upload",
"old_value" => ($item->file) ? $item->file->file->id : null,
"new_value" => $file->id
]);
} else {
if($r->submit_plan_file_id) {
$file = new File($r->submit_plan_file_id);
$history = ConstructionConsentHistory::create([
"constructionconsent_id" => $item->id,
"key" => "rimo_plan",
"old_value" => ($item->file) ? $item->file->file->id : null,
"new_value" => $file->id
]);
}
}
if($file && $file->id) {
$ccf = ConstructionConsentFile::create([
'constructionconsent_id' => $item->id,
'file_id' => $file->id,
'filename' => "zustimmungserklärung-" . $item->id . "-plan.png",
]);
// delete previous image
$img = ConstructionConsentFile::getFirst(["constructionconsent_id" => $item->id]);
if ($img) {
$img->file->delete();
$img->delete();
}
if (!$ccf->save()) {
$this->layout()->setFlash("Fehler beim Speichern des Plans", "warning");
}
$history->save();
}
$this->layout()->setFlash("Zustimmungserklärung erfolgreich gespeichert", "success");
$this->redirect("ConstructionConsent", "View", ["id" => $item->id]);
}
protected function deleteAction() {
$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->id) {
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
$item->delete();
$this->layout()->setFlash("Zustimmungserklärung erfolgreich gelöscht", "success");
$this->redirect("ConstructionConsent");
}
protected function saveDate() {
$r = $this->request;
$id = $r->consent_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 {
$this->layout()->setFlash("Zustimmungserklärung nicht gefunden", "error");
$this->redirect("ConstructionConsent");
}
$data = [];
if($r->inspection_date_planner) {
$data["inspection_date_planner"] = Layout::dateToInt($r->inspection_date_planner);
}
if($r->inspection_date_electrician) {
$data["inspection_date_electrician"] = Layout::dateToInt($r->inspection_date_electrician);
}
if(count($data)) {
$item->update($data);
if(!$item->save()) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
return $this->addAction();
}
}
$this->layout()->setFlash("Zustimmungserklärung erfolgreich gespeichert", "success");
$this->redirect("ConstructionConsent", "View", ["id" => $item->id]);
}
protected function uploadFileAction() {
$id = $this->request->consent_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");
}
$upload_type = $this->request->upload_type;
if($upload_type != "planner" && $upload_type != "electrician") {
$this->layout()->setFlash("Falscher Dateityp", "error");
$this->redirect("ConstructionConsent", "View", ["id" => $item->id]);
}
if(is_array($_FILES) && array_key_exists("inspection_protocol_file", $_FILES) && !$_FILES['inspection_protocol_file']['error']) {
try {
// returns File object or throws Exception on error
$file = mfUpload::handleFormUpload("inspection_protocol_file", false, TT_CONSTRUCTIONCONSENT_FILE_UPLOAD_SUBFOLDER);
} catch (Exception $ex) {
$this->layout()->setFlash("Fehler beim Hochladen: " . $ex->getMessage(), "warning");
$this->redirect("ConstructionConsent", "View", ["id" => $item->id]);
}
$history = ConstructionConsentHistory::create([
"constructionconsent_id" => $item->id,
"key" => "inspection_protocol_$upload_type",
"old_value" => ($item->{"inspection_protocol_$upload_type"}) ? $item->{"inspection_protocol_$upload_type"}->file->id : null,
"new_value" => $file->id
]);
$ccf = ConstructionConsentFile::create([
'constructionconsent_id' => $item->id,
'file_id' => $file->id,
'filename' => "inspection_protocol_$upload_type",
]);
// delete previous image
if($item->{"inspection_protocol_$upload_type"}) {
$item->{"inspection_protocol_$upload_type"}->file->delete();
$item->{"inspection_protocol_$upload_type"}->delete();
}
if (!$ccf->save()) {
$this->layout()->setFlash("Fehler beim Speichern des Begehungsprotokolls", "warning");
}
$history->save();
$this->layout()->setFlash("Begehungsprotokoll erfolgreich hochgeladen", "success");
}
$this->redirect("ConstructionConsent", "View", ["id" => $item->id]);
}
protected function deleteFileAction()
{
$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");
}
$file_id = $this->request->file_id;
$file = ConstructionConsentFile::getFirst(["constructionconsent_id" => $id, "file_id" => $file_id]);
if(!$file) {
$this->layout()->setFlash("Datei nicht gefunden", "error");
$this->redirect("ConstructionConsent", "View", ["id" => $id]);
}
$file->file->delete();
$file->delete();
$this->layout()->setFlash("Datei gelöscht", "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;
case "findAddress":
$return = $this->findAddressApi();
break;
case "getRimoPlanPreview":
$return = $this->getRimoPlanPreviewApi();
break;
case "saveRimoPlanPreview":
$return = $this->saveRimoPlanPreviewApi();
break;
case "saveCheckbox":
$return = $this->saveCheckboxApi();
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;
}
private function findAddressApi() {
$addresses = [];
$search = trim($this->request->q);
$project_id = $this->request->project_id;
$include_gst = $this->request->include_gst ? $this->request->include_gst : false;
$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";
}
$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;
if($include_gst) {
$address_string .= " | GST: ".$data->grund_nr;
}
$sort_key = $data->plz." ".$data->ortschaft." ".$data->strasse;
$address = [];
$address['id'] = $data->hausnummer_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;
}
private function getRimoPlanPreviewApi() {
$adb_hausnummer_id = $this->request->building_id;
$hausnummer = new ADBHausnummer($adb_hausnummer_id);
if(!$hausnummer->id) {
return false;
}
$filename = "consent_plan_map_h{$adb_hausnummer_id}";
/*$bpi_file = PreorderFile::getFirst(["preorder_id" => $this->id, "filename" => $filename]);
if($bpi_file) {
return $bpi_file;
}*/
// get trenches from rimo
$geodataResponse = Rimoapi::getBuildingGeoJson($hausnummer->rimo_id);
$this->log->debug(__METHOD__.": ".print_r($geodataResponse, true));
//return false;
if (is_object($geodataResponse)) {
if (property_exists($geodataResponse, "homeSection")) {
if (property_exists($geodataResponse->homeSection, "features") && is_array($geodataResponse->homeSection->features)) {
foreach ($geodataResponse->homeSection->features as $feature) {
$home_trench = [];
foreach ($feature->geometry->coordinates as $coords) {
$long = $coords[0];
$lat = $coords[1];
$home_trench[] = [$lat, $long];
}
if ($hausnummer->home_trench != $home_trench) {
$hausnummer->home_trench = json_encode($home_trench);
$hausnummer->save();
}
}
}
}
if (property_exists($geodataResponse, "borderPoint")) {
if (property_exists($geodataResponse->borderPoint, "features") && is_array($geodataResponse->borderPoint->features)) {
foreach ($geodataResponse->borderPoint->features as $feature) {
$coords = $feature->geometry->coordinates;
$long = $coords[0];
$lat = $coords[1];
if ($hausnummer->borderpoint_lat != $lat || $hausnummer->borderpoint_long != $long) {
$hausnummer->borderpoint_lat = $lat;
$hausnummer->borderpoint_long = $long;
$hausnummer->save();
}
}
}
}
if (property_exists($geodataResponse, "trenches") && $geodataResponse->trenches->features) {
$trenches = [];
foreach ($geodataResponse->trenches->features as $feature) {
$feature_coords = [];
foreach ($feature->geometry->coordinates as $coords) {
$long = $coords[0];
$lat = $coords[1];
$feature_coords[] = [$lat, $long];
}
$trenches[] = $feature_coords;
}
if (count($trenches)) {
$hausnummer->trenches = json_encode($trenches);
$hausnummer->save();
}
}
}
// get new Borderpoint Image from Mapbox API
$params = [
"pin" => null,
"gps_lat" => $hausnummer->gps_lat,
"gps_long" => $hausnummer->gps_long,
"zoom" => 19,
"size_x" => 640,
"size_y" => 640,
"style" => "satellite-streets-v12",
"paths" => "",
"access_token" => TT_MAPBOX_TILE_API_TOKEN
];
if($hausnummer->trenches) {
$trenches = json_decode($hausnummer->trenches);
$params["paths"] = [
"line_width" => 5,
"line_color" => "ff0000",
"line_opacity" => 1,
"line_fill_color" => "ff0000",
"line_fill_opacity" => 1,
"coords" => $trenches
];
}
$image_content = Mapbox_StaticImageApi::getImageFileContent($params);
if(!$image_content) {
return false;
}
$fs_filename = "$filename.jpg";
if(!file_put_contents(MFUPLOAD_FILE_SAVE_PATH."/".TT_CONSTRUCTIONCONSENT_FILE_UPLOAD_SUBFOLDER."/$fs_filename", $image_content)) {
$this->log->error(__METHOD__.": Error saving Borderpoint Static Map Image File");
return false;
}
$file = FileModel::create([
"name" => "consent_plan_map",
"description" => $adb_hausnummer_id,
"filename" => "$filename.jpg",
"orig_filename" => "$filename.jpg",
"store_filename" => $fs_filename,
"subfolder" => TT_CONSTRUCTIONCONSENT_FILE_UPLOAD_SUBFOLDER,
]);
if(!$file->save()) {
$this->log->error(__METHOD__.": Error saving File Object");
return false;
}
$file->mimetype = $file->getMimetype();
$file->save();
return ["image_mimetype" => $file->mimetype, "image_base64" => base64_encode($image_content), "file_id" => $file->id];
}
private function saveRimoPlanPreviewApi() {
$adb_hausnummer_id = $this->request->building_id;
$consent_id = $this->request->constructionconsent_id;
}
private function saveCheckboxApi() {
$r = $this->request;
$consent_id = $r->id;
$consent = new ConstructionConsent($consent_id);
if(!$consent->id) return false;
$data = [];
if($r->isset("inspection_planner")) {
$data["inspection_planner"] = ($r->inspection_planner > 0) ? date("U") : 0;
}
if($r->isset("inspection_electrician")) {
$data["inspection_electrician"] = ($r->inspection_electrician > 0) ? date("U") : 0;
}
if($r->isset("conduit_installed_building")) {
$data["conduit_installed_building"] = ($r->conduit_installed_building > 0) ? date("U") : 0;
}
if($r->isset("conduit_installed_ftu")) {
$data["conduit_installed_ftu"] = ($r->conduit_installed_ftu > 0) ? date("U") : 0;
}
if($r->isset("inhouse_cabling")) {
$data["inhouse_cabling"] = ($r->inhouse_cabling > 0) ? date("U") : 0;
}
if(count($data)) {
$consent->update($data);
$consent->save();
}
return ["message" => "ConstructionConsent saved successfully"];
}
}