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


class DBWorkWebReplikatSettings extends DBWork
{
  public function __construct($pDBnr = 1, $pNewInstance = false)
  {
    parent::__construct($pDBnr, $pNewInstance);
    $this->select = 'name, value ';
  }

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

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

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

    return $tmp;
  }

  
  public function getAllHighestPkSettings()
  {
    if ($this->dboperation->getDBName() !== 'MYSQL') {
      return array();
    }
    $sql = "SELECT $this->select FROM web_replikat_settings WHERE Lower(name) like 'highestpk%'";
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getReplicatedAt($pDg = '-')
  {
    if (is_numeric($pDg)) {
      $suffix = '_Group' . $this->dboperation->getInt4Sql($pDg);
    } else {
      $suffix = '';
    }

    return $this->getSettingValue('replicatedAt' . $suffix);
  }

  
  public function getReplicatedAtTable($pTable = '')
  {
    if (trim($pTable) != '') {
      $suffix = '_' . $pTable;
    } else {
      $suffix = '';
    }

    return $this->getSettingValuesLike('replicatedAt_Table' . $suffix);
  }

  
  public function getHighestPks()
  {
    return $this->getSettingValuesLike('highestpk');
  }

  
  public function getHighestTNNr()
  {
    return $this->getMaxPK('teilnehm');
  }

  
  public function getHighestDozNr()
  {
    return $this->getMaxPK('dozenten');
  }

  
  public function getHighestAdrNr()
  {
    return $this->getMaxPK('adressen');
  }

  
  public function getHighestTnNotenId($pRefreshCache = false)
  {
    
    static $result = null;
    if ($result != null && !$pRefreshCache) {
      return $result;
    }

    $result = $this->getMaxPK('tn_noten');

    return $result;
  }

  
  public function getHighestKTid()
  {
    return $this->getMaxPK('kurstage');
  }

  
  public function getSettingValue($pSettingName)
  {
    if ($this->dboperation->getDBName() !== 'MYSQL') {
      return $this->getNewObject();
    }
    static $cache = null;
    if (is_null($cache)) {
      
      $this->dboperation->clear();
      $this->dboperation->query("SHOW TABLES LIKE 'web_replikat_settings'");
      if ($this->dboperation->rowcount() == 0) {
        $dm = $this->getNewObject();
        $dm->name = $pSettingName;

        return $dm;
      }
      $sql = 'SELECT ' . $this->select . ' FROM web_replikat_settings';
      $this->dboperation->clear();
      $this->dboperation->query($sql);
      $allRows = $this->getAllRows();
      $cache = ArrayOperations::setAttributAsKey($allRows, 'name');
    }
    if (isset($cache[$pSettingName])) {
      return $cache[$pSettingName];
    } else {
      $dm = $this->getNewObject();
      $dm->name = $pSettingName;

      return $dm;
    }
  }

  
  public function getSettingValuesLike($pSettingName)
  {
    
    $sql = 'SELECT ' . $this->select . ' FROM web_replikat_settings '
      . 'WHERE name like \'%' . $pSettingName . '%\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function isOldTN($tnr)
  {
    if (!is_numeric($tnr) || ($tnr < 0)) {
      return false;
    }

    $highestTnrSetting = $this->getHighestTNNr();

    return ($tnr <= $highestTnrSetting->value);
  }

  
  public function isOldDoz($dnr)
  {
    if (!is_numeric($dnr) || ($dnr < 0)) {
      return false;
    }

    $highestDnrSetting = $this->getHighestDozNr();

    return ($dnr <= $highestDnrSetting->value);
  }

  
  public function getMaxPK($tablename)
  {
    if ($this->dboperation->getDBName() !== 'MYSQL') {
      return $this->getNewObject();
    }
    $tablename = StringOperations::strtolower($tablename);

    $sql = "SELECT value FROM web_replikat_settings WHERE Lower(name) like 'highestpk$tablename:%'";
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $this->dboperation->selectrow(0);

    return $this->getFirstRow();
  }

  
  public function canupdate($varname)
  {
    return false;
  }
}