From 7a82b2636b385468e6c8c574f8a31ea1cbfe9076 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Wed, 2 Aug 2023 08:43:56 +0200 Subject: [PATCH] Added OAID management --- Layout/default/OpenAccessId/Importer.php | 112 +++++++++ Layout/default/OpenAccessId/Index.php | 172 ++++++++++++++ application/OpenAccessId/OpenAccessId.php | 96 ++++++++ .../OpenAccessId/OpenAccessIdController.php | 147 ++++++++++++ .../OpenAccessId/OpenAccessIdModel.php | 221 ++++++++++++++++++ .../PreordercampaignController.php | 2 +- 6 files changed, 749 insertions(+), 1 deletion(-) create mode 100644 Layout/default/OpenAccessId/Importer.php create mode 100644 Layout/default/OpenAccessId/Index.php create mode 100644 application/OpenAccessId/OpenAccessId.php create mode 100644 application/OpenAccessId/OpenAccessIdController.php create mode 100644 application/OpenAccessId/OpenAccessIdModel.php diff --git a/Layout/default/OpenAccessId/Importer.php b/Layout/default/OpenAccessId/Importer.php new file mode 100644 index 000000000..44af474e2 --- /dev/null +++ b/Layout/default/OpenAccessId/Importer.php @@ -0,0 +1,112 @@ +getUrl($Mod,"Index"); + $pagination_baseurl_params = ["filter" => $filter]; + $pagination_entity_name = "Open Access IDs"; +?> + + + +
+
+
+
+ +
+

Open Access IDs

+
+
+
+ + +
+
+ +
+
+

Open Access IDs importieren

+ +
" enctype="multipart/form-data"> +
+
+ + +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ + Aktiv = Werden für neue Zuweisungen verwendet +
+
+ +
+ +
+ + Identifiziert das OAID-Set. Kann leer sein. +
+
+ +
+ +
+ + Semikolon-getrennt; OAID in erster Spalte +
+
+ +
+ +
+ +
+ +
+ +
+ +
+ +
+
+ +
+
+
+ + +
+
+ +
+
+ + + diff --git a/Layout/default/OpenAccessId/Index.php b/Layout/default/OpenAccessId/Index.php new file mode 100644 index 000000000..807611282 --- /dev/null +++ b/Layout/default/OpenAccessId/Index.php @@ -0,0 +1,172 @@ +getUrl($Mod,"Index"); + $pagination_baseurl_params = ["filter" => $filter]; + $pagination_entity_name = "Open Access IDs"; +?> + + + +
+
+
+
+ +
+

Open Access IDs

+
+
+
+ + +
+
+ +
+
+

Filter

+ +
"> +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ +
+ + +
+ + + +
+ +
+
+ + ">Filter zurücksetzen +
+ +
+
+ +
+
+ + +
+
+
+
+
+

Open Access IDs

