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


class DBWorkDozFachbereich extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);

    $this->select = 'ID, Kennz, Text, dvvneu, kennz_bvv, kennz_land';
  }

  
  public function _idOk($pID)
  {
    return !empty($pID) && is_numeric($pID);
  }

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

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

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

    $obj->id = $this->getDatamodulValue('ID');
    $obj->kennz = $this->getDatamodulValue('kennz');
    $obj->kennz_bvv = $this->getDatamodulValue('kennz_bvv');
    $obj->kennz_land = $this->getDatamodulValue('kennz_land');
    $obj->text = $this->getDatamodulValue('text');
    $obj->dvvneu = $this->getDatamodulValue('dvvneu');

    return $obj;
  }

  
  private function getDvvVersionCheck()
  {
    $dbwGrundSys = new DBWorkHLGrundSys();
    $dvvVersionSetting = $dbwGrundSys->getFeldWert('DVV_VERSION');
    $dvvVersion = $this->getDvvVersionFromSetting($dvvVersionSetting);

    return ' AND (dvvneu=' . $dvvVersion . ') ';
  }

  
  private function getDvvVersionFromSetting($dvvVersionSetting)
  {
    switch ($dvvVersionSetting) {
      case 0:
        return 0;
      case 1:
      case 2:
        return 1;
    }

    throw new \RuntimeException("Unerwartetes Setting $dvvVersionSetting");
  }

  
  public function getFachbereichById($pID)
  {
    if (!$this->_idOK($pID)) {
      return null;
    }
    $this->dboperation->clear();
    $this->dboperation->query("SELECT $this->select FROM fachber WHERE ID = $pID " . $this->getDvvVersionCheck());

    if ($this->dboperation->rowcount() == 0) {
      return null;
    }

    return $this->getFirstRow();
  }

  
  public function getFachbereichByDNr($pDNr)
  {
    if (!$this->_idOK($pDNr)) {
      return array();
    }
    $this->dboperation->clear();
    $this->dboperation->query("SELECT $this->select FROM fachber fb WHERE fb.kennz IN(Select fad.kennz from fach_adr fad where nr = $pDNr)" . $this->getDvvVersionCheck());

    if ($this->dboperation->rowcount() == 0) {
      return array();
    }

    return $this->getAllRows();
  }

  
  public function getFachbereichAll()
  {
    $this->dboperation->clear();
    $sql = "SELECT $this->select FROM fachber WHERE 1=1 " . $this->getDvvVersionCheck() . 'order by kennz, text';
    $this->dboperation->query($sql);

    if ($this->dboperation->rowcount() == 0) {
      return array();
    }

    return $this->getAllRows();
  }

  
  public function getFachbereichByKursNrs($pKnrs)
  {
    
    if (!is_array($pKnrs) || count($pKnrs) == 0) {
      return array();
    }

    $knrs = $this->dboperation->getStrings4Sql($pKnrs);
    $sql = 'SELECT ' . $this->select . ' FROM fachber f WHERE f.kennz IN ('
            . 'Select k.fachb FROM kurse k WHERE ' . $this->createInStatement('k.nr', $knrs)
            . ' UNION'
            . ' Select substring(k.FACHB, 1, 1) FROM kurse k WHERE ' . $this->createInStatement('k.nr', $knrs) . ')'
            . $this->getDvvVersionCheck()
            . ' ORDER BY f.kennz, f.text';

    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $allRows = $this->getAllRows();
    $result = array();
    $tmpAry = array();

    foreach ($allRows as $row) {
      if (!in_array($row->id, $tmpAry)) {
        $tmpAry[] = $row->id;
        $result[] = $row;
      }
    }

    return $result;
  }

  

  
  public function getFachbereichByKennz($pKennz)
  {
    $kennz = $this->dboperation->getString4Sql($pKennz);
    static $cache = null;
    if (is_null($cache)) {
      $alleFachb = $this->getFachbereichAll();
      $cache = array();
      foreach ($alleFachb as $fachb) {
        $cache[$fachb->kennz] = $fachb;
      }
    }
    if (isset($cache[$kennz])) {
      return $cache[$kennz];
    } else {
      return $this->getNewObject();
    }
  }
}