From 7af6d36bc135b40e54a60b0fc977c239bc5a3539 Mon Sep 17 00:00:00 2001 From: Frank Schubert Date: Fri, 23 Aug 2024 11:12:57 +0200 Subject: [PATCH] WIP Mailtemplate 2024-08-22 --- Layout/default/Mailtemplate/Form.php | 179 ++++++++++++++++++ Layout/default/Mailtemplate/Index.php | 89 +++++++++ application/Mailtemplate/Mailtemplate.php | 46 +++++ .../Mailtemplate/MailtemplateController.php | 105 ++++++++++ .../Mailtemplate/MailtemplateModel.php | 139 ++++++++++++++ .../20240822153505_create_mailtemplate.php | 40 ++++ 6 files changed, 598 insertions(+) create mode 100644 Layout/default/Mailtemplate/Form.php create mode 100644 Layout/default/Mailtemplate/Index.php create mode 100644 application/Mailtemplate/Mailtemplate.php create mode 100644 application/Mailtemplate/MailtemplateController.php create mode 100644 application/Mailtemplate/MailtemplateModel.php create mode 100644 db/migrations/20240822153505_create_mailtemplate.php diff --git a/Layout/default/Mailtemplate/Form.php b/Layout/default/Mailtemplate/Form.php new file mode 100644 index 000000000..4891b7980 --- /dev/null +++ b/Layout/default/Mailtemplate/Form.php @@ -0,0 +1,179 @@ + + + +
+
+
+
+ +
+

id) ? "Emailtemplate bearbeiten" : "Neues Emailtemplate" ?>

+
+
+
+ + +
+
+ +
+
+ +
" enctype="multipart/form-data"> + +
+
+ + + +
+ + +
+ +

Emailtemplate

+ + +
+ + +
+ + + +

Dateianhänge

+
+
+
+ + +
+
+
+ + files) && count($template->files)): ?> +
+
+
+
    + files as $file): ?> +
  • filename?>
  • + +
+
+ +
+
+ + +
+
+ +
+
+
+ +
+ +
+
+
+
+ +
+ +
+ + +
+
+ + + +
+
+
+ +
+
+ + + + + + \ No newline at end of file diff --git a/Layout/default/Mailtemplate/Index.php b/Layout/default/Mailtemplate/Index.php new file mode 100644 index 000000000..95bd3fc1b --- /dev/null +++ b/Layout/default/Mailtemplate/Index.php @@ -0,0 +1,89 @@ + + + +
+
+
+
+ +
+

Emailtemplates

+
+
+
+ + +
+
+ +
+
+

Filter

+ +
"> +
+ +
+ + +
+ +
+ + +
+ +
+ +
+
+ + ">Filter zurücksetzen +
+
+
+ +
+
+ + +
+
+
+

Liste aller Emailtemplates

