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

class KursplanungDozenten extends Datamodul
{
  
  public $kommunikation = array();

  
  public $kursplanung_id;

  
  public $nr;
  
  public $typ;
  
  public $name;
  
  public $vorname;
  
  public $name_titel;
  
  public $strasse;
  
  public $ort;
  
  public $gemeinde;
  
  public $stadtteil;
  
  public $bundesland;
  
  public $bezirk;

  public $honorarsatz;
  public $lfdnr;

  
  public $fahrtkosten_nach_km;

  
  public $dienstst;

  public function getTyp($pFieldName)
  {
    switch ($pFieldName) {
      default:
        return parent::getTyp($pFieldName);
      case 'kursplanung_id':
      case 'nr':
      case 'lfdnr':
        return \DatamodulVarType::TYPE_INT;
      case 'honorarsatz':
        return \DatamodulVarType::TYPE_FLOAT_DOT;
      case 'fahrtkosten_nach_km':
        return \DatamodulVarType::TYPE_BOOL_WF;
    }
  }

  public function getVarValue($name)
  {
    switch ($name) {
      case 'nurplz':
        $found = array();
        preg_match('/(\\d*)(.*)/', $this->getVarValue('ort'), $found);
        if (isset($found[1])) {
          return $found[1];
        }

        return '';
      case 'nurort':
        $found = array();
        preg_match('/(\\d*)(.*)/', $this->getVarValue('ort'), $found);
        if (isset($found[2])) {
          return trim($found[2]);
        }

        return '';
      case 'email':
        return $this->getKommEintrag('E', 1);
      case 'email2':
        return $this->getKommEintrag('E', 2);
      case 'tel':
        return $this->getKommEintrag('T', 1);
      case 'tel2':
        return $this->getKommEintrag('T', 2);
      case 'mobil':
        return $this->getKommEintrag('M', 1);
      case 'mobil2':
        return $this->getKommEintrag('M', 2);
      case 'weblink':
        return $this->getKommEintrag('W', 1);

      case 'name_vorname_ohne_komma': return trim($this->getVarValue('name') . ' ' . $this->getVarValue('vorname'));
      case 'titel_name_vorname': return $this->fuegeTitelHinzu($this->getVarValue('name_vorname_ohne_komma'), $this->getVarValue('name_titel'));
      case 'vorname_name_ohne_komma': return trim($this->getVarValue('vorname') . ' ' . $this->getVarValue('name'));
      case 'titel_vorname_name': return $this->fuegeTitelHinzu($this->getVarValue('vorname_name_ohne_komma'), $this->getVarValue('name_titel'));

      default: return parent::getVarValue($name);
    }
  }

  private function fuegeTitelHinzu($name, $nameTitel)
  {
    static $dbwTitel = null;
    if (is_null($dbwTitel)) {
      $dbwTitel = new DBWorkTitel(1, true);
    }

    return $dbwTitel->fuegeTitelHinzu($name, $nameTitel);
  }

  public function getKommEintrag($art, $nr = 1)
  {
    if (is_array($this->kommunikation)) {
      $komms = array_filter($this->kommunikation, function($komm) use ($art) {
        return $komm->art == $art;
      });

      
      $komms = array_values($komms);

      if (isset($komms[$nr - 1])) {
        return $komms[$nr - 1]->wert;
      }
    }

    return '';
  }

  public function getDMZValue($fieldname)
  {
    $dmzValue = parent::getDMZValue($fieldname);
    
    if ($fieldname == 'nr') {
      $dmzValue = $this->nr;
    }

    return $dmzValue;
  }

  

  public static function loadFromDozentHonorar($pDozentHonorar, $mitHonorar = true)
  {
    $result = self::loadFromDozent($pDozentHonorar->dozent);
    $result->typ = $pDozentHonorar->typ;
    if ($mitHonorar) {
      $result->honorarsatz = $pDozentHonorar->honfaktor * $pDozentHonorar->honsatz;
    }

    return $result;
  }

  public static function loadFromDozent($pDozent)
  {
    $result = new KursplanungDozenten();

    $result->kommunikation = KursplanungDozentenKomm::loadFromDozent($pDozent);
    $result->nr = $pDozent->id;
    $result->typ = 'D';
    $result->name = $pDozent->name;
    $result->vorname = $pDozent->vorname;
    $result->name_titel = $pDozent->name_titel;
    $result->strasse = $pDozent->strasse;
    $result->ort = $pDozent->ort;

    $result->gemeinde = $pDozent->gemeinde;
    $result->stadtteil = $pDozent->stadtteil;
    $result->bezirk = $pDozent->bezirk;
    $result->bundesland = $pDozent->bundesland;

    return $result;
  }
}