added new custom preorder notification
This commit is contained in:
@@ -502,6 +502,43 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<div class="form-group row p-2 mb-0 border-bottom" id="statustemplate-300-custom-new-order" style="background-color: #<?=($i%2 == 0) ? "fafafa" : "fff"?>">
|
||||||
|
|
||||||
|
<label class="col-lg-2 col-form-label text-right text-monospace" for="mailtemplates_300-custom-new-order">Neue Bestellung mit Wohneinheit Status <300</label>
|
||||||
|
<div class="col-lg-10">
|
||||||
|
<div class="row">
|
||||||
|
<div class="col-8">
|
||||||
|
<div class="input-group">
|
||||||
|
<div class="input-group-prepend text-monospace">
|
||||||
|
<span class="input-group-text" id="basic-addon1">Neu Custom</span>
|
||||||
|
</div>
|
||||||
|
<select name="mailtemplates[300-custom-new-order][mailtemplate_id]" id="mailtemplate-300-custom-new-order" class="form-control select2">
|
||||||
|
<option value=""></option>
|
||||||
|
<?php foreach(MailtemplateModel::search(["is_include" => "0"]) as $template): ?>
|
||||||
|
<option value="<?=$template->id?>" <?=(is_array($campaign->statusnotifcation_mailtemplates) && array_key_exists('300-custom-new-order', $campaign->statusnotifcation_mailtemplates) && $campaign->statusnotifcation_mailtemplates['300-custom-new-order']->mailtemplate_id == $template->id) ? "selected='selected'" : ""?>><?=$template->name?></option>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col">
|
||||||
|
<div class="input-group">
|
||||||
|
<div class="input-group-prepend text-monospace">
|
||||||
|
<span class="input-group-text">@</span>
|
||||||
|
</div>
|
||||||
|
<input type="text" class="form-control" name="test_to" id="test-to-300-custom-new-order"
|
||||||
|
value="" placeholder="Test E-Mail (example@test.tld)" />
|
||||||
|
<div class="input-group-append">
|
||||||
|
<button class="btn btn-outline-success" type="button" onclick="sendTestStatusEmail('300-custom-new-order')"><i class="fas fa-envelope"></i> Testmail versenden</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
|
||||||
<!-- END CUSTOM LOGICAL MAIL TEMPLATES -->
|
<!-- END CUSTOM LOGICAL MAIL TEMPLATES -->
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -517,7 +517,6 @@ class PreordercampaignController extends mfBaseController {
|
|||||||
if($statusmailtemplate) {
|
if($statusmailtemplate) {
|
||||||
$statusmailtemplate->update($mt_data);
|
$statusmailtemplate->update($mt_data);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
$statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::create($mt_data);
|
$statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::create($mt_data);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -528,12 +527,12 @@ class PreordercampaignController extends mfBaseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
foreach($mailtemplates_delete as $mt_code) {
|
foreach($mailtemplates_delete as $mt_code) {
|
||||||
$status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "status_code" => $mt_code]);
|
$status_mt = null;
|
||||||
if($status_mt) {
|
if(preg_match("/^\d+$/", $mt_code)) $status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "status_code" => $mt_code]);
|
||||||
$status_mt->delete();
|
else $status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "logical_config" => json_encode(["type" => $mt_code])]);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if($status_mt) $status_mt->delete();
|
||||||
|
}
|
||||||
|
|
||||||
$this->layout()->setFlash("Vorbestellkampagne erfolgreich gespeichert.", "success");
|
$this->layout()->setFlash("Vorbestellkampagne erfolgreich gespeichert.", "success");
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
require("../../../config/config.php");
|
||||||
|
require_once("init.php");
|
||||||
|
|
||||||
|
define('FRONKDB_SQLDEBUG',false);
|
||||||
|
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
|
||||||
|
|
||||||
|
require_once(LIBDIR . "/mvcfronk/mfRouter/mfRouter.php");
|
||||||
|
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseModel.php");
|
||||||
|
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseController.php");
|
||||||
|
|
||||||
|
$me = new User(1);
|
||||||
|
define("INTERNAL_USER_ID", $me->id);
|
||||||
|
define("INTERNAL_USER_USERNAME", $me->username);
|
||||||
|
define("MFBASE_BYPASS_LOGIN", true);
|
||||||
|
|
||||||
|
$db = FronkDB::singleton();
|
||||||
|
$db = $db->link;
|
||||||
|
|
||||||
|
function logMessage($message): void {
|
||||||
|
echo "[" . date("Y-m-d H:i:s") . "] " . $message . "\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
function sendPreorderEmail($mailtemplate, $preordercampaign, $preorder, $email_type = "300-custom") {
|
||||||
|
$data = ["preorder" => $preorder->getPropertiesAsArray()];
|
||||||
|
|
||||||
|
foreach(["adb_hausnummer", "adb_wohneinheit"] as $prop) {
|
||||||
|
$obj = $preorder->getProperty($prop);
|
||||||
|
if($obj) {
|
||||||
|
$obj_data = (array) $obj->data;
|
||||||
|
unset($obj->data, $obj->_old_data, $obj->db, $obj->log);
|
||||||
|
$data[$prop] = array_merge((array) $obj, $obj_data);
|
||||||
|
} else {
|
||||||
|
$data[$prop] = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$replacers = [];
|
||||||
|
foreach($data as $values) {
|
||||||
|
foreach($values as $key => $value) {
|
||||||
|
if($key[0] !== "\0") $replacers[$key] = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$subject = $mailtemplate->getVariableReplacedSubject($replacers);
|
||||||
|
$body = "<html><head></head><body>";
|
||||||
|
$body .= $mailtemplate->renderBody($replacers);
|
||||||
|
$body .= "</body></html>\n";
|
||||||
|
|
||||||
|
$body_type = $mailtemplate->body_html ? "html" : "text";
|
||||||
|
|
||||||
|
$email = new Emailnotification();
|
||||||
|
$email->setSubject($subject);
|
||||||
|
if($body_type == "html") {
|
||||||
|
$email->setHtmlBody($body);
|
||||||
|
} else {
|
||||||
|
$email->setBody($body);
|
||||||
|
}
|
||||||
|
$email->setFrom($preordercampaign->from_email, $preordercampaign->from_email_name);
|
||||||
|
$email->setTo($preorder->email);
|
||||||
|
$email->setHeader("X-".ucfirst(MFAPPNAME)."-pid", $preorder->id);
|
||||||
|
$email->setHeader("X-".ucfirst(MFAPPNAME)."-ps", '300-custom');
|
||||||
|
// add attachments
|
||||||
|
try {
|
||||||
|
foreach ($mailtemplate->files as $file) {
|
||||||
|
if (!$file->filename || !$file->file_id || !$file->file->store_filename) continue;
|
||||||
|
logMessage("Adding attachment: " . $file->filename);
|
||||||
|
$email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null);
|
||||||
|
}
|
||||||
|
$email->send();
|
||||||
|
logMessage("Email sent successfully for Preorder ID: $preorder->id, Campaign: $preordercampaign->name, Type: $email_type");
|
||||||
|
$psn_log = PreorderStatusnotificationLog::create([
|
||||||
|
"preorder_id" => $preorder->id,
|
||||||
|
"email_type" => $email_type,
|
||||||
|
"email" => $preorder->email,
|
||||||
|
]);
|
||||||
|
$psn_log->save();
|
||||||
|
} catch (Exception $e) {
|
||||||
|
logMessage("Error sending email for Preorder ID $preorder->id: " . $e->getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+9
-65
@@ -1,22 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
require("../../config/config.php");
|
global $db;
|
||||||
|
require_once 'init.php';
|
||||||
define('FRONKDB_SQLDEBUG',false);
|
|
||||||
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
|
|
||||||
|
|
||||||
function logMessage($message): void {
|
|
||||||
echo "[" . date("Y-m-d H:i:s") . "] " . $message . "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
require_once(LIBDIR."/mvcfronk/mfRouter/mfRouter.php");
|
|
||||||
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseModel.php");
|
|
||||||
require_once(LIBDIR."/mvcfronk/mfBase/mfBaseController.php");
|
|
||||||
|
|
||||||
$me = new User(1);
|
|
||||||
define("INTERNAL_USER_ID", $me->id);
|
|
||||||
define("INTERNAL_USER_USERNAME", $me->username);
|
|
||||||
define("MFBASE_BYPASS_LOGIN", true);
|
|
||||||
|
|
||||||
$sql = "WITH first_status_300_plus AS (
|
$sql = "WITH first_status_300_plus AS (
|
||||||
SELECT
|
SELECT
|
||||||
@@ -54,11 +39,8 @@ WHERE p.type = 'order'
|
|||||||
AND psnl.id IS NULL -- No existing '300-custom' notification
|
AND psnl.id IS NULL -- No existing '300-custom' notification
|
||||||
ORDER BY fs.first_status_300_timestamp ASC;";
|
ORDER BY fs.first_status_300_timestamp ASC;";
|
||||||
|
|
||||||
$db = FronkDB::singleton();
|
|
||||||
$db = $db->link;
|
|
||||||
|
|
||||||
$res = $db->query($sql);
|
$res = $db->query($sql);
|
||||||
// exit if no results
|
|
||||||
if ($res->num_rows == 0) {
|
if ($res->num_rows == 0) {
|
||||||
logMessage("No preorders found for custom 300 notification.");
|
logMessage("No preorders found for custom 300 notification.");
|
||||||
exit;
|
exit;
|
||||||
@@ -68,12 +50,7 @@ while($row = $res->fetch_assoc()) {
|
|||||||
logMessage("Processing Preorder ID: " . $row['id']);
|
logMessage("Processing Preorder ID: " . $row['id']);
|
||||||
$preorder_id = $row['id'];
|
$preorder_id = $row['id'];
|
||||||
$campaign_name = $row['campaign_name'];
|
$campaign_name = $row['campaign_name'];
|
||||||
$current_status_code = $row['current_status_code'];
|
|
||||||
$current_status_name = $row['current_status_name'];
|
|
||||||
$first_reached_300 = $row['first_reached_300_plus'];
|
|
||||||
$days_since_first_300 = $row['days_since_first_300'];
|
|
||||||
$mailtemplate_id = $row['mailtemplate_id'];
|
$mailtemplate_id = $row['mailtemplate_id'];
|
||||||
$logical_config = json_decode($row['logical_config'], true);
|
|
||||||
|
|
||||||
$preorder = new Preorder($preorder_id);
|
$preorder = new Preorder($preorder_id);
|
||||||
if (!$preorder || !$preorder->id) {
|
if (!$preorder || !$preorder->id) {
|
||||||
@@ -95,44 +72,11 @@ while($row = $res->fetch_assoc()) {
|
|||||||
|
|
||||||
logMessage("Preparing to send email for Preorder ID: $preorder_id, Campaign: $campaign_name");
|
logMessage("Preparing to send email for Preorder ID: $preorder_id, Campaign: $campaign_name");
|
||||||
|
|
||||||
|
sendPreorderEmail(
|
||||||
|
$mailtemplate,
|
||||||
|
$preordercampaign,
|
||||||
|
$preorder,
|
||||||
|
"300-custom"
|
||||||
|
);
|
||||||
|
|
||||||
$subject = $mailtemplate->subject;
|
|
||||||
$body = "<html><head></head><body>";
|
|
||||||
$body .= $mailtemplate->renderBody();
|
|
||||||
$body .= "</body></html>\n";
|
|
||||||
|
|
||||||
$body_type = $mailtemplate->body_html ? "html" : "text";
|
|
||||||
|
|
||||||
$email = new Emailnotification();
|
|
||||||
$email->setSubject($subject);
|
|
||||||
if($body_type == "html") {
|
|
||||||
$email->setHtmlBody($body);
|
|
||||||
} else {
|
|
||||||
$email->setBody($body);
|
|
||||||
}
|
|
||||||
$email->setFrom($preordercampaign->from_email, $preordercampaign->from_email_name);
|
|
||||||
$email->setTo($preorder->email);
|
|
||||||
$email->setHeader("X-".ucfirst(MFAPPNAME)."-pid", $preorder->id);
|
|
||||||
$email->setHeader("X-".ucfirst(MFAPPNAME)."-ps", '300-custom');
|
|
||||||
// add attachments
|
|
||||||
try {
|
|
||||||
foreach ($mailtemplate->files as $file) {
|
|
||||||
if (!$file->filename || !$file->file_id || !$file->file->store_filename) continue;
|
|
||||||
logMessage("Adding attachment: " . $file->filename);
|
|
||||||
$email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null);
|
|
||||||
}
|
|
||||||
$email->send();
|
|
||||||
logMessage("Email sent successfully for Preorder ID: $preorder_id");
|
|
||||||
$psn_log = PreorderStatusnotificationLog::create([
|
|
||||||
"preorder_id" => $preorder_id,
|
|
||||||
"email_type" => "300-custom",
|
|
||||||
"email" => $preorder->email,
|
|
||||||
]);
|
|
||||||
$psn_log->save();
|
|
||||||
// Log the notification
|
|
||||||
$log = new PreorderStatusnotificationLog();
|
|
||||||
|
|
||||||
} catch (Exception $e) {
|
|
||||||
logMessage("Error sending email for Preorder ID $preorder_id: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
global $db;
|
||||||
|
require_once 'init.php';
|
||||||
|
|
||||||
|
$sql = "SELECT
|
||||||
|
p.id,
|
||||||
|
p.type,
|
||||||
|
p.preordercampaign_id,
|
||||||
|
p.adb_wohneinheit_id,
|
||||||
|
pc.name as campaign_name,
|
||||||
|
ps_current.code as current_status_code,
|
||||||
|
ps_current.name as current_status_name,
|
||||||
|
w.id as wohneinheit_id,
|
||||||
|
adb_status.code as wohneinheit_status_code,
|
||||||
|
adb_status.name as wohneinheit_status_name,
|
||||||
|
pcsmt.mailtemplate_id,
|
||||||
|
pcsmt.logical_config
|
||||||
|
FROM Preorder p
|
||||||
|
INNER JOIN Preordercampaign pc ON p.preordercampaign_id = pc.id
|
||||||
|
INNER JOIN PreordercampaignStatusnotificationMailtemplate pcsmt ON pc.id = pcsmt.preordercampaign_id
|
||||||
|
INNER JOIN Preorderstatus ps_current ON p.status_id = ps_current.id
|
||||||
|
INNER JOIN addressdb.Wohneinheit w ON p.adb_wohneinheit_id = w.id
|
||||||
|
INNER JOIN addressdb.Status adb_status ON w.status_id = adb_status.id
|
||||||
|
LEFT JOIN PreorderStatusnotificationLog psnl ON p.id = psnl.preorder_id AND psnl.email_type = '300-custom-new-order'
|
||||||
|
WHERE p.type = 'order'
|
||||||
|
AND p.adb_wohneinheit_id IS NOT NULL -- Must have a wohneinheit linked
|
||||||
|
AND adb_status.code = 300 -- Wohneinheit status must be 300
|
||||||
|
AND adb_status.type = 'wohneinheit' -- Ensure we're checking the right status type
|
||||||
|
AND pcsmt.notification_type = 'logical'
|
||||||
|
AND JSON_EXTRACT(pcsmt.logical_config, '$.type') = '300-custom-new-order'
|
||||||
|
AND psnl.id IS NULL -- No existing '300-custom-new-order' notification
|
||||||
|
ORDER BY p.id ASC;";
|
||||||
|
|
||||||
|
$res = $db->query($sql);
|
||||||
|
|
||||||
|
if ($res->num_rows == 0) {
|
||||||
|
logMessage("No preorders found for custom 300-new-order notification.");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
logMessage("Found " . $res->num_rows . " preorders for 300-custom-new-order notification.");
|
||||||
|
|
||||||
|
while($row = $res->fetch_assoc()) {
|
||||||
|
logMessage("Processing Preorder ID: " . $row['id']);
|
||||||
|
$preorder_id = $row['id'];
|
||||||
|
$campaign_name = $row['campaign_name'];
|
||||||
|
$mailtemplate_id = $row['mailtemplate_id'];
|
||||||
|
$wohneinheit_status_code = $row['wohneinheit_status_code'];
|
||||||
|
$wohneinheit_status_name = $row['wohneinheit_status_name'];
|
||||||
|
|
||||||
|
logMessage("Wohneinheit Status: {$wohneinheit_status_code} ({$wohneinheit_status_name})");
|
||||||
|
|
||||||
|
$preorder = new Preorder($preorder_id);
|
||||||
|
if (!$preorder || !$preorder->id) {
|
||||||
|
logMessage("Preorder with ID $preorder_id not found. Skipping.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$preordercampaign = new Preordercampaign($preorder->preordercampaign_id);
|
||||||
|
if (!$preordercampaign || !$preordercampaign->id) {
|
||||||
|
logMessage("Preordercampaign with ID {$preorder->preordercampaign_id} not found. Skipping.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$mailtemplate = new Mailtemplate($mailtemplate_id);
|
||||||
|
if(!$mailtemplate || !$mailtemplate->id) {
|
||||||
|
logMessage("Mailtemplate with ID $mailtemplate_id not found. Skipping.");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
logMessage("Preparing to send email for Preorder ID: $preorder_id, Campaign: $campaign_name");
|
||||||
|
|
||||||
|
sendPreorderEmail(
|
||||||
|
$mailtemplate,
|
||||||
|
$preordercampaign,
|
||||||
|
$preorder,
|
||||||
|
"300-custom-new-order"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
logMessage("Completed processing 300-custom-new-order notifications.");
|
||||||
|
|
||||||
|
?>
|
||||||
Reference in New Issue
Block a user