File: /var/www/vhosts/creativefellows.nl/test.creativefellows.nl/tekenmappen/classes/CartController.php
<?php
/**
* Shopping cart session controller
*/
class CartController extends BaseController
{
function __construct($bucket = 'default',$uid=null)
{
if (! isset($_SESSION[$bucket])) {
$_SESSION[$bucket] = [];
}
if (! isset($_SESSION[$bucket][$uid]) && $uid !== null) {
$_SESSION[$bucket][$uid] = [];
}
$this->bucket = $bucket;
$this->uid = $uid;
}
public function set($index, $value)
{
$_SESSION[$this->bucket][$this->uid][$index] = $value;
}
public function setSingle($index,$value)
{
$_SESSION[$this->bucket][$index] = $value;
}
public function getSingle($index)
{
if (! $this->existsSingle($index)) return null;
return $_SESSION[$this->bucket][$index];
}
public function get($uid,$index)
{
if (! $this->exists($uid,$index)) {
return null;
}
return $_SESSION[$this->bucket][$uid][$index];
}
public function exists($uid,$index)
{
return isset($_SESSION[$this->bucket][$uid][$index]);
}
public function existsSingle($index)
{
return isset($_SESSION[$this->bucket][$index]);
}
public function all()
{
return $_SESSION[$this->bucket];
}
public function unset($uid)
{
//if ($this->exists($uid)) {
unset($_SESSION[$this->bucket][$uid]);
// }
}
public function clear()
{
unset($_SESSION[$this->bucket]);
}
public function clearAll($bucket)
{
unset($_SESSION[$bucket]);
}
public function count()
{
return count($this->all());
}
public function getProductTotal($uid){
return $_SESSION[$this->bucket][$uid]["amount"] * $_SESSION[$this->bucket][$uid]["price"];
}
public function getOrderSubtotal()
{
$total = 0;
foreach($_SESSION[$this->bucket] as $product){
$total += $product["amount"] * $product["price"];
}
return $total;
}
public function getAmountSelect($uid,$index)
{
$amount = $_SESSION[$this->bucket][$uid][$index];
$html = "<select class='change-amount' name='amount'>";
for($i= 1; $i<=10; $i++)
{
$selected = ($i == $amount) ? " selected" : null;
$html .= "<option value='$i'$selected>$i</option>";
}
$html .= "</select>";
return $html;
}
public function getOrderTotals($delivery_costs,$global_tax,$round_rumbers=false)
{
$data = [];
$data["subtotal"] = $this->getOrderSubtotal();
$data["delivery"] = $delivery_costs;
$data["total"] = $this->round($data["subtotal"] + $data["delivery"]);
$data["tax"] = $this->round( (($data["total"] / (100 + $global_tax))) * $global_tax);
if($round_rumbers == true)
{
foreach($data as $i => $d)
{
$data[$i] = $this->formatPrice($d);
}
}
return $data;
}
public function setOrderTotals($delivery_costs,$global_tax,$round_rumbers=false){
$amounts = $this->getOrderTotals($delivery_costs,$global_tax,$round_rumbers);
$this->subtotal = $amounts["subtotal"];
$this->delivery = $amounts["delivery"];
$this->total = $amounts["total"];
$this->tax = $amounts["tax"];
}
public function getTotal()
{
return $this->total;
}
public function getSubtotal()
{
return $this->subtotal;
}
public function getTaxTotal()
{
return $this->tax;
}
public function getDelivery()
{
return $this->delivery;
}
private function round($amount,$precision=2){
return number_format($amount,$precision);
//return round($amount,$precision);
}
public function getFieldPostName($field_arr)
{
$eid = (isset($field_arr["elementId"])) ? $field_arr["elementId"] : null;
return $eid.preg_replace("/[^0-9a-zA-Z]/","",strtolower($field_arr["name"]));
}
public function getConfirmElement($array,$size="large-6 small-12",$value="")
{
$full_width = array("tussenkop","submit","textarea","input");
$name = $array["name"];
$desc = (trim($array["desc"]) != "") ? $array["desc"] : $name;
switch($array["type"]){
default:
$el .= '<div class="grid-x grid-padding-x">';
$el .= '<div class="cell small-4"><strong>'. $desc .'</strong></div>';
$el .= '<div class="cell small-8">'. $value .'</div>';
$el .= '</div>';
break;
case "select":
case "radio":
case "select":
$el .= '<div class="grid-x grid-padding-x">';
$el .= '<div class="cell small-4"><strong>'. $desc .'</strong></div>';
$el .= '<div class="cell small-8">'. $array["options"][$value] .'</div>';
$el .= '</div>';
break;
case "tussenkop":
$el .= "<h3>$name</h3>";
break;
}
$data = '<fieldset class="cell small-12">';
$data .= $el;
$data .= '</fieldset>';
return $data;
}
public function getFormElement($array,$size="large-6 small-12",$value="")
{
$hidefieldsets = array("hidden","msg","captcha");
$full_width = array("tussenkop","submit","textarea");
$type = $array["type"];
$name = $array["name"];
$desc = (trim($array["desc"]) != "") ? $array["desc"] : $name;
$opts = (isset($array["options"])) ? $array["options"] : null ;
$eid = (isset($array["elementId"])) ? $array["elementId"] : null;
$req = (isset($array["required"]) && $array["required"] == 1) ? " required" : "";
$clear = $array["clear"] == true;
$inpName = $eid.preg_replace("/[^0-9a-zA-Z]/","",strtolower($name));
$required_msg = '<span class="form-error">Verplicht veld</span>';
$el = "";
switch($type){
case "input":
$el .= '<label><span>'. $desc .'</span>';
$el .= '<input type="text" name="'. $inpName .'" autocomplete="off" value="'. $value .'" placeholder="'. $desc .'" '. $req .' />';
$el .= $required_msg;
$el .= '</label>';
break;
case "select":
$el .= '<label><span>'. $desc .'</span>';
$el .= '<select type="text" name="'. $inpName .'" '. $req .'>';
foreach($opts as $o)
{
$selected = ($value == $o) ? " selected" : "";
$el .= '<option value="'. strtolower(htmlentities($o)) .'"'.$selected.'>'. utf8_decode($o) .'</option>';
}
$el .= "</select>";
$el .= $required_msg;
$el .= '</label>';
break;
case "radio":
$el .= '<legend><span>'. $desc . '</span></legend>';
foreach($opts as $i => $o)
{
$el .= '<input id="'. $inpName . $i .'" name="'. $inpName .'" type="radio" value="'. $i .'" '. ($i == 0 ? " checked" : null) .'><label for="'. $inpName . $i .'">'. $o .'</label>';
}
$el .= $required_msg;
break;
case "checkbox":
$el .= '<legend><span>'. $desc . '</span></legend>';
foreach($opts as $o)
{
$el .= '<input type="checkbox" id="'. $inpName . $i .'" name="'. $inpName .'[]" value="'. $o .'" /> <label for="'. $inpName . $i .'">'. $o .'</label>';
}
break;
case "textarea":
$el .= '<label><span>'. $desc .'</span>';
$el .= '<textarea name="'. $inpName .'"" placeholder="'. $desc .'">'. $value .'</textarea>';
$el .= '</label>';
break;
case "submit":
$el .= '<button class="button primary" type="submit">'. $desc .'</button>';
break;
case "tussenkop":
$el .= "<label class='group'>$name</label>";
if($array["desc"]) $el .= "<p>".$array["desc"]."</p>";
break;
}
$data = "";
if(!in_array($type,$hidefieldsets)) $data .= '<fieldset class="cell '. (!in_array($type,$full_width) && !isset($array["clear"]) ? $size : "small-12" ) .' '. $f[3] .'">';
$data .= $el;
if(!in_array($type,$hidefieldsets)) $data .= '</fieldset>';
return $data;
}
public function output(){
d($_SESSION[$this->bucket]);
}
}
?>