+
+ +
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
idAktivOAIDOAID SetHerkunftBesitzerZugewiesenExportiert
id?>active) ? "" : ""?>oaid?>origin_id?>origin?>owner->getCompanyOrName()?> + termination_id): ?> + termination) && $oaid->termination->building_id) ? $oaid->termination->building->getAddress() : ""?> + termination->name) ? " - ".$oaid->termination->name : ""?> + adb_wohneinheit_id && is_object($oaid->adb_wohneinheit)): ?> + [adb_wohneinheit->hausnummer->strasse->gemeinde->name?>] + adb_wohneinheit->hausnummer->plz->plz?> + adb_wohneinheit->hausnummer->ortschaft->name?>, + adb_wohneinheit->hausnummer->strasse->name?> + adb_wohneinheit->hausnummer->hausnummer?> + adb_wohneinheit) ? " - ".(string)$oaid->adb_wohneinheit : ""?> + + assigned) ? date("d.m.Y H:i",$oaid->assigned) : ""?>exported_to) ? $oaid->exported_to : ""?>exported) ? date("d.m.Y H:i", $oaid->exported) : ""?> + +
+ + + + +
+
+ +
+
+ + + diff --git a/application/OpenAccessId/OpenAccessId.php b/application/OpenAccessId/OpenAccessId.php new file mode 100644 index 000000000..e8bd707dc --- /dev/null +++ b/application/OpenAccessId/OpenAccessId.php @@ -0,0 +1,96 @@ +getFullPath(); + $input = fopen($filename, "r"); + while($csv = fgetcsv($input, 0, ";")) { + $i++; + if($i == 1) continue; + + if(!trim($csv[0])) { + continue; + } + + $name = trim($csv[0]); + + if(!$name) { + $this->log->warning(__FILE__."::importFromCSV(): Name missing"); + continue; + } + + $oaid = OpenAccessIdModel::getFirst(["oaid" => $name]); + if($oaid) continue; + + $oaid = OpenAccessIdModel::create([ + "oaid" => $name, + "active" => $active, + "origin" => $origin, + "origin_id" => ($origin_id) ? $origin_id : null, + "owner_id" => $owner_id + ]); + if(!$oaid->save()) { + $this->log->error(__FILE__."::importFromCSV(): Fehler beim Speichern OAID $name"); + continue; + } + $import_count++; + + } + return $import_count; + + + } catch(Exception $e) { + echo $e->getCode().": ".$e->getMessage();exit; + return false; + } + } + + public function getProperty($name) { + if($this->$name == null) { + + if(!$this->id) { + return null; + } + + if($name == "owner") { + $this->owner = new Address($this->owner_id); + return $this->owner; + } + + if($name == "adb_wohneinheit") { + $this->adb_wohneinheit = new ADBWohneinheit($this->adb_wohneinheit_id); + return $this->adb_wohneinheit; + } + + $classname = ucfirst($name); + $idfield = $name."_id"; + $this->$name = new $classname($this->$idfield); + + if($this->$name->id) { + return $this->$name; + } else { + return null; + } + } + + return $this->$name; + } +} \ No newline at end of file diff --git a/application/OpenAccessId/OpenAccessIdController.php b/application/OpenAccessId/OpenAccessIdController.php new file mode 100644 index 000000000..a0caab2d5 --- /dev/null +++ b/application/OpenAccessId/OpenAccessIdController.php @@ -0,0 +1,147 @@ +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() { + $this->layout()->setTemplate("OpenAccessId/Index"); + + $this->layout->set("filter", $this->request->filter); + if($this->request->filter) { + $filter = $this->getPreparedFilter($this->request->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); + } + + $pagination['maxItems'] = OpenAccessIdModel::count($filter); + $oaids = OpenAccessIdModel::search($filter, $pagination); + $this->layout()->set("pagination", $pagination); + + $this->layout()->set("oaids", $oaids); + + $netowners = AddressModel::search(["addresstype" => ["netowner"]]); + $this->layout()->set("netowners", $netowners); + + } + + private function getPreparedFilter($filter) { + $new_filter = []; + + if(array_key_exists("oaid", $filter) && $filter['oaid']) { + $new_filter["oaid"] = "%".$filter['oaid']."%"; + unset($filter['oaid']); + } + + if(array_key_exists("origin_id", $filter) && $filter['origin_id']) { + $new_filter["origin_id"] = "%".$filter['origin_id']."%"; + unset($filter['origin_id']); + } + + if(array_key_exists("assigned", $filter) && $filter['assigned']) { + if($filter['assigned'] == "yes") { + $new_filter['assigned'] = true; + } elseif($filter['assigned'] == "no") { + $new_filter['assigned'] = false; + } + unset($filter['assigned']); + + } + + foreach($filter as $name => $value) { + $new_filter[$name] = $value; + } + + return $new_filter; + } + + protected function importerAction() { + $this->layout()->setTemplate("OpenAccessId/Importer"); + + $netowners = AddressModel::search(["addresstype" => ["netowner"]]); + $this->layout()->set("netowners", $netowners); + } + + protected function import() { + //var_dump($this->request);exit; + $r = $this->request; + + $origin = false; + + switch($r->origin) { + case "ofaa": + $origin = "ofaa"; + break; + case "thetool": + $origin = "thetool"; + break; + default: + $this->layout()->setFlash("Unbekannte Herkunft!", "error"); + $this->redirect("OpenAccessId", "Importer"); + } + + $origin_id = $r->origin_id; + + $owner_id = $r->owner_id; + if(!$owner_id) { + $this->layout()->setFlash("Besitzer kann nicht leer sein!", "error"); + $this->redirect("OpenAccessId", "Importer"); + } + + if($r->active == 1) { + $active = 1; + } else { + $active = 0; + } + + // check for uploaded file + if(is_array($_FILES) && array_key_exists("oaidcsv", $_FILES) && !$_FILES['oaidcsv']['error']) { + // look for uploaded import file + try { + // returns File object or throws Exception on error + $file = mfUpload::handleFormUpload("oaidcsv"); + } catch (Exception $ex) { + $this->layout()->setFlash("Fehler beim Dateiupload: ".$ex->getMessage(), "error"); + $this->redirect("OpenAccessId", "Importer"); + } + + //var_dump($file);exit; + $oaid = new OpenAccessId(); + $import_count = $oaid->importFromCSV($file, [ + "active" => $active, + "origin" => $origin, + "origin_id" => ($origin_id) ? $origin_id : null, + "owner_id" => $owner_id, + ]); + + $file->delete(); + + $this->layout()->setFlash("$import_count OAIDs erfolgreich importiert!", "success"); + $this->redirect("OpenAccessId"); + + } else { + var_dump($_FILES);exit; + $this->layout()->setFlash("Fehler beim Dateiupload: ".$ex->getMessage(), "error"); + $this->redirect("OpenAccessId", "Importer"); + } + + } +} \ No newline at end of file diff --git a/application/OpenAccessId/OpenAccessIdModel.php b/application/OpenAccessId/OpenAccessIdModel.php new file mode 100644 index 000000000..d8bdc05fb --- /dev/null +++ b/application/OpenAccessId/OpenAccessIdModel.php @@ -0,0 +1,221 @@ + $value) { + if(property_exists(get_called_class(), $field)) { + $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("OpenAccessId", "*", "1=1 ORDER BY id"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new OpenAccessId($data); + } + } + return $items; + + } + + public static function getFirst($filter = null) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + mfLoghandler::singleton()->debug($where); + $res = $db->select("OpenAccessId", "*", "$where ORDER BY id"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new OpenAccessId($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 OpenAccessId + WHERE $where + "; + + $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 * FROM OpenAccessId + WHERE $where + ORDER BY id"; + + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + $res = $db->query($sql); + + //mfLoghandler::singleton()->debug($sql); + + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new OpenAccessId($data); + } + } + return $items; + } + + private static function getSqlFilter($filter) { + $db = FronkDB::singleton(); + $where = "1=1 "; + + if(!is_array($filter)) { + return $where; + } + + if(array_key_exists("owner_id", $filter)) { + $owner_id = $filter['owner_id']; + if(is_numeric($owner_id)) { + $where .= " AND owner_id = $owner_id"; + } + } + + if(array_key_exists("assigned", $filter)) { + $assigned = $filter['assigned']; + if(is_numeric($assigned)) { + $where .= " AND assigned = $assigned"; + } elseif($assigned === true) { + $where .= " AND assigned > 0"; + } elseif($assigned === false || $assigned === null) { + $where .= " AND (assigned = 0 OR assigned IS NULL)"; + } + } + + if(array_key_exists("adb_wohneinheit_id", $filter)) { + $adb_wohneinheit_id = $filter['adb_wohneinheit_id']; + if(is_numeric($adb_wohneinheit_id)) { + $where .= " AND adb_wohneinheit_id = $adb_wohneinheit_id"; + } + } + + if(array_key_exists("termination_id", $filter)) { + $termination_id = $filter['termination_id']; + if(is_numeric($termination_id)) { + $where .= " AND termination_id = $termination_id"; + } + } + + if(array_key_exists("exported", $filter)) { + $exported = $filter['exported']; + if(is_numeric($exported)) { + $where .= " AND exported = $exported"; + } elseif($exported === true) { + $where .= " AND exported > 0"; + } elseif($exported === false || $exported === null) { + $where .= " AND (exported = 0 OR exported IS NULL)"; + } + } + + if(array_key_exists("exported_to", $filter)) { + $exported_to = $db->escape($filter['exported_to']); + if($exported_to) { + $where .= " AND exported_to = '$exported_to'"; + } + } + + if(array_key_exists("oaid", $filter)) { + $oaid = $db->escape($filter['oaid']); + if($oaid) { + $where .= " AND oaid LIKE '$oaid'"; + } + } + + if(array_key_exists("origin", $filter)) { + $origin = $db->escape($filter['origin']); + if($origin) { + $where .= " AND origin = '$origin'"; + } + } + + if(array_key_exists("origin_id", $filter)) { + $origin_id = $db->escape($filter['origin_id']); + if($origin_id) { + $where .= " AND origin_id LIKE '$origin_id'"; + } + } + + if(array_key_exists("address", $filter)) { + $address = $db->escape($filter['address']); + if($address) { + $where .= " AND address LIKE '$address'"; + } + } + + if(array_key_exists("unit_string", $filter)) { + $unit_string = $db->escape($filter['unit_string']); + if($unit_string) { + $where .= " AND unit_string LIKE '$unit_string'"; + } + } + + //var_dump($filter, $where);exit; + return $where; + } + +} \ No newline at end of file diff --git a/application/Preordercampaign/PreordercampaignController.php b/application/Preordercampaign/PreordercampaignController.php index 120c48caa..58aebd4a2 100644 --- a/application/Preordercampaign/PreordercampaignController.php +++ b/application/Preordercampaign/PreordercampaignController.php @@ -521,7 +521,7 @@ class PreordercampaignController extends mfBaseController { $workorders_created++; } - exit; + $errors = []; $warnings = [];