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

namespace Kufer\KuferTools\Allgemein\Aktuelles\LoescheNews;

class LoescheNewsUseCase extends \Kufer\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new LoescheNewsResponse());
  }

  public static function getToolId()
  {
    return 16;
  }

  public static function getToolSectionId()
  {
    return 266;
  }

  public function hasAccess($pToolSectionId = null)
  {
    if (!parent::hasAccess()) {
      return false;
    } elseif (!$this->request->isXsrfOk && !$this->request->isRefresh) {
      return false;
    }

    if (!$this->isRechtAllowed('266.0.3')) { 
      return false;
    }

    
    $news = $this->getNews();
    $user = $this->getUser();
    if ($user->id != $news->autor_id && !\SessionOperations::isAdmin()) {
      return false;
    }

    return true;
  }

  private function getNews()
  {
    $dbwAktuelles = new \DBWorkAktuellesWT(1, true);

    return $dbwAktuelles->getAktuelles($this->request->news_id);
  }

  public function action()
  {
    if (!$this->request->isRefresh) {
      $this->deleteNews();
    }

    return $this->getResponse();
  }

  private function deleteNews()
  {
    $dbwAktuelles = new \DBWorkAktuellesWT(1, true);
    $dbwAktuelles->deleteAktuelles($this->request->news_id);
  }

  protected function getResponse()
  {
    return $this->response;
  }
}

class LoescheNewsRequest
{
  public $news_id;
  public $isRefresh;
  public $isXsrfOk;
}

class LoescheNewsResponse extends \Kufer\KuferTools\Basis\UseCase\ToolResponse
{
}