<?php

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


class ToolNachricht extends ToolBase
{
  public function __construct()
  {
    parent::__construct();
    $this->toolTitle = 'Nachricht an Einrichtung';
  }

  public function init($pPageId)
  {
    $pageLinks = array();
    $pageLinks['action'] = $this->getUrlParamsForFile(array('toolsection_id' => 315, 'sendInfo' => 'vhs'));
    $pageLinks['back'] = ToolsAPI::getLastHistoryEntry();
    $page = new PageNachricht('Nachricht', $pPageId, $this);
    $page->setLinks($pageLinks);

    $versendet = array();
    $nichtVersendet = array();

    if (GetOperations::getVarValue('sendInfo', '') == 'vhs') {
      

      try {
        $teilnehmer = SessionOperations::getCurrentWebToolsTeilnehmer();
      } catch (Exception $e) { 
        $teilnehmer = null;
      }
      $senderEmailData = ToolsAPI::getSenderEmailAddressAndName($teilnehmer);

      $mailAddress = EmailOperations::getToolEmailAddressForVhs($this->toolSettings->vhsEmailTool23Section315);

      $email = new smtpMailTools();
      $email->SetVar2Value('from', $senderEmailData['email']);
      $email->SetVar2Value('sendername', $senderEmailData['name']);
      $email->SetVar2Value('reply', $senderEmailData['reply']);
      $email->SetVar2Value('replyname', $senderEmailData['replyname']);
      $email->SetVar2Value('to', $mailAddress);
      $email->SetVar2Value('contenttype', SMTP_CONTENTTYPE_TEXT);
      $email->SetVar2Value('doNotSplitHTMLBody', true);
      $email->subject = PostOperations::getVarValue('betreff');
      $email->body = PostOperations::getVarValue('info');
      $email->setFooter('Nachricht an die Einrichtung', SessionOperations::getCurrentWebToolsUser());

      if ($email->sendMail()) {
        $versendet[] = $this->allgSettings->vhsname;
      } else {
        $nichtVersendet[] = $this->allgSettings->vhsname;
      }

      $page = new PageNachrichtErfolgreichGesendet('Nachricht', $pPageId, $this, 'Ihre Nachricht an Einrichtung', $versendet, $nichtVersendet);
      $page->setLinks($pageLinks);
    }

    if ($page == null) {
      throw new Exception('ungültige PageId');
    }

    $this->currentPage = $page;
  }

  public function getToolId()
  {
    return \ToolsAPI::TOOL_NACHRICHT_AN_EINRICHTUNG;
  }
}

class PageNachricht extends PageKontakt
{
  public function __construct($pPageTitle, $pPageId, $pTool)
  {
    parent::__construct($pPageTitle, $pPageId, $pTool, '');
  }

  public function getContentHeader()
  {
    return '';
  }

  protected function getBetreffValue()
  {
    return '';
  }

  protected function _getSonstigenFormularinhalt()
  {
    $allgsettings = AllgSettingsAPI::getAllgSettings();
    $name = $allgsettings->vhsname;

    return '<h1>Eine E-Mail an ' . $name . ' senden:</h1>';
  }

  protected function _getLabel()
  {
    return '<h3>Ihre Nachricht an Einrichtung:</h3>';
  }

  protected function _getLinks4Formular()
  {
    $buttons = '<input type="image" name="Absenden" src="layout/button_send.gif" title="Absenden">';
    $buttons .= '<a href="javascript:history.back();" title="Zurueck">
                 <img src="layout/button_close_small.gif"></a>';

    return $buttons;
  }
}