<?php

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


class ToolAktuelles extends ToolBase
{
  public function __construct()
  {
    parent::__construct();
    $this->toolTitle = 'aktuelle Nachrichten';

    

    $this->toolSettings = ToolSettingsAPI::getToolSettings($this->toolId);
  }

  
  public function init($pPageId)
  {
    
    if (!SessionOperations::getVarValueWTIntern('checkDateAktuelles', false)) {
      $this->_delOldNews();
      SessionOperations::setVarValueWTIntern('checkDateAktuelles', true);
    }

    switch ($pPageId) {
      default:
      case 266:
        $page = new PageAktuellesUebersicht($this);
        break;
      case 267:
        $page = new PageAktuellesNeueNachricht($this);
        break;
    }
    if ($page == null) {
      throw new Exception('ungültige PageId');
    }
    $this->currentPage = $page;
  }

  
  private function _delOldNews()
  {
    $dbw = new DBWorkAktuellesWT();

    $dbw->newSearch();
    $heute = DateTimeOperations::getDateNow();
    $dbw->toobject->gueltigBis = DateTimeOperations::getDateDaysBefore($heute, 1);
    $newsToDel = $dbw->getAktuellesBySearch();

    $newsIDsToDel = array();
    foreach ($newsToDel as $news) {
      $newsIDsToDel[] = $news->id;
    }

    $dbw->deleteAktuellesBulk($newsIDsToDel);
  }

  public function getToolId()
  {
    return \ToolsAPI::TOOL_AKTUELLES;
  }
}