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


class DBWorkTexte extends DBWork
{
  
  public function getNewObject()
  {
    return new Text();
  }

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->beschreibung = $this->getDatamodulValue('beschreibung');
    $tmp->wert = $this->getDatamodulValue('wert');

    return $tmp;
  }

  
  public function textExists($pID)
  {
    $id = $this->dboperation->getInt4Sql($pID);
    if ($id > 0) {
      $this->dboperation->clear();
      $this->dboperation->query('SELECT id FROM web_texte WHERE id = ' . $id);

      return $this->dboperation->rowcount() > 0;
    } else {
      return false;
    }
  }

  public function getTextById($pId)
  {
    $id = $this->dboperation->getInt4Sql($pId);
    if ($id > -1) {
      $this->dboperation->clear();
      $this->dboperation->query('SELECT id, beschreibung, ' . $this->clob('wert')
        . ' FROM web_texte WHERE id = ' . $id);

      return $this->getFirstRow();
    } else {
      return $this->getNewObject();
    }
  }

  
  public function updateText($pText)
  {
    if (!$this->textExists($pText->id)) { 

      $id = $this->dboperation->getInt4Sql($pText->id);
      $this->dboperation->clear();
      $this->dboperation->execute('INSERT INTO web_texte (id) VALUES (' . $id . ')');
      $this->saveChanging('web_texte', '', $id, "'0'", 'id', 'I');
    }
    $orgObj = $this->getTextById($id);

    $this->updateObject($pText, $orgObj, 'web_texte', 'id');
    $this->setActionIDs();
  }
}