MailtemplateDispatch finished
This commit is contained in:
@@ -82,7 +82,7 @@ $pagination_entity_name = "Emailaussendungen";
|
||||
<?php if($dispatch->send_finish): ?>
|
||||
<i class="fas fa-fw fa-check text-success" title="Aussendung abgeschlossen"></i>
|
||||
<?php elseif($dispatch->send_lock): ?>
|
||||
<i class="far fa-fw fa-evelope fa-spin-pulse text-primary" title="Versand läuft"></i>
|
||||
<i class="far fa-fw fa-envelope fa-spin-pulse text-primary" title="Versand läuft"></i>
|
||||
<?php elseif($dispatch->tosend_date && !$dispatch->send_lock): ?>
|
||||
<i class="far fa-fw fa-clock" title="Warte auf Versand"></i>
|
||||
<?php endif; ?>
|
||||
|
||||
@@ -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 = "<html><head></head><body>";
|
||||
$body .= $template->renderBody();
|
||||
$body .= "</body></html>\n"; // explicitly add new line, or some mail clients wont show attachment
|
||||
if($template->body_html) {
|
||||
$body = "<html><head></head><body>";
|
||||
$body .= $template->renderBody();
|
||||
$body .= "</body></html>\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'];
|
||||
|
||||
183
application/MailtemplateDispatchLog/MailtemplateDispatchLog.php
Normal file
183
application/MailtemplateDispatchLog/MailtemplateDispatchLog.php
Normal file
@@ -0,0 +1,183 @@
|
||||
<?php
|
||||
|
||||
class MailtemplateDispatchLog extends mfBaseModel {
|
||||
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Phinx\Migration\AbstractMigration;
|
||||
|
||||
final class CreateMailtemplateDispatchLog extends AbstractMigration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
if($this->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") {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
51
scripts/mailtemplate-dispatch.php
Normal file
51
scripts/mailtemplate-dispatch.php
Normal file
@@ -0,0 +1,51 @@
|
||||
#!/usr/bin/php
|
||||
<?php
|
||||
|
||||
//require 'vendor/autoload.php';
|
||||
require("../config/config.php");
|
||||
|
||||
define('FRONKDB_SQLDEBUG', false);
|
||||
error_reporting(E_ALL & ~(E_NOTICE | E_STRICT | E_DEPRECATED));
|
||||
|
||||
require_once(LIBDIR . "/mvcfronk/mfRouter/mfRouter.php");
|
||||
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseModel.php");
|
||||
require_once(LIBDIR . "/mvcfronk/mfBase/mfBaseController.php");
|
||||
|
||||
$me = new User(1);
|
||||
define("INTERNAL_USER_ID", $me->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();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user