Item.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * Item entity file.
  4. *
  5. * Creates the entity and makes it available.
  6. *
  7. * @category Entity
  8. */
  9. /**
  10. * Require dependent entities if not present.
  11. */
  12. require_once(PATH::ENTITY . "Entity.php");
  13. require_once(PATH::ENTITY . "Monster.php");
  14. /**
  15. * An inventory item.
  16. *
  17. * Represents an object from the table 'item'.
  18. *
  19. * @category Entity
  20. */
  21. class Item extends Entity{
  22. /**
  23. * @var int Owner's player identifier.
  24. */
  25. public $player;
  26. /**
  27. * @var int|Monster Item identifier. If its a monster piece, a Monster entity.
  28. */
  29. public $id;
  30. /**
  31. * @var string Item name.
  32. */
  33. public $name;
  34. /**
  35. * @var string Item type name.
  36. */
  37. public $type;
  38. /**
  39. * @var int Item owned amount.
  40. */
  41. public $amount;
  42. /**
  43. * @var bool Indicates if the item are monster pieces.
  44. */
  45. public $is_monster_pices;
  46. /**
  47. * Constructor.
  48. *
  49. * Searches the database and retrieves the information about the
  50. * building, populating it and it's items.
  51. *
  52. * @param int $id Item identifier.
  53. * @param int $type Item type identifier. Optional, will try to guess if null.
  54. */
  55. public function __construct($id, $type = null){
  56. $s = "
  57. SELECT
  58. item.id AS id,
  59. inventory.name AS name,
  60. inventory.description AS description,
  61. item.type AS type_ref,
  62. inventory_type.name AS type,
  63. item.amount AS amount
  64. FROM
  65. item,
  66. inventory,
  67. inventory_type
  68. WHERE
  69. inventory.type = inventory_type.id AND
  70. inventory.id = inventory.id AND
  71. inventory.type = inventory.type AND
  72. inventory.id = :id
  73. ";
  74. if ($type != null){
  75. $s .= "AND inventory.type = :type";
  76. }
  77. $statement = get_context()->get_db()->prepare($s);
  78. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  79. $statement->bindValue(':type', $type, SQLITE3_TEXT);
  80. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  81. if ($r){
  82. $this->type = $r["type"];
  83. $this->amount = $r["amount"];
  84. if ($r["type"] == INVENTORY_TYPE_ID::GUILD_MONSTER_PIECE || $r["type"] == INVENTORY_TYPE_ID::MONSTER_PIECE){
  85. $this->is_monster_pices = true;
  86. $this->id = new Monster($r["id"]);
  87. $this->name = HTML::e($this->id->title) . " Pieces";
  88. $this->description = HTML::e($this->id->title) . " Pieces";
  89. }
  90. else{
  91. $this->is_monster_pices = false;
  92. $this->id = $r["id"];
  93. $this->name = HTML::e($r["name"]);
  94. $this->description = HTML::e($r["description"]);
  95. }
  96. }
  97. }
  98. /**
  99. * Gets the path to the item image. The image must be in the
  100. * img/content/inventory/ directory, and its name must be the item id,
  101. * padded with '0' to 9 digites, and the extension must be '.png'.
  102. *
  103. * @return string Image URL, or a fixed unknown image.
  104. */
  105. public function get_image(){
  106. return APPLICATION::img("INVENTORY", $this->id);
  107. }
  108. }
  109. ?>