PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/item/add.php

https://github.com/chokoleytdesignoper/fluxcp_choko
PHP | 258 lines | 229 code | 23 blank | 6 comment | 49 complexity | f12ccadf6ec6a42902d41a868974c8ab MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, BSD-3-Clause
  1. <?php
  2. if (!defined('FLUX_ROOT')) exit;
  3. require_once 'Flux/Config.php';
  4. $title = 'Add Item';
  5. $itemID = $params->get('item_id');
  6. $viewID = $params->get('view');
  7. $type = $params->get('type');
  8. $identifier = $params->get('name_english');
  9. $itemName = $params->get('name_japanese');
  10. $slots = $params->get('slots');
  11. $npcBuy = $params->get('npc_buy');
  12. $npcSell = $params->get('npc_sell');
  13. $weight = $params->get('weight');
  14. $attack = $params->get('attack');
  15. $defense = $params->get('defense');
  16. $range = $params->get('range');
  17. $weaponLevel = $params->get('weapon_level');
  18. $equipLevel = $params->get('equip_level');
  19. $refineable = $params->get('refineable');
  20. $equipLocs = $params->get('equip_locations');
  21. $equipUpper = $params->get('equip_upper');
  22. $equipJobs = $params->get('equip_jobs');
  23. $equipMale = $params->get('equip_male');
  24. $equipFemale = $params->get('equip_female');
  25. $script = $params->get('script');
  26. $equipScript = $params->get('equip_script');
  27. $unequipScript = $params->get('unequip_script');
  28. // Weight is defaulted to an zero value.
  29. if (is_null($weight)) {
  30. $weight = 0;
  31. }
  32. if (count($_POST) && $params->get('additem')) {
  33. // Equip locations.
  34. if ($equipLocs instanceOf Flux_Config) {
  35. $equipLocs = $equipLocs->toArray();
  36. }
  37. // Equip upper.
  38. if ($equipUpper instanceOf Flux_Config) {
  39. $equipUpper = $equipUpper->toArray();
  40. }
  41. // Equip jobs.
  42. if ($equipJobs instanceOf Flux_Config) {
  43. $equipJobs = $equipJobs->toArray();
  44. }
  45. // Sanitize to NULL: viewid, slots, npcbuy, npcsell, weight, attack, defense, range, weaponlevel, equiplevel
  46. $nullables = array(
  47. 'viewID', 'slots', 'npcBuy', 'npcSell', 'weight', 'attack', 'defense',
  48. 'range', 'weaponLevel', 'equipLevel', 'script', 'equipScript', 'unequipScript'
  49. );
  50. foreach ($nullables as $nullable) {
  51. if (trim($$nullable) == '') {
  52. $$nullable = null;
  53. }
  54. }
  55. // Refineable should be 1 or 0 if it's not null.
  56. if (!is_null($refineable)) {
  57. $refineable = intval((bool)$refineable);
  58. }
  59. if (!$itemID) {
  60. $errorMessage = 'You must specify an item ID.';
  61. }
  62. elseif (!ctype_digit($itemID)) {
  63. $errorMessage = 'Item ID must be a number.';
  64. }
  65. elseif (!is_null($viewID) && !ctype_digit($viewID)) {
  66. $errorMessage = 'View ID must be a number.';
  67. }
  68. elseif (!$identifier) {
  69. $errorMessage = 'You must specify an identifer.';
  70. }
  71. elseif (!$itemName) {
  72. $errorMessage = 'You must specify an item name.';
  73. }
  74. elseif (!is_null($slots) && !ctype_digit($slots)) {
  75. $errorMessage = 'Slots must be a number.';
  76. }
  77. elseif (!is_null($npcBuy) && !ctype_digit($npcBuy)) {
  78. $errorMessage = 'NPC buying price must be a number.';
  79. }
  80. elseif (!is_null($npcSell) && !ctype_digit($npcSell)) {
  81. $errorMessage = 'NPC selling price must be a number.';
  82. }
  83. elseif (!is_null($weight) && !ctype_digit($weight)) {
  84. $errorMessage = 'Weight must be a number.';
  85. }
  86. elseif (!is_null($attack) && !ctype_digit($attack)) {
  87. $errorMessage = 'Attack must be a number.';
  88. }
  89. elseif (!is_null($defense) && !ctype_digit($defense)) {
  90. $errorMessage = 'Defense must be a number.';
  91. }
  92. elseif (!is_null($range) && !ctype_digit($range)) {
  93. $errorMessage = 'Range must be a number.';
  94. }
  95. elseif (!is_null($weaponLevel) && !ctype_digit($weaponLevel)) {
  96. $errorMessage = 'Weapon level must be a number.';
  97. }
  98. elseif (!is_null($equipLevel) && !ctype_digit($equipLevel)) {
  99. $errorMessage = 'Equip level must be a number.';
  100. }
  101. else {
  102. if (empty($errorMessage) && is_array($equipLocs)) {
  103. $locs = FLux::getEquipLocationList();
  104. foreach ($equipLocs as $bit) {
  105. if (!array_key_exists($bit, $locs)) {
  106. $errorMessage = 'Invalid equip location specified.';
  107. $equipLocs = null;
  108. break;
  109. }
  110. }
  111. }
  112. if (empty($errorMessage) && is_array($equipUpper)) {
  113. $upper = FLux::getEquipUpperList();
  114. foreach ($equipUpper as $bit) {
  115. if (!array_key_exists($bit, $upper)) {
  116. $errorMessage = 'Invalid equip upper specified.';
  117. $equipUpper = null;
  118. break;
  119. }
  120. }
  121. }
  122. if (empty($errorMessage) && is_array($equipJobs)) {
  123. $jobs = Flux::getEquipJobsList();
  124. foreach ($equipJobs as $bit) {
  125. if (!array_key_exists($bit, $jobs)) {
  126. $errorMessage = 'Invalid equippable job specified.';
  127. $equipJobs = null;
  128. break;
  129. }
  130. }
  131. }
  132. if (empty($errorMessage)) {
  133. require_once 'Flux/TemporaryTable.php';
  134. $tableName = "{$server->charMapDatabase}.items";
  135. $fromTables = array("{$server->charMapDatabase}.item_db", "{$server->charMapDatabase}.item_db2");
  136. $tempTable = new Flux_TemporaryTable($server->connection, $tableName, $fromTables);
  137. $shopTable = Flux::config('FluxTables.ItemShopTable');
  138. $sth = $server->connection->getStatement("SELECT id, name_japanese, origin_table FROM $tableName WHERE id = ? LIMIT 1");
  139. $sth->execute(array($itemID));
  140. $item = $sth->fetch();
  141. if ($item && $item->id) {
  142. $errorMessage = 'An item already exists with that ID.';
  143. $errorMessage = sprintf($errorMessage, $item->name_japanese, $item->origin_table, $item->id);
  144. }
  145. else {
  146. $cols = array('id', 'name_english', 'name_japanese', 'type', 'weight');
  147. $bind = array($itemID, $identifier, $itemName, $type, $weight*10);
  148. $vals = array(
  149. 'view' => $viewID,
  150. 'slots' => $slots,
  151. 'price_buy' => $npcBuy,
  152. 'price_sell' => $npcSell,
  153. 'attack' => $attack,
  154. 'defence' => $defense,
  155. 'range' => $range,
  156. 'weapon_level' => $weaponLevel,
  157. 'equip_level' => $equipLevel,
  158. 'script' => $script,
  159. 'equip_script' => $equipScript,
  160. 'unequip_script' => $unequipScript,
  161. 'refineable' => $refineable
  162. );
  163. foreach ($vals as $col => $val) {
  164. if (!is_null($val)) {
  165. $cols[] = $col;
  166. $bind[] = $val;
  167. }
  168. }
  169. if ($equipLocs) {
  170. $bits = 0;
  171. foreach ($equipLocs as $bit) {
  172. $bits |= $bit;
  173. }
  174. $cols[] = 'equip_locations';
  175. $bind[] = $bits;
  176. }
  177. if ($equipUpper) {
  178. $bits = 0;
  179. foreach ($equipUpper as $bit) {
  180. $bits |= $bit;
  181. }
  182. $cols[] = 'equip_upper';
  183. $bind[] = $bits;
  184. }
  185. if ($equipJobs) {
  186. $bits = 0;
  187. foreach ($equipJobs as $bit) {
  188. $bits |= $bit;
  189. }
  190. $cols[] = 'equip_jobs';
  191. $bind[] = $bits;
  192. }
  193. $gender = null;
  194. if ($equipMale && $equipFemale) {
  195. $gender = 2;
  196. }
  197. elseif ($equipMale) {
  198. $gender = 1;
  199. }
  200. elseif ($equipFemale) {
  201. $gender = 0;
  202. }
  203. if (!is_null($gender)) {
  204. $cols[] = 'equip_genders';
  205. $bind[] = $gender;
  206. }
  207. $sql = "INSERT INTO {$server->charMapDatabase}.item_db2 (".implode(', ', $cols).") ";
  208. $sql .= "VALUES (".implode(', ', array_fill(0, count($bind), '?')).")";
  209. $sth = $server->connection->getStatement($sql);
  210. if ($sth->execute($bind)) {
  211. $session->setMessageData("Your item '$itemName' ($itemID) has been successfully added!");
  212. if ($auth->actionAllowed('item', 'view')) {
  213. $this->redirect($this->url('item', 'view', array('id' => $itemID)));
  214. }
  215. else {
  216. $this->redirect();
  217. }
  218. }
  219. else {
  220. $errorMessage = 'Failed to add item!';
  221. }
  222. }
  223. }
  224. }
  225. }
  226. if (!is_array($equipLocs)) {
  227. $equipLocs = array();
  228. }
  229. if (!is_array($equipUpper)) {
  230. $equipUpper = array();
  231. }
  232. if (!is_array($equipJobs)) {
  233. $equipJobs = array();
  234. }
  235. ?>