diff --git a/Layout/default/Preordernotification/Form.php b/Layout/default/Preordernotification/Form.php index 5623e3046..88e086ffe 100644 --- a/Layout/default/Preordernotification/Form.php +++ b/Layout/default/Preordernotification/Form.php @@ -73,10 +73,10 @@
@@ -85,11 +85,11 @@
@@ -212,7 +212,8 @@
- + +
diff --git a/Layout/default/Preordernotification/Index.php b/Layout/default/Preordernotification/Index.php index 1851e789d..6e43b8ea0 100644 --- a/Layout/default/Preordernotification/Index.php +++ b/Layout/default/Preordernotification/Index.php @@ -89,7 +89,16 @@ - sent_date) ? date('d.m.Y', $notification->sent_date) : ""?> + + send_start && $notification->send_finish): ?> + + send_start && !$notification->send_finish): ?> + + send_start && !$notification->send_finish): ?> + + + sent_date) ? date('d.m.Y', $notification->sent_date) : ""?> + campaign->name?> subject?> recipient_count) ? $notification->recipient_count : ""?> diff --git a/application/Emailnotification/Emailnotification.php b/application/Emailnotification/Emailnotification.php index 3b2e29286..1fa645924 100644 --- a/application/Emailnotification/Emailnotification.php +++ b/application/Emailnotification/Emailnotification.php @@ -80,7 +80,7 @@ class Emailnotification { return false; } - if(!$this->headers['Subject']) { + if(!array_key_exists("Subject", $this->headers) || !$this->headers['Subject']) { $this->setHeader("Subject", $this->subject); } diff --git a/application/Preordernotification/Preordernotification.php b/application/Preordernotification/Preordernotification.php index 9ee38b6d2..aeb68663a 100644 --- a/application/Preordernotification/Preordernotification.php +++ b/application/Preordernotification/Preordernotification.php @@ -9,10 +9,16 @@ class Preordernotification extends mfBaseModel { private $editor; public function getPreorders() { + if(!$this->preordercampaign_id) return false; + $preorder_filter = json_decode($this->preorder_filter, true); + $preorder_filter['preordercampaign_id'] = $this->preordercampaign_id; + + $preorders = PreorderModel::search($preorder_filter); + return $preorders; } - public function sendToPreorder(Preorder $preorder, $email_to) { + public function sendToPreorder(Preorder $preorder, $email_to = false) { $subject = $this->subject; $body = $this->body_html; // TODO: Variable replacement $from = $this->sender_email; @@ -21,10 +27,29 @@ class Preordernotification extends mfBaseModel { if($this->sender_replyto) { $reply_to = $this->sender_replyto; } + $to = $preorder->email; if($email_to) { $to = $email_to; + } else { + // check if email was sent to this preorder already + $log = PreordernotificationLogModel::getFirst([ + 'preordernotification_id' => $this->id, + 'preorder_id' => $preorder->id + ]); + + if(!$log) { + // check if this was sent to the same emailaddress already + $log = PreordernotificationLogModel::getFirst([ + 'preordernotification_id' => $this->id, + 'email' => $preorder->email + ]); + } + + if($log) { + return true; + } } if(!$to) return true; @@ -61,6 +86,17 @@ class Preordernotification extends mfBaseModel { } $email->send(); $this->log->info(__CLASS__."::sendToPreorder(): Sending Preordernotification for Preorder id ".$preorder->id); + + if($preorder && !$email_to) { + // save notification log + $log = PreordernotificationLogModel::create([ + 'preordernotification_id' => $this->id, + 'preorder_id' => $preorder->id, + 'email' => $preorder->email, + 'sent' => date('U') + ]); + $log->save(); + } } return true; diff --git a/application/Preordernotification/PreordernotificationController.php b/application/Preordernotification/PreordernotificationController.php index 5dd50746c..e38a09952 100644 --- a/application/Preordernotification/PreordernotificationController.php +++ b/application/Preordernotification/PreordernotificationController.php @@ -379,7 +379,11 @@ class PreordernotificationController extends mfBaseController { } $this->layout()->setFlash("Erfolgreich gepeichert.", "success"); - $this->redirect("Preordernotification", "edit", ['id' => $new_id]); + if($r->return == "index") { + $this->redirect("Preordernotification"); + } else { + $this->redirect("Preordernotification", "edit", ['id' => $new_id]); + } } } \ No newline at end of file diff --git a/application/Preordernotification/PreordernotificationModel.php b/application/Preordernotification/PreordernotificationModel.php index 063b10dee..4334ada32 100644 --- a/application/Preordernotification/PreordernotificationModel.php +++ b/application/Preordernotification/PreordernotificationModel.php @@ -1,14 +1,19 @@ escape($filter['send_finish']); + if($send_finish === null) { + $where .= " AND Preordernotification.send_finish = NULL"; } } - if(array_key_exists("sla_id", $filter)) { - $sla_id = $filter['sla_id']; - if(is_numeric($sla_id)) { - $where .= " AND sla_id=$sla_id"; - } elseif(is_array($sla_id) && count($sla_id)) { - $where .= " AND Preordernotification.sla_id IN (". implode(",", $sla_id).")"; - } - } - - if(array_key_exists("name", $filter)) { - $name = $db->escape($filter['name']); - if($name) { - $where .= " AND Preordernotification.`name` like '$name'"; - } - } - - if(array_key_exists("name%", $filter)) { - $name = $db->escape($filter['name%']); - if($name) { - $where .= " AND Preordernotification.`name` like '%$name%'"; - } - } - - if(array_key_exists("external", $filter)) { - $external = $filter['external']; - if(is_numeric($external)) { - if($external) { - $where .= " AND Preordernotification.external=1"; - } else { - $where .= " AND Preordernotification.external=0"; - } - } - } - - - if(array_key_exists("customer_type", $filter)) { - $customer_type = $db->escape($filter['customer_type']); - if($customer_type) { - $where .= " AND Preordernotificationtech.`customer_type` = '$customer_type'"; - } - } - - if(array_key_exists("attributename", $filter)) { - $attributename = $db->escape($filter['attributename']); - if($attributename) { - $where .= " AND PreordernotificationtechAttribute.name = '$attributename'"; - } - } - - if(array_key_exists("attributevalue", $filter)) { - $attributevalue = $db->escape($filter['attributevalue']); - if($attributevalue) { - $where .= " AND PreordernotificationAttribute.value = '$attributevalue'"; + if(array_key_exists("send_lock", $filter)) { + $send_lock = $db->escape($filter['send_lock']); + if($send_lock === null) { + $where .= " AND Preordernotification.send_lock = NULL"; } } diff --git a/application/PreordernotificationLog/PreordernotificationLog.php b/application/PreordernotificationLog/PreordernotificationLog.php new file mode 100644 index 000000000..bc8c5a70f --- /dev/null +++ b/application/PreordernotificationLog/PreordernotificationLog.php @@ -0,0 +1,5 @@ + $value) { + if(property_exists(get_called_class(), $field)) { + $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("PreordernotificationLog", "*", "1 = 1 ORDER BY `create`"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new PreordernotificationLog($data); + } + } + return $items; + + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $res = $db->select("PreordernotificationLog", "*", "$where ORDER BY `create` LIMIT 1"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new PreordernotificationLog($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 `PreordernotificationLog` + 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) { + $items = []; + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + + $sql = "SELECT PreordernotificationLog.* FROM `PreordernotificationLog` + WHERE $where + ORDER BY `create` + "; + + mfLoghandler::singleton()->debug($sql); + if(is_array($limit) && count($limit)) { + if(is_numeric($limit['start']) && is_numeric($limit['count'])) { + $sql .= " LIMIT ".$limit['start'].", ".$limit['count']; + } elseif(is_numeric($count)) { + $sql .= " LIMIT ".$limit['count']; + } + } + + $res = $db->query($sql); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new PreordernotificationLog($data); + } + } + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + $db = FronkDB::singleton(); + + //var_dump($filter);exit; + if(array_key_exists("id", $filter)) { + $id = $filter['id']; + if(is_numeric($id)) { + $where .= " AND PreordernotificationLog.`id` = $id"; + } + } + + if(array_key_exists("preordernotification_id", $filter)) { + $preordernotification_id = $filter['preordernotification_id']; + if(is_numeric($preordernotification_id)) { + $where .= " AND preordernotification_id=$preordernotification_id"; + } elseif(is_array($preordernotification_id) && count($preordernotification_id)) { + $where .= " AND PreordernotificationLog.preordernotification_id IN (". implode(",", $preordernotification_id).")"; + } + } + + if(array_key_exists("preorder_id", $filter)) { + $preorder_id = $filter['preorder_id']; + if(is_numeric($preorder_id)) { + $where .= " AND preorder_id=$preorder_id"; + } elseif(is_array($preorder_id) && count($preorder_id)) { + $where .= " AND PreordernotificationLog.preorder_id IN (". implode(",", $preorder_id).")"; + } + } + + if(array_key_exists("email", $filter)) { + $email = $db->escape($filter['email']); + if($email) { + $where .= " AND PreordernotificationLog.email = '$email'"; + } + } + + + //var_dump($filter, $where);exit; + return $where; + } + +} \ No newline at end of file diff --git a/scripts/send_preordernotifications.php b/scripts/send_preordernotifications.php new file mode 100644 index 000000000..c61fafac0 --- /dev/null +++ b/scripts/send_preordernotifications.php @@ -0,0 +1,43 @@ +#!/usr/bin/php + null, "send_lock" => null]) as $notification) { + // check tosend date + if(!$notification->tosend_date || $notification->tosend_date > date('U')) continue; + + // get notification again from DB in case send_lock was set + $notification = new Preordernotification($notification->id); + if($notification->send_lock) continue; + + // first of all set send lock and start date + $mypid = getmypid(); + if(!$notification->send_start) { + $notification->send_start = date("U"); + } + $notification->send_lock = ($mypid) ? $mypid : 1; + $notification->save(); + + foreach($notification->getPreorders() as $preorder) { + $notification->sendToPreorder($preorder); + } + + // get notification from DB again and update send lock and finish date + $notification = new Preordernotification($notification->id); + $notification->send_finish = date('U'); + $notification->send_lock = null; + $notification->save(); + +} \ No newline at end of file