<?php
    /**
     * Guild entity file.
     *
     * Creates the entity and makes it available.
     *
     * @category Entity
     */

    /**
     * Require dependent entities if not present.
     */
    require_once(PATH::ENTITY . "Entity.php");
    require_once(PATH::ENTITY . "Guild_Member.php");

    /**
     * Guild.
     *
     * Represents an object from the table 'guild'.
     *
     * @category Entity
     */
    class Guild extends Entity{

        /**
         * @var int @var int Guild identifier.
         */
        public $id;

        /**
         * @var int @var string Guild name.
         */
        public $name;

        /**
         * @var int @var int Guild level.
         */
        public $level;

        /**
         * @var int Guild total experience.
         */
        public $experience;

        /**
         * @var bool Indicates if the guild is ecruiting.
         */
        public $recruiting;

        /**
         * @var int Number of members.
         */
        public $total_members;

        /**
         * @var GUild_Member Guild Leader.
         */
        public $leader;

        /**
         * @var string Guild comment.
         */
        public $comment;

        /**
         * @var string Guild notice.
         */
        public $notice;

        /**
         * @var \Guild_Member[] List of {@see Guild_Member}s.
         */
        public $members = [];

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * guild, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Guild identifier.
         */
        public function __construct($db, $id){
            global $path;
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  name, " .
              "  name, " .
              "  level, " .
              "  experience, " .
              "  recruiting, " .
              "  members, " .
              "  leader, " .
              "  comment, " .
              "  notice " .
              "FROM guild " .
              "WHERE id = $id; ";
            $q = $this->db->query($s);
            $r = $q->fetchArray(SQLITE3_ASSOC);
            if ($r){
                $this->id = $r["id"];
                $this->name = HTML::e($r["name"]);
                $this->level = $r["level"];
                $this->experience = $r["experience"];
                $this->recruiting = $r["recruiting"];
                $this->total_members = $r["members"];
                $this->leader = $r["leader"];
                $this->comment = HTML::e($r["comment"]);
                $this->notice = HTML::e($r["notice"]);
            }
            $s =
              "SELECT id " .
              "FROM guild_member " .
              "WHERE guild = $this->id " .
              "ORDER BY " .
              "  grade = 1 DESC, " .
              "  grade = 3 DESC, " .
              "  grade = 2 DESC, " .
              "  arena_score DESC; ";
            $q = $this->db->query($s);
            while ($r = $q->fetchArray(SQLITE3_ASSOC)){
                array_push($this->members, new Guild_Member($db, $r["id"]));
            }
        }


    }
?>

