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
+96
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;
}
}
@@ -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");
}
}
}
@@ -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;
}
}
@@ -521,7 +521,7 @@ class PreordercampaignController extends mfBaseController {
$workorders_created++;
}
exit;
$errors = [];
$warnings = [];