Files
thetool/application/ADBRimoFcp/ADBRimoFcpController.php

153 lines
6.6 KiB
PHP

<?php
class ADBRimoFcpController extends TTCrud {
protected string $headerTitle = 'Rimo FCPs';
protected string $singleText = 'Rimo FCP';
// @formatter:off
protected array $columns = [
['key' => 'netzgebiet_id', 'text' => 'Netzgebiet', 'required' => true, 'modal' => ['type' => 'select', 'items' => []], 'table' => ['filter' => 'select']],
['key' => 'name', 'text' => 'Name', 'required' => true],
['key' => 'rimo_id', 'text' => 'Rimo ID', 'required' => true],
['key' => 'label', 'text' => 'Label', 'required' => false],
['key' => 'building_type', 'text' => 'Gebäudetyp', 'required' => false],
['key' => 'rimo_ex_state', 'text' => 'Rimo Ex State', 'required' => false],
['key' => 'rimo_op_state', 'text' => 'Rimo Op State', 'required' => false],
['key' => 'gps_lat', 'text' => 'GPS Lat', 'required' => false],
['key' => 'gps_long', 'text' => 'GPS Long', 'required' => false],
['key' => 'create', 'text' => 'Erstellt', 'required' => true, 'modal' => false],
['key' => 'edit', 'text' => 'Bearbeitet', 'required' => true, 'modal' => false, 'table' => false],
['key' => 'actions', 'text' => 'Aktionen', 'required' => false, 'modal' => false, 'table' => ['filter' => false, 'sortable' => false, 'class' => 'text-center', 'priority' => 10]],
];
public function prepareCrudConfig() {
$netzgebiete = array_map(function ($netzgebiet) {
return ['value' => $netzgebiet->id, 'text' => $netzgebiet->name];
}, ADBNetzgebietModel::getAll());
$this->columns[0]['modal']['items'] = $netzgebiete;
}
public function afterInit() {
$this->model = ADBRimoFcp::class;
}
public function ImportFCPsAction() {
$input = json_decode(file_get_contents('php://input'), true);
$fcpList = $input['data'] ?? [];
$networkAreaId = $input['networkAreaId'];
$counts = ['new' => 0, 'upd' => 0];
$now = date('U');
foreach ($fcpList as $fcpIn) {
$rimoId = $fcpIn['ExternalID'] ?? $fcpIn['External ID'] ?? $fcpIn['Externe ID'] ?? null;
if ($rimoId === null) continue;
if (!is_numeric($networkAreaId)) {
$netzgebiet = ADBNetzgebietModel::getFirst(['extref' =>
explode(':', $fcpIn['FCP Cluster'] ?? '')[0] ?? 'DEFINETLY_NOT_A_VALID_EXTREF'
]);
if ($netzgebiet) {
$networkAreaId = $netzgebiet->id;
} else {
continue;
}
}
$data = [
'netzgebiet_id' => $networkAreaId,
'name' => $fcpIn['Name'] ?? $fcpIn['Name/ID'] ?? null,
'rimo_id' => $rimoId,
'label' => $fcpIn['User label'] ?? $fcpIn['Bezeichnung'] ?? null,
'building_type' => $fcpIn['Building type'] ?? $fcpIn['Typ'] ?? null,
'rimo_ex_state' => $fcpIn['Execution state'] ?? $fcpIn['Ausführungsstatus'] ?? null,
'rimo_op_state' => $fcpIn['Operational state'] ?? $fcpIn['Betriebszustand'] ?? null,
'gps_lat' => isset($fcpIn['Latitude']) ? floatval(str_replace(',', '.', $fcpIn['Latitude'] ?? '0')) : null,
'gps_long' => isset($fcpIn['Latitude']) ? floatval(str_replace(',', '.', $fcpIn['Longitude'] ?? '0')) : null,
'edit' => $now
];
$existing = ADBRimoFcp::getAll(['rimo_id' => $rimoId]);
if (count($existing) > 0 && $existing = $existing[0]) {
$data['id'] = $existing->id;
$data['create'] = $existing->create;
ADBRimoFcp::update($data);
$counts['upd']++;
} else {
$data['create'] = $now;
ADBRimoFcp::create($data);
$counts['new']++;
}
}
$msg = sprintf('%d new, %d updated FCPs.', $counts['new'], $counts['upd']);
self::returnJson(['success' => true, 'message' => $msg]);
}
public function ImportLocationsAction() {
$input = json_decode(file_get_contents('php://input'), true);
$fcpsByName = array_column(ADBRimoFcp::getAll(['netzgebiet_id' => $input['networkAreaId']]), null, 'name');
$counts = ['upd' => 0, 'fcpNF' => 0, 'noFCP' => 0, 'noExtId' => 0];
foreach ($input['data'] as $loc) {
if (empty($fcpsByName)) {
$fcpCluster = explode(':', $loc['FCP Cluster'] ?? '');
$network = ADBNetzgebietModel::getFirst(['extref' => $fcpCluster[0] ?? '']);
if ($network) {
$fcpsByName = array_column(ADBRimoFcp::getAll(['netzgebiet_id' => $network->id]), null, 'name');
} else {
$counts['fcpNF']++;
continue;
}
}
$fcpName = trim($loc['FCP cluster name'] ?? $loc['FCP Clustername'] ?? '');
$extId = $loc['ExternalID'] ?? $loc['Externe ID'] ?? null;
if ($extId === null) $extId = $loc['External ID'] ?? $loc['Externe ID'] ?? null;
if ($fcpName === '') { $counts['noFCP']++; continue; }
if (!isset($fcpsByName[$fcpName])) { $counts['fcpNF']++; continue; }
if ($extId === null) { $counts['noExtId']++; continue; }
$fcp = $fcpsByName[$fcpName];
if ($hn = ADBHausnummerModel::getFirst(['rimo_id' => $extId])) {
$hn->fcp_id = $fcp->id;
$hn->rimo_fcp_name = $fcp->name;
$hn->save(["no_aftersave" => true]);
$counts['upd']++;
}
}
$msg = sprintf('Updated: %d, FCP not Found: %d, No FCP in the CSV: %d, No Rimo ID: %d',
$counts['upd'], $counts['fcpNF'], $counts['noFCP'], $counts['noExtId']);
self::returnJson(['success' => true, 'message' => $msg]);
}
public function MapAction() {
Helper::renderVue($this, "ADBRimoFcpMap", "ADBRimoFcpMap", [
"MAPBOX_KEY" => TT_MAPBOX_TILE_API_TOKEN,
]);
}
public function getAllFCPsAction() {
$input = json_decode(file_get_contents('php://input'), true);
$fcpList = ADBRimoFcp::getAll();
$fcpData = array_map(function ($fcp) {
return [
'id' => $fcp->id,
// 'rimo_ex_state' => $fcp->rimo_ex_state,
// 'rimo_op_state' => $fcp->rimo_op_state,
'gps_lat' => $fcp->gps_lat,
'gps_long' => $fcp->gps_long
];
}, $fcpList);
self::returnJson(['success' => true, 'data' => $fcpData]);
}
}