<?php

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


class DataMenuNewsletterBestaetigungKuferSql extends DataMenuMainTopLeft
{
  private static $alreadyHandled = false; 
  private static $sightId = 612;
  private $nlApi;

  public function __construct($urlparameter)
  {
    parent::__construct($urlparameter);

    
    
    
    if ($this->urlparameter->newsl == '') {
      $this->urlparameter->newsl = RequestOperations::getVarValue('newsl');
    }
    if ($this->urlparameter->nl_step2 == '') {
      $this->urlparameter->nl_step2 = RequestOperations::getVarValue('nl_step2');
    }

    $this->nlApi = new \Kufer\Web\Classes\Api\NewsletterAPI();
  }

  public function _getClickPath($pName = 'Newsletter')
  {
    $this->_setStdClickPath($pName, 'clickNewsletterText');

    return $this->clickpfad;
  }

  public function mainmenu()
  {
    if ($this->toolsettings->showClickpfad) {
      echo $this->getClickPath();
    }
    if(!REMOVE_DIV_HAUPTSEITE_OHNE_STATUS) {
      echo "<div class='hauptseite_ohnestatus'>\n";
    }
    echo $this->getContentAnker();
    $basysSettings = BASysSettingsAPI::getBASysSettings();
    if ($basysSettings->newsletter) {
      echo $this->internerContent();
    } else {
      echo 'Der Newsletter Baustein ist nicht aktiviert!' . BR
      . 'Nähere Informationen finden Sie auf der Webseite '
      . '<a href="http://www.kufer.de" title="Homepage der Kufer Software GmbH">www.kufer.de</a>';
    }
    if(!REMOVE_DIV_HAUPTSEITE_OHNE_STATUS) {
      echo '</div>';
    }
  }

  public function internerContent()
  {
    $result = '';
    if ($this->paramsValid()) {
      if (RequestOperations::getVarValue('nl_step2', '--') !== '--') {
        if (static::$alreadyHandled) {
          $result = $this->showDankeschoen();
        } else {
          if ($this->sendeAnmeldung()) {
            $result = $this->showDankeschoen();
            static::$alreadyHandled = true;
          } else {
            $result = $this->showTechnischeProbleme();
          }
        }
      } else {
        if (static::$alreadyHandled) {
          $result = $this->showDankeschoen();
        } else {
          $result = $this->showStep1();
        }
      }
    } else {
      $result = '<p>Achtung - diese Anfrage enthielt ungültige Parameter</p>';
    }

    return $result;
  }

  private function paramsValid()
  {
    return is_numeric($this->urlparameter->getNr()) && $this->urlparameter->getNr() > 0 && $this->urlparameter->getPersonenTyp() != '';
  }

  
  public function showStep1()
  {
    $this->urlparameter->nl_step2 = '1';
    $this->urlparameter->kathaupt = 277;
    $this->urlparameter->implodeSettings();

    $t3id = GetOperations::getVarValue('id', '');
    if ($t3id != '') {
      $t3id = "&id=$t3id";
    }
    $link = 'index.php?urlparameter=' . $this->urlparameter->stream . $t3id;
    $link = CleanOperations::cleanStringForHtml($link);

    $this->exportadapter->setSightInfo('link', $link);

    $this->getsight();
    $this->dataarrayheader = $this->dataarrayfooter = $this->dataarraybody = array(new Datamodul());
    $this->showheader();
    $this->showbody();
    $this->showfooter();
  }

  public function getsight($sightid = 612)
  {
    parent::getsight($sightid);
  }

  
  public function showDankeschoen()
  {
    return $this->getSightPart(static::$sightId, WB_SIGHT_PART_NOCOLUMN, array(new Datamodul()));
  }

  
  public function showTechnischeProbleme()
  {
    return $this->getSightPart(static::$sightId, WB_SIGHT_PART_NODATA, array(new Datamodul()));
  }

  
  private function sendeAnmeldung()
  {
    $datamodul = new NewsletterXml();
    $datamodul->status = 'J';
    $datamodul->nr = $this->urlparameter->getNr();
    $datamodul->typ = $this->urlparameter->getPersonenTyp();
    $datamodul->sessiontime = SessionOperations::getSessionTime();

    
    if ($this->toolsettings->nlBestKennz !== '' && $this->toolsettings->nlBestKennz > 0) {
      $datamodul->addKennzeichenByParam($this->toolsettings->nlBestKennz);
    }

    return $this->datenSenden($datamodul);
  }

  
  public function datenSenden($pAnmeldung)
  {
    $zentralsettings = ToolSettingsAPI::getToolSettings(0);

    $erfolg = false;
    if ($zentralsettings->savemail2file == ANMELDUNGPER_FILE) {
      $xml = $this->getXMLText($pAnmeldung);
      $erfolg = $this->nlApi->writeToFile($xml, $zentralsettings->emailfilepath);
    } elseif ($zentralsettings->savemail2file == ANMELDUNGPER_MAIL) {
      $allgsettings = AllgSettingsAPI::getAllgSettings();
      $xml = $this->getXMLText($pAnmeldung);
      $erfolg = $this->nlApi->writeToMail($xml, $allgsettings->emailanm);
    } elseif ($zentralsettings->savemail2file == ANMELDUNGPER_ONLINE) {
      $erfolg = $this->writeToDb($pAnmeldung);
    } elseif ($zentralsettings->savemail2file == ANMELDUNGPER_DMZ) {
      $erfolg = $this->writeToDmz($pAnmeldung);
    }

    return $erfolg;
  }

  
  private function getXMLText($pAnmeldung)
  {
    $xmlNode = new XMLNode('NEWSLETTERANMELDUNGEN');
    $xmlNode->addChild($pAnmeldung->getXMLNode4BestaetigungKuferSql());

    return '<?xml version="1.0" encoding="' . \CodingOperations::getInternalEncoding() . '" ?' . ">\r\n" . $xmlNode->getXMLstring($xmlNode);
  }

  
  private function writeToDb($pAnmeldung)
  {
    $dbNewsl = new DBWorkNewslXml(2);

    return $dbNewsl->doNewsletterAnmeldungByNewsletterXml($pAnmeldung);
  }

  
  public function writeToDmz($pAnmeldung)
  {
    $pAnmeldung->gridstatus = -1; 

    $pAnmeldung->id = $pAnmeldung->nr;

    $node = $pAnmeldung->getXMLNode4BestaetigungKuferSqlDmz();
    try {
      $abgleichAPI = new AbgleichAPI();
      $nutzlast = new Nutzlast($node);
      $abgleichAPI->sendeNachricht(AbgleichAPI::ABGLEICH_ANMELDUNGNEWSLETTER, $nutzlast);
    } catch (Exception $e) {
      Logger::logg(
        'NewsletterBestaetigungKuferSql - Folgende Exception ist aufgetreten: ' . $e->getMessage() . ' [' . $e->getTraceAsString() . ']',
        200
      );

      return false;
    }

    return true;
  }
}