Merge branch 'fronkdev' into 'master'

Added RTR C10 reporting

See merge request fronk/thetool!817
This commit is contained in:
Frank Schubert
2024-12-17 20:10:24 +00:00
13 changed files with 1816 additions and 18 deletions
+96 -1
View File
@@ -6,9 +6,15 @@ class Admin_RtrReporting {
private $log;
private $flash = [];
private $systemowner_id = 1;
private $energie_bb_id = 209;
private $netowner_is_systemowner = [
1473,
421,
];
private $additional_owner_networks = [
7 // mortantsch
7, // mortantsch
];
private $ort_replace = [
"St.Ruprecht/Raab" => "Sankt Ruprecht an der Raab",
@@ -444,6 +450,95 @@ class Admin_RtrReporting {
}
protected function c10reportAction() {
$data = [];
$systemowner = new Address($this->systemowner_id);
$energie_bb = new Address($this->energie_bb_id);
if(!$energie_bb->id) {
return [
"error" => "Energie BB id nicht gesetzt",
"template" => "Admin/RtrReporting/Index",
"redirect" => "",
"templateVars" => []
];
}
foreach(ContractModel::searchActive([]) as $contract) {
if(!is_array($contract->product->attributes) || !array_key_exists("rtr_tech_code", $contract->product->attributes) || !$contract->product->attributes["rtr_tech_code"]->value) continue;
$prov_id = false;
$prod_code = $contract->product->attributes["rtr_tech_code"]->value;
if(!$prod_code) continue;
if($contract->termination_id) {
$network_id = $contract->termination->building->network_id;
$netowner_id = $contract->termination->building->network->owner_id;
if(in_array($network_id, $this->additional_owner_networks)) {
$netowner_id = $this->systemowner_id;
}
if(in_array($netowner_id, $this->netowner_is_systemowner)) {
$netowner_id = $this->systemowner_id;
}
$netowner = new Address($netowner_id);
if($netowner->id && is_array($netowner->attributes) && array_key_exists("rtrcode", $netowner->attributes) && $netowner->attributes["rtrcode"]->value) {
$prov_id = $netowner->attributes["rtrcode"]->value;
}
if(!$prov_id) {
$this->log->debug(__METHOD__.": keine provider id trotz termination | netowner: $netowner_id ".$netowner->getCompanyOrName());
}
}
$prod_code_start = substr($prod_code, 0, 4);
$prod_code_p = $prod_code_start."1";
$prod_code_b = $prod_code_start."2";
if(!$prov_id) {
if($prod_code_start == "1432") {
// is OAN product
$prov_id = $energie_bb->attributes["rtrcode"]->value;
}
}
if(!$prov_id) {
$prov_id = $systemowner->attributes["rtrcode"]->value;
}
if(!array_key_exists($prov_id, $data)) {
$data[$prov_id] = [];
}
if(!in_array($prod_code_p, $data[$prov_id])) {
$data[$prov_id][] = $prod_code_p;
}
if(!in_array($prod_code_b, $data[$prov_id])) {
$data[$prov_id][] = $prod_code_b;
}
}
//var_dump($data);exit;
$csv_header = "partnernetz_id;code";
$csv = $csv_header."\n";
foreach($data as $prov_id => $prov) {
foreach($prov as $prod_code) {
$csv .= "$prov_id;";
$csv .= "$prod_code";
$csv .= "\n";
}
}
header("Content-type: text/csv; charset=utf-8");
header('Content-disposition: attachment; filename="rtr-C10-report-'.date('Y-m-d_H-i-s').'.csv"');
echo $csv;
exit;
}
/*
* 1042% = radio
* 1431% = fiber
@@ -88,4 +88,160 @@ class ConstructionConsentController extends mfBaseController {
$this->addAction();
}
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;
}
}
@@ -0,0 +1,180 @@
<?php
class ConstructionConsentFile extends mfBaseModel {
private $file;
private $creator;
private $editor;
public function getProperty($name) {
if($this->$name == null) {
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$this->creator = new User($this->create_by);
if($this->creator->id) {
mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator);
}
return $this->creator;
}
if($name == "editor") {
$this->editor = new User($this->edit_by);
return $this->editor;
}
$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 ConstructionConsentFile();
$table_fields = [
"constructionconsent_id", "file_id", "filename",
"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("ConstructionConsentFile", "*", "1 = 1 ORDER BY file_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentFile($data);
}
}
return $items;
}
public static function getFirst($filter = []) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("ConstructionConsentFile", "*", "$where ORDER BY file_id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentFile($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 `ConstructionConsentFile`
WHERE $where
";
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit=false) {
$items = [];
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentFile.* FROM `ConstructionConsentFile`
WHERE $where
ORDER BY file_id
";
mfLoghandler::singleton()->debug($sql);
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'];
}
}
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentFile($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
$db = FronkDB::singleton();
//var_dump($filter);exit;
if(array_key_exists("mailtemplate_id", $filter)) {
$mailtemplate_id = $filter['mailtemplate_id'];
if(is_numeric($mailtemplate_id)) {
$where .= " AND ConstructionConsentFile.`mailtemplate_id` = $mailtemplate_id";
}
}
if(array_key_exists("file_id", $filter)) {
$file_id = $filter['file_id'];
if(is_numeric($file_id)) {
$where .= " AND ConstructionConsentFile.`file_id` = $file_id";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,167 @@
<?php
class ConstructionConsentJournal extends mfBaseModel {
public function getProperty($name) {
if($this->$name == null) {
$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 ConstructionConsentJournal();
$table_fields = [
"constructionconsent_id", "type", "value", "text",
"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("ConstructionConsentJournal", "*", "1 = 1 ORDER BY `create`");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentJournal($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsentJournal
WHERE $where
ORDER BY `create` LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentJournal($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 ConstructionConsentJournal
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "`create` ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsentJournal
WHERE $where
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 ConstructionConsentJournal($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("constructionconsent_id", $filter)) {
$constructionconsent_id = $filter['constructionconsent_id'];
if(is_numeric($constructionconsent_id)) {
$where .= " AND constructionconsent_id=$constructionconsent_id";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,187 @@
<?php
class ConstructionConsentNetwork extends mfBaseModel {
private $adb_netzgebiet;
public function getProperty($name) {
if($this->$name == null) {
if($name == "adb_netzgebiet") {
$netzgebiet = new ADBNetzgebiet($this->adb_netzgebiet_id);
if(!$netzgebiet->id) {
return null;
}
$this->adb_netzgebiet = $netzgebiet;
return $this->adb_netzgebiet;
}
$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 ConstructionConsentNetwork();
$table_fields = [
"constructionconsentproject_id", "adb_netzgebiet_id",
"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("ConstructionConsentNetwork", "*", "1 = 1 ORDER BY adb_netzgebiet_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentNetwork($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentNetwork.* FROM ConstructionConsentNetwork
WHERE $where
ORDER BY adb_netzgebiet_id
LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentNetwork($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 ConstructionConsentNetwork
WHERE $where
ORDER BY adb_netzgebiet_id
";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "adb_netzgebiet_id ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT ConstructionConsentNetwork.* FROM ConstructionConsentNetwork
WHERE $where
GROUP BY ConstructionConsentNetwork.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 ConstructionConsentNetwork($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("constructionconsentproject_id", $filter)) {
$constructionconsentproject_id = $filter["constructionconsentproject_id"];
if(is_numeric($constructionconsentproject_id)) {
$where .= " AND constructionconsentproject_id='$constructionconsentproject_id'";
}
}
if(array_key_exists("adb_netzgebiet_id", $filter)) {
$adb_netzgebiet_id = $filter["adb_netzgebiet_id"];
if(is_numeric($adb_netzgebiet_id)) {
$where .= " AND adb_netzgebiet_id='$adb_netzgebiet_id'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,167 @@
<?php
class ConstructionConsentOwner extends mfBaseModel {
public function getProperty($name) {
if($this->$name == null) {
$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 ConstructionConsentOwner();
$table_fields = [
"constructionconsent_id", "owner_name", "owner_street", "owner_zip", "owner_city", "owner_country", "owner_phone",
"owner_fax", "owner_email", "status", "result", "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("ConstructionConsentOwner", "*", "1 = 1 ORDER BY owner_name");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new ConstructionConsentOwner($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsentOwner
WHERE $where
ORDER BY adb_hausnummer_id LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new ConstructionConsentOwner($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 ConstructionConsentOwner
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "owner_name ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM ConstructionConsentOwner
WHERE $where
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 ConstructionConsentOwner($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("constructionconsent_id", $filter)) {
$constructionconsent_id = $filter['constructionconsent_id'];
if(is_numeric($constructionconsent_id)) {
$where .= " AND constructionconsent_id=$constructionconsent_id";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,208 @@
<?php
class ConstructionConsentProject extends mfBaseModel {
private $consents;
private $networks;
private $adb_networks;
public function getProperty($name) {
if($this->$name == null) {
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
LEFT JOIN ConstructionConsentNetwork ON (ConstructionConsentNetwork.constructionconsentproject_id = ConstructionConsentNetwork.id)
WHERE $where
GROUP BY ConstructionConsentProject.id
";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $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("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}
@@ -0,0 +1,186 @@
<?php
class ConstructionConsentProjectController 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("ConstructionConsentProject/Index");
if ($this->request->resetFilter) {
unset($_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter']);
}
$filter = [];
if (is_array($this->request->filter)) {
$filter = $this->request->filter;
$_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter'] = $filter;
} else {
if (array_key_exists(MFAPPNAME . '-ConstructionConsentProject-filter', $_SESSION) && count($_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter'])) {
$filter = $_SESSION[MFAPPNAME . '-ConstructionConsentProject-filter'];
}
}
$this->layout->set("filter", $filter);
$filter = $this->getPreparedFilter($filter);
// pagination defaults
$pagination = [];
$pagination['start'] = 0;
$pagination['count'] = 25;
$pagination['maxItems'] = 0;
if (is_numeric($this->request->s)) {
$pagination['start'] = intval($this->request->s);
}
//var_dump($filter);exit;
$pagination['maxItems'] = ConstructionConsentProject::count($filter);
$projects = ConstructionConsentProject::getAll();
$this->layout()->set("projects", $projects);
$this->layout()->set("pagination", $pagination);
}
private function getPreparedFilter($filter) {
$new_filter = [];
if (is_array($filter) && count($filter)) {
foreach ($filter as $name => $value) {
$new_filter[$name] = $value;
}
}
return $new_filter;
}
protected function addAction() {
$this->layout()->setTemplate("ConstructionConsentProject/Form");
}
protected function editAction() {
$id = $this->request->id;
if(!$id || $id < 1) {
$this->layout()->setFlash("Projekt nicht gefunden", "error");
$this->redirect("ConstructionConsentProject");
}
$project = new ConstructionConsentProject($id);
if(!$project->id) {
$this->layout()->setFlash("Projekt nicht gefunden", "error");
$this->redirect("ConstructionConsentProject");
}
$this->layout()->set("project", $project);
return $this->addAction();
}
protected function saveAction() {
if(!$this->me->is("Admin")) {
$this->redirect("ConstructionConsent");
}
$r = $this->request;
//var_dump($r->get());exit;
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$project = new ConstructionConsentProject($id);
if(!$project->id) {
$this->layout()->setFlash("Zustimmungserklärungsprojekt nicht gefunden", "error");
$this->redirect("ConstructionConsentProject");
}
} else {
$id = false;
$mode = "add";
}
$data = [];
$data["name"] = $r->name;
$data["sender_name"] = $r->sender_name;
$data["sender_email"] = $r->sender_email;
$data["sender_reply_to"] = $r->sender_reply_to;
$data["email"] = $r->email;
$data["phone"] = $r->phone;
$data["note"] = $r->note;
if($mode == "add") {
$project = ConstructionConsentProject::create($data);
} else {
$project->update($data);
}
$this->layout()->set("project", $project);
if(!$r->name || !$r->sender_name || !$r->sender_email || !$r->email || !$r->phone) {
$this->layout()->setFlash("Bitte alle erforderlichen Felder ausfüllen", "error");
return $this->addAction();
}
if(!is_array($r->adb_netzgebiet_id) || !count($r->adb_netzgebiet_id)) {
$this->layout()->setFlash("Bitte mindestens ein Netzgebiet auswählen", "error");
return $this->addAction();
}
$netzgebiete = [];
if(!$project->save()) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
return $this->addAction();
}
foreach($r->adb_netzgebiet_id as $netzgebiet_id) {
$netzgebiet = new ADBNetzgebiet($netzgebiet_id);
if(!$netzgebiet->id) continue;
$netzgebiete[] = $netzgebiet_id;
}
/*
if($mode == "add") {
$project = ConstructionConsentProject::create($data);
} else {
$project->update($data);
}*/
//save networks
foreach($netzgebiete as $netzgebiet_id) {
$ccn = ConstructionConsentNetwork::getFirst(["constructionconsentproject_id" => $project->id, "adb_netzgebiet_id" => $netzgebiet_id]);
if(!$ccn) {
$ccn = ConstructionConsentNetwork::create([
"constructionconsentproject_id" => $project->id,
"adb_netzgebiet_id" => $netzgebiet_id
]);
$ccn->save();
}
}
foreach(ConstructionConsentNetwork::search(["constructionconsentproject_id" => $project->id]) as $ccn) {
if(!in_array($ccn->adb_netzgebiet_id, $netzgebiete)) {
$ccn->delete();
}
}
$this->layout()->setFlash("Zustimmungserklärungsprojekt erfolgreich gespeichert", "success");
$this->redirect("ConstructionConsentProject");
}
}