<?php
class
CartAPI {
private
$CartArray
=
array
();
private
$CartCount
;
public
$Expires
= 86400;
public
function
__construct(
$Id
=
""
,
$Name
=
""
,
$Price1
=
""
,
$Price2
=
""
,
$Price3
=
""
,
$Count
=
""
,
$Image
=
""
,
$Expires
= 86400) {
if
(
$Id
!=
""
&&
is_numeric
(
$Id
)) {
$this
->Expires =
$Expires
;
$this
->addCart(
$Id
,
$Name
,
$Price1
,
$Price2
,
$Price3
,
$Count
,
$Image
);
}
}
public
function
addCart(
$Id
,
$Name
,
$Price1
,
$Price2
,
$Price3
,
$Count
,
$Image
) {
$this
->CartArray =
$this
->CartView();
if
(
$this
->checkItem(
$Id
)) {
$this
->ModifyCart(
$Id
,
$Count
,0);
return
false;
}
$this
->CartArray[0][
$Id
] =
$Id
;
$this
->CartArray[1][
$Id
] =
$Name
;
$this
->CartArray[2][
$Id
] =
$Price1
;
$this
->CartArray[3][
$Id
] =
$Price2
;
$this
->CartArray[4][
$Id
] =
$Price3
;
$this
->CartArray[5][
$Id
] =
$Count
;
$this
->CartArray[6][
$Id
] =
$Image
;
$this
->save();
}
public
function
ModifyCart(
$Id
,
$Count
,
$Flag
=
""
) {
$tmpId
=
$Id
;
$this
->CartArray =
$this
->CartView();
$tmpArray
= &
$this
->CartArray;
if
(!
is_array
(
$tmpArray
[0]))
return
false;
if
(
$Id
< 1) {
return
false;
}
foreach
(
$tmpArray
[0]
as
$item
) {
if
(
$item
===
$tmpId
) {
switch
(
$Flag
) {
case
0:
$tmpArray
[5][
$Id
] +=
$Count
;
break
;
case
1:
$tmpArray
[5][
$Id
] -=
$Count
;
break
;
case
2:
if
(
$Count
== 0) {
unset(
$tmpArray
[0][
$Id
]);
unset(
$tmpArray
[1][
$Id
]);
unset(
$tmpArray
[2][
$Id
]);
unset(
$tmpArray
[3][
$Id
]);
unset(
$tmpArray
[4][
$Id
]);
unset(
$tmpArray
[5][
$Id
]);
unset(
$tmpArray
[6][
$Id
]);
break
;
}
else
{
$tmpArray
[5][
$Id
] =
$Count
;
break
;
}
case
3:
unset(
$tmpArray
[0][
$Id
]);
unset(
$tmpArray
[1][
$Id
]);
unset(
$tmpArray
[2][
$Id
]);
unset(
$tmpArray
[3][
$Id
]);
unset(
$tmpArray
[4][
$Id
]);
unset(
$tmpArray
[5][
$Id
]);
unset(
$tmpArray
[6][
$Id
]);
break
;
default
:
break
;
}
}
}
$this
->save();
}
public
function
RemoveAll() {
$this
->CartArray =
array
();
$this
->save();
}
public
function
CartView() {
$cookie
=
stripslashes
(
$_COOKIE
['CartAPI']);
if
(!
$cookie
)
return
false;
$tmpUnSerialize
= unserialize(
$cookie
);
return
$tmpUnSerialize
;
}
public
function
checkCart() {
$tmpArray
=
$this
->CartView();
if
(
count
(
$tmpArray
[0]) < 1) {
return
false;
}
return
true;
}
public
function
CountPrice() {
$tmpArray
=
$this
->CartArray =
$this
->CartView();
$outArray
=
array
();
$i
= 0;
if
(
is_array
(
$tmpArray
[0])) {
foreach
(
$tmpArray
[0]
as
$key
=>
$val
) {
$outArray
[0] +=
$tmpArray
[2][
$key
] *
$tmpArray
[5][
$key
];
$outArray
[1] +=
$tmpArray
[3][
$key
] *
$tmpArray
[5][
$key
];
$outArray
[2] +=
$tmpArray
[4][
$key
] *
$tmpArray
[5][
$key
];
$outArray
[3] +=
$tmpArray
[5][
$key
];
$i
++;
}
}
return
$outArray
;
}
public
function
CartCount() {
$tmpArray
=
$this
->CartView();
$tmpCount
=
count
(
$tmpArray
[0]);
$this
->CartCount =
$tmpCount
;
return
$tmpCount
;
}
public
function
save() {
$tmpArray
=
$this
->CartArray;
$tmpSerialize
= serialize(
$tmpArray
);
setcookie(
"CartAPI"
,
$tmpSerialize
,time()+
$this
->Expires);
}
private
function
checkItem(
$Id
) {
$tmpArray
=
$this
->CartArray;
if
(!
is_array
(
$tmpArray
[0]))
return
;
foreach
(
$tmpArray
[0]
as
$item
) {
if
(
$item
===
$Id
)
return
true;
}
return
false;
}
}
?>