/site/lib/dkp/loottable/dkpLootTable.php

https://github.com/lukas89/WebDKP · PHP · 206 lines · 125 code · 10 blank · 71 comment · 11 complexity · 56b2456b24607e75ccf5fa9bff315591 MD5 · raw file

  1. <?php
  2. /*===========================================================
  3. CLASS DESCRIPTION
  4. =============================================================
  5. Class Description should be placed here.
  6. */
  7. include_once("dkpLootTableSection.php");
  8. include_once("dkpLootTableEntry.php");
  9. include_once("lib/stats/wowstats.php");
  10. class dkpLootTable {
  11. /*===========================================================
  12. MEMBER VARIABLES
  13. ============================================================*/
  14. var $id;
  15. var $name;
  16. var $guild;
  17. var $sections = array();
  18. var $loot = array();
  19. const tablename = "dkp_loottable";
  20. /*===========================================================
  21. DEFAULT CONSTRUCTOR
  22. ============================================================*/
  23. function dkpLootTable()
  24. {
  25. $this->tablename = dkpLootTable::tablename;
  26. }
  27. /*===========================================================
  28. loadFromDatabase($id)
  29. Loads the information for this class from the backend database
  30. using the passed string.
  31. ============================================================*/
  32. function loadFromDatabase($id)
  33. {
  34. global $sql;
  35. $row = $sql->QueryRow("SELECT * FROM $this->tablename WHERE id='$id'");
  36. $this->loadFromRow($row);
  37. }
  38. /*===========================================================
  39. loadFromDatabaseByName($name)
  40. Loads the loot table information given a guild id and a name
  41. ============================================================*/
  42. function loadFromDatabaseByName($guildid, $name){
  43. global $sql;
  44. $guildid = sql::Escape($guildid);
  45. $name = sql::Escape($name);
  46. $row = $sql->QueryRow("SELECT * FROM $this->tablename WHERE guild='$guildid' AND name='$name'");
  47. $this->loadFromRow($row);
  48. }
  49. /*===========================================================
  50. loadFromRow($row)
  51. Loads the information for this class from the passed database row.
  52. ============================================================*/
  53. function loadFromRow($row)
  54. {
  55. $this->id=$row["id"];
  56. $this->name = $row["name"];
  57. $this->guild = $row["guild"];
  58. }
  59. /*===========================================================
  60. loadTableData
  61. Loads loot table information for this single table.
  62. This will load the loot table sections as well as individual loot
  63. table data.
  64. The parameter "mode" specifies how the loot table should be
  65. Possible values are:
  66. "section" - (default) Loot table data is stored within each section
  67. "flat" - all loot data is stored at the root level in $this->loot
  68. "both" - all loot data is loaded and stored in both locations
  69. (uses twice as much memory)
  70. ============================================================*/
  71. function loadTableData($mode = "section"){
  72. global $sql;
  73. //load loot sections
  74. $this->sections = array();
  75. $id = sql::Escape($this->id);
  76. $result = $sql->Query("SELECT * FROM dkp_loottable_section
  77. WHERE loottable = '$id'");
  78. while($row = mysql_fetch_array($result)) {
  79. $section = new dkpLootTableSection();
  80. $section->loadFromRow($row);
  81. $this->sections[$section->id] = $section;
  82. }
  83. //load loot data
  84. $this->loot = array();
  85. $result = $sql->Query("SELECT * FROM dkp_loottable_data
  86. WHERE loottable = '$id'");
  87. while($row = mysql_fetch_array($result)) {
  88. $loot = new dkpLootTableEntry();
  89. $loot->loadFromRow($row);
  90. //depending on mode, store the loaded data
  91. //differently in memory
  92. if($mode == "both" || $mode == "flat")
  93. $this->loot[] = $loot;
  94. if($mode == "both" || $mode == "section") {
  95. if(isset($this->sections[$loot->section])) {
  96. $this->sections[$loot->section]->loot[] = $loot;
  97. }
  98. }
  99. }
  100. }
  101. /*===========================================================
  102. Converts loaded item names into links suitable to be picked up
  103. by wowhead
  104. ============================================================*/
  105. function loadTooltips(){
  106. foreach($this->loot as $loot){
  107. $loot->name = wowstats::GetTextLink($loot->name);
  108. }
  109. foreach($this->sections as $section) {
  110. foreach($section->loot as $loot) {
  111. $loot->name = wowstats::GetTextLink($loot->name);
  112. }
  113. }
  114. }
  115. /*===========================================================
  116. save()
  117. Saves data into the backend database using the supplied id
  118. ============================================================*/
  119. function save()
  120. {
  121. global $sql;
  122. $name = sql::Escape($this->name);
  123. $guild = sql::Escape($this->guild);
  124. $sql->Query("UPDATE $this->tablename SET
  125. name = '$name',
  126. guild = '$guild'
  127. WHERE id='$this->id'");
  128. }
  129. /*===========================================================
  130. saveNew()
  131. Saves data into the backend database as a new row entry. After
  132. calling this method $id will be filled with a new value
  133. matching the new row for the data
  134. ============================================================*/
  135. function saveNew()
  136. {
  137. global $sql;
  138. $name = sql::Escape($this->name);
  139. $guild = sql::Escape($this->guild);
  140. $sql->Query("INSERT INTO $this->tablename SET
  141. name = '$name',
  142. guild = '$guild'
  143. ");
  144. $this->id=$sql->GetLastId();
  145. }
  146. /*===========================================================
  147. delete()
  148. Deletes the row with the current id of this instance from the
  149. database
  150. ============================================================*/
  151. function delete()
  152. {
  153. global $sql;
  154. $id = sql::Escape($this->id);
  155. $sql->Query("DELETE FROM $this->tablename WHERE id = '$id'");
  156. $sql->Query("DELETE FROM dkp_loottable_section WHERE loottable = '$id'");
  157. $sql->Query("DELETE FROM dkp_loottable_data WHERE loottable = '$id'");
  158. }
  159. /*===========================================================
  160. exists()
  161. STATIC METHOD
  162. Returns true if the given entry exists in the database
  163. database
  164. ============================================================*/
  165. function exists($guildid, $name)
  166. {
  167. global $sql;
  168. $name = sql::escape($name);
  169. $guildid = sql::escape($guildid);
  170. $tablename = dkpLootTable::tablename;
  171. $exists = $sql->QueryItem("SELECT id FROM $tablename WHERE guild='$guildid' AND name='$name'"); //MODIFY THIS LINE
  172. return ($exists != "");
  173. }
  174. /*===========================================================
  175. setupTable()
  176. Checks to see if the classes database table exists. If it does not
  177. the table is created.
  178. ============================================================*/
  179. function setupTable()
  180. {
  181. if(!sql::TableExists(dkpLootTable::tablename)) {
  182. $tablename = dkpLootTable::tablename;
  183. global $sql;
  184. $sql->Query("CREATE TABLE `$tablename` (
  185. `id` INT NOT NULL AUTO_INCREMENT ,
  186. `name` VARCHAR (256) NOT NULL,
  187. `guild` INT NOT NULL ,
  188. PRIMARY KEY ( `id` ),
  189. KEY `name` (`guild`,`name`)
  190. ) ENGINE = innodb;");
  191. }
  192. }
  193. }
  194. dkpLootTable::setupTable()
  195. ?>