From 255091212dad936fabc3eeb1b4f36ba9afbbc338 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 14 Jan 2025 20:03:19 +0100 Subject: [PATCH] MailtemplateDispatch Index and Form done --- Layout/default/Mailtemplate/Index.php | 5 + Layout/default/MailtemplateDispatch/Form.php | 320 ++++++++++++++++++ Layout/default/MailtemplateDispatch/Index.php | 110 ++++++ application/Mailtemplate/Mailtemplate.php | 2 +- .../Mailtemplate/MailtemplateController.php | 65 ++++ .../MailtemplateDispatch.php | 264 +++++++++++++++ .../MailtemplateDispatchController.php | 150 ++++++++ ...114141019_create_mailtemplate_dispatch.php | 49 +++ 8 files changed, 964 insertions(+), 1 deletion(-) create mode 100644 Layout/default/MailtemplateDispatch/Form.php create mode 100644 Layout/default/MailtemplateDispatch/Index.php create mode 100644 application/MailtemplateDispatch/MailtemplateDispatch.php create mode 100644 application/MailtemplateDispatch/MailtemplateDispatchController.php create mode 100644 db/migrations/20250114141019_create_mailtemplate_dispatch.php diff --git a/Layout/default/Mailtemplate/Index.php b/Layout/default/Mailtemplate/Index.php index 01d51ff3e..51adcfaa3 100644 --- a/Layout/default/Mailtemplate/Index.php +++ b/Layout/default/Mailtemplate/Index.php @@ -1,3 +1,8 @@ +getUrl($Mod, "Index"); +$pagination_baseurl_params = ["filter" => $filter]; +$pagination_entity_name = "Emailtemplates"; +?> diff --git a/Layout/default/MailtemplateDispatch/Form.php b/Layout/default/MailtemplateDispatch/Form.php new file mode 100644 index 000000000..c7ca010e3 --- /dev/null +++ b/Layout/default/MailtemplateDispatch/Form.php @@ -0,0 +1,320 @@ + + + +
+
+
+
+ +
+

id) ? "Emailaussendung bearbeiten" : "Neue Emailaussendung" ?>

+
+
+
+ + +
+
+ +
+
+ +
" enctype="multipart/form-data"> + +
+
+ + + +

Absender

+ +
+ +
+ + Wird beim Empfänger als Absender angezeigt +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +

Empfänger

+

Die Aussendung wird an die Kontaktemailadresse aller Bestellungen versendet. Wählen Sie folgende Konditionen aus, um die Empfänger zu beschränken:

+ + +
+ +
+ +
+
+ +
+ +
+

Bestellungen

+
+ recipient_filter_array) && $dispatch->recipient_filter_array["order_open"]) ? 'checked="checked"' : ''?> /> + +
+ +

Aktive Contracts

+ + +

Allgemein

+
+ recipient_filter_array) && $dispatch->recipient_filter_array["fibu_account"]) ? 'checked="checked"' : ''?> /> + +
+
+ + recipient_filter_array)) ? implode(", ", $dispatch->recipient_filter_array["zip"]) : ""?>" /> + PLZ' mit Leerzeichen oder Komma getrennt +
+
+
+ +
+ + +

Email

+ +

Wählen Sie das gewünschte Emailtemplate aus. Der Betreff kann für diese Aussendung angepasst werden.

+ +
+ + +
+ +
+ + +
+ +
+ mailtemplate_id): ?> + mailtemplate->body_html): ?> + mailtemplate->renderBody()?> + + mailtemplate->renderBody()))?> + + + Emailvorschau + +
+ + +

Dateianhänge

+
+
+ +
+
+ +
+
+ +
+
+ +

Versand

+
+ +
+ + tosend_date) : ""?>" /> + Emailversand wird zu diesem Zeitpunkt gestartet +
+
+ + +
+ +
+ + + +
+ +
+ +
+
+
+ +
+
+ +
+ +
+ +
+
+ +
+
+ +
+
+ + +
+
+ +
+
+
+ +
+ +
+
+
+
+ +
+ +
+ + +
+
+ + + +
+
+
+ +
+
+ + + + + + \ No newline at end of file diff --git a/Layout/default/MailtemplateDispatch/Index.php b/Layout/default/MailtemplateDispatch/Index.php new file mode 100644 index 000000000..cdd2b171a --- /dev/null +++ b/Layout/default/MailtemplateDispatch/Index.php @@ -0,0 +1,110 @@ +getUrl($Mod, "Index"); +$pagination_baseurl_params = ["filter" => $filter]; +$pagination_entity_name = "Emailversände"; +?> + + + +
+
+
+
+ +
+

