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

namespace Kufer\Web\KuferTools\Utils\DatamodulHelper\Kurse;

class MitarbeiterAnmeldungKurse extends KursHelper
{
  private $columns;

  public function __construct($toolSettings, $user, $columns)
  {
    parent::__construct($toolSettings, $user);
    $this->columns = $columns;
  }

  
  protected function getKursnummern($dbwKurse)
  {
    if ($this->toolSettings->useBereichsRechte) {
      return $dbwKurse->getKursIdsByBereichsrechte($this->user->getIdentifier());
    } else {
      return $dbwKurse->getKursIds();
    }
  }

  protected function setzeFilter($dbwKurse, $pToolId, $pRequestSuchParam, $pRequestEinstieg, $pSetDbwOrderBySettings)
  {
    $searchAPI = new \KurssuchAPIwSearch(isset($this->toolSettings->teilnehmerSuche) && $this->toolSettings->teilnehmerSuche);
    $dbwKurse->fromobject = $searchAPI->getFromObjectBySession($dbwKurse->fromobject, 'tool' . $pToolId);
    $dbwKurse->toobject = $searchAPI->getToObjectBySession($dbwKurse->toobject, 'tool' . $pToolId);

    $filterKursnummer = \RequestOperations::getVarValue('filterKursnummer', '');
    if (trim($filterKursnummer) !== '') {
      $dbwKurse->fromobject->id = \StringOperations::replaceWildcards($filterKursnummer);
      if (\StringOperations::strpos($filterKursnummer, '?') !== false || \StringOperations::strpos($filterKursnummer, '*') !== false) {
        $dbwKurse->toobject->id = '';
      } else {
        $dbwKurse->toobject->id = $dbwKurse->fromobject->id . 'ZZ';
      }
    }

    return $dbwKurse;
  }

  protected function setzeSortierung($dbwKurse, $pRequestEinstieg = false, $pSortKriteria = array())
  {
    $dbwKurse = parent::setzeSortierung($dbwKurse);
    $columns = $this->columns;
    if (\RequestOperations::isDefined('iSortCol_0')) {
      $iSortingCols = intval(\RequestOperations::getVarValue('iSortingCols'));
      for ($i = 0; $i < $iSortingCols; $i++) {
        $iSortCol = intval(\RequestOperations::getVarValue('iSortCol_' . $i));
        if (\RequestOperations::getVarValue('bSortable_' . $iSortCol) == 'true') {
          $dbwKurse->orderby = $columns[$iSortCol]->fieldname;
          $dbwKurse->orderbyasc = \RequestOperations::getVarValue('sSortDir_' . $i) === 'asc';
        }
      }
    }

    return $dbwKurse;
  }

  protected function setzeEinstellungen($dbwKurse)
  {
    $dbwKurse = parent::setzeEinstellungen($dbwKurse);
    if ($this->toolSettings->sameAussenst && !$this->user->isAdmin()) {
      $dbwben = new \DBWorkBASysUser();
      $ben = $dbwben->getBASysUser($this->user->ben);
      if (!is_null($ben->id)) {
        $dbwKurse->setDBObject4Ast($ben->aussenstelle);
      }
    }

    $dbwKurse->fromobject->verwaltend = $this->toolSettings->verwaltend;
    $dbwKurse->fromobject->planend = $this->toolSettings->planend;

    $dbwKurse->beratungstageKurseAusblenden = false;

    return $dbwKurse;
  }
}