Files
thetool/application/OpenAccessId/OpenAccessId.php
2023-08-02 08:43:56 +02:00

96 lines
2.2 KiB
PHP

<?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;
}
}