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


class DBWorkGebiet extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, name';
    $this->_dataModulFields = array('name', 'id');
  }

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

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

  public function getGebieteMitKursenImSemester(?string $semlist = null)
  {
    $builder = $this->getAllQuery();
    $subqueryBuilder = $this->getSubQuery4Kurse($semlist);

    $builder->addToWhere('name in (' . $subqueryBuilder->getSQL() . ')');
    $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() . ' g')
        ->orderByAsc('name');
  }

  public function getSubQuery4Kurse(?string $semlist)
  {
    $result = $this->getSelectQueryBuilder()
        ->select('DISTINCT( k.gebiet)')
        ->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->addToWhere(' k.semester in (' . $this->stringop->convertCommaSeparatedList2InList($semlist) . ')');
    }
    
    $result->addToWhere(' AND ' . $dbwKurseInetAktiv->getGueltigPart('k.'));

    return $result;
  }
}