diff --git a/Layout/default/Preordercampaign/Form.php b/Layout/default/Preordercampaign/Form.php index 1cefc1812..145f96444 100644 --- a/Layout/default/Preordercampaign/Form.php +++ b/Layout/default/Preordercampaign/Form.php @@ -395,17 +395,38 @@

Emailtemplates

-
- +
+
- +
+
+
+
+ code?> +
+ +
+
+
+
+
+ @ +
+ +
+ +
+
+
+
+
@@ -530,5 +551,31 @@ closeOnSelect: false, background: "bg-danger" }); + + function sendTestStatusEmail(status_code) { + if(!status_code) return; + + var template_id = $("#mailtemplate-" + status_code).val(); + var email_to = $("#test-to-" + status_code).val(); + + $.post("", + { + do: "sendStatusEmail", + campaign_id: id?>, + template_id: template_id, + to_email: email_to + }, + (success) => { + if(success.status == "OK") { + window.notify("success", "Testmail wurde versendet"); + } else { + window.notify("error", "Beim versenden ist ein Fehler aufgetragen."); + } + + console.log(success); + }, + "json" + ); + } \ No newline at end of file diff --git a/application/Mailtemplate/MailtemplateModel.php b/application/Mailtemplate/MailtemplateModel.php index 6082ff8dc..63456ec84 100644 --- a/application/Mailtemplate/MailtemplateModel.php +++ b/application/Mailtemplate/MailtemplateModel.php @@ -59,6 +59,9 @@ class MailtemplateModel { $where = self::getSqlFilter($filter); $res = $db->select("Mailtemplate", "*", "$where ORDER BY name LIMIT 1"); + + mfLoghandler::singleton()->debug($where); + if($db->num_rows($res)) { $data = $db->fetch_object($res); $item = new Mailtemplate($data); diff --git a/application/Preorder/Preorder.php b/application/Preorder/Preorder.php index 4b40d3bc4..5819ee9f1 100644 --- a/application/Preorder/Preorder.php +++ b/application/Preorder/Preorder.php @@ -167,33 +167,51 @@ class Preorder extends mfBaseModel { $this->log->debug(__METHOD__ . ": Looking for $classname in $filepath"); - if(!file_exists($filepath)) { - $this->log->debug(__METHOD__ . ": $filepath not found"); - continue; + if(file_exists($filepath)) { + require_once $filepath; } - require_once $filepath; + if(class_exists($classname)) { + $trigger = new $classname($this, $new_status); + $trigger->run(); - if(!class_exists($classname)) { - $this->log->debug(__METHOD__ . ": $classname not found"); - continue; - } - - $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] = []; + //var_dump($status["action"]); + if (array_key_exists("action", $status) && is_array($status)) { + foreach ($status["action"] as $type => $action) { + if (!array_key_exists($type, $actions)) { + $actions[$type] = []; + } + $action["status_code"] = $intermediate_code; + $actions[$type][] = $action; } - $action["status_code"] = $intermediate_code; - $actions[$type][] = $action; } + } else { + $this->log->debug(__METHOD__ . ": $classname not found"); + } + + // TODO create email action if status templates configured in Campaign + $campaign = new Preordercampaign($this->preordercampaign_id); + if(!$campaign->id) continue; + + $status_mail = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $this->preordercampaign_id, "status_code" => $intermediate_code]); + if($status_mail) { + $mailtemplate = $status_mail->mailtemplate; + if(!$mailtemplate) continue; + + if(!array_key_exists("email", $actions)) { + $actions["email"] = []; + } + $actions["email"][] = [ + "template" => $mailtemplate->code, + "from" => "campaign", + "data" => "preorder, adb_hausnummer, adb_wohneinheit", + "status_code" => $intermediate_code + ]; } } + //var_dump($actions);exit; + // run last email action if(array_key_exists("email", $actions) && is_array($actions["email"]) && count($actions["email"])) { // get last email action, ignore previous status' @@ -210,6 +228,7 @@ class Preorder extends mfBaseModel { } if($send_email) { + $this->log->debug(__METHOD__.": Sending Email Action for Status ".$email_action["status_code"]); $email_to = $this->runTriggerEmailAction($email_action); if($email_to === false) { $this->log->warning(__METHOD__ . ": Could not send preorder action email (Preorder " . $this->id . ")"); @@ -223,7 +242,7 @@ class Preorder extends mfBaseModel { $psn_log->save(); } } else { - $this->log->warning(__METHOD__.": Not sending status email because same or higher status email was sent already (Preorder ".$this->id.")"); + $this->log->warning(__METHOD__.": Not sending status (".$email_action["status_code"].") email because same or higher status email was sent already (Preorder ".$this->id.")"); } } @@ -242,6 +261,11 @@ class Preorder extends mfBaseModel { } $to = $this->email; + + if(array_key_exists("to", $action) && $action["to"]) { + $to = $action["to"]; + } + if(!$to) { $this->log->warning(__METHOD__.": Keine To Adresse (Preorder ".$this->id.")"); return false; @@ -289,7 +313,9 @@ class Preorder extends mfBaseModel { } $subject = $mailtemplate->getVariableReplacedSubject($replacers); - $body = $mailtemplate->renderBody($replacers); + $body = ""; + $body .= $mailtemplate->renderBody($replacers); + $body .= "\n"; $body_type = $mailtemplate->body_html ? "html" : "text"; @@ -306,7 +332,7 @@ class Preorder extends mfBaseModel { $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); + if(!$file->filename || !$file->file_id || !$file->file->store_filename) continue; $this->log->debug($file->file->getFullPath()); $email->addAttachment($file->file->getFullPath(), null, $file->filename, $file->file->mimetype ?: null); } diff --git a/application/Preorder/PreorderModel.php b/application/Preorder/PreorderModel.php index 0bb86d8e9..26bed9c25 100644 --- a/application/Preorder/PreorderModel.php +++ b/application/Preorder/PreorderModel.php @@ -338,7 +338,7 @@ class PreorderModel { 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)) { + } elseif(is_numeric($limit['count'])) { $sql .= " LIMIT ".$limit['count']; } } diff --git a/application/Preordercampaign/Preordercampaign.php b/application/Preordercampaign/Preordercampaign.php index 4ec503171..9044951ac 100644 --- a/application/Preordercampaign/Preordercampaign.php +++ b/application/Preordercampaign/Preordercampaign.php @@ -22,6 +22,7 @@ class Preordercampaign extends mfBaseModel { private $all_fcp_names; private $active_operators; private $passive_operators; + private $statusnotifcation_mailtemplates; public function getAllFcpNames() { if(!$this->id) return []; @@ -255,6 +256,17 @@ class Preordercampaign extends mfBaseModel { return $this->active_operators; } + if($name == "statusnotifcation_mailtemplates") { + $snmts = PreordercampaignStatusnotificationMailtemplate::search(["preordercampaign_id" => $this->id]); + if(!count($snmts)) { + return []; + } + foreach($snmts as $snmt) { + $this->statusnotifcation_mailtemplates[$snmt->status_code] = $snmt; + } + return $this->statusnotifcation_mailtemplates; + } + if($name == "apiusers") { $items = PreordercampaignApiuserModel::search(["preordercampaign_id" => $this->id]); foreach($items as $poa) { diff --git a/application/Preordercampaign/PreordercampaignController.php b/application/Preordercampaign/PreordercampaignController.php index 5c8424874..b2727b3bf 100644 --- a/application/Preordercampaign/PreordercampaignController.php +++ b/application/Preordercampaign/PreordercampaignController.php @@ -213,7 +213,7 @@ class PreordercampaignController extends mfBaseController { } $r = $this->request; - //var_dump($r);exit; + //var_dump($r->get());exit; $id = $r->id; if(is_numeric($id) && $id > 0) { $mode = "edit"; @@ -478,6 +478,46 @@ class PreordercampaignController extends mfBaseController { } } + // Delete Status Email Templates from Campaign + /*foreach(PreordercampaignStatusnotificationMailtemplate::search(["preordercampaign_id" => $campaign->id]) as $status_mailtemplate) { + $status_mailtemplate->delete(); + }*/ + + $mailtemplates_delete = []; + // Save Status Email Templates + foreach($r->mailtemplates as $status_code => $mailtemplate_id) { + if(!$mailtemplate_id) { + $mailtemplates_delete[] = $status_code; + continue; + } + + $mailtemplate = new Mailtemplate($mailtemplate_id); + if(!$mailtemplate) continue; + + $mt_data = [ + "preordercampaign_id" => $campaign->id, + "status_code" => $status_code, + "mailtemplate_id" => $mailtemplate_id, + ]; + + $statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "status_code" => $status_code]); + if($statusmailtemplate) { + $statusmailtemplate->update($mt_data); + } else { + $statusmailtemplate = PreordercampaignStatusnotificationMailtemplate::create($mt_data); + } + + if(!$statusmailtemplate->save()) { + $this->layout()->setFlash("Fehler beim Speichern der Status Emailtemplates", "warn"); + } + } + + foreach($mailtemplates_delete as $mt_code) { + $status_mt = PreordercampaignStatusnotificationMailtemplate::getFirst(["preordercampaign_id" => $campaign->id, "status_code" => $mt_code]); + if($status_mt) { + $status_mt->delete(); + } + } $this->layout()->setFlash("Vorbestellkampagne erfolgreich gespeichert.", "success"); @@ -595,7 +635,75 @@ class PreordercampaignController extends mfBaseController { } } - + + protected function apiAction() { + if(!$this->me->is(["Admin","netowner"]) && !$this->me->can("Preorder")) { + $this->redirect("Dashboard"); + } + $do = $this->request->do; + $data = []; + + switch($do) { + case "sendStatusEmail": + $return = $this->sendStatusEmailApi(); + 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 sendStatusEmailApi() { + $campaign_id = $this->request->campaign_id; + $template_id = $this->request->template_id; + $to_email = $this->request->to_email; + + if(!$to_email) { + return false; + } + + if(!is_numeric($campaign_id) || $campaign_id < 1) { + return false; + } + + $campaign = new Preordercampaign($campaign_id); + if(!$campaign->id) { + return false; + } + + $preorder = PreorderModel::getFirst(["preordercampaign_id" => $campaign_id]); + if(!$preorder) { + return false; + } + + $template = new Mailtemplate($template_id); + if(!$template->id || !$template->code) { + return false; + } + + $email_data = [ + "template" => $template->code, + "from" => "campaign", + "to" => $to_email, + "data" => "preorder, adb_hausnummer, adb_wohneinheit" + ]; + + $preorder->runTriggerEmailAction($email_data); + + return ["message" => "Testemail sent"]; + } + + /**************************** + * begin Campaign Admin actions + */ + protected function adminAction() { $this->layout()->setTemplate("Preordercampaign/Admin"); diff --git a/application/PreordercampaignStatusnotificationMailtemplate/PreordercampaignStatusnotificationMailtemplate.php b/application/PreordercampaignStatusnotificationMailtemplate/PreordercampaignStatusnotificationMailtemplate.php new file mode 100644 index 000000000..744bb891f --- /dev/null +++ b/application/PreordercampaignStatusnotificationMailtemplate/PreordercampaignStatusnotificationMailtemplate.php @@ -0,0 +1,181 @@ +$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 PreordercampaignStatusnotificationMailtemplate(); + + $table_fields = [ + "preordercampaign_id", "status_code", "mailtemplate_id", + "result","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("PreordercampaignStatusnotificationMailtemplate", "*", "1 = 1 ORDER BY preordercampaign_id, status_code"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new PreordercampaignStatusnotificationMailtemplate($data); + } + } + return $items; + + } + + public static function getFirst($filter) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM PreordercampaignStatusnotificationMailtemplate + WHERE $where + ORDER BY preordercampaign_id, status_code LIMIT 1"; + //var_dump($sql);exit; + $res = $db->query($sql); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new PreordercampaignStatusnotificationMailtemplate($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 PreordercampaignStatusnotificationMailtemplate + 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 = "preordercampaign_id ASC, status_code ASC"; + } + + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $sql = "SELECT * FROM PreordercampaignStatusnotificationMailtemplate + 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 PreordercampaignStatusnotificationMailtemplate($data); + } + } + + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + if(array_key_exists("preordercampaign_id", $filter)) { + $preordercampaign_id = $filter['preordercampaign_id']; + if(is_numeric($preordercampaign_id)) { + $where .= " AND PreordercampaignStatusnotificationMailtemplate.preordercampaign_id=$preordercampaign_id"; + } + } + + if(array_key_exists("status_code", $filter)) { + $status_code = $filter['status_code']; + if(is_numeric($status_code)) { + $where .= " AND PreordercampaignStatusnotificationMailtemplate.status_code=$status_code"; + } + } + + if(array_key_exists("mailtemplate_id", $filter)) { + $mailtemplate_id = $filter['mailtemplate_id']; + if(is_numeric($mailtemplate_id)) { + $where .= " AND PreordercampaignStatusnotificationMailtemplate.mailtemplate_id=$mailtemplate_id"; + } + } + + 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/20241209131734_create_preordercampaign_statusnotification_mailtemplate.php b/db/migrations/20241209131734_create_preordercampaign_statusnotification_mailtemplate.php new file mode 100644 index 000000000..aa957b0fb --- /dev/null +++ b/db/migrations/20241209131734_create_preordercampaign_statusnotification_mailtemplate.php @@ -0,0 +1,40 @@ +getEnvironment() == "thetool") { + $table = $this->table('PreordercampaignStatusnotificationMailtemplate'); + $table->addColumn("preordercampaign_id", "integer", ["null" => false]); + $table->addColumn("status_code", "integer", ["null" => false]); + $table->addColumn("mailtemplate_id", "integer", ["null" => false]); + + $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('PreordercampaignStatusnotificationMailtemplate')->drop()->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +} diff --git a/lib/mvcfronk/mfBase/mfBaseController.php b/lib/mvcfronk/mfBase/mfBaseController.php index 0fbb3dd71..8b45c2da5 100644 --- a/lib/mvcfronk/mfBase/mfBaseController.php +++ b/lib/mvcfronk/mfBase/mfBaseController.php @@ -342,7 +342,7 @@ class mfBaseController $t[3] = 0; } } else { - $t = array(0, 0, 0); + $t = array(0, 0, 0, 0); } // make and return timestamp $ts = mktime($t[1], $t[2], $t[3], $d[2], $d[1], $d[3]);