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



class DBWorkDozentenBewerbung extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'dzb.id, dzb.ktid, dzb.doznr, dzb.datum, dzb.status, dzb.kursnr, ' . $this->clob('dzb.ziel') . ', ' . $this->clob('dzb.info') . ', ' . $this->clob('dzb.methode') . ', dzb.material, ' . $this->clob('dzb.bemerkung');
  }

  
  public function getNewObject()
  {
    $dozBewergung = new DozentenBewerbung();
    $dbwKurse = new DBWorkKurseBase();
    $dozBewergung->kurs = $dbwKurse->getNewObject();

    return $dozBewergung;
  }

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->ktid = $this->getDatamodulValue('ktid');
    $tmp->doznr = $this->getDatamodulValue('doznr');
    $tmp->datum = $this->getDatamodulValue('datum');
    $tmp->status = $this->getDatamodulValue('status');
    $tmp->kursnr = $this->getDatamodulValue('kursnr');
    $tmp->ziel = $this->getDatamodulValue('ziel');
    $tmp->info = $this->getDatamodulValue('info');
    $tmp->methode = $this->getDatamodulValue('methode');
    $tmp->material = $this->getDatamodulValue('material');
    $tmp->bemerkung = $this->getDatamodulValue('bemerkung');

    return $tmp;
  }

  protected function postGetAllRowsHook($pDatamodules, $pRefreshCache = false)
  {
    $datamodules = parent::postGetAllRowsHook($pDatamodules, $pRefreshCache);

    
    $kureByKnr = array();
    if (isset($this->dbwkurse)) {
      $kursnummern = ArrayOperations::mapAttribut($datamodules, 'knr');
      $kurse = $this->dbwkurse->getKurseByKursnummernKeepOrder($kursnummern, $pRefreshCache);
      $kureByKnr = ArrayOperations::setAttributAsKey($kurse, 'id');
    }

    foreach ($datamodules as $key => $dozBewerbung) {
      if (isset($kureByKnr[$dozBewerbung->kursnr])) {
        $datamodules[$key]->kurs = $kureByKnr[$dozBewerbung->kursnr];
      }
    }

    return $datamodules;
  }

  
  public function canupdate($varname)
  {
    switch ($varname) {
      default:
        return parent::canupdate($varname);
      case 'kurs':
      case 'id': return false;
    }
  }

  
  public function getBewerbungen()
  {
    $this->dboperation->clear();
    $sql = 'SELECT ' . $this->select . " \n
            FROM dozbewerbung dzb ";
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getBewerbungen4DozentExists($pDoz_Id, $pKt_Id)
  {
    $doz_id = $this->dboperation->getInt4Sql($pDoz_Id);
    $kt_id = $this->dboperation->getInt4Sql($pKt_Id);
    $this->dboperation->clear();
    $sql = 'SELECT ' . $this->select . " \n
            FROM dozbewerbung dzb \n
            WHERE dzb.doznr = " . $doz_id . ' AND dzb.ktid = ' . $kt_id;
    $this->dboperation->query($sql);

    return ($this->dboperation->rowcount() > 0);
  }

  public function getBewerbungen4DozentAndKurs($pDnr, $pKnr)
  {
    $cleanDnr = $this->dboperation->getInt4Sql($pDnr);
    $cleanKnr = $this->dboperation->getString4Sql($pKnr);
    $this->dboperation->clear();
    $sql = 'SELECT ' . $this->select . " \n
            FROM dozbewerbung dzb \n
            WHERE dzb.doznr = " . $cleanDnr . " AND dzb.kursnr = '" . $cleanKnr . "'";
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getBewerbungen4Kurstag($pKt_Id)
  {
    $kt_id = $this->dboperation->getInt4Sql($pKt_Id);
    $this->dboperation->clear();
    $sql = 'SELECT ' . $this->select . ' FROM dozbewerbung WHERE ktid = ' . $kt_id;
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getBewerbung4KurstagExists($pkt_id)
  {
    return (count($this->getBewerbungen4Kurstag($pkt_id)) > 0);
  }

  
  public function getBewerbung($pID)
  {
    $id = $this->dboperation->getInt4Sql($pID);
    $this->dboperation->clear();
    $sql = 'SELECT ' . $this->select . " \n
            FROM dozbewerbung dzb \n
            WHERE id=" . $id;
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function insertBewerbung($dozbewerbung)
  {
    $dozbewerbung->id = $this->insertRecord('dozbewerbung', 'id', 'Doznr');
    $this->saveChanging('dozbewerbung', '', '', $dozbewerbung->id, 'id', 'I');
    $orgdozbewerbung = $this->getBewerbung($dozbewerbung->id);
    $this->updateObject($dozbewerbung, $orgdozbewerbung, 'dozbewerbung', 'id', 'A');
    $this->setActionIDs();

    return $dozbewerbung->id;
  }
}