<?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';
  }

  function  getFachbereicheMitKursenImSemester(?string $semlist = null)
  {
    $builder = $this->getAllQuery();
    $subqueryBuilder = $this->getSubQuery4Kurse($semlist);
    $builder->and('kennz in ('. $subqueryBuilder->getSQL().')');
    $this->getDvvVersionCheck($builder);
    $builder->getSQL();
    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());
    return $this->getAllRows();
  }

  public function getAllQuery()
  {
    return $this->getSelectQueryBuilder()
       ->select($this->select)
       ->from($this->getTableName().' f')
       ->orderByAsc('kennz');
  }

  public function getSubQuery4Kurse(?string $semlist)
  {
    $result = $this->getSelectQueryBuilder()
        ->select('DISTINCT( k.fachb)')
        ->from('kurse k');

    static $dbwKurseInetAktiv = null;
    if (is_null($dbwKurseInetAktiv)) {
      $dbwKurseInetAktiv = new DBWorkKurseINetAktiv();
    }
    if(is_null($semlist)){
      $semlist = $dbwKurseInetAktiv->objsettings_web->semlist;
    }
    if (($semlist != '') && ($semlist != '\'\'')) {
      
      $result->and( ' k.semester in (' . $this->stringop->convertCommaSeparatedList2InList($semlist) . ')');
    }
    
    $result->and($dbwKurseInetAktiv->getGueltigPart('k.'));

    return $result;
  }

  
  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($sqlBuilder = null)
  {
    $dbwGrundSys = new DBWorkHLGrundSys();
    $dvvVersionSetting = $dbwGrundSys->getFeldWert('DVV_VERSION');
    $dvvVersion = $this->getDvvVersionFromSetting($dvvVersionSetting);
    if (!is_null($sqlBuilder)) {
      $sqlBuilder->and('(dvvneu=:dvvVersion:) ');
      $sqlBuilder->paramInt('dvvVersion', $dvvVersion);
      return $sqlBuilder;
    }

    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();
    }
  }
}