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


class DBWorkEMailXMLAttach extends DBWorkStrict
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, emailid, typ, filename, beschreibung, nachweis,' . $this->clob('content');
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->emailid = $this->getDatamodulValue('emailid');
    $tmp->filename = $this->getDatamodulValue('filename');
    $tmp->typ = $this->getDatamodulValue('typ');
    $tmp->content = $this->getDatamodulValue('content');
    $tmp->beschreibung = $this->getDatamodulValue('beschreibung');
    $tmp->nachweis = $this->getDatamodulValue('nachweis');

    return $tmp;
  }

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

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

    return $this->getFirstRow();
  }

  
  public function insertEmailXMLAttach($pToInsert)
  {
    $pToInsert->id = $this->insertRecord('emailxmlattach', 'id', '', true);
    $this->saveChanging('emailxmlattach', '', '', $pToInsert->id, 'id', 'I');
    $org = $this->getXMLMailAttach($pToInsert->id);
    $this->updateObject($pToInsert, $org, 'emailxmlattach', 'id', 'A');
    $this->setActionIDs();
    if ($this->verifyXMLMailAttach($pToInsert)) {
      return $pToInsert->id;
    } else {
      return false;
    }
  }

  
  public function verifyXMLMailAttach($pToVerify)
  {
    
    $verifyObject = $this->getXMLMailAttach($pToVerify->id);

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

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

        return false;
      }
    }

    return true;
  }
}