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

class DBWorkDozenten2 extends DBWorkGenerated
{
  
  private $_personExpander;

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $dozObj = $this->getNewObject();
    $this->_personExpander = new ExpandPersonData('dozenten2', $dozObj, $this);

    $this->_personExpander->addField('kirchengemeinde');
    $this->_personExpander->addField('kirchenbezirk');
    $this->_personExpander->addField('vertrag_digital');
    $this->_personExpander->addField('gemeinde');
    $this->_personExpander->addField('stadtteil');
    $this->_personExpander->addField('bezirk');
    $this->_personExpander->addField('bundesland');

    $this->_dataModulFields = $this->_personExpander->getDataModulFields();
    $this->select = $this->_personExpander->select;
  }

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

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

  
  public function canupdate($varname)
  {
    
    $noupdate = array('nr', 'id');
    if (in_array($varname, $noupdate)) {
      return false;
    }
    
    return in_array($varname, $this->_dataModulFields);
  }

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

  
  public function getByIds($pIdAry, $pDozentenAry = null, $pRefreshCache = false)
  {
    return $this->_personExpander->getByIds($pIdAry, $pDozentenAry, $pRefreshCache);
  }

  
  public function insertOrUpdateDozent2($pDozent, $pDontProtocol = false, $pDmzBetrieb = false)
  {

    
    
    
    $orig = $this->getOne($pDozent->id, 'nr', '', false);

    if ($orig->isEmpty()) {
      $this->insertDozent2($pDozent, $pDontProtocol, $pDmzBetrieb);
    } else {
      $this->updateDozent2($pDozent, $pDontProtocol, $pDmzBetrieb);
    }

    return $pDozent->id;
  }

  
  public function updateDozent2($pDozent, $pDontProtocol = false, $pDmzBetrieb = false)
  {
    $orig = $this->getOne($pDozent->id, 'nr', '', false);
    if ($orig->isEmpty()) {
      throw new InvalidArgumentException('In Tabelle dozenten2 gibt es keinen Datensatz mit Nr ' . $pDozent->id);
    }
    $orig->id = $pDozent->id;
    $this->updateObject($pDozent, $orig, 'dozenten2', 'id', 'U', $pDontProtocol, $pDmzBetrieb);
    $this->setActionIDs();
  }

  
  public function insertDozent2($pDozent, $pDontProtocol = false, $pDmzBetrieb = false)
  {
    $cleanId = $this->dboperation->getInt4Sql($pDozent->id);
    $this->dboperation->clear();
    $this->dboperation->execute('INSERT INTO ' . $this->getTableName() . ' (nr) VALUES (' . $cleanId . ')');
    if (!$pDontProtocol) {
      $this->saveChanging('dozenten2', '', '', $pDozent->id, 'id', 'I', $pDmzBetrieb);
    }

    $orig = $this->getOne($pDozent->id, 'nr', '', false);

    $this->updateObject($pDozent, $orig, 'dozenten2', 'id', 'A', $pDontProtocol, $pDmzBetrieb);
    $this->setActionIDs();

    return $pDozent->id;
  }
}