This repository has been archived by the owner on Feb 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDefault.aspx.cs
360 lines (294 loc) · 12.5 KB
/
Default.aspx.cs
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Net;
using System.ComponentModel;
public partial class _Default : System.Web.UI.Page
{
protected string errorMessage = "";
protected Dictionary<string, string> userInfo;
protected Dictionary<int, TableGroup> tables;
protected Dictionary<int, TableGroup> cartItems = new Dictionary<int, TableGroup>();
protected DataView cartView = new DataView();
protected void Page_Load(object sender, EventArgs e)
{
if (Session.IsNewSession == true || Session["LoggedIn"] == null)
Response.Redirect("login.aspx");
//try
//{
Database db = new Database();
tables = db.getTables();
userInfo = db.getUser(Session["email"].ToString());
/*}
catch (Exception ex)
{
errorMessage = "Could not load database: " + ex.Message;
}*/
if (!IsPostBack)
{
reloadSelectionDropdown();
emailTextbox.Text = userInfo["email"];
nameTextbox.Text = userInfo["name"];
phoneTextbox.Text = userInfo["phone"];
schoolTextbox.Text = userInfo["school"];
comments.Text = userInfo["comment"];
Session["userInfo"] = userInfo;
}
else
{
errorMessage = ViewState["errormsg"] as String;
cartItems = ViewState["cartItems"] as Dictionary<int, TableGroup>;
cartView = new DataView(remakeCartDataTable());
ShoppingCart.DataSource = cartView;
ShoppingCart.DataBind();
}
}
private bool isNull(Object obj)
{
return obj == null;
}
// Setup the dropdown lists for tables and chairs
private void reloadSelectionDropdown()
{
DataTable tablesDataTable = new DataTable();
tablesDataTable.Columns.Add(new DataColumn("Text", typeof(String)));
tablesDataTable.Columns.Add(new DataColumn("Value", typeof(int)));
foreach (TableGroup singleTable in tables.Values)
{
int seatsAvailable = singleTable.seatsAvailable();
if (cartItems.Keys.Contains(singleTable.tableNumber))
seatsAvailable -= cartItems[singleTable.tableNumber].seatsTaken();
if (seatsAvailable > 0)
{
DataRow dr = tablesDataTable.NewRow();
dr[0] = singleTable.tableNumber;
dr[1] = singleTable.tableNumber;
tablesDataTable.Rows.Add(dr);
}
}
DataView dv = new DataView(tablesDataTable);
dv.Sort = "Value";
tableNum.DataSource = dv;
tableNum.DataTextField = "Text";
tableNum.DataValueField = "Value";
tableNum.DataBind();
reloadChairOptions( Convert.ToInt32(tableNum.SelectedValue.ToString()) );
cartView = new DataView(remakeCartDataTable());
ShoppingCart.DataSource = cartView;
ShoppingCart.DataBind();
ViewState["cartItems"] = cartItems;
}
// For when the button is clicked to add a table/chair combo to the cart
protected void button_addToCart(object sender, EventArgs e)
{
int chairsSelected = Convert.ToInt32(chairNum.SelectedItem.Text);
int tableNumber = Convert.ToInt32(tableNum.SelectedItem.Text);
int seatsAvailable = cartItems.Keys.Contains(tableNumber) ?
cartItems[tableNumber].seatsAvailable() : Config.SEATS_PER_TABLE;
if (tableNumber > Config.TOTAL_TABLES || chairsSelected > seatsAvailable)
throw new IndexOutOfRangeException("Number of tables or chairs exceeds the limit.");
TableGroup newTable;
if (cartItems.Keys.Contains(tableNumber))
{
chairsSelected += cartItems[tableNumber].seatsTaken();
cartItems.Remove(tableNumber);
}
newTable = new TableGroup(tableNumber);
for (int i = 0; i < chairsSelected; i++)
{
Chair newSeat = new Chair(null);
newTable.addSeat(newSeat);
}
cartItems.Add(tableNumber, newTable);
reloadSelectionDropdown();
DataTable cartTable = remakeCartDataTable();
cartView = new DataView(cartTable);
ShoppingCart.DataSource = cartView;
ShoppingCart.DataBind();
ViewState["cartItems"] = cartItems;
}
private DataTable remakeCartDataTable()
{
DataTable cartTable = new DataTable();
cartTable.Columns.Add("Table Number", typeof(string));
cartTable.Columns.Add("Number of Chairs", typeof(string));
DataRow row;
foreach (TableGroup cartItem in cartItems.Values)
{
row = cartTable.NewRow();
row[0] = cartItem.tableNumber;
row[1] = cartItem.seatsTaken();
cartTable.Rows.Add(row);
}
return cartTable;
}
// When the first dropdown list is changed, update so the second one has the correct number of seats shown
protected void ddl_changeChairNums(object sender, EventArgs e)
{
int selectedTable = Convert.ToInt32(tableNum.SelectedValue.ToString());
reloadChairOptions(selectedTable);
}
// Show the correct number of seats available per table
private void reloadChairOptions(int selectedTable)
{
DataTable chairListDataTable = new DataTable();
chairListDataTable.Columns.Add(new DataColumn("Text", typeof(String)));
chairListDataTable.Columns.Add(new DataColumn("Value", typeof(int)));
int freeCounter = 0;
int seatsAvailable = tables[selectedTable].seatsAvailable();
if (cartItems.Keys.Contains(selectedTable))
seatsAvailable -= cartItems[selectedTable].seatsTaken();
for (int i = 0; i < seatsAvailable; i++)
{
DataRow dr = chairListDataTable.NewRow();
dr[0] = 1 + freeCounter++;
dr[1] = i;
chairListDataTable.Rows.Add(dr);
}
chairNum.DataSource = new DataView(chairListDataTable);
chairNum.DataTextField = "Text";
chairNum.DataValueField = "Value";
chairNum.DataBind();
}
protected void ShoppingCart_ItemCommand(object sender, DataGridCommandEventArgs e)
{
cartItems.Remove(Convert.ToInt32(e.Item.Cells[0].Text));
reloadSelectionDropdown();
remakeCartDataTable();
cartView = new DataView(remakeCartDataTable());
ShoppingCart.DataSource = cartView;
ShoppingCart.DataBind();
ViewState["cartItems"] = cartItems;
}
protected void button_submitOrder(object sender, EventArgs e)
{
if (cartItems.Count == 0)
{
errorMessage = "Cart is empty, please select at least one seat.";
return;
}
userInfo["name"] = nameTextbox.Text;
userInfo["phone"] = phoneTextbox.Text;
userInfo["school"] = schoolTextbox.Text;
userInfo["comment"] = comments.Text;
Session["userInfo"] = userInfo;
Session["cart"] = cartItems;
Session["totalCost"] = calculateCost();
try
{
Database db = new Database();
db.update_user(userInfo["email"], userInfo["name"], userInfo["school"], userInfo["phone"], userInfo["comment"]);
db.purchaseChairs(cartItems, userInfo["email"]);
}
catch (ArgumentException ex)
{
errorMessage = "Error: " + ex.Message;
return;
}
catch (Exception ex)
{
errorMessage = "Could not store your selection, contact the system administrator with the following message:<br>" + ex.Message;
return;
}
try
{
sendEmail(userInfo["email"]);
}
catch (Exception ex)
{
errorMessage = "Your selection was stored but a confirmation email could not be sent, contact the system administrator with the following message:<br>" + ex.Message;
return;
}
Response.Redirect("~/Confirmation.aspx");
}
private int calculateCost()
{
int cost = 0;
foreach (TableGroup table in cartItems.Values)
cost += table.seatsTaken() * Config.SEAT_PRICE;
return cost;
}
private string generateSeatString()
{
string seatString = "";
foreach (TableGroup singleTable in cartItems.Values)
seatString += "Table #" + singleTable.tableNumber + " for " + singleTable.seatsTaken() + " seat(s)\n";
return seatString;
}
private void sendEmail(string emailAddress)
{
int cost = calculateCost();
MailMessage outgoingMessage = new MailMessage();
outgoingMessage.From = new MailAddress(Config.SMTP_FROM_EMAIL, Config.SMTP_FROM_NAME);
outgoingMessage.Bcc.Add(Config.EVENT_HOST_EMAIL);
outgoingMessage.To.Add(emailAddress);
outgoingMessage.Subject = Config.SMTP_CONFIRM_SUBJECT;
string seatString = generateSeatString();
outgoingMessage.Body = String.Format(Config.SMTP_CONFIRM_BODY,
userInfo["name"],
userInfo["school"],
seatString,
cost,
Config.EVENT_HOST_NAME,
Config.EVENT_HOST_EMAIL,
Config.EVENT_CHEQUE_PAYABLE);
SmtpClient server = new SmtpClient(Config.SMTP_SERVER, Config.SMTP_PORT);
server.EnableSsl = false;
server.UseDefaultCredentials = true;
server.Send(outgoingMessage);
}
// Draws the tables onto the page in a grid
public void renderTables(int rows, int columns, int startingIndex)
{
for (int rowI = 0; rowI < rows; rowI++)
{
Response.Write("<div style=\"width:960px; height:85px; position:relative; float:left;\">\n");
for (int colI = 0; colI < columns; colI++)
{
string tableColorLocation;
int tableIndex = (rowI * columns) + colI + startingIndex;
if (tables[tableIndex].isFull())
tableColorLocation = "images/redCircle.png";
else if (cartItems.Keys.Contains(tableIndex) && Config.SEATS_PER_TABLE == tables[tableIndex].seatsTaken() + cartItems[tableIndex].seatsTaken())
tableColorLocation = "images/yellowCircle.png";
else
tableColorLocation = "images/blueCircle.png";
Response.Write("<div style=\"width:85px; height:85px; position:relative; float:left;text-align: center;\">\n");
Response.Write("<img src=\"" + tableColorLocation +
"\" style=\"left:24px; top:24px; position:absolute;\" width=37 height=37>\n");
Response.Write("<p style=\"position: relative; margin-left:auto; margin-right:auto; margin-top:32px;\">" + (tableIndex) + "</p>\n");
for (int chairI = 0; chairI < Config.SEATS_PER_TABLE; chairI++)
{
double chairAngle = chairI * Math.PI / 5;
double height = Math.Cos(chairAngle) * 32;
double width = Math.Sin(chairAngle) * 32;
int leftLoc = (int)width - 8 + 42; // -8 for the offset of the size of the chair image, + 42 to correctly place it in around the table
int topLoc = (int)height - 8 + 42;
if (chairI < tables[tableIndex].seatsTaken())
{
Response.Write("<img style=\"left:" + leftLoc + "px; top:" + topLoc +
"px; position:absolute; width=16 height=16\" src=\"images/redCircle.png\" width=16 height=16>\n");
}
else if (cartItems.Keys.Contains(tableIndex) && chairI < tables[tableIndex].seatsTaken() + cartItems[tableIndex].seatsTaken())
{
Response.Write("<img style=\"left:" + leftLoc + "px; top:" + topLoc +
"px; position:absolute; width=16 height=16\" src=\"images/yellowCircle.png\" width=16 height=16>\n");
}
else
{
Response.Write("<img style=\"left:" + leftLoc + "px; top:" + topLoc +
"px; position:absolute; width=16 height=16\" src=\"images/blueCircle.png\" width=16 height=16>\n");
}
}
Response.Write("</div>");
}
Response.Write("</div>");
}
}
}