<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, 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(\Foto $pFoto, bool $pIsDmzBetrieb, ?int $pId = null): int
  {
    if (!isset($pId)) {
      $id = $this->insertRecord('foto', 'id', 'portrait');
    } else {
      $id = $this->insertRecordWithId($pId);
    }
    $pFoto->id = $id;

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

    return $pFoto->id;
  }

  
  public function updatePortrait(\Foto $pFoto, bool $pIsDmzBetrieb): void
  {
    $orgPortrait = $this->getPortraitById($pFoto->id);
    $this->updateObject($pFoto, $orgPortrait, 'foto', 'id', 'U', false, $pIsDmzBetrieb);
    
    $this->setActionIDs();
  }

  
  public function deletePortrait(int $pFotoId, bool $pIsDmzBetrieb): void
  {
    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM foto WHERE id = ' . $pFotoId);
    $this->saveChanging('foto', '', '', "'" . $pFotoId . "'", '', 'D', $pIsDmzBetrieb);
    
    $this->setActionIDs();
  }

  
  public function getPortraitById(int $pFotoId): \Foto
  {
    $queryBuilder = $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from('foto f')
      ->where('f.id = :id:')
      ->paramInt('id', $pFotoId);

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

    return $this->getFirstRow();
  }

  
  public function getPortrait(string $pTyp, int $pNr, bool $pIsKuferWeb = false): \Foto
  {
    $queryBuilder = $this->getSelectQueryBuilder()
      ->select($this->select)
      ->from('foto f')
      ->where('f.typ = :typ:')
      ->and('f.nr = :nr:')
      ->paramString('typ', $pTyp)
      ->paramInt('nr', $pNr);
    if ($pIsKuferWeb) {
      $queryBuilder->and(
        $this->dboperation->dbparser->bitAND('f.portrait_flag', PORTRAIT_FLAG_KUFERWEB)
        . ' = ' . PORTRAIT_FLAG_KUFERWEB
      );
    }

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

    return $this->getFirstRow();
  }

  
  protected function insertRecordWithId(int $pFotoId): int
  {
    $queryBuilder = $this->getInsertIntoQueryBuilder()
      ->insertInto($this->getTableName())
      ->columns(array('id'))
      ->values(array(':id:'))
      ->paramInt('id', $pFotoId);

    $this->dboperation->clear();
    $this->dboperation->execute($queryBuilder->getSQL());

    return $pFotoId;
  }
}