Inventory.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Inventory base 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. /**
  14. * An inventory base item.
  15. *
  16. * Represents an object from the table 'inventory'.
  17. *
  18. * @category Entity
  19. */
  20. class Inventory extends Entity{
  21. /**
  22. * @var int Item identifier.
  23. */
  24. public $id;
  25. /**
  26. * @var string Item name.
  27. */
  28. public $name;
  29. /**
  30. * @var string Item type name.
  31. */
  32. public $type;
  33. /**
  34. * @var string Item description.
  35. */
  36. public $description;
  37. /**
  38. * Constructor.
  39. *
  40. * Searches the database and retrieves the information about the
  41. * building, populating it and it's items.
  42. *
  43. * @param int $id Item identifier.
  44. * @param int $type Item type identifier. Optional, will try to guess if null.
  45. */
  46. public function __construct($id, $type = null){
  47. $s = "
  48. SELECT
  49. inventory.id AS id,
  50. inventory.name AS name,
  51. inventory.description AS description,
  52. inventory_type.name AS type
  53. FROM
  54. inventory,
  55. inventory_type
  56. WHERE
  57. inventory.type = inventory_type.id AND
  58. inventory.id = :id
  59. ";
  60. if ($type != null){
  61. $s .= "AND inventory.type = :type";
  62. }
  63. $statement = get_context()->get_db()->prepare($s);
  64. $statement->bindValue(':id', $id, SQLITE3_TEXT);
  65. $statement->bindValue(':type', $type, SQLITE3_TEXT);
  66. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  67. if ($r){
  68. $this->type = $r["type"];
  69. $this->id = $r["id"];
  70. $this->name = HTML::e($r["name"]);
  71. $this->description = HTML::e($r["description"]);
  72. }
  73. }
  74. /**
  75. * Gets the path to the item image. The image must be in the
  76. * img/content/inventory/ directory, and its name must be the item id,
  77. * padded with '0' to 9 digites, and the extension must be '.png'.
  78. *
  79. * @return string Image URL, or a fixed unknown image.
  80. */
  81. public function get_image(){
  82. return APPLICATION::img("INVENTORY", $this->id);
  83. }
  84. }
  85. ?>