forked from ryanshoover/Gift-Registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshop.php
228 lines (215 loc) · 8.29 KB
/
shop.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
<?php
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
require_once(dirname(__FILE__) . "/includes/funcLib.php");
require_once(dirname(__FILE__) . "/includes/MySmarty.class.php");
$smarty = new MySmarty();
$opt = $smarty->opt();
session_start();
if (!isset($_SESSION["userid"])) {
header("Location: " . getFullPath("login.php"));
exit;
}
else {
$userid = $_SESSION["userid"];
}
$action = "";
if (!empty($_GET["action"])) {
$action = $_GET["action"];
$itemid = (int) $_GET["itemid"];
if ($action == "reserve") {
adjustAllocQuantity($itemid,$userid,0,+1, $smarty->dbh(), $smarty->opt());
}
else if ($action == "purchase") {
// decrement reserved.
adjustAllocQuantity($itemid,$userid,0,-1, $smarty->dbh(), $smarty->opt());
// increment purchased.
adjustAllocQuantity($itemid,$userid,1,+1, $smarty->dbh(), $smarty->opt());
}
else if ($action == "return") {
// increment reserved.
adjustAllocQuantity($itemid,$userid,0,+1, $smarty->dbh(), $smarty->opt());
// decrement purchased.
adjustAllocQuantity($itemid,$userid,1,-1, $smarty->dbh(), $smarty->opt());
}
else if ($action == "release") {
adjustAllocQuantity($itemid,$userid,0,-1, $smarty->dbh(), $smarty->opt());
}
else if ($action == "copy") {
/*
can't do this because MySQL 3.x doesn't seem to support it (at least the version i was using).
$query = "INSERT INTO items(userid,description,price,source,url,category) SELECT $userid, description, price, source, url, category FROM items WHERE itemid = " . $_GET["itemid"];
*/
/* TODO: copy the image too? */
$stmt = $smarty->dbh()->prepare("SELECT userid, description, price, source, url, category, comment FROM {$opt["table_prefix"]}items WHERE itemid = ?");
$stmt->bindParam(1, $itemid, PDO::PARAM_INT);
$stmt->execute();
if ($row = $stmt->fetch()) {
$desc = $row["description"];
$source = $row["source"];
$url = $row["url"];
$comment = $row["comment"];
$price = (float) $row["price"];
$cat = (int) $row["category"];
$stmt = $smarty->dbh()->prepare("INSERT INTO {$opt["table_prefix"]}items(userid,description,price,source,url,comment,category,ranking,quantity) VALUES(?, ?, ?, ?, ?, ?, ?, 1, 1");
$stmt->bindParam(1, $userid, PDO::PARAM_INT);
$stmt->bindParam(2, $desc, PDO::PARAM_STR);
$stmt->bindParam(3, $price);
$stmt->bindParam(4, $source, PDO::PARAM_STR);
$stmt->bindParam(5, $url, PDO::PARAM_STR);
$stmt->bindParam(6, $comment, PDO::PARAM_STR);
$stmt->bindParam(7, $cat, PDO::PARAM_INT);
$stmt->execute();
stampUser($userid, $smarty->dbh(), $smarty->opt());
$message = "Added '" . $desc . "' to your gift list.";
}
}
}
$shopfor = (int) $_GET["shopfor"];
if ($shopfor == $userid) {
echo "Nice try! (You can't shop for yourself.)";
exit;
}
$stmt = $smarty->dbh()->prepare("SELECT * FROM {$opt["table_prefix"]}shoppers WHERE shopper = ? AND mayshopfor = ? AND pending = 0");
$stmt->bindParam(1, $userid, PDO::PARAM_INT);
$stmt->bindParam(2, $shopfor, PDO::PARAM_INT);
$stmt->execute();
if (!($stmt->fetch())) {
echo "Nice try! (You can't shop for someone who hasn't approved it.)";
exit;
}
if (!isset($_GET["sort"])) {
$sortby = "rankorder DESC, description";
}
else {
$sort = $_GET["sort"];
switch ($sort) {
case "ranking":
$sortby = "rankorder DESC, description";
break;
case "description":
$sortby = "description";
break;
case "source":
$sortby = "source, rankorder DESC, description";
break;
case "price":
$sortby = "price, rankorder DESC, description";
break;
case "url":
$sortby = "url, rankorder DESC, description";
break;
case "status":
$sortby = "reservedid DESC, boughtid DESC, rankorder DESC, description";
break;
case "category":
$sortby = "c.category, rankorder DESC, description";
break;
default:
$sortby = "rankorder DESC, description";
}
}
/* here's what we're going to do: we're going to pull back the shopping list along with any alloc record
for those items with a quantity of 1. if the item's quantity > 1 we'll query alloc when we
get to that record. the theory is that most items will have quantity = 1 so we'll make the least
number of trips. */
$stmt = $smarty->dbh()->prepare("SELECT i.itemid, description, price, source, c.category, url, image_filename, " .
"ub.fullname AS bfullname, ub.userid AS boughtid, " .
"ur.fullname AS rfullname, ur.userid AS reservedid, " .
"rendered, i.comment, i.quantity " .
"FROM {$opt["table_prefix"]}items i " .
"LEFT OUTER JOIN {$opt["table_prefix"]}categories c ON c.categoryid = i.category " .
"LEFT OUTER JOIN {$opt["table_prefix"]}ranks r ON r.ranking = i.ranking " .
"LEFT OUTER JOIN {$opt["table_prefix"]}allocs a ON a.itemid = i.itemid AND i.quantity = 1 " . // only join allocs for single-quantity items.
"LEFT OUTER JOIN {$opt["table_prefix"]}users ub ON ub.userid = a.userid AND a.bought = 1 " .
"LEFT OUTER JOIN {$opt["table_prefix"]}users ur ON ur.userid = a.userid AND a.bought = 0 " .
"WHERE i.userid = $shopfor " .
"ORDER BY " . $sortby);
$stmt->bindParam(1, $shopfor, PDO::PARAM_INT);
$stmt->execute();
$shoprows = array();
while ($row = $stmt->fetch()) {
$row['price'] = formatPrice($row['price'], $opt);
if ($row['quantity'] > 1) {
// check the allocs table to see what has been allocated.
$avail = $row['quantity'];
$substmt = $smarty->dbh()->prepare("SELECT a.quantity, a.bought, a.userid, " .
"ub.fullname AS bfullname, ub.userid AS boughtid, " .
"ur.fullname AS rfullname, ur.userid AS reservedid " .
"FROM {$opt["table_prefix"]}allocs a " .
"LEFT OUTER JOIN {$opt["table_prefix"]}users ub ON ub.userid = a.userid AND a.bought = 1 " .
"LEFT OUTER JOIN {$opt["table_prefix"]}users ur ON ur.userid = a.userid AND a.bought = 0 " .
"WHERE a.itemid = ? " .
"ORDER BY a.bought, a.quantity");
$substmt->bindValue(1, $row['itemid'], PDO::PARAM_INT);
$substmt->execute();
$ibought = 0;
$ireserved = 0;
$itemallocs = array();
while ($allocrow = $substmt->fetch()) {
if ($allocrow['bfullname'] != '') {
if ($allocrow['boughtid'] == $userid) {
$ibought += $allocrow['quantity'];
$itemallocs[] = ($allocrow['quantity'] . " bought by you.");
}
else {
if (!$opt["anonymous_purchasing"]) {
$itemallocs[] = ($allocrow['quantity'] . " bought by " . $allocrow['bfullname'] . ".");
}
else {
$itemallocs[] = ($allocrow['quantity'] . " bought.");
}
}
}
else {
if ($allocrow['reservedid'] == $userid) {
$ireserved += $allocrow['quantity'];
$itemallocs[] = ($allocrow['quantity'] . " reserved by you.");
}
else {
if (!$opt["anonymous_purchasing"]) {
$itemallocs[] = ($allocrow['quantity'] . " reserved by " . $allocrow['rfullname'] . ".");
}
else {
$itemallocs[] = ($allocrow['quanitity'] . " reserved.");
}
}
}
$avail -= $allocrow['quantity'];
}
$row['allocs'] = $itemallocs;
$row['avail'] = $avail;
$row['ibought'] = $ibought;
$row['ireserved'] = $ireserved;
}
$shoprows[] = $row;
}
/* okay, I *would* retrieve the shoppee's fullname from the items recordset,
except that I wouldn't get it if he had no items, so I *could* LEFT OUTER
JOIN, but then it would complicate the iteration logic, so let's just
hit the DB again. */
$stmt = $smarty->dbh()->prepare("SELECT fullname FROM {$opt["table_prefix"]}users WHERE userid = ?");
$stmt->bindParam(1, $shopfor, PDO::PARAM_INT);
$stmt->execute();
if ($row = $stmt->fetch()) {
$ufullname = $row["fullname"];
}
$smarty->assign('ufullname', $ufullname);
$smarty->assign('shopfor', $shopfor);
$smarty->assign('shoprows', $shoprows);
$smarty->assign('userid', $userid);
$smarty->assign('protocol', effectiveProtocol());
if (isset($message)) {
$smarty->assign('message', $message);
}
$smarty->display('shop.tpl');
?>