Building_Page.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * Building view page file.
  4. *
  5. * Provides a class with all the properties and methods to display the page.
  6. *
  7. * @author Iñigo Valentin <i@inigovalentin.com>
  8. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  9. * @package SWDB
  10. */
  11. require_once(PATH::PAGE . "Page.php");
  12. require_once(PATH::ENTITY . "Unit.php");
  13. /**
  14. * Building view page model.
  15. *
  16. * @category Page
  17. */
  18. class Building_Page extends Page{
  19. /**
  20. * @var Buildable|Decorative Selected building.
  21. */
  22. private $building;
  23. /**
  24. * @var bool Checks if the selected item is a Decorative.
  25. */
  26. private $type_decorative = false;
  27. /**
  28. * Constructor.
  29. *
  30. * Retrieves the data and initializes the variables.
  31. *
  32. * @param string $id Selected unit id
  33. */
  34. public function __construct($id){
  35. parent::__construct();
  36. $this->set_public(true);
  37. $this->set_view("building.php");
  38. $statement = get_context()->get_db()->prepare("
  39. SELECT
  40. id,
  41. type
  42. FROM (
  43. SELECT id, 'buildable' AS type from buildable
  44. UNION
  45. SELECT id, 'decorative' as type FROM decorative
  46. )
  47. WHERE id = :id;
  48. ");
  49. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  50. $result = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  51. if (! $result){
  52. $this->set_code(404);
  53. $this->set_message("Building not found");
  54. }
  55. else{
  56. if ($result["type"] == "buildable"){
  57. $this->building = new Buildable($id);
  58. $this->type_decorative = false;
  59. }
  60. elseif ($result["type"] == "decorative"){
  61. $this->building = new Decorative($id);
  62. $this->type_decorative = true;
  63. }
  64. if ($this->building == null || $this->building->get_id() == null){
  65. $this->set_code(404);
  66. $this->set_message("Building not found");
  67. }
  68. else{
  69. $this->set_code(200);
  70. $this->set_message("OK");
  71. $this->set_title($this->building->get_name());
  72. $this->set_description($this->building->get_name() . " details");
  73. $this->set_canonical("buildings/" . $this->building->get_id());
  74. $this->add_css("building.css");
  75. }
  76. }
  77. }
  78. /**
  79. * Retrieves the selected building.
  80. *
  81. * @return Buildable|Decorative Selected building.
  82. */
  83. public function get_building(){
  84. return $this->building;
  85. }
  86. /**
  87. * Checks if the selcted item is a decorative.
  88. *
  89. * @return bool True if Decorative, false if Buildable.
  90. */
  91. public function is_decorative(){
  92. return $this->type_decorative;
  93. }
  94. /**
  95. * Checks if the selcted item is a buildable.
  96. *
  97. * @return bool True if Buildable, false if Decorative.
  98. */
  99. public function is_buildable(){
  100. return (! $this->type_decorative);
  101. }
  102. }