<?php

include_once './includes/menu/MenuEntry.inc.php';
include_once './includes/menu/MenuEntryFormatter.inc.php';
include_once './includes/menu/MenuEntryLister.inc.php';

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

/**
 * This class represents the administration part of the menu system.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class MenuAdministrationPage extends AdministrationPage {

    private $title;

    private $content;

    protected function constructAdmin() {
        $this->title = Localization::menuEntryAdministration;
        if (key_exists('entryIdentifier', $_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("entryIdentifier", $_GET)) {
            $this->modify();
        } else {
            $this->listing();
        }
    }

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

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

    private function save() {

        $identifier = $_POST['entryIdentifier'];
        $name = $_POST['entryName'];
        $hint = $_POST['entryHint'];
        $url = $_POST['entryUrl'];
        $menuId = $_POST['entryMenuId'];
        $weight = $_POST['entryWeight'];

        if ($identifier >= 0) {
            $entry = MenuEntry::Get($identifier);
            $entry->setContent($name, $hint, $url, $menuId, $weight);
        } else {
            $article = MenuEntry::Create($name, $hint, $url, $menuId, $weight);
        }

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

    private function create() {
        $name = Localization::menuEntryNew;
        $hint = Localization::menuEntryNew;
        $url = "Index";
        $menuId = 0;
        $weight = 0;

        $this->form(-1, $name, $hint, $url, $menuId, $weight);

    }

    private function modify() {
        $entry = MenuEntry::Get($_GET['entryIdentifier']);

        //TODO: Some security if entry doesn't exist.

        $identifier = $entry->getIdentifier();
        $name = $entry->getName();
        $hint = $entry->getHint();
        $url = $entry->getUrl();
        $menuId = $entry->getMenuId();
        $weight = $entry->getWeight();

        $this->form($identifier, $name, $hint, $url, $menuId, $weight);
    }

    private function form($identifier, $name, $hint, $url, $menuId, $weight) {
        $this->title = Localization::menuEntryEdition . " : " . $name;

        $labelName = Localization::menuEntryName;
        $labelHint = Localization::menuEntryHint;
        $labelUrl = Localization::menuEntryURL;
        $labelMenuId = Localization::menuEntryMenuId;
        $labelWeight = Localization::menuEntryWeight;

        $this->content = <<<FORM
<form name="menuCreation" method="post" action="./?page=MenuAdministration">
    <input name="entryIdentifier" type="hidden" value="$identifier">
    <label for="entryName">$labelName</label>
    <input name="entryName" type="text" value="$name"><br>
    <label for="entryHint">$labelHint</label>
    <input name="entryHint" type="text" value="$hint"><br>
    <label for="entryUrl">$labelUrl</label>
    <input name="entryUrl" type="text" value="$url"><br>
    <label for="entryMenuId">$labelMenuId</label>
    <input name="entryMenuId" type="text" value="$menuId"><br>
    <label for="entryWeight">$labelWeight</label>
    <input name="entryWeight" type="text" value="$weight"><br>
    <input type="submit" name="Submit" value="Submit">
</form>
FORM;
    }

    private function listing() {
        $entriesArray = MenuEntryLister::getAllEntries();

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

        foreach ($entriesArray as $entry) {
            $this->content .= MenuEntryFormatter::getSummaryBox($entry);
            $this->content .= "<a href='./?page=MenuAdministration&entryIdentifier=" .
                    $entry->getIdentifier() . "'>$labelEdit</a> - ";
            $this->content .= "<a href='./?page=MenuAdministration&entryIdentifier=" .
                    $entry->getIdentifier() . "&type=delete'>$labelDelete</a><br><br>";
        }

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

    private function delete() {
        if (!key_exists("sure", $_GET)) {
            $labelDelete = Localization::generalDelete;

            $this->content = Localization::generalDeletionConfirmation . "<br>";
            $this->content.= "<a href='./?page=MenuAdministration&entryIdentifier=" .
                    $_GET['entryIdentifier'] . "&type=delete&sure=yes'>$labelDelete</a><br><br>";
        } else {
            $result = MenuEntry::Remove($_GET['entryIdentifier']);
            $this->content = $result . " " . Localization::generalDeletionNumber;
            $this->content.= "<br>";
            $this->content.= "<a href='./?page=MenuAdministration'>" . Localization::generalBackAdmin . "</a>";
        }
    }
}
?>