Emailversand

+
+
+
+ + +
+
+ +
+
+

Filter

+ +
"> +
+ +
+ + " /> +
+ +
+ + " /> +
+ +
+ +
+
+ + ?resetFilter=1">Filter zurücksetzen +
+
+
+ +
+
+ + +
+
+
+

Emailversand

+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
VersandstatusAbsenderBetreffAnhängeAnzahl EmpfängerGeplanter VersandErstelltBearbeitet
+ send_finish): ?> + + send_lock): ?> + + tosend_date && !$dispatch->send_lock): ?> + + + sender_name?> <sender_email?>>subject?>files)) ? count($dispatch->files) : ""?>recipient_count?>create)?> (editor->name?>)edit)?> (creator->name?>) + $dispatch->id])?>"> + $dispatch->id])?>" class="text-danger" onclick="if(!confirm('Emailversand wirklich löschen?')) return false;" title="Emailversand Löschen"> +
+
+
+ +
+
+ + diff --git a/application/Mailtemplate/Mailtemplate.php b/application/Mailtemplate/Mailtemplate.php index 68068181b..4c6f81bf1 100644 --- a/application/Mailtemplate/Mailtemplate.php +++ b/application/Mailtemplate/Mailtemplate.php @@ -20,7 +20,7 @@ class Mailtemplate extends mfBaseModel { foreach($replaceVars as $key => $replacement) { $key = strtoupper($key); - $text = str_replace("{{".$key."}}", $replacement, $text); + $text = str_ireplace("{{".$key."}}", $replacement, $text); } return $text; diff --git a/application/Mailtemplate/MailtemplateController.php b/application/Mailtemplate/MailtemplateController.php index a4e571295..e796e0085 100644 --- a/application/Mailtemplate/MailtemplateController.php +++ b/application/Mailtemplate/MailtemplateController.php @@ -284,4 +284,69 @@ class MailtemplateController extends mfBaseController { } + + protected function apiAction() { + if(!$this->me->is(["Admin"])) { + $this->redirect("Dashboard"); + } + $do = $this->request->do; + $data = []; + + switch($do) { + case "getMailtemplate": + $return = $this->getMailtemplateApi(); + break; + default: + $return = false; + } + + if(!is_array($return) || !count($return)) { + $data = ["status" => "error"]; + $this->returnJson($data); + } + $data['status'] = "OK"; + $data['result'] = $return; + $this->returnJson($data); + } + + private function getMailtemplateApi() { + $mt_id = $this->request->id; + + if(!$mt_id) { + $this->log->debug(__METHOD__.": mt_id invalid"); + return false; + } + + $template = new Mailtemplate($mt_id); + if(!$template->id) { + $this->log->debug(__METHOD__.": Mailtemplate not found"); + return false; + } + + $data = $template->data; + + $data->is_include = boolval($data->is_include); + if($data->body_text) { + $data->body_text = $template->renderBody(); + } + if($data->body_html) { + $data->body_html = $template->renderBody(); + } + unset($data->create_by, $data->edit_by, $data->note); + + $template_files = []; + if(is_array($template->files) && count($template->files)) { + foreach($template->files as $file) { + $template_files[] = [ + "file_id" => $file->file_id, + "filename" => $file->filename + ]; + } + } + + $data->files = $template_files; + + return (array)$data; + + } } \ No newline at end of file diff --git a/application/MailtemplateDispatch/MailtemplateDispatch.php b/application/MailtemplateDispatch/MailtemplateDispatch.php new file mode 100644 index 000000000..5e8922624 --- /dev/null +++ b/application/MailtemplateDispatch/MailtemplateDispatch.php @@ -0,0 +1,264 @@ +getRecipients(); + if(!$emails) return true; + + + } + + public function getRecipients($list_recipients = false) { + $datasources = $this->getProperty("filter_datasource_array"); + $filter = $this->getProperty("recipient_filter_array"); + + if(!is_array($datasources) || !count($datasources)) { + $this->log->warn(__METHOD__.": Mailtemplate ".$this->id.": no datasource"); + return false; + } + + $emails = []; + + if(in_array("contract", $datasources)) { + + } + if(in_array("order", $datasources)) { + + } + } + + public function getProperty($name) { + if($this->$name == null) { + + if($name == "filter_datasource_array") { + $json = $this->filter_datasource; + if(!$json) { + return []; + } + $ds = json_decode($json, true); + if(!is_array($ds)) { + return []; + } + $this->filter_datasource_array = $ds; + return $this->filter_datasource_array; + } + + if($name == "recipient_filter_array") { + $json = $this->recipient_filter; + if(!$json) { + return []; + } + $rf = json_decode($json, true); + if(!is_array($rf)) { + return []; + } + $this->recipient_filter_array = $rf; + return $this->recipient_filter_array; + } + + + if($name == "file") { + if(!$this->id) return null; + $file = MailtemplateDispatchFile::getFirst(["MailtemplateDispatch_id" => $this->id]); + if($file) { + $this->file = $file; + } + return $this->file; + } + + + if($name == "creator") { + $this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by); + if($this->creator === null) { + $this->creator = new User($this->create_by); + if($this->creator->id) { + mfValuecache::singleton()->set("Worker-id-".$this->create_by, $this->creator); + } + } + return $this->creator; + } + + if($name == "editor") { + $this->editor = mfValuecache::singleton()->get("Worker-id-".$this->edit_by); + if($this->editor === null) { + $this->editor = new User($this->edit_by); + if($this->editor->id) { + mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor); + } + } + return $this->editor; + } + + $classname = ucfirst($name); + $idfield = $name."_id"; + $this->$name = mfValuecache::singleton()->get("mfObjectmodel-$name-".$this->$idfield); + if(!$this->$name) { + $this->$name = new $classname($this->$idfield); + } + + if($this->$name->id) { + mfValuecache::singleton()->set("mfObjectmodel-$name-".$this->$name->id, $this->$name); + return $this->$name; + } else { + return null; + } + + } + + return $this->$name; + } + + /******************************** + * Begin static Model functions + */ + + public static function create(Array $data) { + $model = new MailtemplateDispatch(); + + $table_fields = [ + "mailtemplate_id", "sender_email","sender_name", "sender_replyto", "tosend_date", "send_start", "send_finish", "send_lock", "recipient_count", + "subject", "body_text", "body_html", "filter_datasource", "recipient_filter", + "note", "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("MailtemplateDispatch", "*", "1 = 1 ORDER BY `create`"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new MailtemplateDispatch($data); + } + } + return $items; + + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM MailtemplateDispatch + WHERE $where + ORDER BY `create` LIMIT 1"; + //var_dump($sql);exit; + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new MailtemplateDispatch($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 MailtemplateDispatch + 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 = "`create` ASC"; + } + + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM MailtemplateDispatch + 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 MailtemplateDispatch($data); + } + } + + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + if(array_key_exists("mailtemplate_id", $filter)) { + $mailtemplate_id = $filter['mailtemplate_id']; + if(is_numeric($mailtemplate_id)) { + $where .= " AND MailtemplateDispatch.mailtemplate_id=$mailtemplate_id"; + } + } + + if(array_key_exists("subject", $filter)) { + $subject = FronkDB::singleton()->escape($filter["subject"]); + if($subject) { + $where .= " AND subject='$subject'"; + } + } + + + 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/application/MailtemplateDispatch/MailtemplateDispatchController.php b/application/MailtemplateDispatch/MailtemplateDispatchController.php new file mode 100644 index 000000000..2b8816a5c --- /dev/null +++ b/application/MailtemplateDispatch/MailtemplateDispatchController.php @@ -0,0 +1,150 @@ +needlogin = true; + $me = new User(); + $me->loadMe(); + $this->me = $me; + $this->layout()->set("me", $me); + + if(!$me->is(["Admin", "netowner", "salespartner"])) { + $this->redirect("Dashboard"); + } + } + + protected function indexAction() { + $this->layout()->setTemplate("MailtemplateDispatch/Index"); + + $rfilter = $this->request->filter; + if(!is_array($rfilter)) { + $rfilter = []; + } + + $this->layout->set("filter", $rfilter); + + $where = ""; + $filter = $this->getPreparedFilter($rfilter); + + // pagination defaults + $pagination = []; + $pagination['start'] = 0; + $pagination['count'] = 25; + $pagination['maxItems'] = 0; + + if(is_numeric($this->request->s)) { + $pagination['start'] = intval($this->request->s); + } + + $pagination["maxItems"] = MailtemplateDispatch::count($filter); + $this->layout()->set("pagination", $pagination); + $dispatches = MailtemplateDispatch::search($filter); + $this->layout()->set("dispatches", $dispatches); + + } + + private function getPreparedFilter($filter) { + $new_filter = []; + + if(is_array($filter) && count($filter)) { + foreach($filter as $name => $value) { + $new_filter[$name] = $value; + } + } + + return $new_filter; + } + + protected function addAction() { + $this->layout()->setTemplate("MailtemplateDispatch/Form"); + } + + protected function editAction() { + $id = $this->request->id; + $dispatch = new MailtemplateDispatch($id); + if(!$dispatch->id) { + $this->layout()->setFlash("Emailversand nicht gefunden.", "error"); + $this->redirect("MailtemplateDispatch"); + } + + $this->layout()->set("dispatch", $dispatch); + + return $this->addAction(); + } + + protected function saveAction() { + $r = $this->request; + //var_dump($r->get());exit; + $id = $r->id; + + if(is_numeric($id) && $id > 0) { + $mode = "edit"; + $dispatch = new MailtemplateDispatch($id); + if(!$dispatch->id) { + $this->layout()->setFlash("Emailversand nicht gefunden", "error"); + $this->redirect("MailtemplateDispatch"); + } + } else { + $mode = "add"; + } + + $data = []; + $data["mailtemplate_id"] = $r->mailtemplate_id; + $data["sender_name"] = $r->sender_name; + $data["sender_email"] = $r->sender_email; + $data["sender_replyto"] = $r->sender_replyto; + $data["subject"] = $r->subject; + $data["filter_datasource"] = null; + $data['tosend_date'] = null; + + if(is_array($r->filter_datasource) && count($r->filter_datasource)) { + $data["filter_datasource"] = json_encode($r->filter_datasource); + } + + if($r->tosend_day) { + $tosend_day = $r->tosend_day; + $tosend_hour = $r->tosend_hour; + if($tosend_hour < 0 || $tosend_hour > 23) { + $tosend_hour = 0; + } + $data['tosend_date'] = self::dateToTimestamp($tosend_day . " " . str_pad($tosend_hour, 2, "0", STR_PAD_LEFT) . ":00:00"); + } + + $filter = []; + if($r->filter_fibu_account) { + $filter["fibu_account"] = true; + } + if($r->filter_order_open) { + $filter["order_open"] = true; + } + + if($r->filter_zip) { + foreach(preg_split("/[, ]+/", $r->filter_zip) as $zip) { + $filter["zip"][] = trim($zip); + } + } + + $data["recipient_filter"] = json_encode($filter); + + //var_dump($data); + if($mode == "edit") { + $dispatch->update($data); + } else { + $dispatch = MailtemplateDispatch::create($data); + } + $this->layout()->set("dispatch", $dispatch); + + //var_dump($dispatch);exit; + + if(!$dispatch->save()) { + $this->layout()->setFlash("Fehler beim Speichern", "error"); + return $this->addAction(); + } + + $this->layout()->setFlash("Emailversand erfolgreich gespeichert", "success"); + $this->redirect("MailtemplateDispatch"); + + + } +} \ No newline at end of file diff --git a/db/migrations/20250114141019_create_mailtemplate_dispatch.php b/db/migrations/20250114141019_create_mailtemplate_dispatch.php new file mode 100644 index 000000000..1b4748652 --- /dev/null +++ b/db/migrations/20250114141019_create_mailtemplate_dispatch.php @@ -0,0 +1,49 @@ +getEnvironment() == "thetool") { + $table = $this->table("MailtemplateDispatch"); + $table->addColumn("mailtemplate_id", "integer", ["null" => false]); + $table->addColumn("sender_email", "string", ["null" => false, "limit" => 64]); + $table->addColumn("sender_name", "string", ["null" => false, "limit" => 64]); + $table->addColumn("sender_replyto", "string", ["null" => true, "default" => null, "limit" => 64]); + $table->addColumn("tosend_date", "integer", ["null" => true, "default" => null]); + $table->addColumn("send_start", "integer", ["null" => true, "default" => null]); + $table->addColumn("send_finish", "integer", ["null" => true, "default" => null]); + $table->addColumn("send_lock", "integer", ["null" => true, "default" => null]); + $table->addColumn("recipient_count", "integer", ["null" => false, "default" => 0]); + $table->addColumn("subject", "string", ["null" => false, "limit" => 255]); + $table->addColumn("filter_datasource", "json", ["null" => true, "default" => null]); + $table->addColumn("recipient_filter", "json", ["null" => true, "default" => null]); + $table->addColumn("note", "text", ["null" => true, "default" => null]); + + $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("MailtemplateDispatch")->drop()->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +}