From 8eb8893a9e2688288a4144def1fdae3e606572ae Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 26 Nov 2024 22:11:34 +0100 Subject: [PATCH] Preorder Status Notification finished --- application/Mailtemplate/Mailtemplate.php | 1 + application/Preorder/Preorder.php | 139 ++++++++++++++-- application/Preorder/statustrigger/130.php | 28 ++++ application/Preorder/statustrigger/245.php | 22 +-- .../PreorderStatusnotificationLog.php | 157 ++++++++++++++++++ ...create_preorder_statusnotification_log.php | 37 +++++ 6 files changed, 352 insertions(+), 32 deletions(-) create mode 100644 application/Preorder/statustrigger/130.php create mode 100644 application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php create mode 100644 db/migrations/20241126203227_create_preorder_statusnotification_log.php diff --git a/application/Mailtemplate/Mailtemplate.php b/application/Mailtemplate/Mailtemplate.php index 670b5080c..68068181b 100644 --- a/application/Mailtemplate/Mailtemplate.php +++ b/application/Mailtemplate/Mailtemplate.php @@ -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); } diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 55f13386e..bff2b9cf2 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -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; diff --git a/application/Preorder/statustrigger/130.php b/application/Preorder/statustrigger/130.php new file mode 100644 index 000000000..b891cd2d8 --- /dev/null +++ b/application/Preorder/statustrigger/130.php @@ -0,0 +1,28 @@ +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; + } +} \ No newline at end of file diff --git a/application/Preorder/statustrigger/245.php b/application/Preorder/statustrigger/245.php index 7eaef027f..0f9f38f2f 100644 --- a/application/Preorder/statustrigger/245.php +++ b/application/Preorder/statustrigger/245.php @@ -1,11 +1,12 @@ 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(); } diff --git a/application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php b/application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php new file mode 100644 index 000000000..30b189054 --- /dev/null +++ b/application/PreorderStatusnotificationLog/PreorderStatusnotificationLog.php @@ -0,0 +1,157 @@ + $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; + } +} \ No newline at end of file diff --git a/db/migrations/20241126203227_create_preorder_statusnotification_log.php b/db/migrations/20241126203227_create_preorder_statusnotification_log.php new file mode 100644 index 000000000..a845c2d35 --- /dev/null +++ b/db/migrations/20241126203227_create_preorder_statusnotification_log.php @@ -0,0 +1,37 @@ +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") { + + } + } +}