<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */

namespace Kufer\Web\KuferTools\Allgemein\Aktuelles;

use Kufer\Web\KuferTools\Utils\DatamodulHelper\Aktuelles\AktuellesWTHelper;

abstract class SingleNewsUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  private $news;

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess($pToolSectionId)) {
      return false;
    }

    if (!\CleanOperations::isInt($this->request->news_id)) {
      return false;
    }

    $news = $this->getNews();
    if (is_null($news)) {
      return false;
    }

    return true;
  }

  protected function getNews(): ?\Aktuelles
  {
    if (!is_null($this->news)) {
      return $this->news;
    }

    $dbwAktuelles = \DBWorkAktuellesWT::getDefaultInstance();

    $filter = AktuellesWTHelper::createBasic($this->toolSettings, $this->getUser())->getFilter($this->getToolId());
    $filter->equalsId((int)$this->request->news_id);

    $this->news = $dbwAktuelles->findOneByFilter($filter);

    return $this->news;
  }
}