<?php

include_once './includes/users/userRights/MemberRight.inc.php';
include_once './includes/users/userRights/MemberRightFormatter.inc.php';
include_once './includes/users/userRights/MemberRightLister.inc.php';

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

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


    private $content;

    private $title;

    protected function constructAdmin() {

        $this->content = "";

        if (key_exists('memberRightIdentifier', $_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("memberRightIdentifier", $_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::memberRightNew;

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

    private function modify() {
        $identifier = $_GET['memberRightIdentifier'];
        $memberRight = MemberRight::Get($identifier);
        $name = $memberRight->getName();

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

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

        $nameLabel = Localization::memberRightName;
        $submitLabel = Localization::generalSubmit;

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

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

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

        $rights = MemberRightLister::GetAll();

        foreach ($rights as $right) {
            $this->content .= MemberRightFormatter::getSummaryBox($right);
            $this->content .= "<a href='./?page=MemberRightAdministration&memberRightIdentifier=" .
                    $right->getIdentifier() . "'>$editLabel</a> - ";
            $this->content .= "<a href='./?page=MemberRightAdministration&memberRightIdentifier=" .
                    $right->getIdentifier() . "&type=delete'>$deleteLabel</a><br><br>";
        }

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

    private function save() {

        $identifier = $_POST['memberRightIdentifier'];
        $name = $_POST['memberRightName'];

        if ($identifier >= 0) {
            $memberRight = MemberRight::Get($identifier);
            $this->title = Localization::memberRightEdition . " " . $name;
            $memberRight->setName($name);
        } else {
            $article = MemberRight::Create($name);
            $this->title = Localization::memberRightNew . " " . $name;
        }

        $this->content.= "<br>";
        $this->content.= "<a href='./?page=MemberRightAdministration'>" . 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=MemberRightAdministration&memberRightIdentifier=" .
                    $_GET['memberRightIdentifier'] . "&type=delete&sure=yes'>$deleteLabel</a><br><br>";
        } else {
            $result = MemberRight::Remove($_GET['memberRightIdentifier']);
            $this->content .= "$result $deletionLabel";
            $this->content .= "<br>";
            $this->content .= "<a href='./?page=MemberRightAdministration'>" . Localization::generalBackAdmin . "</a>";
        }
    }
}
?>