Files
thetool/application/PreorderIFrame/PreorderIFrameController.php
2025-06-24 15:21:13 +02:00

154 lines
6.2 KiB
PHP

<?php
// in /controllers/PreorderIFrameController.php
class PreorderIFrameController extends mfBaseController
{
private PreorderIFrameModel $preorderIFrameModel;
public function init()
{
// The model is autoloaded or included elsewhere
// 'X-Requested-With': 'XMLHttpRequest', 'X-Frame-Options': 'SAMEORIGIN', 'X-Frame-Referrer': document.referrer
$this->preorderIFrameModel = new PreorderIFrameModel();
}
/**
* Serves the main order form HTML.
* This action injects the necessary configuration into the Vue app.
*/
public function indexAction()
{
$clusterId = $this->request->get('clusterId', 'NULL');
$color = $this->request->get('color', 'blue');
$vue_config = [
'baseUrl' => '/PreorderIFrame', // URL to this controller
'clusterId' => $clusterId !== NULL ? intval($clusterId) : null,
'color' => htmlspecialchars($color),
];
$this->layout()->set("JSGlobals", $vue_config);
$this->layout()->setTemplate("VueViews/PreorderIFrame");
}
// --- API ENDPOINTS ---
public function getClustersAction() {
self::returnJson(['clusters' => $this->preorderIFrameModel->getClusters($_SERVER['HTTP_X_FRAME_REFERRER'])]);
}
public function getClusterInfoAction()
{
$clusterId = $this->request->get('cluster_id');
if (!$clusterId) self::sendError("Cluster ID is required.");
$allClusters = $this->preorderIFrameModel->getClusters($_SERVER['HTTP_X_FRAME_REFERRER']);
if (!$allClusters) self::sendError("No cluster found for the given ID.");
$clusterInfo = null;
foreach ($allClusters as $cluster) {
if ($cluster['id'] == $clusterId) {
$clusterInfo = $cluster;
break;
}
}
$preorderCampaign = new Preordercampaign($clusterInfo['campaign_id']);
self::returnJson(['iframe_consents' => json_decode($preorderCampaign->iframe_consents ?? '[]')]);
}
public function findCityAction()
{
$allowedClusters = $this->preorderIFrameModel->getClusters($_SERVER['HTTP_X_FRAME_REFERRER']);
$zip = $this->request->get('zip');
$clusterId = $this->request->get('cluster_id');
$cities = $this->preorderIFrameModel->findCities($zip, $clusterId);
self::returnJson(['cities' => $cities]);
}
public function findStreetAction()
{
// $this->checkOriginAndGetCampaign(); // Security check
$zip = $this->request->get('zip');
$city = $this->request->get('city');
$clusterId = $this->request->get('cluster_id');
$streets = $this->preorderIFrameModel->findStreets($zip, $city, $clusterId);
self::returnJson(['streets' => $streets]);
}
public function findAddressAction()
{
$addresses = $this->preorderIFrameModel->findAddresses($_GET);
self::returnJson(['addresses' => $addresses]);
}
public function submitOrderAction()
{
$requestBody = file_get_contents('php://input');
$preorderData = json_decode($requestBody, true);
if (json_last_error() !== JSON_ERROR_NONE) self::sendError("Invalid JSON data.");
$tt_network = NetworkModel::getFirst(['adb_network_id' => $preorderData['additionalData']['clusterId']]);
if (!$tt_network) self::sendError("No network found for the given cluster ID.");
$campaign = PreordercampaignModel::getFirst(['network_id' => $tt_network->id]);
if (!$campaign) self::sendError("No campaign found for the given cluster ID.");
$h = new ADBHausnummer($preorderData['address']['hausnummer_id']);
if (!$h->id) self::sendError("Invalid house number ID provided.");
$w = new ADBWohneinheit($preorderData['address']['wohneinheit_id']);
if ($preorderData['address']['wohneinheit_id'] && !$w->id) self::sendError("Invalid unit ID provided.");
$data = [];
$data['preordercampaign_id'] = $campaign->id;
$data['adb_hausnummer_id'] = $preorderData['address']['hausnummer_id'];
$data['adb_wohneinheit_id'] = $preorderData['address']['wohneinheit_id'];
$new_status = null;
if ($data['adb_wohneinheit_id'] && $w->id) {
$status_code = max($w->status->code, $w->hausnummer->status->code);
$new_status = PreorderstatusModel::getFirst(["code" => $status_code]);
} elseif ($data['adb_hausnummer_id'] && $h->id) {
$new_status = PreorderstatusModel::getFirst(["code" => $h->status->code]);
}
$data["status_id"] = $new_status ? $new_status->id : 1;
$data['type'] = $preorderData['connectionType'] === 'vorsorge' ? 'provision' : 'order';
$data['connection_type'] = $preorderData['customerType'] === 'business' ? 'business' : 'single-dwelling';
$data['accept_agb'] = $preorderData['acceptAgb'] ? 1 : 0;
$data['accept_dsgvo'] = $preorderData['acceptDsgvo'] ? 1 : 0;
$data['accept_marketing'] = $preorderData['acceptMarketing'] ? 1 : 0;
$data['accept_withdrawal'] = $preorderData['acceptWithdrawal'] ? 1 : 0;
$data['submit_request'] = json_encode($preorderData);
$data['firstname'] = trim($preorderData['customer']['firstname']);
$data['lastname'] = trim($preorderData['customer']['lastname']);
$data['company'] = (trim($preorderData['customer']['company'])) ?: null;
$data['street'] = (trim($preorderData['customer']['street'])) ?: null;
$data['housenumber'] = (trim($preorderData['customer']['housenumber'])) ?: null;
$data['zip'] = (trim($preorderData['customer']['zip'])) ?: null;
$data['city'] = (trim($preorderData['customer']['city'])) ?: null;
$data['phone'] = (trim($preorderData['customer']['phone'])) ?: null;
$data['email'] = (trim($preorderData['customer']['email'])) ?: null;
$data['edit_by'] = 1;
$data['create_by'] = 1;
$preorder = PreorderModel::create($data);
$preorder->createUcode();
$new_id = $preorder->save();
if (!$new_id) {
self::sendError("Failed to create preorder record.");
}
self::returnJson(['orderCode' => $preorder->ucode, 'status' => 'success']);
}
}