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

class DataMenuZsaRegistrierungBestaetigenDblOptIn extends DataMenuMain
{
  private $dbwDblOptIn;
  private $dbwBkBvTn;

  const DBLOPTIN_SUCCESS = 'erfolgreich';
  const DBLOPTIN_TOKEN_NOT_FOUND = 'token_nicht_gefunden';
  const DBLOPTIN_REGISTRIERUNG_NICHT_EINDEUTIG = 'registrierung_nicht_eindeutig';
  const DBLOPTIN_DMZ_FEHLER = 'dmz_fehler';
  const DBLOPTIN_STATUS_NICHT_J = 'status_nicht_j';
  const DBLOPTIN_TEILNEHMER_NICHT_GEFUNDEN = 'teilnehmer_nicht_gefunden';

  public function __construct($urlparameter)
  {
    
    
    parent::__construct(new URLParameterNavigation());
    $this->dbwDblOptIn = new DBWorkDblOptinZsaRegistrierungAktivieren(1, true);
    $this->dbwBkBvTn = new DBWorkZsaBkBvTn(1, true);
    $this->exportadapter = new ExpBkBvTn();

    $this->dataarrayheader = array();
  }

  private function getDblOptInObj()
  {
    $token = $this->urlparameter->token;

    return $this->dbwDblOptIn->getDblOptinByOptin($token);
  }

  private function getTnrsByEmail($email)
  {
    $dbwKommunikation = new DBWorkKommunikation('T', 1, true);
    $kommunikationEintraege = $dbwKommunikation->findByEmail($email, 'T');

    return ArrayOperations::mapAttribut($kommunikationEintraege, 'nr');
  }

  private function filterTnrsByName($tnrs, $name, $vorname)
  {
    
    $dbwTeilnehm = new DBWorkTeilnehmer(1, true);
    $dbwTeilnehm->newSearch();
    
    $dbwTeilnehm->fromobject->name = $name;
    $dbwTeilnehm->fromobject->vorname = $vorname;
    $teilnehmer = $dbwTeilnehm->getTeilnehmByIdArrayAndFilter($tnrs);

    return ArrayOperations::mapAttribut($teilnehmer, 'id');
  }

  private function getTnrFromDblOptIn($dblOptIn)
  {
    if (is_numeric($dblOptIn->krit3) && $dblOptIn->krit3 > 0) {
      return $dblOptIn->krit3;
    } else {
      $tnrsVonKommunikation = $this->getTnrsByEmail($dblOptIn->email);
      $gefilterteTnrs = $this->filterTnrsByName($tnrsVonKommunikation, $dblOptIn->name, $dblOptIn->vorname);
      if (count($gefilterteTnrs) == 1) {
        return $gefilterteTnrs[0];
      } else {
        return null;
      }
    }
  }

  private function getBkBvTnAry($tnr, $zb, $ds)
  {
    $this->dbwBkBvTn->newSearch();
    $this->dbwBkBvTn->fromobject->typ = 'TN';
    $this->dbwBkBvTn->fromobject->tnr = $tnr;
    $this->dbwBkBvTn->fromobject->zb = $zb;
    $this->dbwBkBvTn->fromobject->ds = $ds;

    return $this->dbwBkBvTn->getByFilter();
  }

  private function handleDblOptIn()
  {
    $dblOptIn = $this->getDblOptInObj();

    if (is_null($dblOptIn)) {
      return self::DBLOPTIN_TOKEN_NOT_FOUND;
    }

    $tnr = $this->getTnrFromDblOptIn($dblOptIn);
    if (is_null($tnr)) {
      return self::DBLOPTIN_TEILNEHMER_NICHT_GEFUNDEN;
    }

    
    $bkbvtnAry = $this->getBkBvTnAry($tnr, $dblOptIn->krit2, $dblOptIn->krit1);

    if (count($bkbvtnAry) != 1) {
      return self::DBLOPTIN_REGISTRIERUNG_NICHT_EINDEUTIG;
    }

    $bkbvtn = $bkbvtnAry[0];
    $bkbvtnOrig = clone($bkbvtn);
    if ($bkbvtn->status != 'J') {
      return self::DBLOPTIN_STATUS_NICHT_J;
    }

    $bkbvtn->status = 'A';
    $success = ZsaBkBvTnAPI::updateZsaBkBvTn($bkbvtn, $bkbvtnOrig);
    if ($success) {
      $this->dbwDblOptIn->deleteByOptin($dblOptIn->optin);
      $this->dataarrayheader = array($bkbvtn);

      return self::DBLOPTIN_SUCCESS;
    } else {
      return self::DBLOPTIN_DMZ_FEHLER;
    }
  }

  public function getdataarrayheader()
  {
    return $this->dataarrayheader;
  }

  public function mainmenu()
  {
    $statusCode = $this->handleDblOptIn();
    if ($statusCode == self::DBLOPTIN_SUCCESS) {
      parent::mainmenu();
    } else {
      
      $this->prepareMainmenu();
      if ($this->toolsettings->showClickpfad) {
        echo $this->getClickPath();
      }
      echo "<div class='hauptseite'>\n";
      $this->statusmenu();
      echo $this->getContentAnker();
      $dm = new Datamodul();
      $dm->status = $statusCode;
      $this->dataarraynodata = array($dm);
      $this->shownodata();
      echo '</div>';
    }
  }

  public function getSight($pSightId = '547')
  {
    parent::getSight($pSightId);
  }
}