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


class DBWorkFachgebieteVisitenkartenWeb extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, web_visitenkarte_id, sachg';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->web_visitenkarte_id = $this->getDatamodulValue('web_visitenkarte_id');
    $tmp->sachg = $this->getDatamodulValue('sachg');
    $tmp->text = $this->getDatamodulValue('text');

    return $tmp;
  }

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

  
  public function getFachgebietById($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    $sql = 'SELECT ' . $this->select . ' FROM web_fachg_visitenkart a '
            . 'WHERE a.id = ' . $id;
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function getFachgebieteByVkId($pId)
  {
    $sql = 'SELECT wf.web_visitenkarte_id, hl.sachg, hl.text FROM hlsachgebiete hl '
            . 'RIGHT JOIN web_fachg_visitenkart wf ON hl.sachg = wf.sachg '
            . 'WHERE wf.web_visitenkarte_id = ' . $pId . ' ';
    $sql .= $this->getCompleteOrderByDefault('hl.sachg', 'hl');
    if ($this->getOrderbyDefault('id') != 'id') {
      $sql .= ', id';
    }
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getFachgebieteFromVisitenkarten()
  {
    $sql = 'SELECT hl.sachg, hl.text FROM hlsachgebiete hl '
           . 'WHERE EXISTS (SELECT * FROM web_fachg_visitenkart a WHERE hl.sachg = a.sachg)';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function insertFachgebiet($pFachgebiet, $pDontProtocol = false)
  {
    $pFachgebiet->id = $this->insertRecord('web_fachg_visitenkart', 'id', 'sachg');
    if (!$pDontProtocol) {
      $this->saveChanging('web_fachg_visitenkart', '', '', $pFachgebiet->id, 'id', 'I', true);
    }
    $origFachgebiet = $this->getFachgebietById($pFachgebiet->id);
    $this->updateObject($pFachgebiet, $origFachgebiet, 'web_fachg_visitenkart', 'id', 'U', $pDontProtocol, true);

    $this->setActionIDs();

    return $pFachgebiet->id;
  }

  
  public function deleteFachgebiet($pVkrtId, $pSachgebietId, $pDontProtocol = false)
  {
    $vkrtId = $this->dboperation->getInt4Sql($pVkrtId);
    $sachgebietId = $this->dboperation->getInt4Sql($pSachgebietId);

    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM web_fachg_visitenkart WHERE web_visitenkarte_id = ' . $vkrtId . ' AND sachg = ' . $sachgebietId);

    if (!$pDontProtocol) {
      $this->saveChanging('web_fachg_visitenkart', '', '', "'" . $vkrtId . '_' . $sachgebietId . "'", '', 'D');
    }
    $this->setActionIDs();
  }

  
  public function deleteAllFachgebieteByVkrtId($pVkrtId, $pDontProtocol = false)
  {
    $vkrtId = $this->dboperation->getInt4Sql($pVkrtId);
    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM web_fachg_visitenkart WHERE web_visitenkarte_id = ' . $vkrtId);

    if (!$pDontProtocol) {
      $this->saveChanging('web_fachg_visitenkart', '', '', "'" . $vkrtId . "'", '', 'D');
    }
    $this->setActionIDs();
  }
}