Preorder Status Notification finished

This commit is contained in:
Frank Schubert
2024-11-26 22:11:34 +01:00
parent 9a3eca761e
commit 8eb8893a9e
6 changed files with 352 additions and 32 deletions

View File

@@ -19,6 +19,7 @@ class Mailtemplate extends mfBaseModel {
if(!is_array($replaceVars)) return $text;
foreach($replaceVars as $key => $replacement) {
$key = strtoupper($key);
$text = str_replace("{{".$key."}}", $replacement, $text);
}

View File

@@ -182,38 +182,153 @@ class Preorder extends mfBaseModel {
$trigger = new $classname($this, $new_status);
$trigger->run();
//var_dump($status["action"]);
if(array_key_exists("action", $status) && is_array($status) && property_exists($trigger, "run_action") && $trigger->run_action) {
foreach($status["action"] as $type => $action) {
if(!array_key_exists($type, $actions)) {
$actions[$type] = [];
}
$actions[$type][] = $trigger;
$action["status_code"] = $intermediate_code;
$actions[$type][] = $action;
}
}
}
// run last email action
if(array_key_exists("email", $actions) && is_array($actions["email"]) && count($actions["email"])) {
// get last email action, ignore previous status'
// we only want to send emails for the highest new status
$email_action = array_pop($actions["email"]);
// check if this or a higher-status email was sent already
$send_email = true;
foreach(PreorderStatusnotificationLog::search(["preorder_id" => $this->id]) as $log) {
if($log->status_code >= $email_action["status_code"]) {
$send_email = false;
break;
}
}
// run last email action
if(array_key_exists("email", $actions) && is_array($actions["email"]) && count($actions["email"])) {
$email_action = array_pop($actions["email"]);
$this->runTriggerEmailAction($email_action);
if($send_email) {
$email_to = $this->runTriggerEmailAction($email_action);
if($email_to === false) {
$this->log->warning(__METHOD__ . ": Could not send preorder action email (Preorder " . $this->id . ")");
} elseif(is_string($email_to)) {
// TODO Save history
$psn_log = PreorderStatusnotificationLog::create([
"preorder_id" => $this->id,
"status_code" => $email_action["status_code"],
"email" => $email_to
]);
$psn_log->save();
}
} else {
$this->log->warning(__METHOD__.": Not sending status email because same or higher status email was sent already (Preorder ".$this->id.")");
}
}
return true;
}
private function runTriggerEmailAction($action) {
public function runTriggerEmailAction($action) {
if(!array_key_exists("template", $action)) return false;
if(!array_key_exists("from", $action)) return false;
//if(!array_key_exists("to", $action)) return false;
//if(!array_key_exists("data", $action)) return false;
$from_name = $this->getProperty("campaign")->from_email_name;
$from_email = $this->getProperty("campaign")->from_email;
if(!$from_email || !$from_name) {
return true;
}
$to = $this->email;
if(!$to) {
$this->log->warning(__METHOD__.": Keine To Adresse (Preorder ".$this->id.")");
return false;
}
$data = [];
if(array_key_exists("data", $action)) {
foreach(preg_split('/ *, */', $action["data"]) as $data_type) {
if($data_type == "preorder") {
$data["preorder"] = $this->getPropertiesAsArray();
}
if($data_type == "adb_hausnummer") {
// TODO Array convertion in jeweilige Klasse auslagern
$hausnummer = $this->getProperty("adb_hausnummer");
if(!$hausnummer) continue;
$hausnummer_data = (array) $this->getProperty("adb_hausnummer")->data;
unset($hausnummer->data, $hausnummer->_old_data, $hausnummer->db, $hausnummer->log);
$data["adb_hausnummer"] = array_merge((array) $hausnummer, $hausnummer_data);
}
if($data_type == "adb_wohneinheit") {
$wohneinheit = $this->getProperty("adb_wohneinheit");
if(!$wohneinheit) continue;
$wohneinheit_data = (array) $this->getProperty("adb_wohneinheit")->data;
unset($wohneinheit->data, $wohneinheit->_old_data, $wohneinheit->db, $wohneinheit->log);
$data["adb_wohneinheit"] = array_merge((array) $wohneinheit, $wohneinheit_data);
}
}
}
$mailtemplate = MailtemplateModel::getFirst(["code" => $action["template"]]);
if(!$mailtemplate) {
$this->log->warning(__METHOD__.": Mailtemplate nicht gefunden: ".$action["template"]." (Preorder ".$this->id.")");
return false;
}
$replacers = [];
foreach($data as $type => $values) {
foreach($values as $key => $value) {
if(substr($key, 0, 1) == "\0") continue;
$replacers[$key] = $value;
}
}
$subject = $mailtemplate->getVariableReplacedSubject($replacers);
$body = $mailtemplate->renderBody($replacers);
$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($from_email, $from_name);
$email->setTo($to);
$email->setHeader("X-".ucfirst(MFAPPNAME)."-pid", $this->id);
$email->setHeader("X-".ucfirst(MFAPPNAME)."-ps", $this->getProperty("status")->code);
// add attachments
foreach($mailtemplate->files as $file) {
if($file->filename && $file->file_id && $file->file->storage_filename);
$this->log->debug($file->file->getFullPath());
$email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null);
}
$email->send();
$this->log->info(__METHOD__.": Sending StatusTrigger Email to $to");
return $to;
}
private function getPropertiesAsArray() {
$preorder = clone $this;
unset($preorder->data, $preorder->_old_data, $preorder->db, $preorder->log);
$status = $this->getProperty("status");
$array = (array) $preorder;
$array = array_merge($array, (array) $this->data);
$return = [];
foreach($array as $key => $value) {
if(substr($key, 0, 1) == "\0") continue;
$return[$key] = $value;
}
return $return;
}
/*
@@ -1069,7 +1184,7 @@ class Preorder extends mfBaseModel {
$this->partner = null;
$this->building = null;
$this->adb_hausnummer = null;
$this->adb_wohneinheit;
$this->adb_wohneinheit = null;
// cleanup Preorder data
$this->create_by = $me->id;

View File

@@ -0,0 +1,28 @@
<?php
class Preorder_Statustrigger_130 {
private $log;
private $preorder;
private $new_status;
public $run_action = true;
public function __construct(Preorder $preorder, Preorderstatus $new_status) {
$this->log = mfLoghandler::singleton();
$this->preorder = $preorder;
$this->new_status = $new_status;
}
public function run() {
$this->log->debug(__METHOD__.": running trigger");
$changes = false;
if($changes) {
$this->preorder->save();
}
return true;
}
}

View File

@@ -1,11 +1,12 @@
<?php
class Preorder_Statustrigger_140 {
class Preorder_Statustrigger_245 {
private $log;
private $preorder;
private $new_status;
public $run_action = true;
public function __construct(Preorder $preorder, Preorderstatus $new_status) {
$this->log = mfLoghandler::singleton();
@@ -18,25 +19,6 @@ class Preorder_Statustrigger_140 {
$changes = false;
if(!$this->preorder->ciftoken) {
$this->preorder->ciftoken = $this->preorder->createCiftoken();
$changes = true;
}
/*if(!$this->preorder->ciftoken) {
$this->log->warning("Error creating ciftoken for preorder ".$this->preorder->id);
return true;
}*/
if(!$this->preorder->cifurl) {
$this->preorder->cifurl = $this->preorder->generateCifUrl();
$changes = true;
}
if(!$this->preorder->cifcableurl) {
$this->preorder->cifcableurl = $this->preorder->generateCifCableUrl();
$changes = true;
}
if($changes) {
$this->preorder->save();
}

View File

@@ -0,0 +1,157 @@
<?php
class PreorderStatusnotificationLog extends mfBaseModel {
/********************************
* Begin static Model functions
*/
public static function create(Array $data) {
$model = new PreorderStatusnotificationLog();
$table_fields = [
"preorder_id", "status_code", "email",
"create_by", "edit_by", "create", "edit"
];
foreach($data as $field => $value) {
if(in_array($field, $table_fields)) {
$model->$field = $value;
}
}
$me = new User();
$me->loadMe();
if($model->create_by === null) {
$model->create_by = $me->id;
}
if($model->edit_by === null) {
$model->edit_by = $me->id;
}
return $model;
}
public static function getAll() {
$items = [];
$db = FronkDB::singleton();
$res = $db->select("PreorderStatusnotificationLog", "*", "1 = 1 ORDER BY preorder_id, status_code");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreorderStatusnotificationLog($data);
}
}
return $items;
}
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderStatusnotificationLog
WHERE $where
ORDER BY adb_hausnummer_id LIMIT 1";
//var_dump($sql);exit;
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreorderStatusnotificationLog($data);
if($item->id) {
return $item;
} else {
return null;
}
}
return null;
}
public static function count($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT COUNT(*) as cnt FROM PreorderStatusnotificationLog
WHERE $where";
//mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
return $data->cnt;
}
return 0;
}
public static function search($filter, $limit = false, $order = false) {
//var_dump($filter);exit;
$items = [];
if(!$order) {
$order = "preorder_id, status_code ASC";
}
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$sql = "SELECT * FROM PreorderStatusnotificationLog
WHERE $where
ORDER BY $order";
if(is_array($limit) && count($limit)) {
if(is_numeric($limit['start']) && is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['start'].", ".$limit['count'];
} elseif(is_numeric($limit['count'])) {
$sql .= " LIMIT ".$limit['count'];
}
}
mfLoghandler::singleton()->debug($sql);
$res = $db->query($sql);
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[$data->id] = new PreorderStatusnotificationLog($data);
}
}
return $items;
}
private static function getSqlFilter($filter) {
$where = "1=1 ";
if(array_key_exists("preorder_id", $filter)) {
$preorder_id = $filter['preorder_id'];
if(is_numeric($preorder_id)) {
$where .= " AND PreorderStatusnotificationLog.preorder_id=$preorder_id";
}
}
if(array_key_exists("status_code", $filter)) {
$status_code = $filter['status_code'];
if(is_numeric($status_code)) {
$where .= " AND PreorderStatusnotificationLog.status_code=$status_code";
}
}
if(array_key_exists("email", $filter)) {
$email = FronkDB::singleton()->escape($filter["email"]);
if($email) {
$where .= " AND PreorderStatusnotificationLog.email='$email'";
}
}
if(array_key_exists("add-where", $filter)) {
$where .= " ".$filter['add-where'];
}
//var_dump($filter, $where);exit;
return $where;
}
}

View File

@@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
use Phinx\Migration\AbstractMigration;
final class CreatePreorderStatusnotificationLog extends AbstractMigration
{
public function up(): void
{
if($this->getEnvironment() == "thetool") {
$table = $this->table("PreorderStatusnotificationLog");
$table->addColumn("preorder_id", "integer", ["null" => false]);
$table->addColumn("status_code", "integer", ["null" => false]);
$table->addColumn("email", "string", ["null" => false, "limit" => 255]);
$table->addColumn("create_by", "integer", ["null" => false]);
$table->addColumn("edit_by", "integer", ["null" => false]);
$table->addColumn("create", "integer", ["null" => false]);
$table->addColumn("edit", "integer", ["null" => false]);
$table->create();
}
if($this->getEnvironment() == "addressdb") {
}
}
public function down(): void
{
if($this->getEnvironment() == "thetool") {
$this->table("PreorderStatusnotificationLog")->drop()->save();
}
if($this->getEnvironment() == "addressdb") {
}
}
}