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


class DBWorkKursFach extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = ' kf.id as kfid, kf.knr, kf.fach_nr, kf.fach, kf.bemerkung, kf.lfd, kf.ue, kf.artwp, kf.mindpunkte, kf.bearbeiter, kf.bearbdatum, kf.erfasstvon, kf.erfasstam, kf.nofeedback';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('kfid');
    $tmp->knr = $this->getDatamodulValue('knr');
    $tmp->fach_nr = $this->getDatamodulValue('fach_nr');
    $tmp->fach = $this->getDatamodulValue('fach');
    $tmp->bemerkung = $this->getDatamodulValue('bemerkung');
    $tmp->lfd = $this->getDatamodulValue('lfd');
    $tmp->ue = $this->getDatamodulValue('ue');
    $tmp->artwp = $this->getDatamodulValue('artwp');
    $tmp->mindpunkte = $this->getDatamodulValue('mindpunkte');
    $tmp->bearbeiter = $this->getDatamodulValue('bearbeiter');
    $tmp->bearbdatum = $this->getDatamodulValue('bearbdatum');
    $tmp->erfasstvon = $this->getDatamodulValue('erfasstvon');
    $tmp->erfasstam = $this->getDatamodulValue('erfasstam');
    $tmp->nofeedback = $this->getDatamodulValue('nofeedback');

    return $tmp;
  }

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from('kurs_fach', 'kf')
      ->paramRaw('table', 'kf')
      ->orderByAsc('kf.fach');
  }

  public function getOrigFachIdsByKursFachIds($kursFachIds)
  {
    if (empty($kursFachIds) || !is_array($kursFachIds)) {
      return array();
    }

    $builder = $this->getAllQuery()
      ->select('DISTINCT kf.fach_nr')
      ->and('kf.id IN (:kursFachIds:)')
      ->paramIntArray('kursFachIds', $kursFachIds);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getColumnFromRows('fach_nr');
  }

  
  public function getFaecherByKurs($pKursNummer)
  {
    $kursNummer = $this->dboperation->getString4Sql($pKursNummer);
    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM kurs_fach kf';
    $sql .= ' WHERE kf.knr = \'' . $kursNummer . '\' ORDER BY kf.fach';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  public function getFaecherByKursIdQuery(string $knr): \SelectQueryBuilder
  {
    return $this->getAllQuery()
      ->and('kf.knr = :knr:')
      ->paramString('knr', $knr);
  }

  
  public function getFachIdsByKursId(string $knr): array
  {
    $builder = $this->getFaecherByKursIdQuery($knr)
      ->select('kf.fach_nr');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRowsSingleColumn('fach_nr');
  }

  
  public function getFaecherByKursnummern($pKursNummerAry)
  {
    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM kurs_fach kf';
    $sql .= ' WHERE kf.knr IN(\'' . implode("', '", $pKursNummerAry) . '\') order by kf.fach';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getFachById($pFachID)
  {
    $fachID = $this->dboperation->getInt4Sql($pFachID);
    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM kurs_fach kf';
    $sql .= ' WHERE kf.id= ' . $fachID;
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function getFachByKurstag($pKurstagid)
  {
    $tagId = $this->dboperation->getInt4Sql($pKurstagid);
    $sql = 'SELECT ' . $this->select;
    $sql .= ' FROM kurs_fach kf join kurstage kt on kt.fachid = kf.id';
    $sql .= ' WHERE kt.id  =' . $tagId;
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function canupdate($pVarname)
  {








    return false;
  }

  public function findByFilterQuery(\Kufer\Web\Classes\Dbfilter\KurseFaecherSQLFilter $filter): \SelectQueryBuilder
  {
    $condition = $filter->createSql($this->dboperation);

    return $this->getAllQuery()
      ->and($condition);
  }

  
  public function findByFilter(\Kufer\Web\Classes\Dbfilter\KurseFaecherSQLFilter $filter): array
  {
    $builder = $this->findByFilterQuery($filter);

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQL());

    return $this->getAllRows();
  }

  
  public function countByFilter(\Kufer\Web\Classes\Dbfilter\KurseFaecherSQLFilter $filter): int
  {
    $builder = $this->findByFilterQuery($filter)
      ->select('COUNT(*) AS anzahl');

    $this->dboperation->clear();
    $this->dboperation->query($builder->getSQLForInnerQuery());

    $this->dboperation->selectrow(0);

    return (int)$this->getValue('anzahl', \DatamodulVarType::TYPE_INT);
  }

  public function existsByFilter(\Kufer\Web\Classes\Dbfilter\KurseFaecherSQLFilter $filter): bool
  {
    return $this->countByFilter($filter) > 0;
  }
}