Added OAID management

This commit is contained in:
Frank Schubert
2023-08-02 08:43:56 +02:00
parent 8b545d9e36
commit 7a82b2636b
6 changed files with 749 additions and 1 deletions

View File

@@ -0,0 +1,112 @@
<?php
$pagination_baseurl = $this->getUrl($Mod,"Index");
$pagination_baseurl_params = ["filter" => $filter];
$pagination_entity_name = "Open Access IDs";
?>
<?php include(realpath(dirname(__FILE__)."/../../$mfLayoutPackage")."/header.php"); ?>
<!-- start page title -->
<div class="row">
<div class="col-12">
<div class="page-title-box">
<div class="page-title-right">
<ol class="breadcrumb m-0">
<li class="breadcrumb-item"><a href="<?=self::getUrl("Dashboard")?>"><?=MFAPPNAME_SLUG?></a></li>
<li class="breadcrumb-item active">Open Access IDs</li>
</ol>
</div>
<h4 class="page-title">Open Access IDs</h4>
</div>
</div>
</div>
<!-- end page title -->
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body">
<h4 class="header-title mb-2">Open Access IDs importieren</h4>
<form class="form-horizontal" method="post" action="<?=self::getUrl("OpenAccessId", "import")?>" enctype="multipart/form-data">
<div class="card">
<div class="card-body">
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="origin">Herkunft *</label>
<div class="col-lg-10">
<select class="select2 form-control " name="origin" id="origin_id">
<option value="ofaa">OFAA</option>
<option value="thetool">thetool</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="owner_id">Besitzer *</label>
<div class="col-lg-10">
<select class="select2 form-control " name="owner_id" id="owner_id">
<option></option>
<?php foreach($netowners as $netowner): ?>
<option value="<?=$netowner->id?>" <?=(isset($netowner_id) && $netowner->id == $netowner_id) ? "selected='selected'" : ""?>><?=$netowner->getCompanyOrName()?></option>
<?php endforeach; ?>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="active">Aktiviert *</label>
<div class="col-lg-10">
<select class="select2 form-control " name="active" id="active">
<option value="1">Aktiviert</option>
<option value="0">Deaktiviert</option>
</select>
<small>Aktiv = Werden für neue Zuweisungen verwendet</small>
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="origin_id">OAID-Set Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="origin_id" id="origin_id" value="" />
<small>Identifiziert das OAID-Set. Kann leer sein.</small>
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for="oaidcsv">CSV-Datei *</label>
<div class="col-lg-10">
<input type="file" class="form-control" name="oaidcsv" id="oaidcsv" accept="text/csv" />
<small>Semikolon-getrennt; OAID in erster Spalte</small>
</div>
</div>
<div class="form-group row">
<label class="col-lg-2 col-form-label" for=""></label>
<div class="col-lg-10 form-check pl-4">
<label><input type="checkbox" class="form-check-input" name="csv_header" value="1" /> Erste Zeile enthält Überschrift</label>
</div>
</div>
<div class="form-group row">
<label class="col-lg-2"></label>
<div class="col-lg-10">
<button type="submit" class="btn btn-primary">Importieren</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<?php include(realpath(dirname(__FILE__)."/../../$mfLayoutPackage")."/footer.php"); ?>

View File

