<?php

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

class DataMenuMatAusgInteresseBestaetigung extends DataMenuMainTopLeft
{
  public function __construct($pUrlparameter)
  {
    parent::__construct($pUrlparameter);
    $this->exportadapter = new ExpMatAusg();
  }

  public function getsight($pSightId = 731)
  {
    parent::getsight($pSightId);
  }

  public function _getClickPath($pName = 'Interessenbekundung')
  {
    $detailText = $this->toolsettings->clickMaterialAusgabeDetailText != '' ? $this->toolsettings->clickMaterialAusgabeDetailText : 'Materialausgabedetails';
    $uebersichtText = $this->toolsettings->clickMaterialAusgabeUebersichtText != '' ? $this->toolsettings->clickMaterialAusgabeUebersichtText : 'Materialausgabe Übersicht';

    DataMenuMainTopLeft::_getClickPath();
    
    if ($this->toolsettings->titleKursBereich != '') {
      $this->clickpfad = '<span class="clickadresse"> ' . CleanOperations::cleanStringForHtml(stripslashes($this->toolsettings->titleKursBereich)) . ' </span>' . PHP_EOL;
      $this->clickpfad .= '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>' . PHP_EOL;
    }

    $this->clickpfad .= $this->getClickPathStep(
      $this->urlparameter->getEmptyStreamWith(
        array('kathaupt'),
        array('281')
      ),
      'Materialausgabe Übersicht öffnen',
      $uebersichtText
    );

    $this->clickpfad .= $this->getClickPathStep(
      $this->urlparameter->getEmptyStreamWith(
        array('kathaupt', 'material'),
        array('282', $this->getMaterial()->id)
      ),
      'Materialausgabe Details öffnen',
      $detailText
    );

    $this->clickpfad .= '<span class="clickadresse">' . CleanOperations::cleanStringForHtml($pName) . "</span>\n";
    $this->clickpfad .= '<span class="trennzeichen"> ' . CleanOperations::cleanStringContainingHtml($this->toolsettings->trennzeichenKategorie) . ' </span>';
    $this->clickpfad .= '<span class="clickadrbold">' . CleanOperations::cleanStringForHtml('Bestätigung') . "</span>\n";

    return $this->clickpfad;
  }
  public function mainmenu()
  {
    $this->prepareMainmenu();
    if ($this->toolsettings->showClickpfad) {
      echo $this->getClickPath();
    }
    echo "<div class='hauptseite'>\n";
    $this->statusmenu();
    $this->beforeContent();
    echo $this->getContentAnker();
    if (count($this->dataarraybody) > 0) {
      $this->showheader();
      $this->showbody();
      $this->showfooter();
    } else {
      $this->shownodata();
    }
    SessionOperations::clearEditTNFromSession('_matinteresse');
    echo '</div>';
  }

  protected function beforeContent()
  {
    $retour = $this->sendMail();
    if ($retour) {
      $this->getsight(733);
    } else {
      $this->getsight(731);
      $this->dataarraybody = array();
    }
  }

  protected function getExportAdapterBody()
  {
    $expTeilnehmer = new \ExpTeilnehmerRekursiv();
    $expTeilnehmer->export->expCalculation->enforceParamForValueTeilnehmerEdit = true;
    $expTeilnehmer->export->expCalculation->isDataBinding = false;

    return $expTeilnehmer;
  }

  public function getdataarrayheader()
  {
    $this->dataarrayheader = array($this->getMaterial());
  }

  public function getdataarraybody()
  {
    if ($this->urlparameter->material != '') {
      $this->dataarraybody = SessionOperations::getTeilnehmerFromSession('matinteresse');
    } else {
      $this->dataarraybody = array();
    }
  }

  public function getdataarrayfooter()
  {
    $this->dataarrayfooter = array($this->getMaterial());
  }

  private function getMaterial()
  {
    $dbwmat = DBWorkMatAusg::getDefaultInstance();

    return $dbwmat->getMaterialById($this->urlparameter->material);
  }

  private function sendMail()
  {
    $allgsettings = AllgSettingsAPI::getAllgSettings();
    $vhsmail = CleanOperations::cleanStringForHtml($allgsettings->vhsemail);
    $eingegebeneMail = CleanOperations::cleanStringForHtml($this->toolsettings->matInteresseMail);
    $eingegebenerBetreff = CleanOperations::cleanStringForHtml($this->toolsettings->matInteresseBetreff);
    $standartbetreff = 'Automatisch generierte eMail - Interesse an Material' . $this->getMaterial()->text;
    $to = empty($eingegebeneMail) ? $vhsmail : $eingegebeneMail;
    $subject = empty($eingegebenerBetreff) ? $standartbetreff : $eingegebenerBetreff;

    $email = new smtpMail();
    $email->SetVar2Value('to', $to);
    $email->SetVar2Value('subject', $subject);
    $email->SetVar2Value('contenttype', SMTP_CONTENTTYPE_HTML);
    $email->SetVar2Value('body', $this->getsightcomplete(734, $this->getExportAdapterBody(), new ExpMatAusg(), new ExpMatAusg()));
    $retour = $email->sendMail();

    if (!$retour) {
      Logger::logg('Konnte eMail an ' . $to . ' nicht versenden', 8000);
    } else {
      Logger::logg('eMail an ' . $to . ' versendet', 5000);
    }

    return $retour;
  }
}