MailtemplateDispatch Index and Form done
This commit is contained in:
264
application/MailtemplateDispatch/MailtemplateDispatch.php
Normal file
264
application/MailtemplateDispatch/MailtemplateDispatch.php
Normal file
@@ -0,0 +1,264 @@
|
||||
<?php
|
||||
|
||||
class MailtemplateDispatch extends mfBaseModel {
|
||||
private $mailtemplate;
|
||||
private $filter_datasource_array;
|
||||
private $recipient_filter_array;
|
||||
|
||||
private $creator;
|
||||
private $editor;
|
||||
|
||||
|
||||
public function sendToRecipients($test_to_email = false) {
|
||||
$emails = $this->getRecipients();
|
||||
if(!$emails) return true;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function getRecipients($list_recipients = false) {
|
||||
$datasources = $this->getProperty("filter_datasource_array");
|
||||
$filter = $this->getProperty("recipient_filter_array");
|
||||
|
||||
if(!is_array($datasources) || !count($datasources)) {
|
||||
$this->log->warn(__METHOD__.": Mailtemplate ".$this->id.": no datasource");
|
||||
return false;
|
||||
}
|
||||
|
||||
$emails = [];
|
||||
|
||||
if(in_array("contract", $datasources)) {
|
||||
|
||||
}
|
||||
if(in_array("order", $datasources)) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public function getProperty($name) {
|
||||
if($this->$name == null) {
|
||||
|
||||
if($name == "filter_datasource_array") {
|
||||
$json = $this->filter_datasource;
|
||||
if(!$json) {
|
||||
return [];
|
||||
}
|
||||
$ds = json_decode($json, true);
|
||||
if(!is_array($ds)) {
|
||||
return [];
|
||||
}
|
||||
$this->filter_datasource_array = $ds;
|
||||
return $this->filter_datasource_array;
|
||||
}
|
||||
|
||||
if($name == "recipient_filter_array") {
|
||||
$json = $this->recipient_filter;
|
||||
if(!$json) {
|
||||
return [];
|
||||
}
|
||||
$rf = json_decode($json, true);
|
||||
if(!is_array($rf)) {
|
||||
return [];
|
||||
}
|
||||
$this->recipient_filter_array = $rf;
|
||||
return $this->recipient_filter_array;
|
||||
}
|
||||
|
||||
|
||||
if($name == "file") {
|
||||
if(!$this->id) return null;
|
||||
$file = MailtemplateDispatchFile::getFirst(["MailtemplateDispatch_id" => $this->id]);
|
||||
if($file) {
|
||||
$this->file = $file;
|
||||
}
|
||||
return $this->file;
|
||||
}
|
||||
|
||||
|
||||
if($name == "creator") {
|
||||
$this->creator = mfValuecache::singleton()->get("Worker-id-".$this->create_by);
|
||||
if($this->creator === null) {
|
||||
$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 = mfValuecache::singleton()->get("Worker-id-".$this->edit_by);
|
||||
if($this->editor === null) {
|
||||
$this->editor = new User($this->edit_by);
|
||||
if($this->editor->id) {
|
||||
mfValuecache::singleton()->set("Worker-id-".$this->edit_by, $this->editor);
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
/********************************
|
||||
* Begin static Model functions
|
||||
*/
|
||||
|
||||
public static function create(Array $data) {
|
||||
$model = new MailtemplateDispatch();
|
||||
|
||||
$table_fields = [
|
||||
"mailtemplate_id", "sender_email","sender_name", "sender_replyto", "tosend_date", "send_start", "send_finish", "send_lock", "recipient_count",
|
||||
"subject", "body_text", "body_html", "filter_datasource", "recipient_filter",
|
||||
"note", "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("MailtemplateDispatch", "*", "1 = 1 ORDER BY `create`");
|
||||
if($db->num_rows($res)) {
|
||||
while($data = $db->fetch_object($res)) {
|
||||
$items[] = new MailtemplateDispatch($data);
|
||||
}
|
||||
}
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
public static function getFirst($filter) {
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT * FROM MailtemplateDispatch
|
||||
WHERE $where
|
||||
ORDER BY `create` LIMIT 1";
|
||||
//var_dump($sql);exit;
|
||||
$res = $db->query($sql);
|
||||
if($db->num_rows($res)) {
|
||||
$data = $db->fetch_object($res);
|
||||
$item = new MailtemplateDispatch($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 MailtemplateDispatch
|
||||
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 = "`create` ASC";
|
||||
}
|
||||
|
||||
$db = FronkDB::singleton();
|
||||
|
||||
$where = self::getSqlFilter($filter);
|
||||
$sql = "SELECT * FROM MailtemplateDispatch
|
||||
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 MailtemplateDispatch($data);
|
||||
}
|
||||
}
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
private static function getSqlFilter($filter) {
|
||||
$where = "1=1 ";
|
||||
|
||||
if(array_key_exists("mailtemplate_id", $filter)) {
|
||||
$mailtemplate_id = $filter['mailtemplate_id'];
|
||||
if(is_numeric($mailtemplate_id)) {
|
||||
$where .= " AND MailtemplateDispatch.mailtemplate_id=$mailtemplate_id";
|
||||
}
|
||||
}
|
||||
|
||||
if(array_key_exists("subject", $filter)) {
|
||||
$subject = FronkDB::singleton()->escape($filter["subject"]);
|
||||
if($subject) {
|
||||
$where .= " AND subject='$subject'";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if(array_key_exists("add-where", $filter)) {
|
||||
$where .= " ".$filter['add-where'];
|
||||
}
|
||||
|
||||
//var_dump($filter, $where);exit;
|
||||
return $where;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
|
||||
class MailtemplateDispatchController extends mfBaseController {
|
||||
|
||||
protected function init() {
|
||||
$this->needlogin = true;
|
||||
$me = new User();
|
||||
$me->loadMe();
|
||||
$this->me = $me;
|
||||
$this->layout()->set("me", $me);
|
||||
|
||||
if(!$me->is(["Admin", "netowner", "salespartner"])) {
|
||||
$this->redirect("Dashboard");
|
||||
}
|
||||
}
|
||||
|
||||
protected function indexAction() {
|
||||
$this->layout()->setTemplate("MailtemplateDispatch/Index");
|
||||
|
||||
$rfilter = $this->request->filter;
|
||||
if(!is_array($rfilter)) {
|
||||
$rfilter = [];
|
||||
}
|
||||
|
||||
$this->layout->set("filter", $rfilter);
|
||||
|
||||
$where = "";
|
||||
$filter = $this->getPreparedFilter($rfilter);
|
||||
|
||||
// pagination defaults
|
||||
$pagination = [];
|
||||
$pagination['start'] = 0;
|
||||
$pagination['count'] = 25;
|
||||
$pagination['maxItems'] = 0;
|
||||
|
||||
if(is_numeric($this->request->s)) {
|
||||
$pagination['start'] = intval($this->request->s);
|
||||
}
|
||||
|
||||
$pagination["maxItems"] = MailtemplateDispatch::count($filter);
|
||||
$this->layout()->set("pagination", $pagination);
|
||||
$dispatches = MailtemplateDispatch::search($filter);
|
||||
$this->layout()->set("dispatches", $dispatches);
|
||||
|
||||
}
|
||||
|
||||
private function getPreparedFilter($filter) {
|
||||
$new_filter = [];
|
||||
|
||||
if(is_array($filter) && count($filter)) {
|
||||
foreach($filter as $name => $value) {
|
||||
$new_filter[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return $new_filter;
|
||||
}
|
||||
|
||||
protected function addAction() {
|
||||
$this->layout()->setTemplate("MailtemplateDispatch/Form");
|
||||
}
|
||||
|
||||
protected function editAction() {
|
||||
$id = $this->request->id;
|
||||
$dispatch = new MailtemplateDispatch($id);
|
||||
if(!$dispatch->id) {
|
||||
$this->layout()->setFlash("Emailversand nicht gefunden.", "error");
|
||||
$this->redirect("MailtemplateDispatch");
|
||||
}
|
||||
|
||||
$this->layout()->set("dispatch", $dispatch);
|
||||
|
||||
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";
|
||||
$dispatch = new MailtemplateDispatch($id);
|
||||
if(!$dispatch->id) {
|
||||
$this->layout()->setFlash("Emailversand nicht gefunden", "error");
|
||||
$this->redirect("MailtemplateDispatch");
|
||||
}
|
||||
} else {
|
||||
$mode = "add";
|
||||
}
|
||||
|
||||
$data = [];
|
||||
$data["mailtemplate_id"] = $r->mailtemplate_id;
|
||||
$data["sender_name"] = $r->sender_name;
|
||||
$data["sender_email"] = $r->sender_email;
|
||||
$data["sender_replyto"] = $r->sender_replyto;
|
||||
$data["subject"] = $r->subject;
|
||||
$data["filter_datasource"] = null;
|
||||
$data['tosend_date'] = null;
|
||||
|
||||
if(is_array($r->filter_datasource) && count($r->filter_datasource)) {
|
||||
$data["filter_datasource"] = json_encode($r->filter_datasource);
|
||||
}
|
||||
|
||||
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");
|
||||
}
|
||||
|
||||
$filter = [];
|
||||
if($r->filter_fibu_account) {
|
||||
$filter["fibu_account"] = true;
|
||||
}
|
||||
if($r->filter_order_open) {
|
||||
$filter["order_open"] = true;
|
||||
}
|
||||
|
||||
if($r->filter_zip) {
|
||||
foreach(preg_split("/[, ]+/", $r->filter_zip) as $zip) {
|
||||
$filter["zip"][] = trim($zip);
|
||||
}
|
||||
}
|
||||
|
||||
$data["recipient_filter"] = json_encode($filter);
|
||||
|
||||
//var_dump($data);
|
||||
if($mode == "edit") {
|
||||
$dispatch->update($data);
|
||||
} else {
|
||||
$dispatch = MailtemplateDispatch::create($data);
|
||||
}
|
||||
$this->layout()->set("dispatch", $dispatch);
|
||||
|
||||
//var_dump($dispatch);exit;
|
||||
|
||||
if(!$dispatch->save()) {
|
||||
$this->layout()->setFlash("Fehler beim Speichern", "error");
|
||||
return $this->addAction();
|
||||
}
|
||||
|
||||
$this->layout()->setFlash("Emailversand erfolgreich gespeichert", "success");
|
||||
$this->redirect("MailtemplateDispatch");
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user