<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */


namespace Kufer\Web\KuferTools\Basis\KursUebersicht\Detailsreiter\Honorar\AddDozent;






abstract class AddDozentDataTableUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request, $response = null)
  {
    parent::__construct($request, $response ?? new AddDozentDataTableResponse());
  }

  abstract protected function getReiterDozentenId(): int;

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    }
    if (!$this->isRechtAllowed(static::getToolSectionId() . '.0.2') && !$this->isRechtAllowed($this->getReiterDozentenId() . '.0.2')) {
      return false;
    }

    return true;
  }

  public function action()
  {
    
    

    $jsonObject = $this->_handleAjaxAddDozentDT();

    return $this->getResponse($jsonObject);
  }

  
  private function _handleAjaxAddDozentDT()
  {
    $dbwDoz = new \DBWorkDozenten();
    $dbwDozHon = new \DBWorkDozentenHonorar(1, true);

    
    
    
    
    
    $aColumns = $this->request->aColumns;
    $aColumnsCnt = count($aColumns);

    
    
    $iSortCol_0 = $this->request->iSortCol_0;
    if ($iSortCol_0 != '') {
      
      for ($i = 0; $i < $this->request->iSortingCols; $i++) {
        $iSortCol = $this->request->iSortColAry[$i];
        
        
        if ($this->request->bSortableAry[$i] == 'true') {
          
          $dbwDoz->orderby = $aColumns[$iSortCol];
          $dbwDoz->orderbyasc = $this->request->sSortDirAry[$i] === 'asc';
        }
      } 
    } 


    
    
    
    $dbwDoz->newSearch();
    for ($i = 0; $i < $aColumnsCnt; $i++) {
      $searchValue = $this->request->sSearchAry[$i];
      
      
      
      
      if ($searchValue != '') {
        switch ($aColumns[$i]) {
          default:
          case 'id':
            $dbwDoz->fromobject->id = $searchValue;
            
            $dbwDoz->toobject->id = '';
            break;
          case 'name':
            $dbwDoz->fromobject->name = $searchValue;
            $dbwDoz->toobject->name = '';
            break;
          case 'vorname':
            $dbwDoz->fromobject->vorname = $searchValue;
            $dbwDoz->toobject->vorname = '';
            break;
          case 'strasse':
            $dbwDoz->fromobject->strasse = $searchValue;
            $dbwDoz->toobject->strasse = '';
            break;
          case 'ort': 
            $dbwDoz->fromobject->ort = $searchValue;
            $dbwDoz->toobject->ort = '';
            break;
            
          case 'nurplz':
            
            
            
            
            break;
        } 
      } 
    } 
    
    
    
    $dozenten = $dbwDoz->getDozenten();

    $dozObjAry = array();

    
    
    $vorhandeneDozHonorare = $dbwDozHon->getHonorarebyKNR($this->request->knr);
    $vorhDozNr = array();
    foreach ($vorhandeneDozHonorare as $vorhDozHon) {
      $vorhDozNr[] = $vorhDozHon->dnr;
    }

    

    if (is_array($dozenten)) {
      foreach ($dozenten as $dozent) {
        
        if (!in_array($dozent->id, $vorhDozNr)) {
          
          $dozObj = array('id' => $dozent->id, 'name' => $dozent->name, 'vorname' => $dozent->vorname, 'strasse' => $dozent->strasse,
                  'ort' => $dozent->ort, 'tel_anzeige' => $dozent->tel_anzeige, 'email_anzeige' => $dozent->email_anzeige,
                  'nurplz' => $dozent->nurplz);
          $dozObjAry[] = $dozObj;
        }
      } 
    } 

    
    $iTotal = count($dozObjAry);
    
    $iFilteredTotal = count($dozObjAry); 


    
    
    $sEcho = $this->request->sEcho;

    
    $output = array(
      'sEcho' => $sEcho,           
      'iTotalRecords' => $iTotal,
      'iTotalDisplayRecords' => $iFilteredTotal,
      'aaData' => $dozObjAry        
    );

    
    return \JsonOperations::encode($output);
    
    
  }

  

  protected function getResponse($pJsonObject)
  {
    $this->handleResponseStrategies();
    
    
    $this->response->jsonObject = $pJsonObject;

    return $this->response;
  }

  protected function getResponseStrategies()
  {
    return array();
  }
}

class AddDozentDataTableRequest
{
  public $knr;  
  public $sEcho;
  public $iDisplayStart;
  public $iDisplayLength;
  public $iSortCol_0; 
  public $iSortColAry;  
  public $iSortingCols; 
  public $bSortableAry; 
  public $sSortDirAry; 
  public $sSearchAry;  
  public $aColumns;
  
}

class AddDozentDataTableResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
  public $jsonObject;
}