fluxcp_choko /modules/item/edit.php

Language PHP Lines 312
MD5 Hash cc39975c858fc18c286e7101cedd7d06
Repository https://github.com/chokoleytdesignoper/fluxcp_choko.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<?php
if (!defined('FLUX_ROOT')) exit;

require_once 'Flux/Config.php';
require_once 'Flux/TemporaryTable.php';

$tableName  = "{$server->charMapDatabase}.items";
$fromTables = array("{$server->charMapDatabase}.item_db", "{$server->charMapDatabase}.item_db2");
$tempTable  = new Flux_TemporaryTable($server->connection, $tableName, $fromTables);

$title = 'Modify Item';

$itemID = $params->get('id');

if (!$itemID) {
	$this->deny();
}

$col  = "id, view, type, name_english, name_japanese, slots, price_buy, price_sell, weight/10 AS weight, attack, ";
$col .= "defence, range, weapon_level, equip_level, refineable, equip_locations, equip_upper, ";
$col .= "equip_jobs, equip_genders, script, equip_script, unequip_script, origin_table";
$sql  = "SELECT $col FROM $tableName WHERE id = ? LIMIT 1";
$sth  = $server->connection->getStatement($sql);
$sth->execute(array($itemID));

$item = $sth->fetch();

// Check if item exists, first.
if ($item) {
	$isCustom      = preg_match('/item_db2$/', $item->origin_table) ? true : false;
	
	if ($params->get('edititem')) {
		$viewID        = $params->get('view');
		$type          = $params->get('type');
		$identifier    = $params->get('name_english');
		$itemName      = $params->get('name_japanese');
		$slots         = $params->get('slots');
		$npcBuy        = $params->get('npc_buy');
		$npcSell       = $params->get('npc_sell');
		$weight        = $params->get('weight');
		$attack        = $params->get('attack');
		$defense       = $params->get('defense');
		$range         = $params->get('range');
		$weaponLevel   = $params->get('weapon_level');
		$equipLevel    = $params->get('equip_level');
		$refineable    = $params->get('refineable');
	}
	else {
		$viewID        = $item->view;
		$type          = $item->type;
		$identifier    = $item->name_english;
		$itemName      = $item->name_japanese;
		$slots         = $item->slots;
		$npcBuy        = $item->price_buy;
		$npcSell       = $item->price_sell;
		$weight        = $item->weight;
		$attack        = $item->attack;
		$defense       = $item->defence;
		$range         = $item->range;
		$weaponLevel   = $item->weapon_level;
		$equipLevel    = $item->equip_level;
		$refineable    = $item->refineable;
	}
	
	if ($item->equip_locations) {
		$item->equip_locations = Flux::equipLocationsToArray($item->equip_locations);
	}
	if ($item->equip_upper) {
		$item->equip_upper = Flux::equipUpperToArray($item->equip_upper);
	}
	if ($item->equip_jobs) {
		$item->equip_jobs = Flux::equipJobsToArray($item->equip_jobs);
	}
	
	$equipLocs     = $params->get('equip_locations') ? $params->get('equip_locations') : $item->equip_locations;
	$equipUpper    = $params->get('equip_upper')     ? $params->get('equip_upper')     : $item->equip_upper;
	$equipJobs     = $params->get('equip_jobs')      ? $params->get('equip_jobs')      : $item->equip_jobs;
	
	$equipMale     = ($item->equip_genders == 2 || $item->equip_genders == 1) ? true : false;
	$equipFemale   = ($item->equip_genders == 2 || $item->equip_genders == 0) ? true : false;
	
	$script        = $params->get('script') ? $params->get('script') : $item->script;
	$equipScript   = $params->get('equip_script') ? $params->get('equip_script') : $item->equip_script;
	$unequipScript = $params->get('unequip_script') ? $params->get('unequip_script') : $item->unequip_script;

	// Equip locations.
	if ($equipLocs instanceOf Flux_Config) {
		$equipLocs = $equipLocs->toArray();
	}

	// Equip upper.
	if ($equipUpper instanceOf Flux_Config) {
		$equipUpper = $equipUpper->toArray();
	}

	// Equip jobs.
	if ($equipJobs instanceOf Flux_Config) {
		$equipJobs = $equipJobs->toArray();
	}
	
	if (!is_array($equipLocs)) {
		$equipLocs = array();
	}
	if (!is_array($equipUpper)) {
		$equipUpper = array();
	}
	if (!is_array($equipJobs)) {
		$equipJobs = array();
	}

	if (count($_POST) && $params->get('edititem')) {
		// Sanitize to NULL: viewid, slots, npcbuy, npcsell, weight, attack, defense, range, weaponlevel, equiplevel
		$nullables = array(
			'viewID', 'slots', 'npcBuy', 'npcSell', 'weight', 'attack', 'defense',
			'range', 'weaponLevel', 'equipLevel', 'script', 'equipScript', 'unequipScript'
		);
		foreach ($nullables as $nullable) {
			if (trim($$nullable) == '') {
				$$nullable = null;
			}
		}

		// Weight is defaulted to an zero value.
		if (is_null($weight)) {
			$weight = 0;
		}

		// Refineable should be 1 or 0 if it's not null.
		if (!is_null($refineable)) {
			$refineable = intval((bool)$refineable);
		}

		if (!$itemID) {
			$errorMessage = 'You must specify an item ID.';
		}
		elseif (!ctype_digit($itemID)) {
			$errorMessage = 'Item ID must be a number.';
		}
		elseif (!is_null($viewID) && !ctype_digit($viewID)) {
			$errorMessage = 'View ID must be a number.';
		}
		elseif (!$identifier) {
			$errorMessage = 'You must specify an identifer.';
		}
		elseif (!$itemName) {
			$errorMessage = 'You must specify an item name.';
		}
		elseif (!is_null($slots) && !ctype_digit($slots)) {
			$errorMessage = 'Slots must be a number.';
		}
		elseif (!is_null($npcBuy) && !ctype_digit($npcBuy)) {
			$errorMessage = 'NPC buying price must be a number.';
		}
		elseif (!is_null($npcSell) && !ctype_digit($npcSell)) {
			$errorMessage = 'NPC selling price must be a number.';
		}
		elseif (!is_null($weight) && !ctype_digit($weight)) {
			$errorMessage = 'Weight must be a number.';
		}
		elseif (!is_null($attack) && !ctype_digit($attack)) {
			$errorMessage = 'Attack must be a number.';
		}
		elseif (!is_null($defense) && !ctype_digit($defense)) {
			$errorMessage = 'Defense must be a number.';
		}
		elseif (!is_null($range) && !ctype_digit($range)) {
			$errorMessage = 'Range must be a number.';
		}
		elseif (!is_null($weaponLevel) && !ctype_digit($weaponLevel)) {
			$errorMessage = 'Weapon level must be a number.';
		}
		elseif (!is_null($equipLevel) && !ctype_digit($equipLevel)) {
			$errorMessage = 'Equip level must be a number.';
		}
		else {
			if (empty($errorMessage) && is_array($equipLocs)) {
				$locs = FLux::getEquipLocationList();
				foreach ($equipLocs as $bit) {
					if (!array_key_exists($bit, $locs)) {
						$errorMessage = 'Invalid equip location specified.';
						$equipLocs = null;
						break;
					}
				}
			}
			if (empty($errorMessage) && is_array($equipUpper)) {
				$upper = FLux::getEquipUpperList();
				foreach ($equipUpper as $bit) {
					if (!array_key_exists($bit, $upper)) {
						$errorMessage = 'Invalid equip upper specified.';
						$equipUpper = null;
						break;
					}
				}
			}
			if (empty($errorMessage) && is_array($equipJobs)) {
				$jobs = Flux::getEquipJobsList();
				foreach ($equipJobs as $bit) {
					if (!array_key_exists($bit, $jobs)) {
						$errorMessage = 'Invalid equippable job specified.';
						$equipJobs = null;
						break;
					}
				}
			}
			if (empty($errorMessage)) {
				$cols = array('id', 'name_english', 'name_japanese', 'type', 'weight');
				$bind = array($itemID, $identifier, $itemName, $type, $weight*10);
				$vals = array(
					'view'           => $viewID,
					'slots'          => $slots,
					'price_buy'      => $npcBuy,
					'price_sell'     => $npcSell,
					'attack'         => $attack,
					'defence'        => $defense,
					'range'          => $range,
					'weapon_level'   => $weaponLevel,
					'equip_level'    => $equipLevel,
					'script'         => $script,
					'equip_script'   => $equipScript,
					'unequip_script' => $unequipScript,
					'refineable'     => $refineable
				);

				foreach ($vals as $col => $val) {
					$cols[] = $col;
					$bind[] = $val;
				}

				if ($equipLocs) {
					$bits = 0;
					foreach ($equipLocs as $bit) {
						$bits |= $bit;
					}
					$cols[] = 'equip_locations';
					$bind[] = $bits;
				}

				if ($equipUpper) {
					$bits = 0;
					foreach ($equipUpper as $bit) {
						$bits |= $bit;
					}
					$cols[] = 'equip_upper';
					$bind[] = $bits;
				}

				if ($equipJobs) {
					$bits = 0;
					foreach ($equipJobs as $bit) {
						$bits |= $bit;
					}
					$cols[] = 'equip_jobs';
					$bind[] = $bits;
				}

				$gender = null;
				if ($equipMale && $equipFemale) {
					$gender = 2;
				}
				elseif ($equipMale) {
					$gender = 1;
				}
				elseif ($equipFemale) {
					$gender = 0;
				}

				if (!is_null($gender)) {
					$cols[] = 'equip_genders';
					$bind[] = $gender;
				}

				if ($isCustom) {
					$set = array();
					foreach ($cols as $i => $col) {
						$set[] = "$col = ?";
					}
					
					$sql  = "UPDATE {$server->charMapDatabase}.item_db2 SET ";
					$sql .= implode($set, ', ');
					$sql .= " WHERE id = ?";

					//array_shift($cols);
					//array_shift($bind);
					$bind[] = $itemID;
				}
				else {
					$sql  = "INSERT INTO {$server->charMapDatabase}.item_db2 (".implode(', ', $cols).") ";
					$sql .= "VALUES (".implode(', ', array_fill(0, count($bind), '?')).")";
				}

				$sth = $server->connection->getStatement($sql);
				if ($sth->execute($bind)) {
					$session->setMessageData("Your item '$itemName' ($itemID) has been successfully modified!");
					
					if ($auth->actionAllowed('item', 'view')) {
						$this->redirect($this->url('item', 'view', array('id' => $itemID)));
					}
					else {
						$this->redirect();
					}
				}
				else {
					$errorMessage = 'Failed to modify item!';
				}
			}
		}
	}
}


?>
Back to Top