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


class DBWorkHLAnwesenheitStatus extends DBWorkGenerated
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, anw_hlid, status';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();

    $tmp->id = $this->getDatamodulValue('id');
    $tmp->status = $this->getDatamodulValue('status');
    $tmp->anw_hlid = $this->getDatamodulValue('anw_hlid');

    return $tmp;
  }

  
  public function getAll($pHlId = 1, $pRefreshCache = false): array
  {
    static $cache = array();
    $hlId = $this->dboperation->getInt4Sql($pHlId);
    if (!$pRefreshCache && isset($cache[$hlId])) {
      return $cache[$hlId];
    }
    $sql = "SELECT $this->select FROM anw_hl_status WHERE anw_hlid=$hlId";
    $this->dboperation->clear();
    $this->dboperation->query($sql);
    $cache[$hlId] = $this->getAllRows();

    return $cache[$hlId];
  }

  
  public function getAllowedStati($pHlId = 1)
  {
    $stati = $this->getAll($pHlId);
    $result = array();
    foreach ($stati as $status) {
      $result[] = $status->status;
    }

    return $result;
  }

  
  public function getStatusVorbelegung()
  {
    $statusarray = $this->getAllowedStati();
    $dbwHlAnw = new DBWorkHLAnwesenheit();
    $tableEmpty = $dbwHlAnw->isTableEmpty();
    if ((empty($statusarray)) && $tableEmpty) {
      $statusarray = ConstantOperations::getStatusangemeldetArraySimple();
    }

    return implode('', $statusarray);
  }
}