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

namespace Kufer\KuferTools\Utils\WssField;

class WssFieldKursAlternativkurse extends WssField
{
  protected function getValue($datamodulWert, $isFormular = false)
  {
    
    
    
    $knr = $datamodulWert->getVarValueEx('id', 'kurs');
    $dbwKurse = new \DBWorkKurseINetAktiv();
    $altKurse = $dbwKurse->getAlternativKurseIdsByKnr($knr);

    
    $toolId = \GetOperations::getVarValue('tool_id', 14);

    
    $toolSettings = \ToolSettingsAPI::getToolSettings($toolId);
    $kurshelper = \Kufer\KuferTools\Utils\DatamodulHelper\KursHelper\KursHelperFactory::getKursHelperByToolID($toolId, $toolSettings);
    $erlaubteKursIds = $kurshelper->getIdsOhneFilter();
    $altKurse = array_intersect($altKurse, $erlaubteKursIds);

    
    if (count($altKurse) > 0) {
      return $this->_buildAlternativKursTable($altKurse, $toolId, $knr);
    } else {
      return '';
    }
  }

  protected function encodeValue($value)
  {
    return $this->purifyHtml($value);
  }

  
  private function _buildAlternativKursTable($pAlternativkursIds, $toolId, $knr)
  {
    $link2Details = 'index.php?tool_id=' . $toolId . '&action=kursdetails';

    $dbwkurse = new \DBWorkKurse();
    $altKursTable = '<table class="alternativkurs table-condensed table-bordered" border="1" cellspacing="0" cellpadding="0">' . "\n"
                    . '<colgroup>' . "\n"
                    . '<col width="12%">' . "\n"
                    . '<col width="38%">' . "\n"
                    . '<col width="14%">' . "\n"
                    . '<col width="36%">' . "\n"
                    . '</colgroup>' . "\n"
                    . '<thead>' . "\n"
                    . '<tr>' . "\n"
                    . '<th>Kurs</th>' . "\n"
                    . '<th>Titel</th>' . "\n"
                    . '<th>Datum</th>' . "\n"
                    . '<th>Ort</th>' . "\n"
                    . '</tr>' . "\n"
                    . '</thead>' . "\n"
                    . '<tbody>' . "\n";

    
    foreach ($pAlternativkursIds as $altKursId) {
      $alternativKursObj = $dbwkurse->getKurs($altKursId, true);

      
      $referer = '';
      $altKursHref = '<a href="' . \CleanOperations::cleanStringForHtml($link2Details) . '&knr=' . \CleanOperations::cleanStringForHtml($altKursId) . $referer . '">' . \CleanOperations::cleanStringForHtml($altKursId) . '</a>';

      $altKursTable .= '<tr>' . "\n"
                       . '<td>' . $altKursHref . '</td>' . "\n"
                       . '<td>' . \CleanOperations::cleanStringForHtml($alternativKursObj->getVarValue('titel_zeile1')) . '</td>' . "\n"
                       . '<td>' . \CleanOperations::cleanStringForHtml($alternativKursObj->beginn_dat) . '</td>' . "\n"
                       . '<td>' . \CleanOperations::cleanStringForHtml($alternativKursObj->ort) . '</td>' . "\n"
                       . '</tr>' . "\n";
    } 

    $altKursTable .= '</tbody>' . "\n" . '</table>' . "\n";

    return $altKursTable;
  }

  
}