From 1ddee4350cba50f2a0d6f6644a2eb3f7bd0803d7 Mon Sep 17 00:00:00 2001 From: Andreas Palm Date: Thu, 27 Oct 2022 13:17:19 +0200 Subject: [PATCH] Refactor and improve Versandarten settings --- phpwf/plugins/class.templateparser.php | 27 +--- www/lib/class.versanddienstleister.php | 192 +++++++++++------------- www/lib/intraship.php | 2 +- www/lib/versandarten/sendcloud.php | 34 +---- www/pages/content/versandarten_edit.tpl | 8 +- www/pages/versandarten.php | 136 ++++++++--------- 6 files changed, 172 insertions(+), 227 deletions(-) diff --git a/phpwf/plugins/class.templateparser.php b/phpwf/plugins/class.templateparser.php index 0c626424..bcb99618 100644 --- a/phpwf/plugins/class.templateparser.php +++ b/phpwf/plugins/class.templateparser.php @@ -1,4 +1,4 @@ - +*/ +?> htmlspecialchars($text); } - $ret .= '
'.$text.'
'; + $ret = sprintf('
%s
', $class, $text); if($_var === 'return') - { return $ret; - } - return $this->app->Tpl->Add($_var, $ret); + + $this->app->Tpl->Add($_var, $ret); } public function addSelect($_var, $id, $name, $options, $selected = '', $class = '') diff --git a/www/lib/class.versanddienstleister.php b/www/lib/class.versanddienstleister.php index d744b05d..12507829 100644 --- a/www/lib/class.versanddienstleister.php +++ b/www/lib/class.versanddienstleister.php @@ -1,9 +1,31 @@ app = $app; + if ($id === null || $id === 0) + return; + $this->id = $id; + $row = $this->app->DB->SelectRow("SELECT * FROM versandarten WHERE id=$this->id"); + $this->type = $row['type']; + $this->projectId = $row['projekt']; + $this->labelPrinterId = $row['paketmarke_drucker']; + $this->documentPrinterId = $row['export_drucker']; + $this->shippingMail = $row['versandmail']; + $this->businessLetterTemplateId = $row['geschaeftsbrief_vorlage']; + $this->settings = json_decode($row['einstellungen_json']); + } + public function isEtikettenDrucker(): bool { return false; @@ -147,18 +169,32 @@ abstract class Versanddienstleister { } /** - * @param string $target + * Returns an array of additional field definitions to be stored for this module: + * [ + * 'field_name' => [ + * 'typ' => text(default)|textarea|checkbox|select + * 'default' => default value + * 'optionen' => just for selects [key=>value] + * 'size' => size attribute for text fields + * 'placeholder' => placeholder attribute for text fields + * ] + * ] * - * @return string + * @return array */ - public function Einstellungen($target = 'return') + public function AdditionalSettings(): array { + return []; + } + + /** + * Renders all additional settings as fields into $target + * @param string $target template placeholder for rendered output + * @param array $form data for form values (from database or form submit) + * @return void + */ + public function RenderAdditionalSettings(string $target, array $form): void { - if(!$this->id) - { - return ''; - } - //$id = $this->id; - $struktur = $this->EinstellungenStruktur(); + $fields = $this->AdditionalSettings(); if($this->app->Secure->GetPOST('speichern')) { $json = $this->app->DB->Select("SELECT einstellungen_json FROM versandarten WHERE id = '".$this->id."' LIMIT 1"); @@ -168,7 +204,7 @@ abstract class Versanddienstleister { $json = @json_decode($json, true); }else{ $json = array(); - foreach($struktur as $name => $val) + foreach($fields as $name => $val) { if(isset($val['default'])) { @@ -180,7 +216,7 @@ abstract class Versanddienstleister { { $json = null; } - foreach($struktur as $name => $val) + foreach($fields as $name => $val) { if($modul === $this->app->Secure->GetPOST('modul_name')) @@ -201,101 +237,53 @@ abstract class Versanddienstleister { $json_str = $this->app->DB->real_escape_string(json_encode($json)); $this->app->DB->Update("UPDATE versandarten SET einstellungen_json = '$json_str' WHERE id = '".$this->id."' LIMIT 1"); } - $id = $this->id; $html = ''; - $json = $this->app->DB->Select("SELECT einstellungen_json FROM versandarten WHERE id = '$id' LIMIT 1"); - if($json) + foreach($fields as $name => $val) // set missing default values { - $json = json_decode($json, true); - }else{ - $json = null; - } - - $changed = false; - foreach($struktur as $name => $val) - { - if(isset($val['default']) && !isset($json[$name])) + if(isset($val['default']) && !isset($form[$name])) { - $changed = true; - $json[$name] = $val['default']; + $form[$name] = $val['default']; } } - if($changed) - { - $json_str = $this->app->DB->real_escape_string(json_encode($json)); - $this->app->DB->Update("UPDATE versandarten SET einstellungen_json = '$json_str' WHERE id = '".$this->id."' LIMIT 1"); - } - $first = true; - foreach($struktur as $name => $val) + foreach($fields as $name => $val) { if(isset($val['heading'])) - { $html .= ''.html_entity_decode($val['heading']).''; - } - $html .= ''.($first?'':'').(empty($val['bezeichnung'])?$name:$val['bezeichnung']).''; - $typ = 'text'; - if(!empty($val['typ'])) - { - $typ = $val['typ']; - } + + $html .= ''.($val['bezeichnung'] ?? $name).''; if(isset($val['replace'])) { switch($val['replace']) { case 'lieferantennummer': - $json[$name] = $this->app->erp->ReplaceLieferantennummer(0,$json[$name],0); - if($target !== 'return') - { - $this->app->YUI->AutoComplete($name, 'lieferant', 1); - } - break; + $form[$name] = $this->app->erp->ReplaceLieferantennummer(0,$form[$name],0); + $this->app->YUI->AutoComplete($name, 'lieferant', 1); + break; case 'shop': - $json[$name] .= ($json[$name]?' '.$this->app->DB->Select("SELECT bezeichnung FROM shopexport WHERE id = '".(int)$json[$name]."'"):''); - if($target !== 'return') - { - $this->app->YUI->AutoComplete($name, 'shopnameid'); - } - break; + $form[$name] .= ($form[$name]?' '.$this->app->DB->Select("SELECT bezeichnung FROM shopexport WHERE id = '".(int)$form[$name]."'"):''); + $this->app->YUI->AutoComplete($name, 'shopnameid'); + break; case 'etiketten': - //$json[$name] = $this->app->erp->ReplaceLieferantennummer(0,$json[$name],0); - if($target !== 'return') - { - $this->app->YUI->AutoComplete($name, 'etiketten'); - } - break; - + $this->app->YUI->AutoComplete($name, 'etiketten'); + break; } } - /*if(!isset($json[$name]) && isset($val['default'])) - { - $json[$name] = $val['default']; - }*/ - switch($typ) + switch($val['typ'] ?? 'text') { case 'textarea': - $html .= ''; - break; + $html .= ''; + break; case 'checkbox': - $html .= ''; - break; + $html .= ''; + break; case 'select': - $html .= ''; - break; + $html .= $this->app->Tpl->addSelect('return', $name, $name, $val['optionen'], $form[$name]); + break; case 'submit': if(isset($val['text'])) - { $html .= '
'; - } - break; + break; case 'custom': if(isset($val['function'])) { @@ -305,35 +293,31 @@ abstract class Versanddienstleister { $html .= $this->$tmpfunction(); } } - break; + break; default: - - $html .= ''; + $html .= ''; break; } - if(isset($val['info']) && $val['info'])$html .= ' '.$val['info'].''; + if(isset($val['info']) && $val['info']) + $html .= ' '.$val['info'].''; $html .= ''; - $first = false; - } - - if($target === 'return') { - return $html; } $this->app->Tpl->Add($target, $html); - return ''; } - protected abstract function EinstellungenStruktur(); - - /** - * @param string $target - * - * @return bool - */ - public function checkInputParameters(string $target = ''): bool + /** + * Validate form data for this module + * Form data is passed by reference so replacements (id instead of text) can be performed here as well + * @param array $form submitted form data + * @return array + */ + public function CheckInputParameters(array &$form): array { - return true; + return []; } diff --git a/www/lib/intraship.php b/www/lib/intraship.php index 916d7e14..65fc5837 100644 --- a/www/lib/intraship.php +++ b/www/lib/intraship.php @@ -99,7 +99,7 @@ class Versandart_intraship extends Versanddienstleister{ return 'DHL Instrahship'; } - function EinstellungenStruktur() + function AdditionalSettings() { return array( diff --git a/www/lib/versandarten/sendcloud.php b/www/lib/versandarten/sendcloud.php index c61bfb3a..c89e413d 100644 --- a/www/lib/versandarten/sendcloud.php +++ b/www/lib/versandarten/sendcloud.php @@ -12,32 +12,14 @@ require_once dirname(__DIR__) . '/class.versanddienstleister.php'; class Versandart_sendcloud extends Versanddienstleister { + protected SendCloudApi $api; + protected array $options; - /* @var SendCloudApi $api */ - protected $api; - - /* @var array $settings */ - protected $settings; - - protected $options; - - /** - * Versandart_sendcloud constructor. - * - * @param ApplicationCore $app - * @param int $id - */ - public function __construct($app, $id) + public function __construct(Application $app, ?int $id) { - $this->app = $app; - $this->id = $id; - - //TODO move to better place - $res = $this->app->DB->SelectRow("SELECT * FROM versandarten WHERE id=$this->id"); - $this->settings = json_decode($res['einstellungen_json']); - $this->type = $res['type']; - $this->paketmarke_drucker = $res['paketmarke_drucker']; - + parent::__construct($app, $id); + if (!isset($this->id)) + return; $this->api = new SendCloudApi($this->settings->public_key, $this->settings->private_key); } @@ -59,10 +41,10 @@ class Versandart_sendcloud extends Versanddienstleister $this->options['products'] = array_map(fn(ShippingProduct $x) => $x->Name, $shippingProducts ?? []); $this->options['products'][0] = ''; $this->options['selectedProduct'] = $shippingProducts[$this->settings->shipping_product]; - asort($this->options['products']); + natcasesort($this->options['products']); } - protected function EinstellungenStruktur() + public function AdditionalSettings(): array { $this->FetchOptionsFromApi(); return [ diff --git a/www/pages/content/versandarten_edit.tpl b/www/pages/content/versandarten_edit.tpl index fcb65535..9d6ea025 100644 --- a/www/pages/content/versandarten_edit.tpl +++ b/www/pages/content/versandarten_edit.tpl @@ -14,15 +14,13 @@ {|Bezeichnung|}: - [MSGBEZEICHNUNG] + data-lang="versandart_bezeichnung_[ID]" required> {|Typ|}: - - [MSGTYP] + {|z.B. dhl,ups,etc.|} @@ -64,7 +62,7 @@ {|Textvorlage|}: [SELGESCHAEFTSBRIEF_VORLAGE] - [JSON] + [MODULESETTINGS] diff --git a/www/pages/versandarten.php b/www/pages/versandarten.php index c80e71a9..31d2c812 100644 --- a/www/pages/versandarten.php +++ b/www/pages/versandarten.php @@ -149,7 +149,7 @@ class Versandarten { public function VersandartenEdit(): void { $id = (int)$this->app->Secure->GetGET('id'); - $save = $this->app->Secure->GetPOST('speichern'); + $submit = $this->app->Secure->GetPOST('speichern'); if (!$id) return; @@ -157,62 +157,59 @@ class Versandarten { $this->app->erp->MenuEintrag('index.php?module=versandarten&action=edit&id='.$id,'Details'); $this->app->erp->MenuEintrag('index.php?module=versandarten&action=list','Zurück zur Übersicht'); - $input = $this->GetInput(); - $error = []; - if($save != ''){ - $moduleObject = $this->loadModule($input['selmodul'], $id); - if ($moduleObject === null) - $error[] = sprintf('Versandart "%s" existiert nicht.', $input['selmodul']); + if($submit != '') { // handle form submit + $form = $this->GetInput(); + $obj = $this->loadModule($form['modul'], $id); + if ($obj === null) + $error[] = sprintf('Versandart "%s" existiert nicht.', $form['selmodul']); - if(trim($input['bezeichnung']) == '') { - $error[] = 'Bitte alle Pflichtfelder ausfüllen!'; - $this->app->Tpl->Set('MSGBEZEICHNUNG','Pflichtfeld!'); - } - if(trim($input['typ']) == '') - { - $error[] = 'Bitte alle Pflichtfelder ausfüllen!'; - $this->app->Tpl->Set('MSGTYP','Pflichtfeld!'); - } + if(trim($form['bezeichnung']) == '') + $error[] = 'Bitte eine Bezeichnung angeben!'; - $projektid = 0; - if(!empty($input['projekt'])){ - $projektid = $this->app->DB->Select( - "SELECT `id` FROM `projekt` WHERE `abkuerzung` = '{$input['projekt']}' LIMIT 1" + if(trim($form['type']) == '') + $error[] = 'Bitte einen Typ angeben!'; + + $projektId = 0; + if(!empty($form['projekt'])){ + $projektId = $this->app->DB->Select( + "SELECT `id` FROM `projekt` WHERE `abkuerzung` = '{$form['projekt']}' LIMIT 1" ); } if ($this->app->DB->Select( - "SELECT `id` FROM `versandarten` WHERE `type` = '{$input['typ']}' AND `id` <> $id LIMIT 1" - )) { + "SELECT `id` FROM `versandarten` WHERE `type` = '{$form['type']}' AND `id` <> $id LIMIT 1" + )) $error[] = 'Typ ist bereits für eine andere Versandart vergeben'; + + foreach ($obj->AdditionalSettings() as $k => $v) { + $form[$k] = $this->app->Secure->GetPOST($k); } + $error = array_merge($error, $obj->CheckInputParameters($form)); + foreach ($obj->AdditionalSettings() as $k => $v) { + $json[$k] = $form[$k]; + } + $json = json_encode($json ?? null); foreach ($error as $e) { $this->app->Tpl->addMessage('error', $e); } - $inputCheckResult = $moduleObject->checkInputParameters('MESSAGE'); - if (empty($error) && $inputCheckResult) { + if (empty($error)) { $this->app->DB->Update( "UPDATE `versandarten` - SET `bezeichnung`='{$input['bezeichnung']}', `type` ='{$input['typ']}', - `projekt`=$projektid, `aktiv`={$input['aktiv']}, `modul`='{$input['selmodul']}', - `export_drucker` = {$input['export_drucker']}, - `paketmarke_drucker` = {$input['paketmarke_drucker']}, - `ausprojekt` = {$input['ausprojekt']}, `versandmail` = {$input['versandmail']}, - `geschaeftsbrief_vorlage` = {$input['geschaeftsbrief_vorlage']}, - `keinportocheck`={$input['keinportocheck']} + SET `bezeichnung`='{$form['bezeichnung']}', `type` ='{$form['type']}', + `projekt`=$projektId, `aktiv`={$form['aktiv']}, `modul`='{$form['modul']}', + `export_drucker` = {$form['export_drucker']}, + `paketmarke_drucker` = {$form['paketmarke_drucker']}, + `ausprojekt` = {$form['ausprojekt']}, `versandmail` = {$form['versandmail']}, + `geschaeftsbrief_vorlage` = {$form['geschaeftsbrief_vorlage']}, + `keinportocheck`={$form['keinportocheck']}, + einstellungen_json='$json' WHERE `id` = $id LIMIT 1" ); - if($input['aktiv'] == 1){ - $this->app->Tpl->Set('AKTIV', "checked"); - } - if($input['keinportocheck'] == 1){ - $this->app->Tpl->Set('KEINPORTOCHECK', "checked"); - } - $this->app->Tpl->Set('MESSAGE', "
Die Daten wurden erfolgreich gespeichert!
"); + $this->app->Tpl->addMessage('success', "Die Daten wurden erfolgreich gespeichert!"); } } $daten = $this->app->DB->SelectRow("SELECT * FROM `versandarten` WHERE `id` = $id LIMIT 1"); @@ -222,28 +219,34 @@ class Versandarten { $this->app->erp->Headlines('', $daten['bezeichnung']); $this->app->Tpl->Set('AKTMODUL', $daten['modul']); $obj = $this->loadModule($daten['modul'], $daten['id']); - $bezeichnung = $daten['bezeichnung']; - $typ = $daten['type']; - $projekt = $daten['projekt']; - $aktiv = $daten['aktiv']; - $keinportocheck = $daten['keinportocheck']; - $projektname = $this->app->erp->Projektdaten($projekt, 'abkuerzung'); - $obj->Einstellungen('JSON'); - $etikettendrucker = $obj->isEtikettenDrucker(); + + if (empty($error) || !isset($form)) { //overwrite form data from database if no validation error is present + $form = json_decode($daten['einstellungen_json'],true); + $form['bezeichnung'] = $daten['bezeichnung']; + $form['type'] = $daten['type']; + $form['projekt'] = $this->app->erp->Projektdaten($daten['projekt'], 'abkuerzung'); + $form['aktiv'] = $daten['aktiv']; + $form['keinportocheck'] = $daten['keinportocheck']; + $form['modul'] = $daten['modul']; + $form['export_drucker'] = $daten['export_drucker']; + $form['paketmarke_drucker'] = $daten['paketmarke_drucker']; + } + + $obj->RenderAdditionalSettings('MODULESETTINGS', $form); $drucker_export = $this->app->erp->GetDrucker(); $drucker_export[0] = ''; - asort($drucker_export); + natcasesort($drucker_export); $this->app->Tpl->addSelect('EXPORT_DRUCKER', 'export_drucker', 'export_drucker', - $drucker_export, $daten['export_drucker']); + $drucker_export, $form['export_drucker']); $drucker_paketmarke = $this->app->erp->GetDrucker(); - if($etikettendrucker) + if($obj->isEtikettenDrucker()) $drucker_paketmarke = array_merge($drucker_paketmarke, $this->app->erp->GetEtikettendrucker()); $drucker_paketmarke[0] = ''; - asort($drucker_paketmarke); + natcasesort($drucker_paketmarke); $this->app->Tpl->addSelect('PAKETMARKE_DRUCKER', 'paketmarke_drucker', 'paketmarke_drucker', - $drucker_paketmarke, $daten['paketmarke_drucker']); + $drucker_paketmarke, $form['paketmarke_drucker']); $this->app->YUI->HideFormular('versandmail', array('0'=>'versandbetreff','1'=>'dummy')); $this->app->Tpl->addSelect('SELVERSANDMAIL', 'versandmail', 'versandmail', [ @@ -258,30 +261,19 @@ class Versandarten { LEFT JOIN `projekt` AS `p` ON gv.projekt = p.id ORDER by gv.subjekt" ); - $this->app->Tpl->addSelect('SELGESCHAEFTSBRIEF_VORLAGE', 'geschaeftsbrief_vorlage', 'geschaeftsbrief_vorlage', $geschaeftsbrief_vorlagen, $daten['geschaeftsbrief_vorlage']); - if(empty($error)){ - $selectedModule = $daten['modul'] ?? ''; - } - else { - $selectedModule = $input['selmodul']; - } - $this->app->Tpl->addSelect('SELMODUL', 'selmodul', 'selmodul', - $this->VersandartenSelModul(), $selectedModule); - $this->app->Tpl->Set('BEZEICHNUNG', empty($error)?$bezeichnung:$input['bezeichnung']); - $this->app->Tpl->Set('TYP', empty($error)?$typ:$input['typ']); - $this->app->Tpl->Set('PROJEKT', empty($error)?$projektname:$input['projekt']); - if((empty($error) && $aktiv == 1) || (!empty($error) && $input['aktiv'])){ - $this->app->Tpl->Set('AKTIV', 'checked'); - } - if((empty($error) && $keinportocheck == 1) || (!empty($error) && $input['keinportocheck'])){ - $this->app->Tpl->Set('KEINPORTOCHECK', 'checked'); - } + $this->app->Tpl->addSelect('SELMODUL', 'modul', 'modul', + $this->VersandartenSelModul(), $form['modul']); + $this->app->Tpl->Set('BEZEICHNUNG', $form['bezeichnung']); + $this->app->Tpl->Set('TYPE', $form['type']); + $this->app->Tpl->Set('PROJEKT', $form['projekt']); $this->app->YUI->AutoComplete('projekt', 'projektname', 1); + if($form['aktiv']) $this->app->Tpl->Set('AKTIV', 'checked'); + if($form['keinportocheck']) $this->app->Tpl->Set('KEINPORTOCHECK', 'checked'); if ($obj->Beta ?? false) - $this->app->Tpl->Add('MESSAGE','
Dieses Modul ist noch im Beta Stadium.
'); + $this->app->Tpl->addMessage('warning','Dieses Modul ist noch im Beta Stadium'); $this->app->Tpl->Parse('PAGE', 'versandarten_edit.tpl'); } @@ -747,9 +739,9 @@ class Versandarten { { $input = []; $input['bezeichnung'] = $this->app->Secure->GetPOST('bezeichnung'); - $input['typ'] = $this->app->Secure->GetPOST('typ'); + $input['type'] = $this->app->Secure->GetPOST('type'); $input['projekt'] = $this->app->Secure->GetPOST('projekt'); - $input['selmodul'] = $this->app->Secure->GetPOST('selmodul'); + $input['modul'] = $this->app->Secure->GetPOST('modul'); $input['aktiv'] = (int)$this->app->Secure->GetPOST('aktiv'); $input['keinportocheck'] = (int)$this->app->Secure->GetPOST('keinportocheck'); $input['ausprojekt'] = (int)$this->app->Secure->GetPOST('ausprojekt');