Refactor and improve Versandarten settings

This commit is contained in:
Andreas Palm 2022-10-27 13:17:19 +02:00
parent cd93d643f8
commit 1ddee4350c
6 changed files with 172 additions and 227 deletions

View File

@ -163,28 +163,17 @@ class TemplateParser {
public function addMessage($class, $text, $html = false, $_var = 'MESSAGE')
{
$ret = '';
switch($class)
{
case 'error':
case 'warning':
case 'info':
break;
default:
if(!in_array($class, ['error', 'warning', 'info', 'success']))
$class = 'info';
break;
}
if(!$html)
{
$text = $this->htmlspecialchars($text);
}
$ret .= '<div class="'.$class.'">'.$text.'</div>';
$ret = sprintf('<div class="%s">%s</div>', $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 = '')

View File

@ -1,9 +1,31 @@
<?php
abstract class Versanddienstleister {
/** @var int $id */
public $id;
/** @var Application $app */
public $app;
protected int $id;
protected Application $app;
protected string $type;
protected int $projectId;
protected ?int $labelPrinterId;
protected ?int $documentPrinterId;
protected bool $shippingMail;
protected ?int $businessLetterTemplateId;
protected object $settings;
public function __construct(Application $app, ?int $id)
{
$this->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')
{
if(!$this->id)
{
return '';
public function AdditionalSettings(): array {
return [];
}
//$id = $this->id;
$struktur = $this->EinstellungenStruktur();
/**
* 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
{
$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,100 +237,52 @@ 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($form[$name]))
{
if(isset($val['default']) && !isset($json[$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 .= '<tr><td colspan="2"><b>'.html_entity_decode($val['heading']).'</b></td></tr>';
}
$html .= '<tr><td>'.($first?'<input type="hidden" name="modul_name" value="'.$this->app->DB->Select("SELECT modul FROM versandarten WHERE id = '$id' LIMIT 1").'" />':'').(empty($val['bezeichnung'])?$name:$val['bezeichnung']).'</td><td>';
$typ = 'text';
if(!empty($val['typ']))
{
$typ = $val['typ'];
}
$html .= '<tr><td>'.($val['bezeichnung'] ?? $name).'</td><td>';
if(isset($val['replace']))
{
switch($val['replace'])
{
case 'lieferantennummer':
$json[$name] = $this->app->erp->ReplaceLieferantennummer(0,$json[$name],0);
if($target !== 'return')
{
$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')
{
$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;
}
}
/*if(!isset($json[$name]) && isset($val['default']))
{
$json[$name] = $val['default'];
}*/
switch($typ)
switch($val['typ'] ?? 'text')
{
case 'textarea':
$html .= '<textarea name="'.$name.'" id="'.$name.'">'.(!isset($json[$name])?'':$json[$name]).'</textarea>';
$html .= '<textarea name="'.$name.'" id="'.$name.'">'.($form[$name] ?? '').'</textarea>';
break;
case 'checkbox':
$html .= '<input type="checkbox" name="'.$name.'" id="'.$name.'" value="1" '.((isset($json[$name]) && $json[$name])?' checked="checked" ':'').' />';
$html .= '<input type="checkbox" name="'.$name.'" id="'.$name.'" value="1" '.($form[$name] ?? false ? ' checked="checked" ':'').' />';
break;
case 'select':
$html .= '<select name="'.$name.'">';
if(isset($val['optionen']) && is_array($val['optionen']))
{
foreach($val['optionen'] as $k => $v)
{
$html .= '<option value="'.$k.'"'.($k == (isset($json[$name])?$json[$name]:'')?' selected="selected" ':'').'>'.$v.'</option>';
}
}
$html .= '</select>';
$html .= $this->app->Tpl->addSelect('return', $name, $name, $val['optionen'], $form[$name]);
break;
case 'submit':
if(isset($val['text']))
{
$html .= '<form method="POST"><input type="submit" name="'.$name.'" value="'.$val['text'].'"></form>';
}
break;
case 'custom':
if(isset($val['function']))
@ -307,33 +295,29 @@ abstract class Versanddienstleister {
}
break;
default:
$html .= '<input type="text" '.(!empty($val['size'])?' size="'.$val['size'].'" ':'').' '.(!empty($val['placeholder'])?' placeholder="'.$val['placeholder'].'" ':'').' name="'.$name.'" id="'.$name.'" value="'.(!isset($json[$name])?'':htmlspecialchars($json[$name])).'" />';
$html .= '<input type="text"'
.(!empty($val['size'])?' size="'.$val['size'].'"':'')
.(!empty($val['placeholder'])?' placeholder="'.$val['placeholder'].'"':'')
.' name="'.$name.'" id="'.$name.'" value="'.(isset($form[$name])?htmlspecialchars($form[$name]):'').'" />';
break;
}
if(isset($val['info']) && $val['info'])$html .= ' <i>'.$val['info'].'</i>';
if(isset($val['info']) && $val['info'])
$html .= ' <i>'.$val['info'].'</i>';
$html .= '</td></tr>';
$first = false;
}
if($target === 'return') {
return $html;
}
$this->app->Tpl->Add($target, $html);
return '';
}
protected abstract function EinstellungenStruktur();
/**
* @param string $target
*
* @return 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(string $target = ''): bool
public function CheckInputParameters(array &$form): array
{
return true;
return [];
}

View File

@ -99,7 +99,7 @@ class Versandart_intraship extends Versanddienstleister{
return 'DHL Instrahship';
}
function EinstellungenStruktur()
function AdditionalSettings()
{
return array(

View File

@ -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 [

View File

@ -14,15 +14,13 @@
<td>{|Bezeichnung|}:</td>
<td>
<input type="text" name="bezeichnung" value="[BEZEICHNUNG]" size="40"
data-lang="versandart_bezeichnung_[ID]">
<span style="color:red">[MSGBEZEICHNUNG]</span>
data-lang="versandart_bezeichnung_[ID]" required>
</td>
</tr>
<tr>
<td>{|Typ|}:</td>
<td>
<input type="text" name="typ" value="[TYP]" size="40">
<span style="color:red">[MSGTYP]</span>
<input type="text" name="type" value="[TYPE]" size="40" required>
<i>{|z.B. dhl,ups,etc.|}</i>
</td>
</tr>
@ -64,7 +62,7 @@
<td>{|Textvorlage|}:</td>
<td>[SELGESCHAEFTSBRIEF_VORLAGE]</td>
</tr>
[JSON]
[MODULESETTINGS]
</table>
</fieldset>
<input type="submit" name="speichern" value="{|Speichern|}" id="speichern" style="float:right"/>

View File

@ -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&uuml;ck zur &Uuml;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', "<div class=\"success\">Die Daten wurden erfolgreich gespeichert!</div>");
$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','<div class="info">Dieses Modul ist noch im Beta Stadium.</div>');
$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');