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


class DBWorkEMailXMLKennz extends DBWorkStrict
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    DBWorkStrict::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, emailid, kennz, kennztext';
  }

  
  public function getNewObject()
  {
    return new EmailXMLKennz();
  }

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->emailid = $this->getDatamodulValue('emailid');
    $tmp->kennz = $this->getDatamodulValue('kennz');
    $tmp->kennztext = $this->getDatamodulValue('kennztext');

    return $tmp;
  }

  
  public function getXMLMailKennz($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);

    $this->dboperation->clear();
    $this->dboperation->query('SELECT ' . $this->select . ' FROM emailxmlkennz '
        . 'WHERE id=' . $id);

    return $this->getFirstRow();
  }

  
  public function insertEmailXMLKennz($pEmailXMLKennz)
  {
    $pEmailXMLKennz->id = $this->insertRecord('emailxmlkennz', 'id', '', true);
    $this->saveChanging('emailxmlkennz', '', '', $pEmailXMLKennz->id, 'id', 'I');
    $orgXMLKennz = $this->getXMLMailKennz($pEmailXMLKennz->id);
    $this->updateObject($pEmailXMLKennz, $orgXMLKennz, 'emailxmlkennz', 'id', 'A');
    $this->setActionIDs();
    if ($this->verifyXMLMailKennz($pEmailXMLKennz)) {
      return $pEmailXMLKennz->id;
    } else {
      return false;
    }
  }

  
  public function verifyXMLMailKennz($pEmailXMLKennz)
  {
    
    $verifyObject = $this->getXMLMailKennz($pEmailXMLKennz->id);

    
    for ($i = 0; $i < $pEmailXMLKennz->getVarCount(); $i++) {
      
      $varname = $pEmailXMLKennz->getVarName($i);

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

        return false;
      }
    }

    return true;
  }
}