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


class DBWorkAktuellesWB extends DBWork
{
  private $rssFeedNews;

  public function __construct($pDBTyp = 1, $pNewInstance = false)
  {
    parent::__construct($pDBTyp, $pNewInstance);
    $this->select = 'id, autor, titel, inhalt, bildpfad, erstelldatum, anzeige_news, gueltigAb, gueltigBis, anzeige_rss';

    $this->rssFeedNews = new \Kufer\Web\Classes\Rss\RssFeedNews();
    $this->rssFeedNews->loadPubDates();
  }

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

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

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

  
  public function getCurrentRow($pRefreshCache = false)
  {
    $tmp = $this->getNewObject();
    $tmp->setValue2Var('id', $this->getDatamodulValue('id'));
    $tmp->setValue2Var('titel', strtr($this->getDatamodulValue('titel'), '`', "'"));
    $tmp->setValue2Var('autor', strtr($this->getDatamodulValue('autor'), '`', "'"));
    $tmp->setValue2Var('inhalt', strtr($this->getDatamodulValue('inhalt'), '`', "'"));
    $tmp->setValue2Var('bildpfad', $this->getDatamodulValue('bildpfad'));
    $tmp->setValue2Var('erstelldatum', $this->getDatamodulValue('erstelldatum'));
    $tmp->setValue2Var('anzeige_news', $this->getDatamodulValue('anzeige_news'));
    $tmp->setValue2Var('anzeige_rss', $this->getDatamodulValue('anzeige_rss'));
    $tmp->gueltigBis = $this->getDatamodulValue('gueltigab');
    $tmp->gueltigBis = $this->getDatamodulValue('gueltigbis');

    return $tmp;
  }

  
  public function getNewsById($pNewsId)
  {
    $news_id = $this->dboperation->getInt4Sql($pNewsId);
    if (empty($news_id)) {
      return $this->getNewObject();
    }
    $sql = 'SELECT ' . $this->select . ' FROM web_aktuelles_wb'
           . ' WHERE id = \'' . $news_id . '\'';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getFirstRow();
  }

  
  public function getExistingNewsIds($pNewsIds)
  {
    if (!is_array($pNewsIds)) {
      throw new Exception('Illegal Argument: $pNewsIds is not an array');
    }
    $ids = array();
    foreach ($pNewsIds as $id) {
      $cleanId = $this->dboperation->getInt4Sql($id);
      if ($cleanId == $id) {
        $ids[] = $id;
      }
    }
    if (empty($ids)) {
      return array();
    }
    $sql = 'SELECT id FROM web_aktuelles_wb WHERE id in (' . implode(',', $ids) . ');';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRowsSingleColumn('id');
  }

  
  public function getNews()
  {
    
    $this->_deleteOldNews();

    $sql = 'SELECT ' . $this->select . ' FROM web_aktuelles_wb'
           . ' ORDER BY erstelldatum DESC';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getNewsForRss()
  {
    
    $this->_deleteOldNews();

    $sql = 'SELECT ' . $this->select . ' FROM web_aktuelles_wb '
           . 'WHERE anzeige_rss = 1 '
           . 'ORDER BY erstelldatum DESC';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  public function getNewsForHomepage()
  {
    
    $this->_deleteOldNews();

    $sql = 'SELECT ' . $this->select . ' FROM web_aktuelles_wb '
           . 'WHERE anzeige_news = 1 '
           . 'ORDER BY erstelldatum DESC';
    $this->dboperation->clear();
    $this->dboperation->query($sql);

    return $this->getAllRows();
  }

  
  private function _deleteOldNews()
  {
    $dtop = new DateTimeOperations();

    $sql = 'DELETE FROM web_aktuelles_wb '
           . 'WHERE NOT gueltigBis IS NULL '
           . 'AND NOT gueltigBis = ' . $dtop->getFormatDate4Server(DEFAULTDATE) . ' '
           . 'AND gueltigBis < ' . $dtop->getFormatDate4Server($dtop->getDateNow());

    $this->dboperation->clear();
    $this->dboperation->query($sql);
  }

  
  public function deleteNewsOlderThan($pDatum)
  {
    $dtop = new DateTimeOperations();

    $sql = 'DELETE FROM web_aktuelles_wb '
            . 'WHERE erstelldatum < ' . $dtop->getFormatDate4Server($pDatum);

    $this->dboperation->clear();
    $this->dboperation->query($sql);
  }

  
  public function createNews($pTitle, $pContent, $pAutor, $pBildpfad, $pShowNews, $pShowRss, $pGueltigAb = DEFAULTDATE, $pGueltigBis = DEFAULTDATE)
  {
    $showNews = ($pShowNews === 'false') ? false : $pShowNews;
    $showrss = ($pShowRss === 'false') ? false : $pShowRss;


    $dtop = new DateTimeOperations();
    if (!$dtop->isValidDate($pGueltigBis) || $dtop->isDatebeforeNow($pGueltigBis)) {
      $pGueltigBis = DEFAULTDATE;
    }
    if (!$dtop->isValidDate($pGueltigAb) || !$dtop->isDatebeforeNow($pGueltigAb)) {
      $pGueltigAb = DEFAULTDATE;
    }

    $news = $this->getNewObject();

    $news->id = $this->insertRecord('web_aktuelles_wb', 'id', 'titel', false);
    $news->titel = $pTitle;
    $news->inhalt = $pContent;
    $news->autor = $pAutor;
    $news->bildpfad = $pBildpfad;
    $news->anzeige_news = ($showNews) ? true : false;
    $news->anzeige_rss = ($showrss) ? true : false;
    $news->gueltigAb = $pGueltigAb;
    $news->gueltigBis = $pGueltigBis;
    $news->erstelldatum = date('d.m.Y H:i');
    $this->updateNewsObject($news);

    return $news->id;
  }

  
  public function updateNews(
    $pNewsId,
    $pTitle = null,
    $pContent = null,
    $pAutor = null,
    $pBildpfad = null,
    $pShowNews = null,
    $pShowRss = null,
    $pGueltigAb = null,
    $pGueltigBis = null,
    $pErstelldatum = null
  ) {
    $showNews = ($pShowNews === 'false') ? false : $pShowNews;
    $showrss = ($pShowRss === 'false') ? false : $pShowRss;
    $newsId = $this->dboperation->getInt4Sql($pNewsId);
    $news = $this->getNewsById($newsId);


    $dtop = new DateTimeOperations();
    if (!$dtop->isValidDate($pGueltigBis) || $dtop->isDatebeforeNow($pGueltigBis)) {
      $pGueltigBis = '';
    }
    if (!$dtop->isValidDate($pGueltigAb) || !$dtop->isDatebeforeNow($pGueltigAb)) {
      $pGueltigAb = '';
    }

    if ($pTitle !== null && $pTitle !== '') {
      $news->titel = $pTitle;
    }
    if ($pContent !== null && $pContent !== '') {
      $news->inhalt = $pContent;
    }
    if ($pAutor !== null && $pAutor !== '') {
      $news->autor = $pAutor;
    }
    if ($pBildpfad !== null) {
      $news->bildpfad = $pBildpfad;
    }
    if ($pShowNews !== null) {
      $news->anzeige_news = ($showNews) ? true : false;
    }
    if ($pShowRss !== null) {
      $news->anzeige_rss = ($showrss) ? true : false;
    }
    $gueltigAb = strtotime($pGueltigAb);
    if ($gueltigAb !== false) {
      $news->gueltigAb = date('d.m.Y H:i', $gueltigAb);
    } elseif ($pGueltigAb === '') {
      $news->gueltigAb = DEFAULTDATE;
    }
    $gueltigBis = strtotime($pGueltigBis);
    if ($gueltigBis !== false) {
      $news->gueltigBis = date('d.m.Y H:i', $gueltigBis);
    } elseif ($pGueltigBis === '') {
      $news->gueltigBis = DEFAULTDATE;
    }
    $date = strtotime($pErstelldatum);
    if ($date !== false) {
      $news->erstelldatum = date('d.m.Y H:i', $date);
    }

    $this->updateNewsObject($news);
  }

  
  public function deleteNews($pNewsId)
  {
    $newsId = $this->dboperation->getInt4Sql($pNewsId);

    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM web_aktuelles_wb WHERE id=' . $newsId);
    
    $this->rssFeedNews->clearCache();
  }

  
  public function deleteNewsBulk($pNewsIds)
  {
    if (!is_array($pNewsIds)) {
      throw new Exception('Illegal Argument: $pNewsIds is not an array');
    }
    $ids = array();
    foreach ($pNewsIds as $id) {
      $cleanId = $this->dboperation->getInt4Sql($id);
      if ($cleanId == $id) {
        $ids[] = $id;
      }
    }
    if (empty($ids)) {
      return;
    }
    $this->dboperation->clear();
    $this->dboperation->execute('DELETE FROM web_aktuelles_wb WHERE id in(' . implode(',', $ids) . ')');

    $this->rssFeedNews->clearCache();
  }

  
  private function updateNewsObject($pNews)
  {
    $orgNews = $this->getNewsById($pNews->id);
    if (!empty($orgNews)) {
      $this->updateObject($pNews, $orgNews, 'web_aktuelles_wb', 'id');
      
      $this->rssFeedNews->clearCache();
    }
  }
}