From 7b366ecc2a107d77ce274790207a3e65866a8184 Mon Sep 17 00:00:00 2001 From: Christian Klinger Date: Tue, 2 Aug 2016 15:56:37 +0200 Subject: mail template editor. --- .../dozmod/inc/pagemailtemplates.inc.php | 119 +++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 modules-available/dozmod/inc/pagemailtemplates.inc.php (limited to 'modules-available/dozmod/inc/pagemailtemplates.inc.php') diff --git a/modules-available/dozmod/inc/pagemailtemplates.inc.php b/modules-available/dozmod/inc/pagemailtemplates.inc.php new file mode 100644 index 00000000..fedb13c8 --- /dev/null +++ b/modules-available/dozmod/inc/pagemailtemplates.inc.php @@ -0,0 +1,119 @@ +fetchTemplates(); + } elseif ($action === 'save') { + $this->handleSave(); + } + } + + private function enrichHtml() { + /* for each template */ + foreach ($this->templates as $k => $t) { + $lis = ""; + $optManVars = ""; + $optVars = ""; + foreach ($t['mandatory_variables'] as $var) { + $optManVars .= ""; + $lis .= "
  • $var
  • "; + } + foreach($t['optional_variables'] as $var) { + $optVars .= ""; + $lis .= "
  • $var
  • "; + } + /* also options for hidden inputs */ + + $this->templates[$k]['html_availableVariables'] = $lis; + $this->templates[$k]['html_mandatoryVariables'] = $optManVars; + $this->templates[$k]['html_optionalVariables'] = $optVars; + + /* also for javascript */ + $this->templates[$k]['list_mandatoryVariables'] = + implode(',', $this->templates[$k]['mandatory_variables']); + + $this->templates[$k]['list_optionalVariables'] = + implode(',', $this->templates[$k]['optional_variables']); + } + + } + protected function doRender() + { + //echo '
    ';
    +		//var_dump($this->templates);
    +		//echo '
    '; + //die(); + $this->enrichHtml(); + Render::addTemplate('templates', ['templates' => $this->templates]); + } + + private function handleSave() { + $data = []; + $data['templates'] = Request::post('templates'); + $data = $this->cleanTemplateArray($data); + if ($data!= NULL) { + $data = json_encode($data, JSON_PRETTY_PRINT); + //echo '
    ';
    +			//print_r($data);
    +			//echo '
    '; + //die(); + Database::exec("UPDATE sat.configuration SET value = :value WHERE parameter = 'templates'", array('value' => $data)); + Message::addSuccess('templates-saved'); + + Util::redirect('?do=dozmod§ion=templates&action=show'); + } else { + die('error while encoding'); + } + + } + + private function fetchTemplates() { + $templates= Database::queryFirst('SELECT value FROM sat.configuration WHERE parameter = :param', array('param' => 'templates')); + if ($templates != null) { + $templates = @json_decode($templates['value'], true); + if (is_array($templates)) { + $this->templates = $templates['templates']; + } + } + + } + + private function cleanTemplateArray($in) { + $out = []; + foreach ($in['templates'] as $t) { + $tcopy = $t; + $tcopy['mandatory_variables'] = $this->toArray($t['mandatory_variables']); + $tcopy['optional_variables'] = $this->toArray($t['optional_variables']); + $tcopy['description'] = $t['description']; + $tcopy['name'] = $t['name']; + + $out['templates'][] = $tcopy; + } + return $out; + } + + private function toArray($value) { + if (empty($value)) { + return []; + } else if(is_array($value)) { + return $value; + } else { + return array($value); + } + } +} -- cgit v1.2.3-55-g7522