<?php

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

include_once './includes/tournaments/CategoryLister.inc.php';
include_once './includes/tournaments/CategoryFormatter.inc.php';

/**
 * This class represents the Administration of the Categories part of
 * the site.
 *
 * A category is a style of bow.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class CategoryAdministrationPage extends AdministrationPage {

    private $content;

    private $title;

    protected function constructAdmin() {

        $this->content = "";

        if (key_exists('categoryIdentifier', $_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("categoryIdentifier", $_GET)) {
            $this->modify();
        } else {
            $this->listing();
        }
    }

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

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

    private function create() {
        $identifier = -1;
        $name = Localization::categoryNew;

        $this->form($identifier, $name);
    }

    private function modify() {
        $identifier = $_GET['categoryIdentifier'];
        $category = Category::Get($identifier);
        $name = $category->getName();

        $this->form($identifier, $name);
    }

    private function form($identifier, $name) {
        $this->title = Localization::categoryEdition . ": $name";

        $nameLabel = Localization::categoryName;

        $this->content = <<<FORM
<form name="categoryCreation" method="post" action="./?page=CategoryAdministration">
    <input name="categoryIdentifier" type="hidden" value="$identifier">
    <label for="categoryName">$nameLabel</label>
    <input name="categoryName" id="categoryName" type="text" value="$name"><br>
    <input type="submit" name="Submit" value="Submit">
</form>
FORM;
    }

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

        $editLabel = Localization::generalEdit;
        $deleteLabel = Localization::generalDelete;
        $createLabel = Localization::generalCreate;

        $categoriesArray = CategoryLister::getAllCategories();

        foreach ($categoriesArray as $category) {
            $this->content .= CategoryFormatter::getSummaryBox($category);
            $this->content .= "<a href='./?page=CategoryAdministration&categoryIdentifier=" .
                    $category->getIdentifier() . "'>$editLabel</a> - ";
            $this->content .= "<a href='./?page=CategoryAdministration&categoryIdentifier=" .
                    $category->getIdentifier() . "&type=delete'>$deleteLabel</a><br><br>";
        }

        $this->content .= "<a href='./?page=CategoryAdministration&type=create'>$createLabel</a><br><br>";
    }

    private function save() {

        $identifier = $_POST['categoryIdentifier'];
        $name = $_POST['categoryName'];

        if ($identifier >= 0) {
            $category = Category::Get($identifier);
            $this->title = Localization::categoryEdition . " " . $name;
            $category->setContent($name);
        } else {
            $article = Category::Create($name);
            $this->title = Localization::categoryNew . " " . $name;
        }

        $this->content .= "<br>";
        $this->content .= "<a href='./?page=CategoryAdministration'>" . Localization::generalBackAdmin . "</a>";
    }

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

        $deleteLabel = Localization::generalDelete;
        $deletionLabel = Localization::generalDeletionNumber;

        if (!key_exists("sure", $_GET)) {
            $this->content = Localization::generalDeletionConfirmation . "<br>";
            $this->content.= "<a href='./?page=CategoryAdministration&categoryIdentifier=" .
                    $_GET['categoryIdentifier'] . "&type=delete&sure=yes'>$deleteLabel</a><br><br>";
        } else {
            $result = Category::Remove($_GET['categoryIdentifier']);
            $this->content .= "$result $deletionLabel";
            $this->content .= "<br>";
            $this->content .= "<a href='./?page=CategoryAdministration'>" . Localization::generalBackAdmin . "</a>";
        }
    }
}
?>
