From 5393160273129c64fe992d8c6aad932acf0b0e98 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Tue, 14 Jan 2025 22:41:14 +0100 Subject: [PATCH] MailtemplateDispatch finished --- Layout/default/MailtemplateDispatch/Index.php | 2 +- .../MailtemplateDispatch.php | 69 ++++--- .../MailtemplateDispatchLog.php | 183 ++++++++++++++++++ ...11027_create_mailtemplate_dispatch_log.php | 38 ++++ scripts/mailtemplate-dispatch.php | 51 +++++ 5 files changed, 314 insertions(+), 29 deletions(-) create mode 100644 application/MailtemplateDispatchLog/MailtemplateDispatchLog.php create mode 100644 db/migrations/20250114211027_create_mailtemplate_dispatch_log.php create mode 100644 scripts/mailtemplate-dispatch.php diff --git a/Layout/default/MailtemplateDispatch/Index.php b/Layout/default/MailtemplateDispatch/Index.php index eabb2af8b..96efc92b8 100644 --- a/Layout/default/MailtemplateDispatch/Index.php +++ b/Layout/default/MailtemplateDispatch/Index.php @@ -82,7 +82,7 @@ $pagination_entity_name = "Emailaussendungen"; send_finish): ?> send_lock): ?> - + tosend_date && !$dispatch->send_lock): ?> diff --git a/application/MailtemplateDispatch/MailtemplateDispatch.php b/application/MailtemplateDispatch/MailtemplateDispatch.php index 06dbdfc94..9d3644e95 100644 --- a/application/MailtemplateDispatch/MailtemplateDispatch.php +++ b/application/MailtemplateDispatch/MailtemplateDispatch.php @@ -10,11 +10,18 @@ class MailtemplateDispatch extends mfBaseModel { public function sendToRecipients($test_to_email = false) { + $emails = []; + if(!$test_to_email) { $emails = $this->getRecipients(); - if(!$emails) return true; - if(!$this->tosend_date || $this->tosend_date > date('U')) return false; - if($this->send_lock) return false; + if(!$emails) { + $this->log->debug(__METHOD__.": No recipient emails (".$this->id.")"); + return true; + } + if(!$this->tosend_date || $this->tosend_date > date('U')) { + $this->log->debug(__METHOD__.": No tosend date or in future (".$this->id.")"); + return true; + } } if($test_to_email) { @@ -28,9 +35,14 @@ class MailtemplateDispatch extends mfBaseModel { $template = $this->getProperty("mailtemplate"); if(!$template) return false; - $body = ""; - $body .= $template->renderBody(); - $body .= "\n"; // explicitly add new line, or some mail clients wont show attachment + if($template->body_html) { + $body = ""; + $body .= $template->renderBody(); + $body .= "\n"; // explicitly add new line, or some mail clients wont show attachment + } else { + $body = $template->renderBody(); + } + $subject = $this->subject; @@ -38,7 +50,7 @@ class MailtemplateDispatch extends mfBaseModel { $from_name = $this->sender_name; $attachments = []; - foreach($this->getProperty("mailtemplate")->files as $file) { + foreach($template->files as $file) { $att = []; $att["path"] = MFUPLOAD_FILE_SAVE_PATH."/".$file->file->subfolder."/".$file->file->store_filename; $att["filename"] = $file->filename; @@ -50,23 +62,19 @@ class MailtemplateDispatch extends mfBaseModel { $to = $email; // check for NotificationLog - /*$mnlog = MaintenanceNotificationLog::getFirst(["maintenancenotification_id" => $this->id, "email" => $to]); - if($mnlog) { + $mtdlog = MailtemplateDispatchLog::getFirst(["mailtemplatedispatch_id" => $this->id, "email" => $to]); + if($mtdlog) { // was sent already continue; - }*/ - /*fwrite(STDERR, "Subject: $subject\n\n"); - fwrite(STDERR, "$body\n\n"); - exit;*/ + } $email = new Emailnotification(); $email->setSubject($subject); - if($this->getProperty("mailtemplate")->body_html) { + if($template->body_html) { $email->setHtmlBody($body); } else { $email->setBody($body); } - $email->setFrom($from_email, $from_name); $email->setTo($to); $email->setHeader("X-".ucfirst(MFAPPNAME)."-mtdid", $this->id); @@ -82,24 +90,16 @@ class MailtemplateDispatch extends mfBaseModel { return true; } - /*$mnlog = MaintenanceNotificationLog::create([ - "maintenancenotification_id" => $this->id, + $mtdlog = MailtemplateDispatchLog::create([ + "mailtemplatedispatch_id" => $this->id, "email" => $to, "sent" => date("U"), ]); - $mnlog->save(); - */ + $mtdlog->save(); + + sleep(1); // wait a second } - - //$this->sent = date("U"); - //$this->save(); - - - - - - } public function getRecipients($list_recipients = false, $return_recipients = false) { @@ -463,6 +463,19 @@ class MailtemplateDispatch extends mfBaseModel { } } + if(array_key_exists("send_finish", $filter)) { + $send_finish = $filter['send_finish']; + if($send_finish === null) { + $where .= " AND send_finish IS NULL"; + } + } + + if(array_key_exists("send_lock", $filter)) { + $send_lock = $filter['send_lock']; + if($send_lock === null) { + $where .= " AND send_lock IS NULL"; + } + } if(array_key_exists("add-where", $filter)) { $where .= " ".$filter['add-where']; diff --git a/application/MailtemplateDispatchLog/MailtemplateDispatchLog.php b/application/MailtemplateDispatchLog/MailtemplateDispatchLog.php new file mode 100644 index 000000000..c72bc16db --- /dev/null +++ b/application/MailtemplateDispatchLog/MailtemplateDispatchLog.php @@ -0,0 +1,183 @@ +$name == null) { + + $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 MailtemplateDispatchLog(); + + $table_fields = [ + "mailtemplatedispatch_id", "email", "sent", + "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("MailtemplateDispatchLog", "*", "1 = 1 ORDER BY `create`"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new MailtemplateDispatchLog($data); + } + } + return $items; + + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM MailtemplateDispatchLog + 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 MailtemplateDispatchLog($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 MailtemplateDispatchLog + 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 MailtemplateDispatchLog + 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 MailtemplateDispatchLog($data); + } + } + + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + if(array_key_exists("mailtemplatedispatch_id", $filter)) { + $mailtemplatedispatch_id = $filter['mailtemplatedispatch_id']; + if(is_numeric($mailtemplatedispatch_id)) { + $where .= " AND mailtemplatedispatch_id=$mailtemplatedispatch_id"; + } + } + + if(array_key_exists("email", $filter)) { + $email = FronkDB::singleton()->escape($filter["email"]); + if($email) { + $where .= " AND email='$email'"; + } + } + + if(array_key_exists("sent", $filter)) { + $sent = $filter['sent']; + if($sent === true) { + $where .= " AND `sent` > 0"; + } elseif($sent === false || $sent === null) { + $where .= " AND (`sent` IS NULL OR `sent` = 0)"; + } elseif(is_numeric($sent)) { + $where .= " AND `sent`=$sent"; + } + } + + + 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/20250114211027_create_mailtemplate_dispatch_log.php b/db/migrations/20250114211027_create_mailtemplate_dispatch_log.php new file mode 100644 index 000000000..2bfd8decf --- /dev/null +++ b/db/migrations/20250114211027_create_mailtemplate_dispatch_log.php @@ -0,0 +1,38 @@ +getEnvironment() == "thetool") { + $log = $this->table("MailtemplateDispatchLog"); + $log->addColumn("mailtemplatedispatch_id", "integer", ["null" => false]); + $log->addColumn("email", "string", ["null" => false]); + $log->addColumn("sent", "integer", ["null" => false, "default" => 0]); + + $log->addColumn("create_by", "integer", ["null" => false]); + $log->addColumn("edit_by", "integer", ["null" => false]); + $log->addColumn("create", "integer", ["null" => false]); + $log->addColumn("edit", "integer", ["null" => false]); + $log->create(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } + + public function down(): void + { + if($this->getEnvironment() == "thetool") { + $this->table("MailtemplateDispatchLog")->drop()->save; + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/scripts/mailtemplate-dispatch.php b/scripts/mailtemplate-dispatch.php new file mode 100644 index 000000000..3e5b0d1d8 --- /dev/null +++ b/scripts/mailtemplate-dispatch.php @@ -0,0 +1,51 @@ +#!/usr/bin/php +id); +define("INTERNAL_USER_USERNAME", $me->username); +define("MFBASE_BYPASS_LOGIN", true); + + +/*$dispatch = new MailtemplateDispatch(1); +$emails = $dispatch->getRecipients(true); + +echo "empfänger emails: ".count($emails)."\n"; +//$dispatch->sendToRecipients("fronk@fronk.at"); +*/ + +foreach(MailtemplateDispatch::search(["send_finish" => null, "send_lock" => null]) as $dispatch) { + // check tosend date + if(!$dispatch->tosend_date || $dispatch->tosend_date > date('U')) continue; + + // get notification again from DB in case send_lock was set + $dispatch = new MailtemplateDispatch($dispatch->id); + if($dispatch->send_lock) continue; + + // first of all set send lock and start date + $mypid = getmypid(); + if(!$dispatch->send_start) { + $dispatch->send_start = date("U"); + } + $dispatch->send_lock = ($mypid) ? $mypid : 1; + $dispatch->save(); + + $dispatch->sendToRecipients(); + + // get Dispatch from DB again and update send lock and finish date + $dispatch = new MailtemplateDispatch($dispatch->id); + $dispatch->send_finish = date('U'); + $dispatch->send_lock = null; + $dispatch->save(); + +} \ No newline at end of file