<?php

include_once './includes/tournaments/Category.inc.php';

include_once './includes/utils/MapperUtils.inc.php';

/**
 * This class allows for easy listing of categories.
 *
 * @author Thomas Schwery <thomas.schwery@epfl.ch>
 */
class CategoryLister {

    public static function GetAllCategories() {
        return CategoryLister::GetCategories("");
    }

    public static function GetCategoryByName($name) {
        $categories = CategoryLister::GetCategories("WHERE name = '$name'");

        if (array_key_exists("0", $categories) AND is_a($categories[0], "Category")) {
            return $categories[0];
        } else {
            return false;
        }
    }

    public static function GetCategoriesForMember(Member $member) {
        $userId = $member->getIdentifier();

        $sql = "SELECT DISTINCT categoryId FROM tournamentsEntries where userId = $userId;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

        if ($result) {
            $result1 = array_map("mapIdentifierToCategory", $result);
            return $result1;
        } else {
            return array();
        }
    }

    public static function GetCategoriesForTournament(Tournament $tournament) {
        $tournamentId = $tournament->getIdentifier();

        $sql = "SELECT DISTINCT categoryId FROM tournamentsEntries where tournamentId = $tournamentId;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

        if ($result) {
            $result1 = array_map("mapIdentifierToCategory", $result);
            return $result1;
        } else {
            return array();
        }
    }

    private static function GetCategories($restriction) {
        $sql = "SELECT identifier FROM categories $restriction;";
        $result = MySQLDatabase::getInstance()->runRequest($sql);

        if ($result) {
            $result1 = array_map("mapIdentifierToCategory", $result);
            return $result1;
        } else {
            return array();
        }
    }
}
?>
