<?php

include_once './pages/PageInterface.inc.php';

include_once './includes/news/Article.inc.php';
include_once './includes/news/ArticleLister.inc.php';
include_once './includes/news/ArticleFormatter.inc.php';

include_once './includes/tournaments/Tournament.inc.php';
include_once './includes/tournaments/TournamentLister.inc.php';
include_once './includes/tournaments/TournamentFormatter.inc.php';

include_once './includes/users/Member.inc.php';
include_once './includes/users/MemberLister.inc.php';
include_once './includes/users/MemberFormatter.inc.php';

include_once './includes/utils/DateFormatter.inc.php';

/**
 * This class represents the viewing of news.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class NewsPage implements PageInterface {

    private $title;

    private $content;

    public function  __construct() {
        if (key_exists("articleIdentifier", $_GET)) {
            $this->articleView();
        } else {
            $this->listing();
        }
    }

    private function articleView() {
        $articleIdentifier = $_GET['articleIdentifier'];
        $article = Article::Get($articleIdentifier);

        if ($article) {
            $this->title = $article->getAbstract();
            $this->content = ArticleFormatter::getFullArticle($article);
        }
    }

    private function listing() {

        $this->title = Localization::newsList;

        if (key_exists("windowStart", $_GET)) {
            $windowStart = $_GET['windowStart'];
        } else {
            $windowStart = strtotime("today");
        }

        $windowEnd = strtotime("+1 month", $windowStart);
        $windowPrevious = strtotime("-1 month", $windowStart);
        $windowStart = strtotime("-1 day", $windowStart);

        $start = DateFormatter::formatDMY($windowStart);
        $end = DateFormatter::formatDMY($windowEnd);

        $display_text = Localization::displayingPeriod($start, $end);
        $previous_text = Localization::previousMonth;
        $next_text = Localization::nextMonth;

        $content =  <<<CONT
        $display_text <br>
        <div class='center_menu'>
        <a href='./?page=News&amp;windowStart=$windowPrevious'> $previous_text </a>
         -
        <a href='./?page=News&amp;windowStart=$windowEnd'> $next_text </a>
        </div>
CONT;

        $articleArray = ArticleLister::getArticleByDate($windowStart, $windowEnd);
        $tournamentArray = TournamentLister::getTournamentsByDate($windowStart, $windowEnd);
        $usersArray = MemberLister::getByBirthday($windowStart, $windowEnd);

        $entriesArray = array_merge($articleArray, $tournamentArray, $usersArray);
        usort($entriesArray, "dateCmp");

        if (count($entriesArray) == 0) {
            $content .= "<p>";
            $content .= Localization::noEntries;
            $content .= "</p>";
        }

        foreach ($entriesArray as $entry) {
            if ($entry instanceof Article) {
                $content .= ArticleFormatter::getSummaryBox($entry);
            }
            if ($entry instanceof Tournament) {
                $content .= TournamentFormatter::getSummary($entry);
            }
            if ($entry instanceof Member) {
                $content .= MemberFormatter::getSummaryBox($entry);
            }
        }

        $this->content = $content;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getContent() {
        return $this->content;
    }

    public function getHeaderEntry() {
        if (file_exists("./news_rss.php")) {
        return <<<HEADER
<link rel="alternate" title="RSS Feed" href="news_rss.php" type="application/rss+xml" >
HEADER;
        } else {
            return "";
        }
    }

    public static function getStaticTitle() {
        return Localization::newsList;
    }
}

function dateCmp($a, $b) {
    if ($a->getDate() == $b->getDate()) {
        return 0;
    }
    return ($a->getDate() < $b->getDate()) ? -1 : 1;
}
?>
