Files
thetool/application/ADBNetzgebiet/ADBNetzgebietController.php

138 lines
5.6 KiB
PHP

<?php
class ADBNetzgebietController extends mfBaseController {
public User $me;
private array $postData = [];
protected function init(): void {
$this->needlogin = true;
$this->me = new User();
$this->me->loadMe();
$this->layout()->set("me", $this->me);
if (!$this->me->is("Admin")) {
$this->redirect("Dashboard");
}
$rawInput = file_get_contents('php://input');
if ($rawInput) $this->postData = json_decode($rawInput, true) ?? [];
}
protected function indexAction(): void {
Helper::renderVue3($this, $this->mod, "Netzgebietverwaltung", [
"GET_URL" => $this::getUrl("ADBNetzgebiet/getNetzgebiete"),
"SAVE_URL" => $this::getUrl("ADBNetzgebiet/save"),
"HISTORY_URL" => $this::getUrl("ADBNetzgebiet/getHistory"),
"NETWORK_URL" => $this::getUrl("Network/Index"),
"NETWORK_CREATE_URL" => $this::getUrl("Network/add"),
"CAMPAIGN_URL" => $this::getUrl("Preordercampaign/edit"),
"CAMPAIGN_CREATE_URL" => $this::getUrl("Preordercampaign/add"),
"CONSENT_URL" => $this::getUrl("ConstructionConsentProject/edit"),
"CONSENT_CREATE_URL" => $this::getUrl("ConstructionConsentProject/add"),
"HIDE_PAGE_TITLE" => true,
"USER_ID" => $this->me->id,
]);
}
protected function getNetzgebieteAction(): void {
$filter = [];
if (!empty($_GET['name'])) $filter['name'] = $_GET['name'];
if (!empty($_GET['extref'])) $filter['extref'] = $_GET['extref'];
if (!empty($_GET['source'])) $filter['=source'] = $_GET['source'];
if (!empty($_GET['source_id'])) $filter['source_id'] = $_GET['source_id'];
$allNetzgebiete = ADBNetzgebiet::getAll($filter, null, 0, ['column' => 'name', 'dir' => 'ASC']);
$response = [];
foreach ($allNetzgebiete as $netzgebiet) {
$response[] = [
'netzgebiet' => $netzgebiet->toArray(),
'related' => [
'networks' => $netzgebiet->relations->networks,
'campaigns' => $netzgebiet->relations->campaigns,
'consent_projects' => $netzgebiet->relations->consentProjects
]
];
}
self::returnJson(['success' => true, 'data' => $response, 'total' => count($response)]);
}
protected function saveAction(): void {
$data = $this->postData;
if (empty($data)) { self::sendError("No data received."); return; }
$isNew = empty($data['id']);
$model = $isNew ? new ADBNetzgebiet() : ADBNetzgebiet::get($data['id']);
if (!$model) { self::sendError("Netzgebiet not found."); return; }
if (isset($data['name'])) $model->name = trim($data['name']) ?: null;
if (array_key_exists('extref', $data)) $model->extref = trim($data['extref']) ?: null;
if (array_key_exists('rimo_id', $data)) $model->rimo_id = trim($data['rimo_id']) ?: null;
if (isset($data['source'])) $model->source = $data['source'] ?: null;
if (array_key_exists('source_id', $data)) $model->source_id = trim($data['source_id']) ?: null;
if (array_key_exists('borderpoly', $data)) $model->borderpoly = $data['borderpoly'] ?: null;
if (isset($data['freigabe'])) {
$model->freigabe = is_array($data['freigabe'])
? json_encode(array_values($data['freigabe']))
: $data['freigabe'];
}
if (isset($data['options'])) {
if (is_array($data['options'])) {
$options = $data['options'];
if (isset($options['mph_min_homes_tool_automatic_count'])) {
$options['mph_min_homes_tool_automatic_count'] = (int)$options['mph_min_homes_tool_automatic_count'];
}
$boolFields = ['create_address_parts', 'update_freigabe', 'update_address',
'hausnummer_dont_overwrite_netzgebiet', 'create_preorder', 'preorder_only_oaid',
'wo_ignore_status', 'delete_units', 'unit_create_oaid'];
foreach ($boolFields as $field) {
if (isset($options[$field])) $options[$field] = $options[$field] ? 1 : 0;
}
$model->options = json_encode($options);
} else {
$model->options = $data['options'];
}
}
if (!$model->save()) { self::sendError("Failed to save Netzgebiet."); return; }
self::returnJson([
'success' => true,
'message' => $isNew ? 'Netzgebiet created.' : 'Netzgebiet saved.',
'id' => $model->getId()
]);
}
protected function getHistoryAction(): void {
$id = $_GET['id'] ?? $this->postData['id'] ?? null;
if (empty($id)) { self::sendError("ID required."); return; }
$model = ADBNetzgebiet::get($id);
if (!$model) { self::sendError("Netzgebiet not found."); return; }
$history = $model->getJournalHistory();
$userIds = array_unique(array_filter(array_column($history, 'user_id')));
$users = [];
foreach ($userIds as $userId) {
$user = new User($userId);
if ($user->id) $users[$user->id] = $user->name ?? 'User #' . $user->id;
}
foreach ($history as $entry) {
$entry->user_name = $users[$entry->user_id] ?? 'System';
}
self::returnJson(['success' => true, 'data' => $history]);
}
// TODO: Implement RIMO API check
protected function checkRimoSourceIdAction(): void {
self::returnJson(['success' => false, 'message' => "RIMO API check not available."]);
}
}