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


class DBWorkKurseFreidefFelderAb17 extends DBWork
{
  
  protected $_dataModulFields;

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->_dataModulFields = \array_map(function($i) {
      return 'kursauswahl' . $i;
    }, \range(17, 40));

    $this->select = 'nr, ' . \implode(',', $this->_dataModulFields);
  }

  
  public function getNewObject()
  {
    return new \Kurs();
  }

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

  public function getKursauswahlFields(): array
  {
    return $this->_dataModulFields;
  }

  public function translateVarName($varname)
  {
    switch ($varname) {
      default: return $varname;
      case 'id': return 'nr';
    }
  }

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('nr');
    foreach ($this->_dataModulFields as $varname) {
      $tmp->{$varname} = $this->getDatamodulValue($varname);
    }

    return $tmp;
  }

  public function getAllQuery(): \SelectQueryBuilder
  {
    return $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from($this->getTableName())
      ->orderByAsc('nr');
  }

  
  public function findGeneratorByIds(array $kursIds)
  {
    $builder = $this->getAllQuery()
      ->and('nr IN (:kurseIds:)')
      ->paramStringArray('kurseIds', $kursIds);

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

    yield from $this->getAllRowsGenerator();
  }

  
  public function findByIds(array $kursIds): array
  {
    $builder = $this->getAllQuery()
      ->and('nr IN (:kurseIds:)')
      ->paramStringArray('kurseIds', $kursIds);

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

    return $this->getAllRows();
  }

  public function findById(string $kursId): \Kurs
  {
    $builder = $this->getAllQuery()
      ->and('nr = :kursId:')
      ->paramString('kursId', $kursId);

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

    return $this->getFirstRow();
  }

  
  public function canupdate($varname)
  {
    $matches = null;
    if (\preg_match('/^kursauswahl([0-9]+)$/i', $varname, $matches) === 1) {
      return $matches[1] >= 17; 
    }

    return false; 
  }

  
  public function updateFreidefFeld($kurs, $pDmzBetrieb = false, $pDontProtocol = false)
  {
    $orgkurs = $this->findById($kurs->id);
    if (!empty($orgkurs->id)) {
      $this->updateObject($kurs, $orgkurs, $this->getTableName(), 'id', 'U', $pDontProtocol, $pDmzBetrieb);
      $this->setActionIDs();
    } elseif (!empty($kurs->id)) {
      $cleanId = $this->dboperation->getString4Sql($kurs->id);
      $this->dboperation->clear();
      $this->dboperation->execute('INSERT INTO ' . $this->getTableName() . ' (nr) VALUES (\'' . $cleanId . '\')');

      if (!$pDontProtocol) {
        $this->saveChanging($this->getTableName(), '', '', $kurs->id, 'id', 'I', $pDmzBetrieb);
      }
      $orig = $this->findById($kurs->id);
      $this->updateObject($kurs, $orig, $this->getTableName(), 'id', 'A', $pDontProtocol, $pDmzBetrieb);
      $this->setActionIDs();
    }
  }
}