Guild_Skill_Group.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Guild skill group entity file.
  4. *
  5. * Creates the entity and makes it available.
  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::ENTITY . "Entity.php");
  12. require_once(PATH::ENTITY . "Guild_Skill.php");
  13. /**
  14. * Guild skill group.
  15. *
  16. * Represents a category of guild skills.
  17. *
  18. * @category Entity
  19. */
  20. class Guild_Skill_Group extends Entity{
  21. /**
  22. * @var int Group ID.
  23. */
  24. private $id;
  25. /**
  26. * @var string Group name.
  27. */
  28. private $name;
  29. /**
  30. * @var string Group description.
  31. */
  32. private $description;
  33. /**
  34. * @var bool Internal flag to check if the group skills have been loaded into the entity.
  35. */
  36. private $flag_skills = false;
  37. /**
  38. * @var Guild_Skill[] Skills in the group.
  39. */
  40. private $skills = [];
  41. /**
  42. * Constructor.
  43. *
  44. * Searches the database and retrieves the information about the
  45. * skill group, populating it and it's items.
  46. *
  47. * @param int $id Skill group identifier.
  48. */
  49. public function __construct($id){
  50. $statement = get_context()->get_db()->prepare("
  51. SELECT
  52. id,
  53. name,
  54. description
  55. FROM guild_skill_group
  56. WHERE id = :id;
  57. ");
  58. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  59. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  60. if ($r){
  61. $this->id = $r["id"];
  62. $this->name = HTML::e($r["name"]);
  63. $this->description = HTML::e($r["description"]);
  64. }
  65. }
  66. /**
  67. * Loads the skills in the group.
  68. *
  69. * Must be called only once before trying to get the skills. Sets the flag
  70. * {@link Guild_Skill_Group:$flag_skills} to true.
  71. */
  72. private function load_skills(){
  73. $statement = get_context()->get_db()->prepare("
  74. SELECT id
  75. FROM guild_skill
  76. WHERE skill_group = :id;
  77. ");
  78. $statement->bindValue(':id', $this->id, SQLITE3_TEXT);
  79. $q = $statement->execute();
  80. while ($r = $q->fetchArray(SQLITE3_ASSOC)){
  81. array_push($this->skills, new Guild_Skill($r["id"]));
  82. }
  83. $this->flag_skills = true;
  84. }
  85. /**
  86. * Retrieves the identifier of the skill group.
  87. *
  88. * @return int Group ID
  89. */
  90. public function get_id(){
  91. return $this->id;
  92. }
  93. /**
  94. * Retrieves the name of the skill_group.
  95. *
  96. * @return string Group name.
  97. */
  98. public function get_name(){
  99. return $this->name;
  100. }
  101. /**
  102. * Retrieves the description of the skill group.
  103. *
  104. * @return string Group description.
  105. */
  106. public function get_description(){
  107. return $this->description;
  108. }
  109. /**
  110. * Retrieves the skills in the group.
  111. *
  112. * @return Guild_Skill[] Skills in the group.
  113. */
  114. public function get_skills(){
  115. if (! $this->flag_skills){
  116. $this->load_skills();
  117. }
  118. return $this->skills;
  119. }
  120. /**
  121. * Gets the pathto the skill group image.
  122. *
  123. * @return string Image URL, or a fixed unknown image.
  124. */
  125. public function get_image(){
  126. return APPLICATION::img("SKILL_GUILD", $this->id);
  127. }
  128. }