Files
thetool/application/Preordernotification/Preordernotification.php
2025-01-24 15:04:24 +01:00

225 lines
6.9 KiB
PHP

<?php
class Preordernotification extends mfBaseModel {
private $campaign;
private $filter;
private $files;
private $creator;
private $editor;
protected function beforeUpdate($data) {
if(!array_key_exists("edit_by", $data)) {
$me = new User();
$me->loadMe();
$data["edit_by"] = $me->id;
}
return $data;
}
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::searchActive($preorder_filter);
return $preorders;
}
public function sendToPreorder(Preorder $preorder, $email_to = false) {
$subject = $this->subject;
$body = "<html><head></head><body>";
$body .= $this->replaceBodyVariables($this->body_html, $preorder); // TODO: Variable replacement
$body .= "</body></html>";
$body .= "\n"; // explicitlyadd new line, or some mail clients wont show attachment
// TODO: Extract text part
//var_dump($body);exit;
$from = $this->sender_email;
$from_name = $this->sender_name;
$reply_to = null;
if($this->sender_replyto) {
$reply_to = $this->sender_replyto;
}
$borderpoint_file = false;
if($this->attach_borderpoint) {
$borderpoint_file = $preorder->getBorderpointImageFile();
if(!$borderpoint_file) return false;
if(!file_exists($borderpoint_file->file->getFullPath())) return false;
}
$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;
// 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);
if($reply_to) {
$email->setHeader("Reply-To", $reply_to);
}
$email->setHeader("X-".ucfirst(MFAPPNAME)."-Pnid", $this->id);
if(count($attachments)) {
foreach($attachments as $file) {
$email->addAttachment($file["path"], null, $file["filename"], $file['mimetype']);
}
}
if($this->attach_borderpoint && $borderpoint_file) {
$email->addAttachment($borderpoint_file->file->getFullPath(), null, $borderpoint_file->file->filename, $borderpoint_file->file->mimetype);
}
$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;
}
private function replaceBodyVariables($body, $preorder) {
$fullname = "";
if($preorder->firstname) {
$fullname = $preorder->firstname;
if($preorder->lastname) {
$fullname .= " ".$preorder->lastname;
}
} elseif($preorder->lastname) {
$fullname = $preorder->lastname;
}
$replacers = [
'VORNAME' => $preorder->firstname,
'NACHNAME' => $preorder->nachname,
'FIRMA' => $preorder->company,
'FIRMA_ODER_NAME' => ($preorder->company) ? $preorder->company : $fullname,
'EMAIL' => $preorder->email,
'ANSCHLUSS_OAID' => ($preorder->adb_wohneinheit_id) ? $preorder->adb_wohneinheit->oaid : $preorder->adb_hausnummer->oaid,
'ANSCHLUSS_STRASSE' => $preorder->adb_hausnummer->strasse->name,
'ANSCHLUSS_HAUSNUMMER' => $preorder->adb_hausnummer->hausnummer,
'ANSCHLUSS_PLZ' => $preorder->adb_hausnummer->plz->plz,
'ANSCHLUSS_ORT' => $preorder->adb_hausnummer->ortschaft->name,
'ANSCHLUSS_GEMEINDE' => $preorder->adb_hausnummer->strasse->gemeinde->name,
'PREORDER_CODE' => $preorder->ucode
];
foreach($replacers as $key => $replacement) {
$body = str_replace("{{".$key."}}", $replacement, $body);
}
return $body;
}
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;
}
}