Merge branch 'fronkdev' into 'master'
Added Mailtemplate See merge request fronk/thetool!615
This commit is contained in:
@@ -1,13 +1,41 @@
|
||||
<?php
|
||||
|
||||
class Mailtemplate extends mfBaseModel {
|
||||
private $files;
|
||||
private $creator;
|
||||
private $editor;
|
||||
|
||||
|
||||
public function getVariableReplacedSubject($replaceVars) {
|
||||
return $this->replaceVariables($this->subject, $replaceVars);
|
||||
}
|
||||
|
||||
public function getVariableReplacedBody($replaceVars) {
|
||||
return $this->replaceVariables($this->body, $replaceVars);
|
||||
}
|
||||
|
||||
private function replaceVariables($text, $replaceVars) {
|
||||
if(!is_array($replaceVars)) return false;
|
||||
|
||||
foreach($replaceVars as $key => $replacement) {
|
||||
$body = str_replace("{{".$key."}}", $replacement, $body);
|
||||
}
|
||||
|
||||
return $body;
|
||||
}
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
if($name == "files") {
|
||||
$files = MailtemplateFileModel::search(["mailtemplate_id" => $this->id]);
|
||||
if(!count($files)) {
|
||||
return [];
|
||||
}
|
||||
$this->files = $files;
|
||||
return $this->files;
|
||||
}
|
||||
|
||||
if($name == "creator") {
|
||||
$user = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
|
||||
if($user) {
|
||||
|
||||
@@ -63,6 +63,8 @@ class MailtemplateController extends mfBaseController {
|
||||
protected function addAction() {
|
||||
$this->layout()->setTemplate("Mailtemplate/Form");
|
||||
|
||||
$embeds = MailtemplateModel::search(["is_include" => 1]);
|
||||
$this->layout()->set("embeds", $embeds);
|
||||
}
|
||||
|
||||
protected function editAction() {
|
||||
@@ -80,7 +82,7 @@ class MailtemplateController extends mfBaseController {
|
||||
|
||||
protected function saveAction() {
|
||||
$r = $this->request;
|
||||
var_dump($r->get());exit;
|
||||
//var_dump($r->get());exit;
|
||||
$id = $r->id;
|
||||
|
||||
if(is_numeric($id) && $id > 0) {
|
||||
@@ -96,10 +98,186 @@ class MailtemplateController extends mfBaseController {
|
||||
|
||||
$data = [];
|
||||
$data["name"] = $r->name;
|
||||
$data["subject"] = $r->subject;
|
||||
//$data[""] = $r->;
|
||||
//$data[""] = $r->;
|
||||
//$data[""] = $r->;
|
||||
$data["code"] = null;
|
||||
$data["is_include"] = ($r->is_include) ? 1 : 0;
|
||||
$data["subject"] = ($r->subject) ? $r->subject : null;
|
||||
|
||||
$data["description"] = $r->description;
|
||||
$data["note"] = ($r->note) ? $r->note : null;
|
||||
|
||||
if($r->is_html) {
|
||||
$data["body_html"] = ($r->body_html) ? $r->body_html : null;
|
||||
$data["body_text"] = null;
|
||||
} else {
|
||||
$data["body_text"] = ($r->body_text) ? $r->body_text : null;
|
||||
$data["body_html"] = null;
|
||||
}
|
||||
|
||||
if($mode == "edit") {
|
||||
$data["code"] = $template->code;
|
||||
}
|
||||
|
||||
if(!$data["code"]) {
|
||||
$this->log->debug("if !code [".$data["code"]."]");
|
||||
$new_code = strtolower($data["name"]);
|
||||
$new_code = iconv("utf-8", "ASCII//TRANSLIT", $new_code);
|
||||
$new_code = preg_replace('/[^a-z0-9-]/i', "-", $new_code);
|
||||
$this->log->debug("1st try new code [".$new_code."]");
|
||||
if(!$new_code) {
|
||||
$this->layout()->setFlash("Fehler beim Code generieren. Bitte sinnvollen Name vergeben!", "error");
|
||||
$this->redirect("Mailtemplate");
|
||||
}
|
||||
|
||||
// special chars at the end
|
||||
while(preg_match('/[^a-z0-9]$/', $new_code)) {
|
||||
$this->log->debug("while preg_match() end [$new_code]");
|
||||
$new_code = substr($new_code, 0, -1);
|
||||
if(!$new_code) {
|
||||
$this->layout()->setFlash("Fehler beim Code generieren. Bitte sinnvollen Name vergeben!", "error");
|
||||
$this->redirect("Mailtemplate");
|
||||
}
|
||||
}
|
||||
|
||||
// special chars at the beginning
|
||||
while(preg_match('/^[^a-z0-9]/', $new_code)) {
|
||||
$this->log->debug("while preg_match() beginning [$new_code]");
|
||||
$new_code = substr($new_code, 1);
|
||||
if(!$new_code) {
|
||||
$this->layout()->setFlash("Fehler beim Code generieren. Bitte sinnvollen Name vergeben!", "error");
|
||||
$this->redirect("Mailtemplate");
|
||||
}
|
||||
}
|
||||
|
||||
while($check_mt = MailtemplateModel::getFirst(["code" => $new_code])) {
|
||||
if($mode == "edit" && $check_mt->id == $id) break;
|
||||
$this->log->debug("while getFirst() [".$data["code"]."]");
|
||||
$last_char = substr($new_code, -1, 1);
|
||||
if(is_numeric($last_char)) {
|
||||
$last_two_chars = substr($new_code, -2);
|
||||
if(is_numeric($last_two_chars)) {
|
||||
// avoid increment if the last number could be a status code like 141
|
||||
$new_code .= "-1";
|
||||
} else {
|
||||
$new_code = substr($new_code, 0, -1);
|
||||
$new_code .= ++$last_char;
|
||||
}
|
||||
} else {
|
||||
$new_code .= "-1";
|
||||
}
|
||||
|
||||
}
|
||||
$data["code"] = $new_code;
|
||||
|
||||
}
|
||||
|
||||
|
||||
//var_dump($data);exit;
|
||||
|
||||
if($mode == "edit") {
|
||||
$template->update($data);
|
||||
} else {
|
||||
$template = MailtemplateModel::create($data);
|
||||
}
|
||||
|
||||
if(!$template->save()) {
|
||||
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
||||
$this->redirect("Mailtemplate");
|
||||
}
|
||||
|
||||
|
||||
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_MAILTEMPLATE_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_MAILTEMPLATE_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 {
|
||||
$mtf = [];
|
||||
$mtf['mailtemplate_id'] = $template->id;
|
||||
$mtf['file_id'] = $file_id;
|
||||
$mtf['filename'] = $file->filename;
|
||||
|
||||
$template_file = MailtemplateFileModel::create($mtf);
|
||||
if(!$template_file->save()) {
|
||||
$file->delete();
|
||||
unlink($upload->getSavepath()."/".$upload->getFilename());
|
||||
$this->layout()->setFlash("Dateiupload fehlgeschlagen", "warn");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* delete files
|
||||
*/
|
||||
//var_dump($r->deletefile);exit;
|
||||
if(is_array($r->deletefile)) {
|
||||
foreach($r->deletefile as $mtf_id => $check) {
|
||||
if(!$check) continue;
|
||||
$mtf = new MailtemplateFile($mtf_id);
|
||||
if($mtf->mailtemplate_id != $template->id) continue;
|
||||
$mtf->file->delete();
|
||||
$mtf->delete();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$this->layout()->setFlash("Emailtemplate erfolgreich gespeichert", "success");
|
||||
if($r->return == "index") {
|
||||
$this->redirect("Mailtemplate");
|
||||
} else {
|
||||
$this->redirect("Mailtemplate", "edit", ["id" => $template->id]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,14 @@
|
||||
<?php
|
||||
|
||||
class MailtemplateModel {
|
||||
|
||||
public $name;
|
||||
public $code;
|
||||
public $is_include;
|
||||
public $description;
|
||||
public $subject;
|
||||
public $body;
|
||||
public $body_text;
|
||||
public $body_html;
|
||||
|
||||
public $note = null;
|
||||
public $create_by = null;
|
||||
@@ -118,20 +123,43 @@ class MailtemplateModel {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
//var_dump($filter);exit;
|
||||
if(array_key_exists("is_include", $filter)) {
|
||||
$is_include = $filter['is_include'];
|
||||
if($is_include) {
|
||||
$where .= " AND Mailtemplate.`is_include` = 1";
|
||||
} else {
|
||||
$where .= " AND Mailtemplate.`is_include` = 0";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("name", $filter)) {
|
||||
$name = $db->escape($filter['name']);
|
||||
if($name) {
|
||||
$where .= " AND Mailtemplate.`name` = '$name'";
|
||||
$where .= " AND Mailtemplate.`name` LIKE '%$name%'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("code", $filter)) {
|
||||
$code = $db->escape($filter['code']);
|
||||
if($code) {
|
||||
$where .= " AND Mailtemplate.`code` = '$code'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("subject", $filter)) {
|
||||
$subject = $db->escape($filter['subject']);
|
||||
if($subject) {
|
||||
$where .= " AND Mailtemplate.`subject` = '$subject'";
|
||||
$where .= " AND Mailtemplate.`subject` LIKE '%$subject%'";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("description", $filter)) {
|
||||
$description = $db->escape($filter['description']);
|
||||
if($description) {
|
||||
$where .= " AND Mailtemplate.`description` LIKE '%$description%'";
|
||||
}
|
||||
}
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
class MailtemplateFile extends mfBaseModel {
|
||||
private $mailtemplate;
|
||||
private $file;
|
||||
private $creator;
|
||||
private $editor;
|
||||
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
class MailtemplateFileModel {
|
||||
public $mailtemplate_id;
|
||||
public $file_id;
|
||||
public $filename;
|
||||
|
||||
|
||||
public $note = null;
|
||||
public $create_by = null;
|
||||
public $edit_by = null;
|
||||
public $create = null;
|
||||
public $edit = null;
|
||||
|
||||
|
||||
public static function create(Array $data) {
|
||||
$model = new MailtemplateFile();
|
||||
|
||||
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("MailtemplateFile", "*", "1 = 1 ORDER BY filename");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new MailtemplateFile($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function getFirst($filter = []) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$res = $db->select("MailtemplateFile", "*", "$where ORDER BY filename LIMIT 1");
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new MailtemplateFile($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 `MailtemplateFile`
|
||||
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 MailtemplateFile.* FROM `MailtemplateFile`
|
||||
WHERE $where
|
||||
ORDER BY filename
|
||||
";
|
||||
|
||||
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($limit['count'])) {
|
||||
$sql .= " LIMIT ".$limit['count'];
|
||||
}
|
||||
}
|
||||
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new MailtemplateFile($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
private static function getSqlFilter($filter) {
|
||||
$where = "1=1 ";
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
//var_dump($filter);exit;
|
||||
if(array_key_exists("mailtemplate_id", $filter)) {
|
||||
$mailtemplate_id = $filter['mailtemplate_id'];
|
||||
if(is_numeric($mailtemplate_id)) {
|
||||
$where .= " AND MailtemplateFile.`mailtemplate_id` = $mailtemplate_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("file_id", $filter)) {
|
||||
$file_id = $filter['file_id'];
|
||||
if(is_numeric($file_id)) {
|
||||
$where .= " AND MailtemplateFile.`file_id` = $file_id";
|
||||
}
|
||||
}
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user