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


class DBWorkFoto extends DBWork
{
  protected static $_cache_fotos = array();

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $tmp_select = ' f.id, f.typ, f.nr, f.knr';

    for ($i = 1; $i <= 6; $i++) {
      $tmp_selecti = $tmp_select . ', f.untertitel' . $i . ' AS untertitel, '
                      . $this->clob('f.beschreibung_bf' . $i, 'beschreibung_bf') . ', '
                      . $this->clob('f.copyright' . $i, 'copyright') . ', '
                      . $this->clob('f.bezugsquelle' . $i, 'bezugsquelle');
      $var = 'select' . $i;
      $this->{$var} = $tmp_selecti;
    }
  }

  
  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->knr = $this->getDatamodulValue('knr');
    $tmp->untertitel = $this->getDatamodulValue('untertitel');
    $tmp->beschreibung_bf = $this->getDatamodulValue('beschreibung_bf');
    $tmp->copyright = $this->getDatamodulValue('copyright');
    $tmp->bezugsquelle = $this->getDatamodulValue('bezugsquelle');

    return $tmp;
  }

  
  public function getID($pTyp, $pNr)
  {
    $nr = $this->dboperation->getString4Sql($pNr);
    if (StringOperations::strtoupper($pTyp) === 'K') {
      $sql = 'SELECT id FROM foto f WHERE typ=\'K\' and knr = \'' . $nr . '\'';
    } else {
      $typ = $this->dboperation->getString4Sql($pTyp);
      $sql = 'SELECT id FROM foto f WHERE typ=\'' . $typ . '\' and nr = \'' . $nr . '\'';
    }
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    if ($this->dboperation->rowcount() > 0) {
      $this->dboperation->selectrow(0);

      return $this->getDatamodulValue('id');
    } else {
      return false;
    }
  }

  
  public function getFotoNummern($pTyp, $pNr)
  {
    $nr = $this->dboperation->getString4Sql($pNr);

    $maxIndexFotos = 6;

    $sql = 'SELECT id';
    for ($i = 1;$i <= $maxIndexFotos;$i++) {
      $sql .= ', ' . $this->dboperation->dbparser->isNull('anzeigeweb' . $i, "'F'") . ' as ' . 'anzeigeweb' . $i;
    }
    $sql .= ' FROM foto f ';
    if (StringOperations::strtoupper($pTyp) === 'K') {
      $sql .= 'WHERE typ=\'K\' and knr = \'' . $nr . '\'';
    } else {
      $typ = $this->dboperation->getString4Sql($pTyp);
      $sql .= ' WHERE typ=\'' . $typ . '\' and nr = \'' . $nr . '\'';
    }
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    if ($this->dboperation->rowcount() > 0) {
      $this->dboperation->selectrow(0);
      
      $result = array();
      for ($i = 1;$i <= $maxIndexFotos;$i++) {
        if ($this->getDatamodulValue('anzeigeweb' . $i) == 'W') {
          $result[] = $i;
        }
      }

      return $result;
    } else {
      return false;
    }
  }

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

    if ($pTyp == 'K') {
      $nrCol = 'knr';
    } else {
      $nrCol = 'nr';
    }

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

    $key = $typ . '|' . $nr . '|' . $lfdNr;

    if ($RefreshCache || !array_key_exists($key, self::$_cache_fotos)) {
      $selectVar = 'select' . $lfdNr;
      $this->dboperation->clear();
      $sql = 'SELECT ' . $this->{$selectVar} . "
            FROM foto f
            WHERE f.typ='" . $typ . "' and
                  f." . $nrCol . " = '" . $nr . "'";
      if ($typ !== 'B' && $typ !== 'T') {
        $sql .= ' AND ' . $this->dboperation->dbparser->isNull('f.anzeigeweb' . $lfdNr, '\'F\'') . ' = \'W\'';
      }
      $this->dboperation->query($sql);

      $foto = $this->getFirstRow();

      $foto->setValue2Var('lfdNr', $lfdNr);

      self::$_cache_fotos[$key] = $foto;
    }

    return self::$_cache_fotos[$key];
  }
}