<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */


class SaveSysSettings extends SaveWebSettings
{
  protected function setSetting($pSetting)
  {
    if ($pSetting->config->type === IT_INFOTEXT) {
      return '';
    }

    $this->fillToolSettingIfNecessary($pSetting);

    if ($pSetting->name == 'dokuUploadAllowByExclude') {
      if ($pSetting->value == 1) {
        $this->toolSettingsAry[$pSetting->toolId]->uploadBlacklist4Ext = PostOperations::getVarValue('uploadBlacklist4Ext');
      } else {
        $this->toolSettingsAry[$pSetting->toolId]->uploadWhitelist4Ext = PostOperations::getVarValue('uploadWhitelist4Ext');
      }
    } elseif ($pSetting->name == 'kursFilterVorbelegen' || $pSetting->name == 'kursFilterKursbeginnVorbelegen') {
      $this->toolSettingsAry[$pSetting->toolId] = $this->setFilterKursSettings($pSetting, $this->toolSettingsAry[$pSetting->toolId]);
    } elseif ($pSetting->name == 'anmstand') {
      $this->toolSettingsAry[$pSetting->toolId] = $this->saveGrafikSettings($this->toolSettingsAry[$pSetting->toolId], $pSetting);
    } elseif ($pSetting->name == 'login_err_disable') {
      $this->toolSettingsAry[$pSetting->toolId] = $this->saveLoginErrDisableSettings($this->toolSettingsAry[$pSetting->toolId]);
    }

    if ($pSetting->name == 'menuBarResponsive') {
      $varName = $pSetting->name;
      $value = StringOperations::htmlentities(PostOperations::getVarValue('menuBarJSON'), ENT_NOQUOTES);
      $value = str_replace('[DBQUOTE]', '"', $value);
      $this->toolSettingsAry[$pSetting->toolId]->{$varName} = $value;
    } else {
      $varName = $pSetting->name;
      $value = $pSetting->value;
      if (!$pSetting->config->isHtml) {
        $value = $this->escapeSpecialChars($value);
      }

      $this->toolSettingsAry[$pSetting->toolId]->{$varName} = $value;
    }
    if (is_null($this->toolSettingsAry[$pSetting->toolId]->id) || $this->toolSettingsAry[$pSetting->toolId]->id == '') {
      $this->toolSettingsAry[$pSetting->toolId]->id = $pSetting->toolId;
    }
  }

  private function saveGrafikSettings($pToolsettings, $pSetting)
  {
    switch (PostOperations::getVarValue('radioAnmStatus1', 'grafik')) {
        default:
        case 'text':     $anmstat1 = 'text'; break;
        case 'grafik':   $anmstat1 = 'grafik'; break;
      }
    switch (PostOperations::getVarValue('radioAnmStatus2', 'grafik')) {
        default:
        case 'text':     $anmstat2 = 'text'; break;
        case 'grafik':   $anmstat2 = 'grafik'; break;
      }
    switch (PostOperations::getVarValue('radioAnmStatus3', 'grafik')) {
        default:
        case 'text':     $anmstat3 = 'text'; break;
        case 'grafik':   $anmstat3 = 'grafik'; break;
      }
    switch (PostOperations::getVarValue('radioAnmStatus4', 'grafik')) {
        default:
        case 'text':     $anmstat4 = 'text'; break;
        case 'grafik':   $anmstat4 = 'grafik'; break;
      }

    $checkstat2 = (PostOperations::getVarValue('checkAnmStatus2', 'false') == 'true');
    $checkstat3 = (PostOperations::getVarValue('checkAnmStatus3', 'false') == 'true');
    $checkstat4 = (PostOperations::getVarValue('checkAnmStatus4', 'false') == 'true');

    $pToolsettings = ToolSettingsAPI::getToolSettings(0);
    $pToolsettings->id = $pSetting->toolId;
    $pToolsettings->optionstatus1 = $anmstat1;
    $pToolsettings->optionstatus2 = $anmstat2;
    $pToolsettings->optionstatus3 = $anmstat3;
    $pToolsettings->optionstatus4 = $anmstat4;
    $pToolsettings->optionstatustext1 = PostOperations::getVarValue('textAnmStatus1');
    $pToolsettings->optionstatustext2 = PostOperations::getVarValue('textAnmStatus2');
    $pToolsettings->optionstatustext3 = PostOperations::getVarValue('textAnmStatus3');
    $pToolsettings->optionstatustext4 = PostOperations::getVarValue('textAnmStatus4');
    $pToolsettings->anmstatuscheck2 = $checkstat2;
    $pToolsettings->anmstatuscheck3 = $checkstat3;
    $pToolsettings->anmstatuscheck4 = $checkstat4;
    $pToolsettings->belegschwelleProz = PostOperations::getVarValue('BelegschwelleProz');
    $pToolsettings->belegschwelleZahl = PostOperations::getVarValue('BelegschwelleZahl');
    $pToolsettings->telefonnummerBeiLoginAnforderung = PostOperations::getVarValue('telefonnummerBeiLoginAnforderung');

    return $pToolsettings;
  }

  private function saveLoginErrDisableSettings($pToolsettings)
  {
    $pToolsettings->login_err_disable_value = PostOperations::getVarValue('login_err_disable_value');
    $pToolsettings->login_err_disable_unit = PostOperations::getVarValue('login_err_disable_unit');

    return $pToolsettings;
  }
}