Saving Preordernotification and test email done

This commit is contained in:
Frank Schubert
2023-01-26 19:24:35 +01:00
parent 844e6ccd2a
commit d2ceb7ead8
14 changed files with 791 additions and 100 deletions

View File

@@ -7,6 +7,7 @@ class Emailnotification {
private $headers = [];
private $subject;
private $body;
private $html;
private $from_name = false;
private $email_from = false;
private $email_to = false;
@@ -52,6 +53,10 @@ class Emailnotification {
$this->body = $body;
}
public function setHtmlBody($html) {
$this->html = $html;
}
public function setFrom($email, $name = false) {
$this->email_from = $email;
$this->from_name = $name;
@@ -65,7 +70,7 @@ class Emailnotification {
if(!$this->email_to) {
return false;
}
if(!$this->body) {
if(!$this->body && !$this->html) {
return false;
}
if(!is_array($this->headers) || !count($this->headers)) {
@@ -93,8 +98,13 @@ class Emailnotification {
$mimeparams['head_charset']="utf-8";
$mime = new Mail_mime();
$mime->setTXTBody($this->body);
if($this->body) {
$mime->setTXTBody($this->body);
}
if($this->html) {
$mime->setHTMLBody($this->html);
}
//var_dump($this->attachments);exit;
if(count($this->attachments)) {

View File

@@ -15,7 +15,11 @@ class File extends mfBaseModel {
$id = $this->id;
// delete file in store
$path = MFUPLOAD_FILE_SAVE_PATH."/documents/".$this->store_filename;
if($this->ubfolder) {
$path = MFUPLOAD_FILE_SAVE_PATH."/".$this->subfolder."/".$this->store_filename;
} else {
$path = MFUPLOAD_FILE_SAVE_PATH."/".$this->store_filename;
}
if(!unlink($path)) {
$this->log->warn(__CLASS__."::delete(): Error unlinking file ($path)");
}

View File

@@ -7,6 +7,7 @@ class FileModel {
public $store_filename;
public $orig_filename;
public $subfolder;
public $mimetype;
public $create_by = null;
public $edit_by = null;
@@ -66,7 +67,7 @@ class FileModel {
}
public static function getFirst() {
public static function getFirst($filter) {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);

View File

@@ -387,6 +387,18 @@ class PreorderModel {
}
}
if(array_key_exists("connection_type", $filter)) {
$connection_type = $filter['connection_type'];
if(is_array($connection_type) && count($connection_type)) {
$where .= " AND connection_type IN ('".implode("','",$connection_type)."')";
} else {
$connection_type = FronkDB::singleton()->escape($filter['connection_type']);
if($connection_type) {
$where .= " AND connection_type like '%$connection_type%'";
}
}
}
if(array_key_exists("ucode", $filter)) {
$ucode = FronkDB::singleton()->escape($filter['ucode']);
if($ucode) {

View File

@@ -1,5 +1,119 @@
<?php
class Preordernotification extends mfBaseModel {
private $campaign;
private $filter;
private $files;
private $creator;
private $editor;
public function getPreorders() {
}
public function sendToPreorder(Preorder $preorder, $email_to) {
$subject = $this->subject;
$body = $this->body_html; // TODO: Variable replacement
$from = "office@xinon.at";
$from_name = "XINON Kundenservice";
$to = $preorder->email;
if($email_to) {
$to = $email_to;
}
if(!$to) return true;
// get attachments
$attachments = [];
foreach($this->getProperty("files") as $file) {
$att = [];
$att["path"] = MFUPLOAD_FILE_SAVE_PATH."/".$file->file->subfolder."/".$file->file->store_filename;
$att["filename"] = $file->filename;
$att["mimetype"] = $file->file->mimetype;
$attachments[] = $att;
}
//var_dump($attachments);exit;
if(!$subject || !$from || !$from_name || !$to) {
$this->log->warn("Preordernotification not sent. subject, from or to missing. Preorder id ".$preorder->id);
return false;
} else {
$email = new Emailnotification();
$email->setSubject($subject);
$email->setHtmlBody($body);
$email->setFrom($from, $from_name);
$email->setTo($to);
$email->setHeader("X-".MFAPPNAME."-Pnid", $this->id);
if(count($attachments)) {
foreach($attachments as $file) {
$email->addAttachment($file["path"], null, $file["filename"], $file['mimetype']);
}
}
$email->send();
$this->log->info(__CLASS__."::sendToPreorder(): Sending Preordernotification for Preorder id ".$preorder->id);
}
return true;
}
public function getProperty($name) {
if($this->$name == null) {
if($name == "campaign") {
$this->campaign = new Preordercampaign($this->preordercampaign_id);
return $this->campaign;
}
if($name == "filter") {
if($this->preorder_filter) {
$this->filter = json_decode($this->preorder_filter, true);
} else {
$this->filter = [];
}
return $this->filter;
}
if($name == "files") {
$this->files = PreordernotificationFileModel::search(["preordernotification_id" => $this->id]);
return $this->files;
}
if($name == "creator") {
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
if($user) {
$this->creator = $user;
return $this->creator;
}
$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 = new User($this->edit_by);
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;
}
}

View File

@@ -71,12 +71,12 @@ class PreordernotificationController extends mfBaseController {
$filter['preordercampaign_id'] = $my_campaign_ids;
}
}
//var_dump($filter);exit;
$pagination['maxItems'] = PreordernotificationModel::count($filter);
$notifications = PreordernotificationModel::search($filter, $pagination);
$this->layout()->set("pagination", $pagination);
$this->layout()->set("preorders", $notifications);
$this->layout()->set("notifications", $notifications);
}
@@ -111,22 +111,271 @@ class PreordernotificationController extends mfBaseController {
protected function editAction() {
$id = $this->request->id;
if(!is_numeric($id) || $id < 1) {
$this->layout()->setFlash("Vorbestellung nicht gefunden", "error");
$this->redirect("Preordercampaign");
$this->layout()->setFlash("Aussendung nicht gefunden", "error");
$this->redirect("Preordernotification");
}
$preorder = new Preorder($id);
if(!$preorder->id) {
$this->layout()->setFlash("Vorbestellung nicht gefunden", "error");
$this->redirect("Preordercampaign");
$notification = new Preordernotification($id);
if(!$notification->id) {
$this->layout()->setFlash("Aussendung nicht gefunden", "error");
$this->redirect("Preordernotification");
}
$this->request->set("preordercampaign_id", $preorder->preordercampaign_id); // needed in addAction()
$this->layout()->set("preorder", $preorder);
//var_dump($notification->filter);exit;
//var_dump($preorder->building->street);exit;
$this->request->set("preordercampaign_id", $notification->preordercampaign_id); // needed in addAction()
$this->layout()->set("notification", $notification);
return $this->addAction();
}
protected function saveAction() {
$r = $this->request;
//var_dump($r->get(), $_FILES);exit;
/*
* add or edit
*/
$id = $r->id;
if(is_numeric($id) && $id > 0) {
$mode = "edit";
$notification = new Preordernotification($id);
if(!$notification->id) {
$this->layout()->setFlash("Aussendung nicht gefunden", "error");
$this->redirect("Preordernotification");
}
} else {
$id = false;
$mode = "add";
}
/*
* check campaign and permissions
*/
$campaign_id = $r->preordercampaign_id;
if(!$campaign_id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordernotification");
}
$campaign = new Preordercampaign($campaign_id);
if(!$campaign->id) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordernotification");
}
// check permission
if(!$this->me->is("Admin")) {
$my_campaign_ids = [];
$my_networks = $this->me->myNetworks(["netowner", "salespartner"]);
//var_dump($my_networks);exit;
foreach($my_networks as $network) {
foreach(PreordercampaignModel::search(['network_id' => $network->id]) as $campaign) {
if(!in_array($campaign->id, $my_campaign_ids)) $my_campaign_ids[] = $campaign->id;
}
}
if(!in_array($campaign_id, $my_campaign_ids)) {
$this->layout()->setFlash("Vorbestellkampagne nicht gefunden", "error");
$this->redirect("Preordernotification");
}
}
/*
* data colletion
*/
$filter = [];
if(is_array($r->type) && count($r->type)) {
$filter['type'] = [];
foreach($r->type as $type) {
switch($type) {
case "interest":
$filter["type"][] = "interest";
break;
case "provision":
$filter["type"][] = "provision";
break;
case "order":
$filter["type"][] = "order";
break;
case "reorder":
$filter["type"][] = "reorder";
break;
}
}
}
if(is_array($r->connection_type) && count($r->connection_type)) {
$filter['connection_type'] = [];
foreach($r->connection_type as $type) {
switch($type) {
case "single-dwelling":
$filter["connection_type"][] = "single-dwelling";
break;
case "multi-dwelling":
$filter["connection_type"][] = "multi-dwelling";
break;
case "apartment-building":
$filter["connection_type"][] = "apartment-building";
break;
case "apartment":
$filter["connection_type"][] = "apartment";
break;
case "business":
$filter["connection_type"][] = "business";
break;
}
}
}
//var_dump($filter);
$data = [];
$data['preordercampaign_id'] = $campaign_id;
$data['subject'] = $r->subject;
$data['body_html'] = $r->body_html;
$data['note'] = $r->note;
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");
}
$data['preorder_filter'] = json_encode($filter);
//var_dump($data);exit;
if($mode == "edit") {
$notification->update($data);
} else {
$notification = PreordernotificationModel::create($data);
}
$new_id = $notification->save();
if(!$new_id) {
$this->layout()->setFlash("Fehler beim Speichern", "error");
if($id) {
$this->redirect("Preordernotification", "edit", ['id' => $id]);
} else {
$this->redirect("Preordernotification");
}
}
/*
* Attachment upload
*/
if(array_key_exists("attachment", $_FILES)) {
$files = $_FILES['attachment'];
if(is_array($files) && count($files)) {
$file_errors = 0;
foreach($files['name'] as $i => $name) {
if(!$name) continue;
$upload_error = false;
try {
$upload = new mfUpload(['attachment', $i]);
$upload->setSavepath(MFUPLOAD_FILE_SAVE_PATH . "/" . TT_PREORDERNOTIFICATION_FILE_UPLOAD_SUBFOLDER);
} catch(Exception $e) {
$this->layout()->setFlash("Dateiupload fehlgeschlagen: ".$e->getMessage(), "warn");
$file_errors++;
continue;
}
if(!$upload->getSize()) {
$this->layout()->setFlash("Dateiupload fehlgeschlagen: Datei ist leer!", "warn");
$upload_error = true;
}
$mime = "";
if(!$upload_error) {
try {
$mime = $upload->getMimetype();
$upload->save();
} catch(Exception $e) {
$this->layout()->setFlash("Dateiupload fehlgeschlagen", "warn");
$upload_error = true;
}
}
if($upload_error) {
$file_errors++;
continue;
}
$file_data = [];
$file_data['name'] = $upload->getOriginalFilename();
$file_data['filename'] = $upload->getOriginalFilename();
$file_data['subfolder'] = TT_PREORDERNOTIFICATION_FILE_UPLOAD_SUBFOLDER;
$file_data['store_filename'] = $upload->getFilename();
$file_data['orig_filename'] = $upload->getOriginalFilename();
$file_data['mimetype'] = $mime;
$file = FileModel::create($file_data);
$file_id = $file->save();
if(!$file_id) {
$this->layout()->setFlash("Dateiupload fehlgeschlagen", "warn");
unlink($upload->getSavepath()."/".$upload->getFilename());
} else {
$pnf = [];
$pnf['preordernotification_id'] = $notification->id;
$pnf['file_id'] = $file_id;
$pnf['filename'] = $file->filename;
$notification_file = PreordernotificationFileModel::create($pnf);
if(!$notification_file->save()) {
$file->delete();
unlink($upload->getSavepath()."/".$upload->getFilename());
$this->layout()->setFlash("Dateiupload fehlgeschlagen", "warn");
}
}
}
}
}
/*
* delete files
*/
if(is_array($r->deletefile)) {
foreach($r->deletefile as $pnf_id) {
$pnf = new PreordernotificationFile($pnf_id);
if($pnf->preordernotification_id != $notification->id) continue;
$pnf->file->delete();
$pnf->delete();
}
}
// get and save preorder count
$preorder_filter = json_decode($notification->preorder_filter, true);
$preorder_filter['preordercampaign_id'] = $notification->preordercampaign_id;
$preorder_count = PreorderModel::count($preorder_filter);
$notification->recipient_count = $preorder_count;
$notification->save();
if($r->send_testmail && $r->testmail_to) {
$preorder = PreorderModel::getFirst($preorder_filter);
if(!$preorder) {
$this->layout()->setFlash("Testmail konnte nicht versendet werden. Keine Empfänger gefunden", "warn");
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
}
$notification->sendToPreorder($preorder, $r->testmail_to);
$this->layout()->setFlash("Testmail versendet", "success");
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
}
$this->layout()->setFlash("Erfolgreich gepeichert.", "success");
$this->redirect("Preordernotification", "edit", ['id' => $new_id]);
}
}

View File

@@ -1,13 +1,14 @@
<?php
class PreordernotificationModel {
public $name = null;
public $subject;
public $body_text;
public $body_html;
public $tosend_date;
public $sent_date;
public $recipient_count;
public $preordercampaign_id;
public $preorder_filter;
public $note = null;
public $create_by = null;

View File

@@ -0,0 +1,28 @@
<?php
class PreordernotificationFile extends mfBaseModel {
private $file;
private $preordernotification;
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;
}
}

View File

@@ -0,0 +1,150 @@
<?php
class PreordernotificationFileModel {
public $preordernotification_id;
public $file_id;
public $filename;
public $create_by = null;
public $edit_by = null;
public $create = null;
public $edit = null;
public static function create(Array $data) {
$model = new PreordernotificationFile();
foreach($data as $field => $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("PreordernotificationFile", "*", "1 = 1 ORDER BY preordernotification_id,file_id");
if($db->num_rows($res)) {
while($data = $db->fetch_object($res)) {
$items[] = new PreordernotificationFile($data);
}
}
return $items;
}
public static function getFirst() {
$db = FronkDB::singleton();
$where = self::getSqlFilter($filter);
$res = $db->select("PreordernotificationFile", "*", "$where ORDER BY preordernotification_id,file_id LIMIT 1");
if($db->num_rows($res)) {
$data = $db->fetch_object($res);
$item = new PreordernotificationFile($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 `PreordernotificationFile`
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 PreordernotificationFile.* FROM `PreordernotificationFile`
WHERE $where
ORDER BY preordernotification_id,file_id
";
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 PreordernotificationFile($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 = $db->escape($filter['id']);
if($id) {
$where .= " AND PreordernotificationFile.`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 PreordernotificationFile.preordernotification_id IN (". implode(",", $preordernotification_id).")";
}
}
if(array_key_exists("file_id", $filter)) {
$file_id = $filter['file_id'];
if(is_numeric($file_id)) {
$where .= " AND PreordernotificationFile.file_id=$file_id";
} elseif(is_array($file_id) && count($file_id)) {
$where .= " AND PreordernotificationFile.file_id IN (". implode(",", $file_id).")";
}
}
//var_dump($filter, $where);exit;
return $where;
}
}