@@ -0,0 +1,172 @@
<?php
$pagination_baseurl = $this->getUrl($Mod,"Index");
$pagination_baseurl_params = ["filter" => $filter];
$pagination_entity_name = "Open Access IDs";
?>
<?php include(realpath(dirname(__FILE__)."/../../$mfLayoutPackage")."/header.php"); ?>
<!-- start page title -->
<div class="row">
<div class="col-12">
<div class="page-title-box">
<div class="page-title-right">
<ol class="breadcrumb m-0">
<li class="breadcrumb-item"><a href="<?=self::getUrl("Dashboard")?>"><?=MFAPPNAME_SLUG?></a></li>
<li class="breadcrumb-item active">Open Access IDs</li>
</ol>
</div>
<h4 class="page-title">Open Access IDs</h4>
</div>
</div>
</div>
<!-- end page title -->
<div class="row">
<div class="col-lg-12">
<div class="card">
<div class="card-body mb-3">
<h4 class="header-title mb-3">Filter</h4>
<form method="get" action="<?=self::getUrl("OpenAccessId")?>">
<div class="row">
<div class="col-1">
<label class="form-label" for="filter_origin">Herkunft</label>
<select name="filter[origin]" id="filter_origin" class="form-control">
<option value=""></option>
<option value="ofaa" <?=($filter['origin'] == "ofaa") ? "selected='selected'" : ""?>>OFAA</option>
<option value="thetool" <?=($filter['origin'] == "thetool") ? "selected='selected'" : ""?>>thetool</option>
</select>
</div>
<div class="col-2">
<label class="form-label" for="filter_owner_id">Besitzer</label>
<select name="filter[owner_id]" id="filter_owner_id" class="form-control">
<option value=""></option>
<?php foreach($netowners as $netowner): ?>
<option value="<?=$netowner->id?>" <?=(is_array($filter) && array_key_exists("owner_id", $filter) && $filter['owner_id'] && $netowner->id == $filter['owner_id']) ? "selected='selected'" : ""?>><?=$netowner->getCompanyOrName()?></option>
<?php endforeach; ?>
</select>
</div>
<div class="col-1">
<label class="form-label" for="filter_oaid">OAID</label>
<input type="text" class="form-control" name="filter[oaid]" id="filter_oaid" value="<?=$filter['oaid']?>" />
</div>
<div class="col-1">
<label class="form-label" for="filter_origin_id">OAID Set</label>
<input type="text" class="form-control" name="filter[origin_id]" id="filter_origin_id" value="<?=$filter['origin_id']?>" />
</div>
<div class="col-2">
<label class="form-label" for="filter_assigned">Zugewiesen</label>
<select name="filter[assigned]" id="filter_assigned" class="form-control">
<option value=""></option>
<option value="yes" <?=($filter['assigned'] == "yes") ? "selected='selected'" : ""?>>Zugewiesen</option>
<option value="no" <?=($filter['assigned'] == "no") ? "selected='selected'" : ""?>>Nicht zugewiesen</option>
</select>
</div>
<div class="col-2">
<label class="form-label" for="filter_exported_to">Exportiert an</label>
<select name="filter[exported_to]" id="filter_exported_to" class="form-control">
<option value=""></option>
<option value="rimo" <?=($filter['exported_to'] == "rimo") ? "selected='selected'" : ""?>>RIMO</option>
</select>
</div>
</div>
<div class="row mt-2">
<div class="col">
<button type="submit" class="btn btn-primary">Filter anwenden</button>
<a class="btn btn-secondary" href="<?=self::getUrl("OpenAccessId")?>">Filter zurücksetzen</a>
</div>
<!--<div class="col">
<button class="btn btn-info" type="button" onclick="refreshMap()"><i class="far fa-map"></i> Auf Karte anzeigen</button>
</div>-->
</div>
</form>
</div>
</div>
<div class="card">
<div class="card-body mb-3">
<div class="row">
<div class="col-12">
<div class="float-left">
<h4 class="header-title">Open Access IDs</h4>
</div>
<div class="float-right">
<a class="btn btn-outline-primary mb-2" href="<?=self::getUrl("OpenAccessId", "importer")?>"><i class="fas fa-plus"></i> Open Access IDs importieren</a>
</div>
</div>
</div>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination.php"); ?>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?>
<table class="table table-striped table-hover">
<tr>
<th>id</th>
<th>Aktiv</th>
<th>OAID</th>
<th>OAID Set</th>
<th>Herkunft</th>
<th>Besitzer</th>
<th>Zugewiesen</th>
<th></th>
<th>Exportiert</th>
<th></th>
<th></th>
</tr>
<?php foreach($oaids as $oaid): ?>
<tr>
<td><?=$oaid->id?></td>
<td><?=($oaid->active) ? "<i class='fas fa-check text-success'></i>" : "<i class='fas fa-xmark text-danger'></i>"?></td>
<td><?=$oaid->oaid?></td>
<td><?=$oaid->origin_id?></td>
<td><?=$oaid->origin?></td>
<td><?=$oaid->owner->getCompanyOrName()?></td>
<td>
<?php if($oaid->termination_id): ?>
<?=(is_object($oaid->termination) && $oaid->termination->building_id) ? $oaid->termination->building->getAddress() : ""?>
<?=($oaid->termination->name) ? " - ".$oaid->termination->name : ""?>
<?php elseif($oaid->adb_wohneinheit_id && is_object($oaid->adb_wohneinheit)): ?>
[<?=$oaid->adb_wohneinheit->hausnummer->strasse->gemeinde->name?>]
<?=$oaid->adb_wohneinheit->hausnummer->plz->plz?>
<?=$oaid->adb_wohneinheit->hausnummer->ortschaft->name?>,
<?=$oaid->adb_wohneinheit->hausnummer->strasse->name?>
<?=$oaid->adb_wohneinheit->hausnummer->hausnummer?>
<?=((string)$oaid->adb_wohneinheit) ? " - ".(string)$oaid->adb_wohneinheit : ""?>
<?php endif; ?>
</td>
<td><?=($oaid->assigned) ? date("d.m.Y H:i",$oaid->assigned) : ""?></td>
<td><?=($oaid->exported_to) ? $oaid->exported_to : ""?></td>
<td><?=($oaid->exported) ? date("d.m.Y H:i", $oaid->exported) : ""?></td>
<td style="text-align: left; letter-spacing: 4px; font-size: 1.1em;">
<!--a href="<?=self::getUrl("OpenAccessId", "edit", ["id" => $oaid->id])?>"><i class="far fa-edit" title="OAID Bearbeiten"></i></a-->
</td>
</tr>
<?php endforeach; ?>
</table>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination-summary.php"); ?>
<?php include(realpath(dirname(__FILE__)."/../")."/tpl/pagination.php"); ?>
</div>
</div>
</div>
</div>
<?php include(realpath(dirname(__FILE__)."/../../$mfLayoutPackage")."/footer.php"); ?>

