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

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


    /**
     * Building.
     *
     * Represents an object from the table 'k_building'.
     *
     * @category Entity
     */
    class K_Building extends Entity{

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

        /**
         * @var string Building name.
         */
        public $name;

        /**
         * @var string Building description.
         */
        public $description;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * building, populating it and it's items.
         *
         * @param resource $db Connection to the database.
         * @param int $id Building identifier.
         */
        public function __construct($db, $id){
            parent::__construct($db);
            $s =
              "SELECT " .
              "  id, " .
              "  name, " .
              "  description " .
              "FROM k_building " .
              "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->description = HTML::e($r["description"]);
            }
        }

        /**
         * Gets the path to the building image. The image must be in the
         * img/building/ directory, and its name must be the monster id,
         * padded with '0' to 4 digites, and the extension must be '.png'.
         *
         * @return string Image URL, or a fixed unknown image.
         */
        public function get_image(){
            if (file_exists(PATH::BASE . "img/building/" . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png")){
                return URL::IMG["BUILDING"] . str_pad($this->id, 4, '0', STR_PAD_LEFT) . ".png";
            }
            else{
                return URL::IMG["UNKNOWN"];
            }
        }

    }
?>

