PageRenderTime 104ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/item/edit.php

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