 <?php

    require_once($path["page"] . "Page.php");
    require_once($path["entity"] . "Guild.php");
    require_once($path["entity"] . "K_Guild_Skill_Group.php");

    /**
     * Guild page.
     */
    class Guild_Page extends Page{

        /**
         * @{see Guild}.
         */
        public $guild = [];

        /**
         * List of {@see K_Guild_Skill_Group}s.
         */
        public $skill_groups = [];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param SQLite3 $db Connection to the database.
         */
        public function __construct($db){
            global $path;
            global $root;
            global $UID;
            parent::__construct($db);
            $this->view = $path["view"] . "guild.php";
            $s = "
              SELECT id
              FROM guild
              WHERE id = (SELECT guild FROM guild_member WHERE id = '$UID');
            ";
            // TODO: Where guild.member.player = ...
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $guild_id =  $r["id"];
                $this->guild = new Guild($db, $guild_id);
                $this->title = $this->guild->name . " - SWDB";
                $this->description = $this->guild->name . " Guild details";
                $s = "
                  SELECT id
                  FROM k_guild_skill_group
                  ORDER BY id;
                ";
                $q = $this->db->query($s);
                while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                    array_push($this->skill_groups, new K_Guild_Skill_Group($db, $r["id"]));
                }
            }
            else{
                $this->title = "Guild - SWDB";
                $this->description = "No guild";
            }
            $this->canonical = $root . "guild/";
        }
    }
?>
