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

namespace Kufer\KuferTools\Utils\WssField;

class WssFieldDozentBranchen extends WssFieldMultiselect
{
  protected function createNewValueDatamodulArray($branchenKennzArray, $datamodul)
  {
    
    $branAdrAry = array();
    foreach ($branchenKennzArray as $branchenKennz) {
      $branAdr = new \BranAdr();
      $branAdr->adr_nr = $datamodul->id;
      $branAdr->bra_nr = $branchenKennz;
      $branAdr->typ = 'D';
      $branAdrAry[] = $branAdr;
    }

    return $branAdrAry;
  }

  protected function getDatamoduleForAvailableItems()
  {
    $dbwBranchen = new \DBWorkBranchen();

    return $dbwBranchen->getAllRows();
  }

  protected function getFieldNichtBearbeitbar($datamodul, $isFormular, string $requestPrefix = '')
  {
    if (\is_null($datamodul->id) || $datamodul->id === '') {
      return '';
    }

    $dbwBranchen = new \DBWorkBranchen();
    $branchen = $dbwBranchen->getBranchen4Dozent($datamodul->id);

    $html = '';
    foreach ($branchen as $branche) {
      $html .= \HTMLOperations::wrapInSpan(array('class' => $this->getWssFieldNameWithPrefix()), $this->htmlEncode($branche->text));
      $html .= BR_WT;
    }

    return $html;
  }

  protected function getOptionsForMultiselect($datamodul)
  {
    $valueFieldName = $this->getValueFieldname();
    $dbwBranchen = new \DBWorkBranchen();
    $branchen = $dbwBranchen->getBranchen4Dozent($datamodul->id);

    $options = array();
    foreach ($branchen as $branche) {
      $options[] = \HTMLOperations::createOptionTag($branche->text, $branche->{$valueFieldName}, true);
    }

    return $options;
  }

  protected function getMultiselectHeight()
  {
    return '5';
  }

  protected function getPrefix()
  {
    return 'dozent';
  }

  protected function getTextFieldname()
  {
    return 'text';
  }

  protected function getValueFieldname()
  {
    return 'kennz';
  }

  public function getFieldName(): string
  {
    return 'arykennzeichen';
  }
}