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

class DataMenuFreeKontaktFormularAbsenden extends DataMenuFreeKontaktFormularBestaetigung
{
  public function mainmenu()
  {
    $this->prepareMainmenu();
    if ($this->toolsettings->showClickpfad) {
      echo $this->getClickPath();
    }

    echo "<div class='hauptseite'>\n";
    $this->statusmenu();
    echo $this->getContentAnker();

    $emailVersendet = $this->sendFormular();
    if ($emailVersendet) {
      SessionOperations::resetFreeKontaktFormular();
      $this->shownocolumn();
    } else {
      $this->shownodata();
    }
    echo '</div>';
  }

  private function getEmailContent()
  {
    $dmbasis = new DataMenuBasis(new URLParameterBasis(false));
    $dmbasis->getSight(486);

    $dmbasis->exportadapter = new ExpFreeKontaktFormular();
    $dmbasis->dataarrayheader = array(new Datamodul());
    $dmbasis->dataarraybody = array(new Datamodul());
    $dmbasis->dataarrayfooter = array(new Datamodul());

    $result = $dmbasis->getheader();
    $result .= $dmbasis->getbody();
    $result .= $dmbasis->getfooter();

    return $result;
  }

  private function sendFormular()
  {
    $allgSettings = AllgSettingsAPI::getAllgSettings();
    $valid = ValidierungWB::getInstance();
    $email = $allgSettings->vhsemail;
    if (trim($this->toolsettings->kontaktFormularAltEmailAdresse) !== '') {
      if ($valid->isValidVar('email', $this->toolsettings->kontaktFormularAltEmailAdresse)) {
        $email = $this->toolsettings->kontaktFormularAltEmailAdresse;
      }
    }

    $subject = 'Kontaktformular';
    if (trim($this->toolsettings->kontaktFormularEmailBetreff) !== '') {
      $subject = $this->toolsettings->kontaktFormularEmailBetreff;
    }

    $smtp = new smtpMail();
    $smtp->SetVar2Value('to', $email);
    $smtp->SetVar2Value('subject', $subject);
    $smtp->SetVar2Value('body', $this->getEmailContent());
    $smtp->SetVar2Value('contenttype', SMTP_CONTENTTYPE_HTML);

    return $smtp->sendMail();
  }
}