Added skip options to mailtemplates in Preordercampaigns

This commit is contained in:
Frank Schubert
2025-02-03 15:01:04 +01:00
parent 5762f01d3f
commit 4f5502f183
5 changed files with 121 additions and 19 deletions

View File

@@ -394,8 +394,9 @@
</div>
<h4 class="mt-3">Emailtemplates</h4>
<?php foreach(PreorderstatusModel::getAll() as $status): ?>
<div class="form-group row" id="statustemplate-<?=$status->code?>">
<?php $i=0; foreach(PreorderstatusModel::getAll() as $status): ?>
<div class="form-group row p-2 mb-0 border-bottom" id="statustemplate-<?=$status->code?>" style="background-color: #<?=($i%2 == 0) ? "fafafa" : "fff"?>">
<label class="col-lg-2 col-form-label text-right text-monospace" for="mailtemplates_<?=$status->code?>"><?=$status->name?></label>
<div class="col-lg-10">
<div class="row">
@@ -404,7 +405,7 @@
<div class="input-group-prepend text-monospace">
<span class="input-group-text" id="basic-addon1"><?=$status->code?></span>
</div>
<select name="mailtemplates[<?=$status->code?>]" id="mailtemplate-<?=$status->code?>" class="form-control select2">
<select name="mailtemplates[<?=$status->code?>][mailtemplate_id]" id="mailtemplate-<?=$status->code?>" 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($status->code, $campaign->statusnotifcation_mailtemplates) && $campaign->statusnotifcation_mailtemplates[$status->code]->mailtemplate_id == $template->id) ? "selected='selected'" : ""?>><?=$template->name?></option>
@@ -424,10 +425,23 @@
</div>
</div>
</div>
<div class="row mt-1">
<div class="col-lg">
<label>
<input type="checkbox" name="mailtemplates[<?=$status->code?>][allow_on_skip]" value="1" id="allow_on_skip-<?=$status->code?>" <?=(is_array($campaign->statusnotifcation_mailtemplates) && array_key_exists($status->code, $campaign->statusnotifcation_mailtemplates) && $campaign->statusnotifcation_mailtemplates[$status->code]->allow_on_skip) ? "checked='checked'" : ""?> />
Benachrichtigung darf bei Überspringen versendet werden, wenn nötig
</label><br />
<label>
<input type="checkbox" name="mailtemplates[<?=$status->code?>][prevent_previous]" value="1" id="prevent_previous-<?=$status->code?>" <?=(is_array($campaign->statusnotifcation_mailtemplates) && array_key_exists($status->code, $campaign->statusnotifcation_mailtemplates) && $campaign->statusnotifcation_mailtemplates[$status->code]->prevent_previous) ? "checked='checked'" : ""?> />
Keine vorigen Benachrichtigungen versenden, wenn dieser Status ohne Mailtemplate eintritt
</label>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<?php $i++; endforeach; ?>
</div>
</div>

View File

@@ -185,8 +185,10 @@ class Preorder extends mfBaseModel {
$trigger = new $classname($this, $new_status);
$trigger->run();
/*
// deprecated, replaced by PreordercampaignStatusnotifictionMailtemplate
//var_dump($status["action"]);
if (array_key_exists("action", $status) && is_array($status)) {
if(is_array($status) && array_key_exists("action", $status)) {
foreach ($status["action"] as $type => $action) {
if (!array_key_exists($type, $actions)) {
$actions[$type] = [];
@@ -194,7 +196,7 @@ class Preorder extends mfBaseModel {
$action["status_code"] = $intermediate_code;
$actions[$type][] = $action;
}
}
}*/
} else {
$this->log->debug(__METHOD__ . ": $classname not found");
}
@@ -215,13 +217,18 @@ class Preorder extends mfBaseModel {
"template" => $mailtemplate->code,
"from" => "campaign",
"data" => "preorder, adb_hausnummer, adb_wohneinheit",
"status_code" => $intermediate_code
"status_code" => $intermediate_code,
"allow_on_skip" => $status_mail->allow_on_skip
];
}
}
//var_dump($actions);exit;
$this->log->debug(print_r($actions, true));
//var_dump($actions);exit;
// run last email action
if(array_key_exists("email", $actions) && is_array($actions["email"]) && count($actions["email"])) {
// get last email action, ignore previous status'
@@ -237,6 +244,23 @@ class Preorder extends mfBaseModel {
}
}
if($new_status->code != $email_action["status_code"]) {
// check if curent status allows sending previous status' notifications
$current_status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $this->preordercampaign_id, "status_code" => $new_status->code]);
if($current_status_mt && $current_status_mt->prevent_previous) {
$send_email = false;
$this->log->debug(__METHOD__.": Sending disallowed by current status");
}
// check if email action allows sending if skipped over it
if(!$email_action["allow_on_skip"]) {
$send_email = false;
$this->log->debug(__METHOD__.": Sending disallowed by previous status");
}
}
if($send_email) {
$this->log->debug(__METHOD__.": Sending Email Action for Status ".$email_action["status_code"]);
$email_to = $this->runTriggerEmailAction($email_action);
@@ -252,7 +276,7 @@ class Preorder extends mfBaseModel {
$psn_log->save();
}
} else {
$this->log->warning(__METHOD__.": Not sending status (".$email_action["status_code"].") email because same or higher status email was sent already (Preorder ".$this->id.")");
$this->log->warning(__METHOD__.": Not sending status (".$email_action["status_code"].") email because disallowed by current status or same or higher status email was sent already (Preorder ".$this->id.")");
}
}
@@ -341,14 +365,20 @@ class Preorder extends mfBaseModel {
$email->setHeader("X-".ucfirst(MFAPPNAME)."-pid", $this->id);
$email->setHeader("X-".ucfirst(MFAPPNAME)."-ps", $this->getProperty("status")->code);
// add attachments
try {
foreach ($mailtemplate->files as $file) {
if (!$file->filename || !$file->file_id || !$file->file->store_filename) continue;
$this->log->debug($file->file->getFullPath());
$email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null);
}
$email->send();
} catch (Exception $e) {
$this->log->warning(__METHOD__.": ".$e->getMessage());
}
$this->log->info(__METHOD__.": Sending StatusTrigger Email to $to");
return $to;
}

