Files
thetool/application/Api/v1/OperationaldataApicontroller.php
2024-04-05 12:21:54 +02:00

265 lines
9.4 KiB
PHP

<?php
use application\Api\v1\Modules;
class OperationaldataApicontroller extends mfBaseApicontroller
{
//private $filter_gemeinde_ids = [];
//private $campaign;
private $campaign;
private $campaigns = [];
private $filter_salescluster_ids = [];
private $campaigns_by_scluster = [];
private $allowed_preordertypes = [];
private $district_is_city = false;
private $hausnummer_add_zusatz = false;
private $exist_is_error = false;
protected function init()
{
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
}
protected function registerRoutes()
{
/*
* TODO: Load Api Modules automatically in mfBaseApiController
*/
/*$modules = [];
foreach (["Cif", "Activation"] as $moduleName) {
$classname = "application\\Api\\v1\\Modules\\Preorder\\" . $moduleName;
$modules[$moduleName] = new $classname([
"get" => $this->get,
"post" => $this->post,
"db" => $this->db(),
"me" => $this->me
]);
}*/
$this->addRoute("/operationaldata/networks", "getNetworks", "GET");
$this->addRoute("/operationaldata/preordercampaigns", "getPreorderCampaigns", "GET");
$this->addRoute("/operationaldata/buildings", "getBuildingsByCampaign", "GET");
/*$this->addRoute("/preorder/open", "getOpenPreorders", "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$modules["Cif"], "getCifData"], "GET");
$this->addRoute("/preorder/customerInstallationFeedback", [$modules["Cif"], "userSetCif"], "POST");
$this->addRoute("/preorder/:code/clientInstallationFinished", [$modules["Cif"], "providerSetCif"], "POST");
$this->addRoute("/preorder/:code/serviceActivated", [$modules["Activation"], "setServiceActive"], "POST");
$this->addRoute("/preorder/:code", "getPreorder", "GET");
$this->addRoute("/preorder/:code", "cancelPreorder", "DELETE");
*/
}
/*
* is called after User is authenticated by API Key
*/
protected function authenticated()
{
$this->registerRoutes();
if ($this->me->is("preorderaddressreporting")) {
return mfResponse::Forbidden();
}
$campaignApiusers = PreordercampaignApiuserModel::search(["worker_id" => $this->me->id]);
foreach ($campaignApiusers as $campaignApiuser) {
$campaign = new Preordercampaign($campaignApiuser->preordercampaign_id);
if ($campaign->id) {
foreach (PreordercampaignSalesclusterModel::search(['preordercampaign_id' => $campaign->id]) as $campain_scluster) {
if (!in_array($campain_scluster->salescluster_id, $this->filter_salescluster_ids)) {
$this->filter_salescluster_ids[] = $campain_scluster->salescluster_id;
}
$this->campaigns_by_scluster[$campain_scluster->salescluster_id] = $campaign->id;
}
$this->campaigns[$campaign->id] = $campaign;
// get allowed preordertypes
if (is_array($campaign->types) && count($campaign->types)) {
foreach ($campaign->types as $type) {
$this->allowed_preordertypes[] = $type->type;
}
}
if ($campaign->district_is_city == 1) {
$this->district_is_city = true;
}
if ($campaign->hausnummer_add_zusatz == 1) {
$this->hausnummer_add_zusatz = true;
}
if ($campaign->exist_is_error == 1) {
$this->exist_is_error = true;
}
if ($campaign->require_connectiontype == 1) {
$this->require_connectiontype = true;
}
if ($campaign->allow_unit_update == 1) {
$this->allow_unit_update = true;
}
} else {
$this->log->debug(__METHOD__ . ": campaign not found (PreordercampaignApiuser::preordercampaign_id " . $campaignApiuser->preordercampaign_id . ")");
$this->log->debug(print_r($campaignApiuser, true));
}
foreach (PreordercampaignOriginhostnameModel::search(['preordercampaign_id' => $campaign->id]) as $origin) {
$this->addAllowedOrigin($origin->hostname);
}
}
$this->allowed_preordertypes = array_unique($this->allowed_preordertypes);
}
protected function getNetworks() {
$return = [];
$my_network_ids = [];
foreach($this->me->my_networks as $network) {
if($network->id && !in_array($network->id, $my_network_ids)) {
$my_network_ids[] = $network->id;
}
}
$networks = NetworkModel::search(["opsystem" => "snopp"]);
foreach($networks as $network) {
if(!in_array($network->id, $my_network_ids)) continue;
$name = $network->name;
if($network->adb_netzgebiet_id) {
$cityname = preg_replace('/^Liezen\s+-\s+/', '', $network->adb_netzgebiet->name);
$name = "ROC_".$network->adb_netzgebiet->extref."_".$cityname;
}
$net = [];
$net["id"] = $network->id;
$net["name"] = $name;
$net["created"] = $network->create;
$net["updated"] = $network->edit;
$return[] = $net;
}
return mfResponse::Ok(["networks" => $return]);
}
protected function getPreorderCampaigns() {
$return = [];
foreach($this->campaigns as $campaign) {
$c = [];
$c["id"] = $campaign->id;
$c["name"] = $campaign->name;
$c["created"] = $campaign->create;
$c["updated"] = $campaign->edit;
$return[] = $c;
}
return mfResponse::Ok(["preordercampaigns" => $return]);
}
protected function getBuildingsByCampaign() {
$buildings = [];
if(!array_key_exists("campaign_id", $this->get) || !$this->get["campaign_id"]) {
return mfResponse::BadRequest(["message" => "mandatory parameter campaign_id is missing."]);
}
$campaign_id = $this->get["campaign_id"];
if(!is_numeric($campaign_id) || $campaign_id < 1) {
return mfResponse::BadRequest(["message" => "Invalid value for campaign_id"]);
}
$campaign = new Preordercampaign($campaign_id);
if(!$campaign) {
return mfResponse::NotFound(["message" => "Network not found"]);
}
$adb_netzgebiet_id = $campaign->adb_netzgebiet->id;
if(!$adb_netzgebiet_id) {
return mfResponse::NotFound(["message" => "Network not found"]);
}
$db = $this->db(ADDRESSDB_DBHOST, ADDRESSDB_DBUSER, ADDRESSDB_DBPASS, ADDRESSDB_DBNAME);
$sql = AddressDB::$wohneinheit_query;
$sql .= " WHERE Hausnummer.netzgebiet_id=$adb_netzgebiet_id AND Hausnummer.visibility = 'public'";
$res = $db->query($sql);
if(!$db->num_rows($res)) {
return mfResponse::Ok(["buildings" => []]);
}
$b = [];
while($data = $db->fetch_object($res)) {
if(!array_key_exists($data->hausnummer_id, $b)) {
$housenumber = $data->hausnummer;
if($this->hausnummer_add_zusatz) {
if($data->zusatz) {
$housenumber .= " (".$data->zusatz.")";
}
}
$b[$data->hausnummer_id] = [
'id' => $data->hausnummer_id,
'oaid' => $data->hausnummer_oaid,
'cluster_id' => $data->netzgebiet_extref,
'zip' => $data->plz,
'city' => $data->gemeinde,
"municipality" => "",
'district' => $data->ortschaft,
'street' => $data->strasse,
'housenumber' => $housenumber,
'gps_lat' => ($data->gps_lat) ? (float)$data->gps_lat : null,
'gps_long' => ($data->gps_long) ? (float)$data->gps_long : null,
"homes" => []
];
if($this->district_is_city) {
$b[$data->hausnummer_id]['city'] = $data->ortschaft;
$b[$data->hausnummer_id]['municipality'] = $data->gemeinde;
} else {
unset($b[$data->hausnummer_id]['municipality']);
}
}
$b[$data->hausnummer_id]["homes"][] = [
'id' => $data->wohneinheit_id,
'oaid' => $data->wohneinheit_oaid,
'num' => (int)$data->num,
'block' => $data->block,
'stiege' => $data->stiege,
'stock' => $data->stock,
'tuer' => $data->tuer,
'zusatz' => $data->zusatz,
];
}
/*foreach($b as $tmp_b) {
$homes = array_values($tmp_b["homes"]);
return mfResponse::Ok(["Homes" => $homes]);
unset($tmp_b["homes"]);
$tmp_b["homes"] = array_values($homes);
$buildings[] = $tmp_b;
}*/
$buildings = array_values($b);
return mfResponse::Ok(["buildings" => $buildings]);
}
}