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


class DBWorkAktuellesKommentar extends DBWork
{
  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, news_id, titel, inhalt, autor_id, autor_typ, erstelldatum';
  }

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->id = $this->getDatamodulValue('id');
    $tmp->news_id = $this->getDatamodulValue('news_id');
    $tmp->titel = $this->getDatamodulValue('titel');
    $tmp->inhalt = $this->getDatamodulValue('inhalt');
    $tmp->erstell_datum = $this->getDatamodulValue('erstelldatum');
    $tmp->erstell_uhrzeit = $this->getDatamodulValue('erstelldatum');
    $tmp->autor_id = $this->getDatamodulValue('autor_id');
    $tmp->autor_typ = $this->getDatamodulValue('autor_typ');

    return $tmp;
  }

  
  public function getSQLValue($fieldname, $datamodul)
  {
    if ($fieldname == 'erstell_datum') {
      return $this->datetimeop->getFormatDateTime4Server($datamodul->getVarValue($fieldname)
                                            . ' ' . $datamodul->getVarValue('erstell_uhrzeit'));
    }

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

  
  public function translateVarName($varname)
  {
    switch ($varname) {
      default: return DBWork::translateVarName($varname);
      case 'erstell_datum': return 'erstelldatum';
    }
  }

  
  public function canupdate($varname)
  {
    switch ($varname) {
      default: return parent::canupdate($varname);
      case 'id': return false;
      case 'erstell_uhrzeit': return false;
    }
  }

  
  public function getAktuellesKommentar($pKommId)
  {
    $komm_id = $this->dboperation->getInt4Sql($pKommId);
    if (empty($komm_id)) {
      return $this->getNewObject();
    }
    $sql = 'SELECT ' . $this->select . ' FROM web_kommentare'
           . ' WHERE id = \'' . $komm_id . '\' ORDER BY id DESC';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function getAktuellesKommentarByNewsID($pNewsId)
  {
    $news_id = $this->dboperation->getInt4Sql($pNewsId);
    if (empty($news_id)) {
      return array();
    }
    $sql = 'SELECT ' . $this->select . ' FROM web_kommentare'
           . ' WHERE news_id = \'' . $news_id . '\' ORDER BY id DESC';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getCountKommentar($pNewsId)
  {
    $news_id = $this->dboperation->getInt4Sql($pNewsId);
    if (empty($news_id)) {
      return 0;
    }
    $sql = 'SELECT id FROM web_kommentare'
           . ' WHERE news_id = \'' . $news_id . '\' ORDER BY id DESC';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->dboperation->rowcount();
  }

  
  public function insertKommentar($pKomm)
  {
    
    $pKomm->id = $this->insertRecord('web_kommentare', 'id', 'titel', false);
    $this->saveChanging('web_kommentare', '', '', $pKomm->id, 'id', 'I');
    $orgAktuelles = $this->getAktuellesKommentar($pKomm->id);

    $this->updateObject($pKomm, $orgAktuelles, 'web_kommentare', 'id', 'A');
    $this->setActionIDs();

    return $pKomm->id;
  }

  
  public function deleteKommentar($pKomm_id)
  {
    $komm_id = $this->dboperation->getInt4Sql($pKomm_id);
    if (!empty($komm_id)) {
      $this->dboperation->clear();
      $this->dboperation->execute('DELETE FROM web_kommentare WHERE id=' . $komm_id);
      $this->saveChanging('web_kommentare', '', '', "'" . $komm_id . "'", '', 'D');
      $this->setActionIDs();
    }
  }

  
  public function deleteKommentarByNewsID($pNews_id)
  {
    $news_id = $this->dboperation->getInt4Sql($pNews_id);
    if (!empty($news_id)) {
      $this->dboperation->clear();
      $this->dboperation->execute('DELETE FROM web_kommentare WHERE news_id=' . $news_id);
      $this->saveChanging('web_kommentare', '', '', "'" . $news_id . "'", '', 'D');
      $this->setActionIDs();
    }
  }
}