View File

@@ -483,21 +483,40 @@ class PreordercampaignController extends mfBaseController {
$status_mailtemplate->delete();
}*/
//var_dump($r->get());exit;
$mailtemplates_delete = [];
// Save Status Email Templates
foreach($r->mailtemplates as $status_code => $mailtemplate_id) {
if(!$mailtemplate_id) {
foreach($r->mailtemplates as $status_code => $status_data) {
$mailtemplate_id = $status_data["mailtemplate_id"];
$allow_on_skip = $status_data["allow_on_skip"];
$prevent_previous = $status_data["prevent_previous"];
/*if($status_code == 920) {
var_dump($status_data);exit;
}*/
if(!$mailtemplate_id && !$allow_on_skip && !$prevent_previous) {
$mailtemplates_delete[] = $status_code;
continue;
}
if($status_code == 920) {
//var_dump($status_data, $allow_on_skip, $prevent_previous);exit;
}
if($mailtemplate_id) {
// check if Mailtemplate exists
$mailtemplate = new Mailtemplate($mailtemplate_id);
if(!$mailtemplate) continue;
if(!$mailtemplate || !$mailtemplate->id) continue;
}
$mt_data = [
"preordercampaign_id" => $campaign->id,
"status_code" => $status_code,
"mailtemplate_id" => $mailtemplate_id,
"mailtemplate_id" => ($mailtemplate_id) ? $mailtemplate->id : null,
"allow_on_skip" => ($allow_on_skip) ? 1 : 0,
"prevent_previous" => ($prevent_previous) ? 1 : 0,
];
$statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "status_code" => $status_code]);

View File

@@ -46,7 +46,7 @@ class PreordercampaignStatusnotificationMailtemplate extends mfBaseModel {
$model = new PreordercampaignStatusnotificationMailtemplate();
$table_fields = [
"preordercampaign_id", "status_code", "mailtemplate_id",
"preordercampaign_id", "status_code", "mailtemplate_id", "allow_on_skip", "prevent_previous",
"result","create_by","edit_by","create","edit"
];

View File

@@ -0,0 +1,39 @@
<?php
declare(strict_types=1);
use Phinx\Db\Adapter\MysqlAdapter;
use Phinx\Migration\AbstractMigration;
final class PreordercampaignStatusnotificationMailtemplateAddSkipActions extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("PreordercampaignStatusnotificationMailtemplate");
$table->addColumn("allow_on_skip", "integer", ["null" => false, "default" => 1, "length" => MySqlAdapter::INT_TINY, "after" => "mailtemplate_id"]);
$table->addColumn("prevent_previous", "integer", ["null" => false, "default" => 0, "length" => MySqlAdapter::INT_TINY, "after" => "allow_on_skip"]);
$table->changeColumn("mailtemplate_id", "integer", ["null" => true, "default" => null]);
$table->update();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("PreordercampaignStatusnotificationMailtemplate")
->removeColumn("allow_on_skip")
->removeColumn("prevent_previous")
->update();
}
if($this->getEnvironment() == "addressdb") {
}
}
}