-
Notifications
You must be signed in to change notification settings - Fork 204
/
ItemShop.php
250 lines (210 loc) · 6.04 KB
/
ItemShop.php
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
<?php
require_once 'Flux/TemporaryTable.php';
require_once 'Flux/ItemExistsError.php';
class Flux_ItemShop {
/**
* @access public
* @var Flux_Athena
*/
public $server;
public function __construct(Flux_Athena $server)
{
$this->server = $server;
}
/**
* Add an item to the shop.
*/
public function add($itemID, $categoryID, $cost, $quantity, $info, $useExisting = 0)
{
$db = $this->server->charMapDatabase;
$table = Flux::config('FluxTables.ItemShopTable');
$sql = "INSERT INTO $db.$table (nameid, category, quantity, cost, info, use_existing, create_date) VALUES (?, ?, ?, ?, ?, ?, NOW())";
$sth = $this->server->connection->getStatement($sql);
$res = $sth->execute(array($itemID, $categoryID, $quantity, $cost, $info, $useExisting));
$sth2 = $this->server->connection->getStatement('SELECT LAST_INSERT_ID() AS insID');
$res2 = $sth2->execute();
if ($res && $res2 && ($insertID=$sth2->fetch()->insID)) {
return $insertID;
}
else {
return false;
}
}
/**
* Modify item info in the shop.
*/
public function edit($shopItemID, $categoryID = null, $cost = null, $quantity = null, $info = null, $useExisting = null)
{
$catQ = '';
$crdQ = '';
$qtyQ = '';
$infQ = '';
$imgQ = '';
$bind = array();
if (!is_null($categoryID)) {
$catQ = "category = ? ";
$bind[] = (int)$categoryID;
}
if (!is_null($cost)) {
if ($catQ) {
$crdQ = ", cost = ? ";
}
else {
$crdQ = "cost = ? ";
}
$bind[] = (int)$cost;
}
if (!is_null($quantity)) {
if ($crdQ) {
$qtyQ = ', quantity = ? ';
}
else {
$qtyQ = "quantity = ? ";
}
$bind[] = (int)$quantity;
}
if (!is_null($info)) {
if ($qtyQ) {
$infQ = ', info = ? ';
}
else {
$infQ = "info = ? ";
}
$bind[] = trim($info);
}
if (!is_null($useExisting)) {
if ($infQ) {
$imgQ = ', use_existing = ? ';
}
else {
$imgQ = "use_existing = ? ";
}
$bind[] = (int)$useExisting;
}
if (empty($bind)) {
return false;
}
$db = $this->server->charMapDatabase;
$table = Flux::config('FluxTables.ItemShopTable');
$sql = "UPDATE $db.$table SET $catQ $crdQ $qtyQ $infQ $imgQ WHERE id = ?";
$sth = $this->server->connection->getStatement($sql);
$bind[] = $shopItemID;
return $sth->execute($bind);
}
/**
*
*/
public function delete($shopItemID)
{
$db = $this->server->charMapDatabase;
$table = Flux::config('FluxTables.ItemShopTable');
$sql = "DELETE FROM $db.$table WHERE id = ?";
$sth = $this->server->connection->getStatement($sql);
return $sth->execute(array($shopItemID));
}
/**
*
*/
public function buy(Flux_DataObject $account, $shopItemID)
{
}
/**
*
*/
public function getItem($shopItemID)
{
$db = $this->server->charMapDatabase;
if($this->server->isRenewal) {
$fromTables = array("$db.item_db_re", "$db.item_db2_re");
} else {
$fromTables = array("$db.item_db", "$db.item_db2");
}
$temp = new Flux_TemporaryTable($this->server->connection, "$db.items", $fromTables);
$shop = Flux::config('FluxTables.ItemShopTable');
$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, ";
$col .= "$shop.nameid AS shop_item_nameid, $shop.info AS shop_item_info, items.name_english AS shop_item_name";
$sql = "SELECT $col FROM $db.$shop LEFT OUTER JOIN $db.items ON items.id = $shop.nameid WHERE $shop.id = ?";
$sth = $this->server->connection->getStatement($sql);
if ($sth->execute(array($shopItemID))) {
return $sth->fetch();
}
else {
return false;
}
}
/**
*
*/
public function getItems($paginator, $categoryID = null)
{
$sqlpartial = "";
$bind = array();
$db = $this->server->charMapDatabase;
if($this->server->isRenewal) {
$fromTables = array("$db.item_db_re", "$db.item_db2_re");
} else {
$fromTables = array("$db.item_db", "$db.item_db2");
}
$temp = new Flux_TemporaryTable($this->server->connection, "$db.items", $fromTables);
$shop = Flux::config('FluxTables.ItemShopTable');
$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, ";
$col .= "$shop.nameid AS shop_item_nameid, $shop.info AS shop_item_info, items.name_english AS shop_item_name";
if (!is_null($categoryID)) {
$sqlpartial = " WHERE $shop.category = ?";
$bind[] = $categoryID;
}
$sql = $paginator->getSQL("SELECT $col FROM $db.$shop LEFT OUTER JOIN $db.items ON items.id = $shop.nameid $sqlpartial");
$sth = $this->server->connection->getStatement($sql);
if ($sth->execute($bind)) {
return $sth->fetchAll();
}
else {
return false;
}
}
/**
*
*/
public function deleteShopItemImage($shopItemID)
{
$serverName = $this->server->loginAthenaGroup->serverName;
$athenaServerName = $this->server->serverName;
$dir = FLUX_DATA_DIR."/itemshop/$serverName/$athenaServerName";
$files = glob("$dir/$shopItemID.*");
foreach ($files as $file) {
unlink($file);
}
return true;
}
/**
*
*/
public function uploadShopItemImage($shopItemID, Flux_Config $file)
{
if ($file->get('error')) {
return false;
}
$validexts = array_map('strtolower', Flux::config('ShopImageExtensions')->toArray());
$extension = strtolower(pathinfo($file->get('name'), PATHINFO_EXTENSION));
if (!in_array($extension, $validexts)) {
return false;
}
$serverName = $this->server->loginAthenaGroup->serverName;
$athenaServerName = $this->server->serverName;
$dir = FLUX_DATA_DIR."/itemshop/$serverName/$athenaServerName";
if (!is_dir(FLUX_DATA_DIR."/itemshop/$serverName")) {
mkdir(FLUX_DATA_DIR."/itemshop/$serverName");
}
if (!is_dir($dir)) {
mkdir($dir);
}
$this->deleteShopItemImage($shopItemID);
if (move_uploaded_file($file->get('tmp_name'), "$dir/$shopItemID.$extension")) {
return true;
}
else {
return false;
}
}
}
?>