<?php

    require_once($path["entity"] . "Entity.php");
    require_once($path["entity"] . "Project_Version_Type.php");
    require_once($path["helper"] . "text.php");

    /**
     * Version of a project.
     *
     * Represents an object from the table 'project_version'.
     */
    class Project_Version extends Entity{

        /**
         * Project identifier.
         */
        public $project;

        /**
         * Version code, autogenerated.
         */
        public $code;

        /**
         * Version name or number.
         */
        public $name;

        /**
         * Version {@see Project_Version_Type}.
         */
        public $type;

        /**
         * Short text about the version, in the defined language.
         */
        public $summary;

        /**
         * Full changelog, in the defined language.
         */
        public $changelog;

        /**
         * Time the version was released at.
         */
        public $dtime;

        /**
         * Indictes wether the version must be visible.
         */
        public $visible;

        /**
         * Constructor.
         *
         * Searches the database and retrieves the information about the
         * version, populating it and its items.
         *
         * @param MySQL_connection $db Connection to the database.
         * @param string $lang Lowercase, two-letter language code.
         * @param int $project Project identifier.
         * @param int $code Version code.
         */
        public function __construct($db, $lang, $project, $code){
            parent::__construct($db, $lang);
            $s =
              "SELECT " .
              "  project, " .
              "  version_code AS code, " .
              "  version_name AS name, " .
              "  type, " .
              "  changelog, " .
              "  dtime, " .
              "  visible " .
              "FROM project_version " .
              "WHERE " .
              "  project = $project AND " .
              "  version_code = $code ;";
            $q = mysqli_query($this->db, $s);
            if (mysqli_num_rows($q) > 0){
                $r = mysqli_fetch_array($q);
                $this->project = $r["project"];
                $this->code = $r["code"];
                $this->name = $r["name"];
                $this->changelog = text($this, $r["changelog"]);
                $this->dtime = $r["dtime"];
                $this->visible = $r["visible"];
                $this->type = new Project_Version_Type($this->db, $this->lang, $r["type"]);
            }
        }
    }
?>