View File

@@ -0,0 +1,96 @@
<?php
class OpenAccessId extends mfBaseModel {
private $owner;
private $adb_wohneinheit;
private $termination;
public function loadRandomUnassigned() {
}
public function importFromCSV(File $file, $attributes = []) {
$active = (int)$attributes['active'];
$origin = $attributes['origin'];
$origin_id = $attributes['origin_id'];
$owner_id = (int)$attributes['owner_id'];
try {
$import_count = 0;
$i = 0;
$filename = $file->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;
}
}

View File

@@ -0,0 +1,147 @@
<?php
class OpenAccessIdController extends mfBaseController {
protected function init() {
$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() {
$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");
}
}
}

View File

@@ -0,0 +1,221 @@
<?php
class OpenAccessIdModel {
public $oaid;
public $active;
public $origin;
public $origin_id;
public $owner_id;
public $assigned;
public $adb_wohneinheit_id;
public $termination_id;
public $exported;
public $exported_to;
public $export_data;
public $address;
public $unit_string;
public $create_by;
public $edit_by;
public $create;
public $edit;
public static function create(Array $data) {
$model = new OpenAccessId();
foreach($data as $field => $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;
}
}

View File

@@ -521,7 +521,7 @@ class PreordercampaignController extends mfBaseController {
$workorders_created++;
}
exit;
$errors = [];
$warnings = [];