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


class DBWorkNewslXml extends DBWorkStrict
{
  
  public function getNewObject()
  {
    return new NewsletterXml();
  }

  public function getTableName(): string
  {
    return 'newslxml';
  }

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $result = $this->getNewObject();
    $result->id = $this->getDatamodulValue('id');
    $result->ewahl = $this->getDatamodulValue('ewahl');
    $result->bearbeiter = $this->getDatamodulValue('bearbeiter');
    $result->bearbdatum = $this->getDatamodulValue('bearbdatum');
    $result->eingelesen = $this->getDatamodulValue('eingelesen');
    $result->status = $this->getDatamodulValue('status');
    $result->bemerkung = $this->getDatamodulValue('bemerkung');
    $result->email = $this->getDatamodulValue('email');
    $result->name = $this->getDatamodulValue('name');
    $result->vorname = $this->getDatamodulValue('vorname');
    $result->geschlecht = $this->getDatamodulValue('geschlecht');
    $result->zusatz = $this->getDatamodulValue('zusatz');
    $result->zusatz2 = $this->getDatamodulValue('zusatz2');
    $result->zusatz3 = $this->getDatamodulValue('zusatz3');
    $result->strasse = $this->getDatamodulValue('strasse');
    $result->ort = $this->getDatamodulValue('ort');
    $result->tel = $this->getDatamodulValue('tel');
    $result->mobil = $this->getDatamodulValue('mobil');
    $result->tel2 = $this->getDatamodulValue('tel2');
    $result->fax = $this->getDatamodulValue('fax');
    $result->krit1 = $this->getDatamodulValue('krit1');
    $result->krit2 = $this->getDatamodulValue('krit2');
    $result->krit3 = $this->getDatamodulValue('krit3');
    $result->krit4 = $this->getDatamodulValue('krit4');
    $result->krit5 = $this->getDatamodulValue('krit5');
    $result->krit6 = $this->getDatamodulValue('krit6');
    $result->sessiontime = $this->getDatamodulValue('sessiontime');
    $result->gridstatus = $this->getDatamodulValue('gridstatus');

    $otherStandardFields = array('dse_unterschrieben_datum', 'dse_unterschrieben_bearbeiter', 'dse_unterschrieben_weg',
        'dse_widerrufen_datum', 'dse_widerrufen_bearbeiter', 'dse_widerrufen_weg', 'nr', 'typ');
    foreach ($otherStandardFields as $varName) {
      $result->{$varName} = $this->getDatamodulValue($this->translateVarName($varName));
    }

    return $result;
  }

  
  public function canupdate($varname)
  {
    switch ($varname) {
      default: return DBWork::canupdate($varname);
      case 'arykennzeichen': return false;
    }
  }

  
  public function getSQLValue($fieldname, $datamodul)
  {
    if ($fieldname == 'gridstatus') {
      return "'-1'"; 
    }

    return parent::getSQLValue($fieldname, $datamodul);
  }

  
  public function getNewslXml($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    $this->dboperation->clear();
    $this->dboperation->query(
      'SELECT id, ewahl, name, vorname, strasse, ort, geschlecht, ' . $this->clob('bemerkung') . ', '
      . 'status, eingelesen, bearbeiter, bearbdatum, zusatz, '
      . 'zusatz2, zusatz3, sessiontime, email, tel, fax, tel2, mobil, '
      . 'krit1, krit2, krit3, krit4, krit5, krit6, gridstatus, '
      . 'dse_unterschrieben_datum, dse_unterschrieben_bearbeiter, dse_unterschrieben_weg, dse_widerrufen_datum, '
      . 'dse_widerrufen_bearbeiter, dse_widerrufen_weg, nr, typ '
      . 'FROM newslxml WHERE id = ' . $id
    );

    return $this->getFirstRow();
  }

  
  public function insertXMLNewsl($xmlmail)
  {
    $xmlmail->id = $this->insertRecord('newslxml', 'id', '', true);
    $this->saveChanging('newslxml', '', '', $xmlmail->id, 'id', 'I');
    $orgxmlmail = $this->getNewslXml($xmlmail->id);
    $this->updateObject($xmlmail, $orgxmlmail, 'newslxml', 'id', 'A');
    $this->setActionIDs();
    
    if ($this->verifyNewslXML($xmlmail)) {
      return $xmlmail->id;
    } else {
      return false;
    }
  }

  
  public function doNewsletterAnmeldung($pNewsl, $pInteressen)
  {
    $this->dboperation->beginTransaction();
    
    $newslxml = NewsletterXml::loadFromNewsletterAnmeldung($pNewsl);
    
    $newslxml->sessiontime = SessionOperations::getSessionTime();
    
    $newlsId = $this->insertXMLNewsl($newslxml);
    if (!$newlsId) {
      $this->dboperation->rollbackTransaction();

      return false;
    }

    
    if ($newslxml->hasKennzeichen()) {
      $kennzeichen = $newslxml->getKennzeichen();

      $dbKennz = new DBWorkNewslXMLKennz(2, true);
      foreach ($kennzeichen as $item) {
        $item->newsletterXmlId = $newlsId;
        if (false === $dbKennz->insertNewslXMLKennz($item)) {
          $this->dboperation->rollbackTransaction();

          return false;
        }
      }
    }

    
    if (is_array($pInteressen) && count($pInteressen) > 0) {
      $dbInt = new DBWorkNewslXMLInt(2, true);
      foreach ($pInteressen as $item) {
        $int = $dbInt->getNewObject();
        $int->newsletterXmlId = $newlsId;
        $int->kennz = $item;
        if (false === $dbInt->insertNewslXMLInt($int)) {
          $this->dboperation->rollbackTransaction();

          return false;
        }
      }
    }

    
    $this->dboperation->commitTransaction();

    return true;
  }

  
  public function doNewsletterAnmeldungByNewsletterXml($pNewsl)
  {
    $pNewsl->eingelesen = DateTimeOperations::getDateNow();
    $pNewsl->bearbdatum = $pNewsl->eingelesen;

    $this->dboperation->beginTransaction();
    
    $newlsId = $this->insertXMLNewsl($pNewsl);
    if (!$newlsId) {
      $this->dboperation->rollbackTransaction();

      return false;
    }

    
    $this->dboperation->commitTransaction();

    return true;
  }

  
  public function verifyNewslXML($xmlmail)
  {
    
    $verifyObject = $this->getNewslXml($xmlmail->id);

    
    $noCheck = array('eingelesen', 'bearbdatum', 'arykennzeichen', 'gridstatus');
    
    for ($i = 0; $i < $xmlmail->getVarCount(); $i++) {
      
      $varname = $xmlmail->getVarName($i);

      
      if (!in_array($varname, $noCheck)) {

        
        
        if (!$this->checkEquality($xmlmail, $verifyObject, $varname)) {
          Logger::logg(__CLASS__ . "Fehler bei Variable $varname :" . '(original)'
              . $xmlmail->getVarValue($varname) . '!='
              . $verifyObject->getVarValue($varname) . '(ausgelesen)', 100);

          return false;
        }
      }
    }

    return true;
  }
}