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

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

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

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

include_once './includes/exceptions/BadFormatException.inc.php';

/**
 * This class represents the administration part of the news system.
 *
 * It is responsible for the editing, the creation and deletion of news.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class NewsAdministrationPage extends AdministrationPage {

    private $content;

    private $title;

    protected function constructAdmin() {
        if (key_exists("articleText", $_POST)) {
            $this->save();
        } else if (key_exists("type", $_GET) AND ($_GET['type'] == "create")) {
            $this->create();
        } else if (key_exists("type", $_GET) AND ($_GET['type'] == "delete")) {
            $this->delete();
        } else if (key_exists("articleId", $_GET)) {
            $this->modify();
        } else {
            $this->listing();
        }
    }

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

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

    private function save() {

        $identifier = $_POST['articleIdentifier'];
        $abstract = $_POST['articleAbstract'];
        $text = $_POST['articleText'];

        if (key_exists("articleDate_wholeDay", $_POST)) {
            $hour = 0;
            $minute = 0;
        } else {
            $hour = $_POST['articleDate_hours'];
            $minute = $_POST['articleDate_minutes'];
        }

        $date = mktime($hour,$minute,0,
                $_POST['articleDate_months'], $_POST['articleDate_days'], $_POST['articleDate_year']);

        $username = $_SESSION['username'];
        $writingDate = time();
        $author = MemberLister::getByLogin($username)->getIdentifier();

        $this->title = Localization::newsEdit;

        if ($identifier >= 0) {
            $article = Article::Get($identifier);
            $article->setContent($abstract, $text, $date);
        } else {
            $article = Article::Create($writingDate, $date, $author, $abstract, $text);
        }

        $this->content .= Localization::newsGo . " <a href='./?page=News&articleIdentifier=" .
                $article->getIdentifier() . "'>" . $article->getAbstract() . "</a><br>\n";
        $this->content.= "<br>";
        $this->content.= "<a href='./?page=NewsAdministration'>" . Localization::generalBackAdmin . "</a>";

    }

    private function create() {
        $identifier = -1;
        $title = Localization::newsNewTitle;
        $text = Localization::newsNewText;

        $date = time();

        $this->form($identifier, $title, $text, $date);
    }

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

        if ($article) {
            $identifier = $article->getIdentifier();
            $title = $article->getAbstract();
            $text = $article->getText();
            $date = $article->getDate();

            $this->form($identifier, $title, $text, $date);
        }
    }

    private function form($identifier, $title, $text, $date) {
        $this->title = Localization::newsEdit . ": " . $title;

        $labelTitle = Localization::newsTitle;
        $labelPublicationDate = Localization::newsDate;
        $labelPublicationHour = Localization::newsHour;
        $labelText = Localization::newsText;

        $formDate = DateFormatter::GetDMYForm("articleDate", $date);
        $formHour = DateFormatter::getHHMMForm("articleDate", $date);

        $this->content = <<<FORM
<form name="articleCreation" method="post" action="./?page=NewsAdministration">
    <input name="articleIdentifier" type="hidden" value="$identifier">
    <label for="articleAbstract">$labelTitle</label>
    <input name="articleAbstract" id="articleAbstract" type="text" value="$title"><br>
    <label for="articleDate">$labelPublicationDate</label>
    $formDate<br>
    <label for="articleDate">$labelPublicationHour</label>
    $formHour<br>
    <label for="articleText">$labelText</label>
    <textarea rows="10" cols="30" name="articleText" id="articleText">$text</textarea>
    <input type="submit" name="Submit" value="Submit">
</form>
FORM;
    }

    private function listing() {
        $this->title = Localization::newsAdmin;

        $articleArray = ArticleLister::getAllArticles();

        $labelEdit = Localization::generalEdit;
        $labelDelete = Localization::generalDelete;
        $labelCreate = Localization::generalCreate;

        foreach ($articleArray as $article) {
            $this->content .= ArticleFormatter::getSummaryBox($article);
            $this->content .= "<a href='./?page=NewsAdministration&articleId=" .
                    $article->getIdentifier() . "'>$labelEdit</a> - ";
            $this->content .= "<a href='./?page=NewsAdministration&articleId=" .
                    $article->getIdentifier() . "&type=delete'>$labelDelete</a><br><br>";
        }

        $this->content .= "<a href='./?page=NewsAdministration&type=create'>$labelCreate</a><br><br>\n\n";
    }

    private function delete() {
        $this->title = Localization::newsAdmin;

        $labelDelete = Localization::generalDelete;
        
        if (!key_exists("sure", $_GET)) {
            $this->content = Localization::generalDeletionConfirmation;
            $this->content.= "<br>\n<a href='./?page=NewsAdministration&articleId=" .
                    $_GET['articleId'] . "&type=delete&sure=yes'>$labelDelete</a><br><br>";
        } else {
            $result = Article::Remove($_GET['articleId']);
            $this->content = $result . " " . Localization::generalDeletionNumber;
            $this->content.= "<br>\n";
            $this->content.= "<a href='./?page=NewsAdministration'>" . Localization::generalBackAdmin . "</a>";
        }
    }
}
?>
