<?php

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

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

/**
 * This class allows for easy formatting of lists of MemberRights.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class MemberRightListFormatter {


    public static function GetForm(Member $user) {
        $rights = MemberRightLister::GetAll();

        if (key_exists("username", $_SESSION)) {
            $adminUser = MemberLister::getByLogin($_SESSION['username']);
            if (!$adminUser) throw new Exception("What the frack is going on ?");
        }

        $form = "<select multiple name='memberRight[]'>";
        $hiddenForm = "";

        foreach($rights as $right) {
            if (MemberRightArray::hasRight($adminUser, $right) || ($right->getName() == "Membre")) {
                $form .= MemberRightListFormatter::GetCheckBox($user, $right);
            } else {
                $hiddenForm .= MemberRightListFormatter::GetHiddenRight($user, $right);
            }
        }
        $form .= "</select>";

        return $form . $hiddenForm;
    }

    public static function GetBlankForm() {
        $rights = MemberRightLister::GetAll();

        if (key_exists("username", $_SESSION)) {
            $adminUser = MemberLister::getByLogin($_SESSION['username']);
            if (!$adminUser) throw new Exception("What the frack is going on ?");
        }

        $form = "<select multiple name='memberRight[]'>";
        $hiddenForm = "";

        foreach($rights as $right) {
            if (MemberRightArray::hasRight($adminUser, $right) || ($right->getName() == "Membre")) {
                $rightId = $right->getIdentifier();
                $rightName = $right->getName();
                $form .= "<option value='$rightId'>$rightName</option><br>";
            }
        }
        $form .= "</select>";

        return $form . $hiddenForm;
    }

    private static function GetCheckBox(Member $user, MemberRight $right) {
        $rightId = $right->getIdentifier();
        $rightName = $right->getName();

        $selected = MemberRightArray::hasRight($user,$right) ? "selected" : "";

        $entry = "<option value='$rightId' $selected>$rightName</option><br>";

        return $entry;
    }

    private static function GetHiddenRight(Member $user, MemberRight $right) {
        $rightId = $right->getIdentifier();

        if (MemberRightArray::hasRight($user,$right)) {
            $entry = "<input type=hidden name='memberHiddenRight[]' value='". $rightId ."'>";
        } else {
            $entry = "";
        }

        return $entry;
    }

    public static function formatArray(array $rights) {

        $rightsString = "";

        foreach ($rights as $right) {
            $rightsString .= MemberRightFormatter::format($right);
            $rightsString .= " ";
        }

        return $rightsString;
    }
}
?>
