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


class DBWorkPortrait extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'f.id, f.typ, f.nr, f.portrait, f.portrait_inArbeit, portrait_flag ';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->typ = $this->getDatamodulValue('typ');
    $tmp->nr = $this->getDatamodulValue('nr');
    $tmp->portrait = $this->getDatamodulValue('portrait');
    $tmp->portrait_inArbeit = $this->getDatamodulValue('portrait_inArbeit');
    $tmp->portrait_flag = $this->getDatamodulValue('portrait_flag');

    return $tmp;
  }

  
  public function insertPortrait($pPortrait, $pIsDmzBetrieb, $pId = null)
  {
    if (!isset($pId)) {
      $id = $this->insertRecord('foto', 'id', 'portrait', false);
    } else {
      $id = $this->insertRecordWithId($pId);
    }
    $pPortrait->id = $id;

    $this->saveChanging('foto', '', '', $pPortrait->id, 'id', 'I');
    $orgPortrait = $this->getPortraitById($pPortrait->id);
    $this->updateObject($pPortrait, $orgPortrait, 'foto', 'id', 'A', false, $pIsDmzBetrieb);
    
    $this->setActionIDs();

    return $pPortrait->id;
  }

  
  public function updatePortrait($pPortrait, $pIsDmzBetrieb)
  {
    $orgPortrait = $this->getPortraitById($pPortrait->id);
    $this->updateObject($pPortrait, $orgPortrait, 'foto', 'id', 'U', false, $pIsDmzBetrieb);
    
    $this->setActionIDs();
  }

  
  public function deletePortrait($pPortraitId, $pIsDmzBetrieb)
  {
    $portraitId = $this->dboperation->getInt4Sql($pPortraitId);
    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM foto WHERE id = ' . $portraitId);
    $this->saveChanging('foto', '', '', "'" . $portraitId . "'", '', 'D', $pIsDmzBetrieb);
    
    $this->setActionIDs();
  }

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

    return $this->getFirstRow();
  }

  
  public function getPortrait($pTyp, $pNr, $pIsKuferWeb = false)
  {
    if (is_null($pTyp) || $pTyp === '' || is_null($pNr) || $pNr === '') {
      return $this->getNewObject();
    }

    $typ = $this->dboperation->getString4Sql($pTyp);
    $nr = $this->dboperation->getString4Sql($pNr);

    $this->dboperation->clear();
    $sql = 'SELECT ' . $this->select . ' '
           . 'FROM foto f '
           . "WHERE f.typ='" . $typ . "' "
           . "AND f.nr = '" . $nr . "'";
    if ($pIsKuferWeb) {
      $sql .= ' AND ' . $this->dboperation->dbparser->bitAND('f.portrait_flag', PORTRAIT_FLAG_KUFERWEB) . ' = ' . PORTRAIT_FLAG_KUFERWEB;
    }

    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  protected function insertRecordWithId($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    $this->dboperation->clear();
    $this->dboperation->execute('INSERT INTO foto (id) VALUES (' . $id . ')');

    return $id;
  }
}