-
Notifications
You must be signed in to change notification settings - Fork 2
/
Session.php
151 lines (133 loc) · 4.47 KB
/
Session.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
<?php
require_once("Autoloader.php");
/**
*
*/
class Session
{
function __construct()
{
if(!isset($_SESSION)) {
session_start();
}
}
// add 'normal' tickets to cart
function AddToCart($eventId, $typeEvent, $amount) {
if(!isset($_SESSION['Tickets'])) {
$_SESSION['Tickets'] = array();
}
$allCartItems = EncryptionHelper::Decrypt($_SESSION['Tickets']);
$newCartItems = array();
$added = false;
foreach ($allCartItems as $cartItem) {
// check if ticket already exists in cart
if ($eventId == $cartItem['EventId'] && $typeEvent == $cartItem['TypeEvent']) {
// if so, update the amount of the ticket and add it back to array
$cartItem['Amount'] = intval($cartItem['Amount']) + intval($amount);
$newCartItems[] = $cartItem;
$added = true;
}
else {
$newCartItems[] = $cartItem;
}
}
if(!$added){
// if ticket wasn't added previously because it didn't exist yet, add it now
$cartItem = array("EventId"=>$eventId,"TypeEvent"=>$typeEvent,"Amount"=>$amount);
$newCartItems[] = $cartItem;
}
// clear and update tickets session
$_SESSION['Tickets'] = null;
$_SESSION['Tickets'] = EncryptionHelper::Encrypt($newCartItems);
}
function AddToCartFood($eventId, $childAmount, $adultAmount, $startTime, $date, $extraInfo) {
$typeEvent = 1;
if (!isset($_SESSION['Tickets'])) {
$_SESSION['Tickets'] = array();
}
$allCartItems = EncryptionHelper::Decrypt($_SESSION['Tickets']);
$newCartItems = array();
$added = false;
foreach ($allCartItems as $cartItem) {
// check if reservation already exists in cart (eventId, startTime and date define if the cartItem already exists in current session)
if ($eventId == $cartItem['EventId'] && $startTime == $cartItem['startTime'] && $date == $cartItem['Date']) {
// reservation already exists in session, update the amount
$cartItem['ChildAmount'] = intval($cartItem['ChildAmount']) + intval($childAmount);
$cartItem['AdultAmount'] = intval($cartItem['AdultAmount']) + intval($adultAmount);
$newCartItems[] = $cartItem;
$added = true;
}
else {
$newCartItems[] = $cartItem;
}
}
if (!$added) {
// if reservation wasn't added previously because it didn't exist yet, add it now
$cartItem = array("EventId"=>$eventId, "TypeEvent"=>$typeEvent, "ChildAmount"=>$childAmount, "AdultAmount"=>$adultAmount, "StartTime"=>$startTime, "Date"=>$date, "ExtraInfo"=>$extraInfo);
$newCartItems[] = $cartItem;
}
// clear and update tickets session
$_SESSION['Tickets'] = null;
$_SESSION['Tickets'] = EncryptionHelper::Encrypt($newCartItems);
}
function RemoveFromCart($eventId, $typeEvent){
if(!isset($_SESSION['Tickets'])) {
$_SESSION['Tickets'] = array();
}
$allCartItems = EncryptionHelper::Decrypt($_SESSION['Tickets']);
$newCartItems = array();
$removedAmount = 0;
foreach ($allCartItems as $cartItem) {
if ($eventId == $cartItem['EventId'] && $typeEvent == $cartItem['TypeEvent']){
$removedAmount = $removedAmount + intval($cartItem['Amount']);
}
else {
$newCartItems[] = $cartItem;
}
}
$_SESSION['Tickets'] = null;
$_SESSION['Tickets'] = EncryptionHelper::Encrypt($newCartItems);
return $removedAmount;
}
function RemoveFromCartFood($eventId, $typeEvent, $amountType) {
if (!isset($_SESSION['Tickets'])) {
$_SESSION['Tickets'] = array();
}
$allCartItems = EncryptionHelper::Decrypt($_SESSION['Tickets']);
$newCartItems = array();
$removedAmount = 0;
foreach ($allCartItems as $cartItem) {
if ($eventId == $cartItem['EventId']) {
// check what reservation type we're dealing with (child or normal) ...
if ($cartItem["ChildAmount"] != '0') {
// it's a childPrice reservation
if ($amountType == 0) {
$removedAmount = $removedAmount + intval($cartItem['ChildAmount']);
$cartItem["ChildAmount"] = '0';
}
}
if ($cartItem["AdultAmount"] != '0') {
// it's an adultPrice reservation
if ($amountType == 1) {
$removedAmount = $removedAmount + intval($cartItem['AdultAmount']);
$cartItem["AdultAmount"] = '0';
}
}
// check if both amounts are NULL, if so don't bother adding ticket back to session
if ($cartItem['ChildAmount'] != '0' || $cartItem['AdultAmount'] != '0') {
$newCartItems[] = $cartItem;
}
}
else {
$newCartItems[] = $cartItem;
}
}
$_SESSION['Tickets'] = null;
$_SESSION['Tickets'] = EncryptionHelper::Encrypt($newCartItems);
return $removedAmount;
}
function CleanCart(){
$_SESSION['Tickets'] = array();
}
}
?>