+
+ + + + + + + + + + + + + + + + + + + +
NameBetreffErstelltBearbeitet
name?>subject?>edit)?> (editor->name?>)create)?> (creator->name?>) + $template->id])?>"> + $template->id])?>" class="text-danger" onclick="if(!confirm('Emailtemplate wirklich löschen?')) return false;" title="Emailtemplate Löschen"> +
+
+
+ +
+
+ + diff --git a/application/Mailtemplate/Mailtemplate.php b/application/Mailtemplate/Mailtemplate.php new file mode 100644 index 000000000..2460ef5c5 --- /dev/null +++ b/application/Mailtemplate/Mailtemplate.php @@ -0,0 +1,46 @@ +$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; + } +} \ No newline at end of file diff --git a/application/Mailtemplate/MailtemplateController.php b/application/Mailtemplate/MailtemplateController.php new file mode 100644 index 000000000..ee8472061 --- /dev/null +++ b/application/Mailtemplate/MailtemplateController.php @@ -0,0 +1,105 @@ +needlogin=true; + $me = new User(); + $me->loadMe(); + $this->me = $me; + $this->layout()->set("me",$me); + + if(!$me->is(["Admin"])) { + $this->redirect("Dashboard"); + } + } + + protected function indexAction() { + if($this->request->resetFilter) { + unset($_SESSION[MFAPPNAME.'-Mailtemplate-filter']); + } + + $filter = []; + if(is_array($this->request->filter)) { + $filter = $this->request->filter; + $_SESSION[MFAPPNAME.'-Mailtemplate-filter'] = $filter; + } else { + if(array_key_exists(MFAPPNAME.'-Mailtemplate-filter', $_SESSION) && count($_SESSION[MFAPPNAME.'-Mailtemplate-filter'])) { + $filter = $_SESSION[MFAPPNAME.'-Mailtemplate-filter']; + } + } + + $this->layout->set("filter", $filter); + $filter = $this->getPreparedFilter($filter); + + + // pagination defaults + $pagination = []; + $pagination['start'] = 0; + $pagination['count'] = 20; + $pagination['maxItems'] = 0; + + if(is_numeric($this->request->s)) { + $pagination['start'] = intval($this->request->s); + } + + $pagination['maxItems'] = MailtemplateModel::count($filter); + $this->layout()->set("pagination", $pagination); + + $templates = MailtemplateModel::search($filter, $pagination); + $this->layout()->set("templates", $templates); + } + + private function getPreparedFilter($filter) { + $new_filter = []; + + foreach($filter as $name => $value) { + $new_filter[$name] = $value; + } + + return $new_filter; + } + + protected function addAction() { + $this->layout()->setTemplate("Mailtemplate/Form"); + + } + + protected function editAction() { + $id = $this->request->id; + $template = new Mailtemplate($id); + if(!$template->id) { + $this->layout()->setFlash("Emailtemplate nicht gefunden.", "error"); + $this->redirect("Mailtemplate"); + } + + $this->layout()->set("template", $template); + + return $this->addAction(); + } + + protected function saveAction() { + $r = $this->request; + var_dump($r->get());exit; + $id = $r->id; + + if(is_numeric($id) && $id > 0) { + $mode = "edit"; + $template = new Mailtemplate($id); + if(!$template->id) { + $this->layout()->setFlash("Emailtemplate nicht gefunden", "error"); + $this->redirect("Mailtemplate"); + } + } else { + $mode = "add"; + } + + $data = []; + $data["name"] = $r->name; + $data["subject"] = $r->subject; + //$data[""] = $r->; + //$data[""] = $r->; + //$data[""] = $r->; + + } +} \ No newline at end of file diff --git a/application/Mailtemplate/MailtemplateModel.php b/application/Mailtemplate/MailtemplateModel.php new file mode 100644 index 000000000..eafa850f0 --- /dev/null +++ b/application/Mailtemplate/MailtemplateModel.php @@ -0,0 +1,139 @@ + $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("Mailtemplate", "*", "1 = 1 ORDER BY name"); + if($db->num_rows($res)) { + while($data = $db->fetch_object($res)) { + $items[] = new Mailtemplate($data); + } + } + return $items; + + } + + public static function getFirst($filter = []) { + $db = FronkDB::singleton(); + + $where = self::getSqlFilter($filter); + $res = $db->select("Mailtemplate", "*", "$where ORDER BY name LIMIT 1"); + if($db->num_rows($res)) { + $data = $db->fetch_object($res); + $item = new Mailtemplate($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 `Mailtemplate` + 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 Mailtemplate.* FROM `Mailtemplate` + WHERE $where + ORDER BY name + "; + + 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 Mailtemplate($data); + } + } + return $items; + } + + private static function getSqlFilter($filter) { + $where = "1=1 "; + + $db = FronkDB::singleton(); + + //var_dump($filter);exit; + if(array_key_exists("name", $filter)) { + $name = $db->escape($filter['name']); + if($name) { + $where .= " AND Mailtemplate.`name` = '$name'"; + } + } + + if(array_key_exists("subject", $filter)) { + $subject = $db->escape($filter['subject']); + if($subject) { + $where .= " AND Mailtemplate.`subject` = '$subject'"; + } + } + + //var_dump($filter, $where);exit; + return $where; + } + +} \ No newline at end of file diff --git a/db/migrations/20240822153505_create_mailtemplate.php b/db/migrations/20240822153505_create_mailtemplate.php new file mode 100644 index 000000000..ef5d08a52 --- /dev/null +++ b/db/migrations/20240822153505_create_mailtemplate.php @@ -0,0 +1,40 @@ +getEnvironment() == "thetool") { + $table = $this->table("Mailtemplate"); + $table->addColumn("name", "string", ["null" => false, "limit" => 255]); + $table->addColumn("description", "string", ["null" => true, "default" => true, "limit" => 1024]); + $table->addColumn("subject", "string", ["null" => false, "limit" => 255]); + $table->addColumn("body", "text", ["null" => false]); + $table->addColumn("note", "text", ["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("Mailtemplate")->drop()->save(); + } + + if($this->getEnvironment() == "addressdb") { + + } + } +}