<?php
    /**
     * Guild page file.
     *
     * Provides a class with all the properties and methods to display the page.
     *
     * @category Page
     */

    /**
     * Require dependent files if not present.
     */
    require_once(PATH::PAGE . "Page.php");
    require_once(PATH::ENTITY . "Guild.php");
    require_once(PATH::ENTITY . "K_Guild_Skill_Group.php");

    /**
     * Guild page model.
     *
     * @category Page
     */
    class Guild_Page extends Page{

        /**
         * @var Guild Player guild.
         */
        public $guild;

        /**
         * @var \K_Guild_Skill_Group[] List of Guild skill groups.
         */
        public $skill_groups = [];

        /**
         * Constructor.
         *
         * Retrieves the data and initializes the variables.
         *
         * @param resource $db Connection to the database.
         * @global int Player id.
         */
        public function __construct($db){
            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');
            ";
            $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 = URL::BASE . "guild/";
        }
    }
?>

