| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- <?php
- require_once(PATH::ENTITY . "Entity.php");
- require_once(PATH::ENTITY . "License.php");
- require_once(PATH::ENTITY . "Project_Type.php");
- require_once(PATH::ENTITY . "Project_Image.php");
- require_once(PATH::ENTITY . "Project_Url.php");
- /**
- * Project.
- *
- * Represents an object from the table 'project'.
- */
- class Project extends Entity{
- /**
- * @var int Project identifier.
- */
- private $id;
- /**
- * @var string Permalink for linking the project (relative).
- */
- private $permalink;
- /**
- * @var int Project index for sorting.
- */
- private $index;
- /**
- * @var int Project type identifier.
- */
- private $type_id;
-
- /**
- * @var Project_Type Type of project.
- */
- private $type;
- /**
- * @var string Project title in the selected language.
- */
- private $title;
- /**
- * @var string Project logo filename.
- */
- private $logo;
- /**
- * @var string Project summary in the selected language.
- */
- private $header;
- /**
- * @var String Project description in the selected language.
- */
- private $text;
- /**
- * @var String Project commentary by the developer.
- */
- private $comment;
-
- /**
- * @var int License identifier.
- */
- private $license_id;
- /**
- * @var License Project license.
- */
- private $license;
- /**
- * @var Project_Image[] List of project images.
- */
- private $images = [];
- /**
- * @var Project_Tag[] List of project tags.
- */
- private $tags = [];
- /**
- * @var Project_URL[] List of the project URLs
- */
- private $urls = [];
-
- /**
- * @var bool[] Control flags to check loading status.
- */
- private $load_flags = [
- "type" => false,
- "images" => false,
- "tags" => false,
- "urls" => false,
- "license" => false
- ];
- /**
- * Constructor.
- *
- * Searches the database and retrieves the information about the
- * project, populating it and it's items.
- *
- * @param string $id Identifier or permalink of the project.
- */
- public function __construct($id){
- Log::debug("Loading project with ID: " . $id);
- $statement = get_context()->get_db()->prepare("
- SELECT id, permalink, idx, type, title, logo, header, text, comment, license
- FROM project
- WHERE id = :id OR permalink = :permalink
- LIMIT 1
- ");
- $statement->bindValue(':id', $id, PDO::PARAM_INT);
- $statement->bindValue(':permalink', $id, PDO::PARAM_STR);
- $statement->execute();
- $r_project = $statement->fetch(PDO::FETCH_ASSOC);
- if ($r_project !== false){
- $this->id = $r_project["id"];
- $this->permalink = $r_project["permalink"];
- $this->idx = $r_project["idx"];
- $this->title = Text::get($r_project["title"]);
- $this->logo = $r_project["logo"];
- $this->header = Text::get($r_project["header"], false);
- $this->text = Text::get($r_project["text"], false);
- $this->comment = Text::get($r_project["comment"], false);
- $this->type_id = $r_project["type"];
- $this->license_id = $r_project["license"];
- }
- else{
- Log::debug("Project with id '$id' doesn't exist");
- }
- }
- /**
- * Retrieves the project identifier
- *
- * @return int Project ID.
- */
- public function get_id(){
- return $this->id;
- }
- /**
- * Retrieves the project permalink
- *
- * @return string Project permalink
- */
- public function get_permalink(){
- return $this->permalink;
- }
- /**
- * Retrieves the project index for sorting purpuses.
- *
- * @return int Project index
- */
- public function get_index(){
- return $this->index;
- }
- /**
- * Loads the project type information.
- *
- * Not exposed, must be called before accessing the project type.
- */
- private function load_type(){
- if ($this->load_flags["type"] === false){
- $this->type = new Project_Type($this->type_id);
- $this->load_flags["type"] = true;
- }
- }
-
- /**
- * Retrieves the project type.
- *
- * @return Project_Type The project type.
- */
- public function get_type(){
- if ($this->load_flags["type"] === false){
- $this->load_type();
- }
- return $this->type;
- }
- /**
- * Retrieves the project title.
- *
- * @return string Title.
- */
- public function get_title(){
- return $this->title;
- }
- /**
- * Retrieves the project logo.
- *
- * @return string The logo filename.
- */
- public function get_logo(){
- return $this->logo;
- }
- /**
- * Retrieves a short project description.
- *
- * @return string Project header.
- */
- public function get_header(){
- return $this->header;
- }
- /**
- * Retrieves the project description.
- *
- * @return string Project description text.
- */
- public function get_text(){
- return $this->text;
- }
- /**
- * Retrieves the project comment.
- *
- * @return string Project comment text.
- */
- public function get_comment(){
- return $this->comment;
- }
- /**
- * Loads the project license information.
- *
- * Not exposed, must be called before accessing the project license.
- */
- private function load_license(){
- if ($this->load_flags["license"] === false){
- $this->license = new License($this->license_id);
- $this->load_flags["license"] = true;
- }
- }
- /**
- * Retrieves the project license.
- *
- * @return License Project license.
- */
- public function get_license(){
- if ($this->load_flags["license"] === false){
- $this->load_license();
- }
- return $this->license;
- }
-
- /**
- * Loads the project images.
- *
- * Not exposed, must be called before accessing the project images.
- */
- private function load_images(){
- if ($this->load_flags["images"] === false){
- $statement = get_context()->get_db()->prepare("
- SELECT id
- FROM project_image
- WHERE project = :id
- ORDER BY idx
- ");
- $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
- $statement->execute();
- while ($r_image = $statement->fetch(PDO::FETCH_ASSOC)){
- array_push($this->images, new Project_Image($r_image["id"]));
- }
- $this->load_flags["images"] = true;
- }
- }
- /**
- * Retrieves the projcet images
- *
- * @return Project_Image[] List of the project images.
- */
- public function get_images(){
- if ($this->load_flags["images"] === false){
- $this->load_images();
- }
- return $this->images;
- }
-
- /**
- * Loads the project tags.
- *
- * Not exposed, must be called before accessing the project tags.
- */
- private function load_tags(){
- if ($this->load_flags["tags"] === false){
- $statement = get_context()->get_db()->prepare("
- SELECT tag
- FROM project_tag
- WHERE project = :id
- ");
- $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
- $statement->execute();
- while ($r_tag = $statement->fetch(PDO::FETCH_ASSOC)){
- array_push($this->tags, Text::get($r_tag["tag"]));
- }
- $this->load_flags["tags"] = true;
- }
- }
- /**
- * Retrieves the project tags.
- *
- * @return Project_Tag[] Project tags.
- */
- public function get_tags(){
- if ($this->load_flags["tags"] === false){
- $this->load_tags();
- }
- return $this->tags;
- }
-
- /**
- * Loads the project URLs.
- *
- * Not exposed, must be called before accessing the project URLs.
- */
- private function load_urls(){
- if ($this->load_flags["urls"] === false){
- $statement = get_context()->get_db()->prepare("
- SELECT id
- FROM project_url
- WHERE project = :id
- ");
- $statement->bindValue(':id', $this->id, PDO::PARAM_INT);
- $statement->execute();
- while ($r_url = $statement->fetch(PDO::FETCH_ASSOC)){
- array_push($this->urls, new Project_Url($r_url["id"]));
- }
- $this->load_flags["urls"] = true;
- }
- }
- /**
- * Retrieves the project URLs
- *
- * @return Project_URL[] List of the project URLs.
- */
- public function get_urls(){
- if ($this->load_flags["urls"] === false){
- $this->load_urls();
- }
- return $this->urls;
- }
- /**
- * Retrieves a state flag
- *
- * @return boolean Flag value, false if it doesn't exist.
- */
- private function get_flag($flag){
- $value = false;
- if (array_key_exists($flag , $this->load_flags)){
- $value = $this->load_flags[$flag];
- }
- return $value;
- }
- /**
- * Sets a load status flag.
- *
- * If, once set, all flags are set to true, mark_as_complete(true)
- * will be automatically called.
- *
- * @param string $flag Flag name.
- * @param boolean $value Flag name.
- */
- private function set_flag($flag, $value){
- $key_found = false;
- $keys = array_keys($this->load_flags);
- foreach($keys as $key){
- if ($key === $flag){
- $key_found = true;
- break;
- }
- }
- if ($key_found == false){
- Log::warn("Trying to set non-existing flag '$flag' in project");
- return false;
- }
- else{
- if ($value !== true && $value !== false){
- Log::warn("Trying to set project flag '$flag' to an invalid value: " . $value);
- return false;
- }
- else{
- $this->load_flags[$flag] = $value;
-
- // Once set, loop all keys to check if they are all true
- $all_loaded = true;
- foreach($this->load_flags as $value){
- if ($value !== true){
- $all_loaded = false;
- break;
- }
- }
- $this->mark_as_complete($all_loaded);
- return true;
- }
- }
- }
- }
|