PageRenderTime 45ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Flux/ItemShop.php

https://github.com/chokoleytdesignoper/fluxcp_choko
PHP | 235 lines | 171 code | 36 blank | 28 comment | 29 complexity | ba8a551ddf656a9f9b49d4f4771b644c MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. require_once 'Flux/TemporaryTable.php';
  3. require_once 'Flux/ItemExistsError.php';
  4. class Flux_ItemShop {
  5. /**
  6. * @access public
  7. * @var Flux_Athena
  8. */
  9. public $server;
  10. public function __construct(Flux_Athena $server)
  11. {
  12. $this->server = $server;
  13. }
  14. /**
  15. * Add an item to the shop.
  16. */
  17. public function add($itemID, $categoryID, $cost, $quantity, $info, $useExisting = 0)
  18. {
  19. $db = $this->server->charMapDatabase;
  20. $table = Flux::config('FluxTables.ItemShopTable');
  21. $sql = "INSERT INTO $db.$table (nameid, category, quantity, cost, info, use_existing, create_date) VALUES (?, ?, ?, ?, ?, ?, NOW())";
  22. $sth = $this->server->connection->getStatement($sql);
  23. $res = $sth->execute(array($itemID, $categoryID, $quantity, $cost, $info, $useExisting));
  24. $sth2 = $this->server->connection->getStatement('SELECT LAST_INSERT_ID() AS insID');
  25. $res2 = $sth2->execute();
  26. if ($res && $res2 && ($insertID=$sth2->fetch()->insID)) {
  27. return $insertID;
  28. }
  29. else {
  30. return false;
  31. }
  32. }
  33. /**
  34. * Modify item info in the shop.
  35. */
  36. public function edit($shopItemID, $categoryID = null, $cost = null, $quantity = null, $info = null, $useExisting = null)
  37. {
  38. $catQ = '';
  39. $crdQ = '';
  40. $qtyQ = '';
  41. $infQ = '';
  42. $imgQ = '';
  43. $bind = array();
  44. if (!is_null($categoryID)) {
  45. $catQ = "category = ? ";
  46. $bind[] = (int)$categoryID;
  47. }
  48. if (!is_null($cost)) {
  49. if ($catQ) {
  50. $crdQ = ", cost = ? ";
  51. }
  52. else {
  53. $crdQ = "cost = ? ";
  54. }
  55. $bind[] = (int)$cost;
  56. }
  57. if (!is_null($quantity)) {
  58. if ($crdQ) {
  59. $qtyQ = ', quantity = ? ';
  60. }
  61. else {
  62. $qtyQ = "quantity = ? ";
  63. }
  64. $bind[] = (int)$quantity;
  65. }
  66. if (!is_null($info)) {
  67. if ($qtyQ) {
  68. $infQ = ', info = ? ';
  69. }
  70. else {
  71. $infQ = "info = ? ";
  72. }
  73. $bind[] = trim($info);
  74. }
  75. if (!is_null($useExisting)) {
  76. if ($infQ) {
  77. $imgQ = ', use_existing = ? ';
  78. }
  79. else {
  80. $imgQ = "use_existing = ? ";
  81. }
  82. $bind[] = (int)$useExisting;
  83. }
  84. if (empty($bind)) {
  85. return false;
  86. }
  87. $db = $this->server->charMapDatabase;
  88. $table = Flux::config('FluxTables.ItemShopTable');
  89. $sql = "UPDATE $db.$table SET $catQ $crdQ $qtyQ $infQ $imgQ WHERE id = ?";
  90. $sth = $this->server->connection->getStatement($sql);
  91. $bind[] = $shopItemID;
  92. return $sth->execute($bind);
  93. }
  94. /**
  95. *
  96. */
  97. public function delete($shopItemID)
  98. {
  99. $db = $this->server->charMapDatabase;
  100. $table = Flux::config('FluxTables.ItemShopTable');
  101. $sql = "DELETE FROM $db.$table WHERE id = ?";
  102. $sth = $this->server->connection->getStatement($sql);
  103. return $sth->execute(array($shopItemID));
  104. }
  105. /**
  106. *
  107. */
  108. public function buy(Flux_DataObject $account, $shopItemID)
  109. {
  110. }
  111. /**
  112. *
  113. */
  114. public function getItem($shopItemID)
  115. {
  116. $db = $this->server->charMapDatabase;
  117. $temp = new Flux_TemporaryTable($this->server->connection, "$db.items", array("$db.item_db", "$db.item_db2"));
  118. $shop = Flux::config('FluxTables.ItemShopTable');
  119. $col = "$shop.id AS shop_item_id, $shop.category AS shop_item_category, $shop.cost AS shop_item_cost, $shop.quantity AS shop_item_qty, $shop.use_existing AS shop_item_use_existing, ";
  120. $col .= "$shop.nameid AS shop_item_nameid, $shop.info AS shop_item_info, items.name_japanese AS shop_item_name";
  121. $sql = "SELECT $col FROM $db.$shop LEFT OUTER JOIN $db.items ON items.id = $shop.nameid WHERE $shop.id = ?";
  122. $sth = $this->server->connection->getStatement($sql);
  123. if ($sth->execute(array($shopItemID))) {
  124. return $sth->fetch();
  125. }
  126. else {
  127. return false;
  128. }
  129. }
  130. /**
  131. *
  132. */
  133. public function getItems($categoryID = null)
  134. {
  135. $bind = array();
  136. $db = $this->server->charMapDatabase;
  137. $temp = new Flux_TemporaryTable($this->server->connection, "$db.items", array("$db.item_db", "$db.item_db2"));
  138. $shop = Flux::config('FluxTables.ItemShopTable');
  139. $col = "$shop.id AS shop_item_id, $shop.cost AS shop_item_cost, $shop.quantity AS shop_item_qty, $shop.use_existing AS shop_item_use_existing, ";
  140. $col .= "$shop.nameid AS shop_item_nameid, $shop.info AS shop_item_info, items.name_japanese AS shop_item_name";
  141. $sql = "SELECT $col FROM $db.$shop LEFT OUTER JOIN $db.items ON items.id = $shop.nameid";
  142. if (!is_null($categoryID)) {
  143. $sql .= " WHERE $shop.category = ?";
  144. $bind[] = $categoryID;
  145. }
  146. $sth = $this->server->connection->getStatement($sql);
  147. if ($sth->execute($bind)) {
  148. return $sth->fetchAll();
  149. }
  150. else {
  151. return false;
  152. }
  153. }
  154. /**
  155. *
  156. */
  157. public function deleteShopItemImage($shopItemID)
  158. {
  159. $serverName = $this->server->loginAthenaGroup->serverName;
  160. $athenaServerName = $this->server->serverName;
  161. $dir = FLUX_DATA_DIR."/itemshop/$serverName/$athenaServerName";
  162. $files = glob("$dir/$shopItemID.*");
  163. foreach ($files as $file) {
  164. unlink($file);
  165. }
  166. return true;
  167. }
  168. /**
  169. *
  170. */
  171. public function uploadShopItemImage($shopItemID, Flux_Config $file)
  172. {
  173. if ($file->get('error')) {
  174. return false;
  175. }
  176. $validexts = array_map('strtolower', Flux::config('ShopImageExtensions')->toArray());
  177. $extension = strtolower(pathinfo($file->get('name'), PATHINFO_EXTENSION));
  178. if (!in_array($extension, $validexts)) {
  179. return false;
  180. }
  181. $serverName = $this->server->loginAthenaGroup->serverName;
  182. $athenaServerName = $this->server->serverName;
  183. $dir = FLUX_DATA_DIR."/itemshop/$serverName/$athenaServerName";
  184. if (!is_dir(FLUX_DATA_DIR."/itemshop/$serverName")) {
  185. mkdir(FLUX_DATA_DIR."/itemshop/$serverName");
  186. }
  187. if (!is_dir($dir)) {
  188. mkdir($dir);
  189. }
  190. $this->deleteShopItemImage($shopItemID);
  191. if (move_uploaded_file($file->get('tmp_name'), "$dir/$shopItemID.$extension")) {
  192. return true;
  193. }
  194. else {
  195. return false;
  196. }
  197. }
  198. }
  199. ?>