<?php

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

namespace Kufer\KuferTools\Utils\BkBvTnHandler;

class Registrierungen4Bildungsverantwortlichen
{
  private $dbwBkBvTn;
  private $tnrBv;
  private $cacheAlleIds;
  private $cacheAlleDatamodule;

  public function __construct($tnrBv)
  {
    $this->dbwBkBvTn = new \DBWorkZsaBkBvTn(1, true);
    $this->dbwBkBvTn->newSearch();
    $this->tnrBv = $tnrBv;
  }

  public function initFilterFromSession($toolId)
  {
    $searchAPI = $this->getSearchAPI();
    $this->dbwBkBvTn->fromobject = $searchAPI->getFromObjectBySession($this->dbwBkBvTn->fromobject, 'tool' . $toolId);
    $this->dbwBkBvTn->toobject = $searchAPI->getToObjectBySession($this->dbwBkBvTn->toobject, 'tool' . $toolId);
    $this->resetCache();
  }

  public function setStatusFilter($status)
  {
    $this->dbwBkBvTn->fromobject->status = $status;
    $this->resetCache();
  }

  
  public function setTnrFilter($tnr)
  {
    $this->dbwBkBvTn->fromobject->tnr = $tnr;
    $this->resetCache();
  }

  public function getAlleIds()
  {
    if (is_null($this->cacheAlleIds)) {
      $this->cacheAlleIds = $this->dbwBkBvTn->getTnEintraegeIds4Bildungsbeauftragten($this->tnrBv, \SessionOperations::getBvOderBkKuerzel());
    }

    return $this->cacheAlleIds;
  }

  public function getAlleDatamodule()
  {
    if (is_null($this->cacheAlleDatamodule)) {
      $ids = $this->getAlleIds();
      $this->cacheAlleDatamodule = $this->getDatamoduleByIds($ids);
    }

    return $this->cacheAlleDatamodule;
  }

  public function getDatamoduleByIds($pIdAry)
  {
    return $this->dbwBkBvTn->getSome($pIdAry);
  }

  public function getDatamodulById($pId)
  {
    return $this->dbwBkBvTn->getOne($pId);
  }

  public function resetCache()
  {
    $this->cacheAlleIds = null;
    $this->cacheAlleDatamodule = null;
  }

  private function getSearchAPI()
  {
    return new \ZsaBkBvTnSuchAPI();
  }
}