<?php

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

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

include_once './includes/config/Configuration.inc.php';

/**
 * This object serves as a formatter for Articles.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class ArticleFormatter {

    /**
     * Builds the HTML syntax for a summary box
     *
     * @return string
     */
    public static function getSummaryBox(Article $article) {
        return ArticleFormatter::getHeaders($article, true);
    }

    /**
     * Builds the HTML syntax for the Article page.
     *
     * @return string
     */
    public static function getFullArticle(Article $article) {
        $identifier = $article->getIdentifier();

        $text = OutputSanitizer::nl2br($article->getText());
        $author = $article->getAuthor();

        $date = DateFormatter::format($article->getDate());

        $content = ArticleFormatter::getHeaders($article, false);

        $content .= <<<PAGE
<div id='news-$identifier-text' class="news_box_full">
    <p>
        $text
    </p>
</div>
PAGE;
        return $content;
    }

    private static function getHeaders(Article $article, $linkOnName) {
        $identifier = $article->getIdentifier();
        $abstract = $article->getAbstract();
        $author = $article->getAuthor();
        $date = DateFormatter::formatDMY($article->getWritingDate());

        $authorWrote = Localization::authorWroteDate($author, $date);

        $title = DateFormatter::formatDMYHM($article->getDate()) . " : " . $abstract;

        if ($linkOnName) {
            $nameLine = "<a href='index.php?page=News&amp;articleIdentifier=$identifier'>$title</a>";
        } else {
            $nameLine = $abstract;
        }

        $nameLine = "<h2>$nameLine</h2>\n";

        $content = <<<BOX

        <div id='news-$identifier' class="news_box">
            $nameLine
            <p>
                $authorWrote
            </p>
        </div>

BOX;
        return $content;
    }

    /**
     * Builds the XML syntax for the RSS.
     *
     * @return string
     */
    public static function getXML(Article $article) {

        $website = Configuration::SiteURL;

        $identifier = $article->getIdentifier();
        $abstract = $article->getAbstract();

        $text = $article->getText();
        $author = $article->getAuthor();

        $title = DateFormatter::formatDMYHM($article->getDate()) . " : " . $abstract;

        $date = DateFormatter::formatRFC($article->getDate());

        $content = <<<ENTRY
<item>
    <title>$title</title>
    <description>$text</description>
    <link>$website</link>
    <guid isPermaLink="false"> $identifier</guid>
    <pubDate>$date</pubDate>
</item>
ENTRY;
        return $content;
    }
}
?>
