<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */


class DBWorkHLGrundSys extends DBWork
{
  
  public const KEY_HYGIENEKONZEPT = 'ZUSTIMMUNG1_ANZEIGE';

  public const KEY_ONLINE_VERTRAEGE = 'OnlineVertraege';

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'feld, wert';
  }

  
  public function getNewObject()
  {
    return new HLGrundSys();
  }

  public function getTableName(): string
  {
    return 'hlgrundsys';
  }

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();

    $tmp->feld = $this->getDatamodulValue('feld');
    $tmp->wert = $this->getDatamodulValue('wert');

    return $tmp;
  }

  public function getSQLValue($fieldname, $datamodul)
  {
    switch ($fieldname) {
      default:
        return $datamodul->getVarValue($fieldname);
    }
  }

  
  public function getSetting($pFeld, $pRefreshCache = false)
  {
    $settingsAry = $this->getSettingsAry(array($pFeld), $pRefreshCache);
    if (isset($settingsAry[$pFeld])) {
      return $settingsAry[$pFeld];
    } else {
      return false;
    }
  }

  
  public function getSettingsAry($pGesuchteFelder, $pRefreshCache = false)
  {
    if (!is_array($pGesuchteFelder)) {
      return array();
    }


    static $cache = array();
    
    
    if (!$pRefreshCache) {
      $felderImCache = array_keys($cache);
      $felderAusDbHolen = array_diff($pGesuchteFelder, $felderImCache);
    } else {
      $felderAusDbHolen = $pGesuchteFelder;
    }

    $felderAusDbHolenLowerCase = array_map(static function(string $str) {
      return \StringOperations::strtolower($str);
    }, $felderAusDbHolen);

    if (count($felderAusDbHolen) > 0) {
      $settingsFromDb = $this->_getSettingsFromDb($felderAusDbHolen);
      foreach ($settingsFromDb as $setting) {
        
        $lcfeld = \StringOperations::strtolower($setting->feld);
        if (in_array($lcfeld, $felderAusDbHolenLowerCase)) {
          $cache[$lcfeld] = $setting;
        }
      }
    }

    $result = array();
    foreach ($pGesuchteFelder as $feld) {
      $lcfeld = \StringOperations::strtolower($feld);
      if (isset($cache[$lcfeld])) {
        $result[$feld] = $cache[$lcfeld];
      } else {
        $result[$feld] = $cache[$lcfeld] ?? false;
      }
    }

    return $result;
  }

  
  private function _getSettingsFromDb($pGesuchteFelder)
  {
    $cleanGesuchteFelder = array_map(array($this->dboperation, 'getString4Sql'), $pGesuchteFelder);
    $feldListe = '\'' . implode('\', \'', $cleanGesuchteFelder) . '\'';

    $sql = "SELECT $this->select FROM hlgrundsys WHERE feld IN($feldListe)";
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getFeldWert($pFieldName, $pDefaultWert = '')
  {
    $hlgrundsys = $this->getSetting($pFieldName);
    if (null == $hlgrundsys || null == $hlgrundsys->wert) {
      return $pDefaultWert;
    } else {
      return $hlgrundsys->wert;
    }
  }

  public function isFeldEinerDerWerte($field, ...$werte)
  {
    $fieldval = $this->getFeldWert($field);
    foreach ($werte as $wert) {
      if ($fieldval === $wert) {
        return true;
      }
    }

    return false;
  }

  public function webAnmeldungZaehltAlsMandat()
  {
    $fieldValue = $this->getFeldWert('EMailXML_AlsMandat', 'F');
    if (System::isKuferSql5()) {
      return $fieldValue == '1' || $fieldValue == 'W';
    } else {
      return $fieldValue == 'W';
    }
  }

  public function digitalesWeitermeldenZaehltAlsMandat(): bool
  {
    $fieldValue = $this->getFeldWert('DigitalesWeitermelden_Mandat_Setzen', 'F');

    return $fieldValue == 'W';
  }

  public function anwSpeichernSetztEintrittAustritt(): bool
  {
    return $this->getFeldWert('ANW_HISTORIE') != 'W';
  }
}