Added status id and flag filter to AddressDB/Index

This commit is contained in:
Frank Schubert
2024-09-25 14:39:38 +02:00
parent 04ff6cf758
commit dbc0336d52
4 changed files with 232 additions and 16 deletions

View File

@@ -167,6 +167,20 @@ class AddressDBController extends mfBaseController {
$new_filter["visibility"] = ["public", "private"];
unset($filter['visibility']);
}
if(array_key_exists("status_id", $filter)) {
if(is_array($filter["status_id"])) {
$new_filter["status_id"] = $filter["status_id"];
}
unset($filter["status_id"]);
}
if(array_key_exists("status_flag", $filter)) {
if(is_array($filter["status_flag"])) {
$new_filter["status_flag"] = $filter["status_flag"];
}
unset($filter["status_flag"]);
}
if(is_array($filter) && count($filter)) {
foreach($filter as $name => $value) {
@@ -544,6 +558,161 @@ class AddressDBController extends mfBaseController {
}
}
protected function statusUpdateimportAction() {
$this->layout()->setTemplate("AddressDB/Statusupdateimport");
}
protected function saveStatusupdateImportAction() {
$headline_included = $this->request->headline;
if(is_array($_FILES) && array_key_exists("statusupdate_csv", $_FILES) && !$_FILES['statusupdate_csv']['error']) {
// look for uploaded import file
try {
// returns File object or throws Exception on error
$file = mfUpload::handleFormUpload("statusupdate_csv");
} catch(Exception $ex) {
$this->layout()->setFlash("Fehler beim Dateiupload: " . $ex->getMessage(), "error");
$this->redirect("Preorder", "statusupdateimport");
}
$i = 0;
$forbidden = 0;
$notfound = 0;
$invalidcode = 0;
$nochange = 0;
$saved = 0;
$flags_saved = 0;
$statusflags = [];
$filename = $file->getFullPath();
$input = fopen($filename, "r");
while($csv = fgetcsv($input, 0, ";")) {
$i++;
if($i == 1 && $headline_included) {
// get statusflag order in header
$col = 2;
while(array_key_exists($col, $csv) && trim($csv[$col])) {
$code = trim($csv[$col]);
if(!is_numeric($code)) {
$this->layout()->setFlash("Ungültige Überschrift für Spalte ".++$col, "error");
$this->redirect("Preorder", "statusupdateimport");
}
$sflag = PreorderStatusflagModel::getFirst(["code" => $code]);
if(!$sflag) {
$this->layout()->setFlash("Statusflag mit Code $code nicht gefunden", "error");
$this->redirect("Preorder", "statusupdateimport");
}
$sflag->col = $col;
$statusflags[$code] = $sflag;
$col++;
}
continue;
}
if(!trim($csv[0])) {
continue;
}
$oaid = trim($csv[0]);
$new_status_code = trim($csv[1]);
$preorder = PreorderModel::getFirstActive(["oaid" => $oaid]);
if(!$preorder) {
$notfound++;
continue;
}
if($preorder->campaign->network->owner_id != $this->me->address_id) {
$forbidden++;
continue;
}
if($preorder->status->code != $new_status_code) {
$new_status = PreorderstatusModel::getFirst(["code" => $new_status_code]);
if(!$new_status_code) {
$invalidcode++;
continue;
}
$preorder->status_id = $new_status->id;
$preorder->save();
$preorder->resetSaveNesting();
$saved++;
} else {
$nochange++;
}
if(count($statusflags)) {
foreach($statusflags as $code => $origin_sflag) {
$this->log->debug(__METHOD__.": $oaid - testing flag $code for update.");
$sflag = clone($origin_sflag);
$sflag->preorder_id = $preorder->id;
if(!$sflag->col) {
$this->layout()->setFlash("Kann Statusflagcode $code nicht zuordnen. Line $i col ".$sflag->col, "error");
$this->redirect("Preorder", "statusupdateimport");
}
if(!array_key_exists($sflag->col, $csv)) {
$this->log->debug(__METHOD__.": no col.");
continue;
}
$value = trim($csv[$sflag->col]);
if(!strlen($value)) {
$this->log->debug(__METHOD__.": no val");
continue;
}
if($value) {
$sflag->value->value = 1;
} else {
$sflag->value->value = 0;
}
$this->log->debug(__METHOD__.": $oaid - saving flag value $code value: ".$sflag->value->value." ($value)");
$sflag->value->save();
$flags_saved++;
//$this->log->debug(__METHOD__.": $oaid - loading preorder again");
}
$preorder = PreorderModel::getFirstActive(["oaid" => $oaid]);
$preorder->resetSaveNesting();
$preorder->afterSave();
$preorder = PreorderModel::getFirstActive(["oaid" => $oaid]);
$preorder->resetSaveNesting();
}
}
$message = "Import erfolgreich. $saved Statusupdates importiert";
if($flags_saved) {
$message .= "<br />$flags_saved Statusflags upgedatet";
}
if($notfound) {
$message .= "<br />$notfound Bestellungen nicht gefunden";
}
if($forbidden) {
$message .= "<br />$forbidden Bestellungen in falschem Netzgebiet";
}
/*if($nochange) {
$message .= "<br />$nochange Bestelllungen haben bereits den neuen Status";
}*/
if($invalidcode) {
$message .= "<br />$invalidcode ungültige Statuscodes";
}
$this->layout()->setFlash($message);
$this->redirect("Preorder", "statusupdateimport");
} else {
$this->layout()->setFlash("Nichts importiert!", "info");
$this->redirect("Preorder", "statusupdateimport");
}
}
protected function apiAction() {
if(!$this->me->is(["Admin","netowner"]) && !$this->me->can("Preorder")) {