public function __construct($controller, $name, $show_actions = true)
 {
     $TempBasketID = Store_BasketController::get_temp_basket_id();
     $order_id = DB::Query("SELECT id FROM `order` WHERE (`TempBasketID`='" . $TempBasketID . "')")->value();
     /* Basket GridField */
     $config = new GridFieldConfig();
     $dataColumns = new GridFieldDataColumns();
     $dataColumns->setDisplayFields(array('getPhoto' => "Photo", 'Title' => 'Product', 'Price' => 'Item Price', 'Quantity' => 'Quantity', 'productPrice' => 'Total Price', 'getfriendlyTaxCalculation' => 'Tax Inc/Exc', 'TaxClassName' => 'Tax'));
     $config->addComponent($dataColumns);
     $config->addComponent(new GridFieldTitleHeader());
     $basket = GridField::create("BasketItems", "", DataObject::get("Order_Items", "(OrderID='" . $order_id . "')"), $config);
     /* Basket Subtotal */
     $subtotal = new Order();
     $subtotal = $subtotal->calculateSubTotal($order_id);
     $subtotal = ReadonlyField::create("SubTotal", "Basket Total (" . Product::getDefaultCurrency() . ")", $subtotal);
     /* Fields */
     $fields = FieldList::create($basket, $subtotal, ReadonlyField::create("Tax", "Tax", "Calculated on the Order Summary page."));
     /* Actions */
     $actions = FieldList::create(CompositeField::create(FormAction::create('continueshopping', 'Continue Shopping'), FormAction::create('placeorder', 'Place Order')));
     /* Required Fields */
     $required = new RequiredFields(array());
     /*
      * Now we create the actual form with our fields and actions defined 
      * within this class.
      */
     return parent::__construct($controller, $name, $fields, $show_actions ? $actions : FieldList::create(), $required);
 }
 /**
  * is_basket_full
  * Check if the basket has items.
  *
  * @return Boolean
  */
 public static function is_basket_full()
 {
     $TempBasketID = Store_BasketController::get_temp_basket_id();
     /**
      * If a TempBasketID doesn't exist then return false.
      */
     if (!$TempBasketID) {
         return false;
     } elseif (DataObject::get("Order_Items", "(`TempBasketID`='" . $TempBasketID . "')")->count() == 0) {
         return false;
     } else {
         return true;
     }
 }
 /**
  * ACTION /addtobasket
  * Add the requested item to the basket.
  */
 public function addtobasket($data)
 {
     /* Retreive the TempBasketID (Cookie) for the current users basket. If it doesn't exist, create one */
     if (Store_BasketController::get_temp_basket_id()) {
         $TempBasketID = Store_BasketController::get_temp_basket_id();
     } else {
         $TempBasketID = Store_BasketController::set_temp_basket_id();
     }
     /* Try to fetch an Order record using the TempBasketID */
     $Order = DataObject::get_one("Order", "(`TempBasketID`='" . $TempBasketID . "')");
     /**
      * If an Order record doesn't exist, create the Order record first.
      */
     if (!$Order) {
         $n = new Order();
         $n->TempBasketID = $TempBasketID;
         $n->write();
         $Order = DataObject::get_one("Order", "(`TempBasketID`='" . $TempBasketID . "')");
     }
     /**
      * Do any Order_Items exist for this Product in the current Order? If yes, increment Quantity.
      * Otherwise, add a new item.
      */
     $count = new SQLQuery("COUNT(*)");
     $count->setFrom("Order_Items")->addWhere("(`OriginalProductID`='" . $data["ProductID"] . "' AND `TempBasketID`='" . $TempBasketID . "')");
     if ($count->execute()->value() > 0) {
         DB::Query("UPDATE Order_Items SET Quantity=Quantity + " . $data["Qty"] . " WHERE (`OriginalProductID`='" . $data["ProductID"] . "' AND `TempBasketID`='" . $TempBasketID . "')");
     } else {
         /**
          * Proceed to add the selected Product to the order as an Order_Items with the same TempBasketID.
          */
         $order_item = new Order_Items();
         $order_item->OrderID = $Order->ID;
         $order_item->OriginalProductID = $data["ProductID"];
         $order_item->Quantity = $data["Qty"];
         $order_item->TempBasketID = $TempBasketID;
         /**
          * As this order is still in its 'Shopping Basket' stages we will have no customer information
          * to calculate tax with at this time. Set tax rate and class to zero for now.
          */
         $order_item->TaxClassName = "To Be Calculated";
         $order_item->TaxClassRate = "0.00";
         /* Write to the database */
         $order_item->write();
     }
     /* Take the user to their Basket */
     return $this->redirect(Store_BasketController::get_link());
 }
 /**
  * OrderSummaryTotals
  * Return a form showing the totals from a given order.
  *
  * @return CustomerExistingAddressForm
  */
 public function OrderSummaryTotals()
 {
     $order = DataObject::get_one("Order", "(`TempBasketID`='" . Store_BasketController::get_temp_basket_id() . "')");
     return OrderSummaryTotals::create($this, "OrderSummaryTotals", $order);
 }
 /**
  * LogoutLink
  * Return a logout link 
  *
  * @param String $location The location to direct to. i.e. storefront, basket, placeorder
  * @return URL
  */
 public function LogoutLink($location = null)
 {
     $security = new Security();
     /* Set $BackURL based on $location */
     switch ($location) {
         /* Basket */
         case "basket":
             $Store_BasketController = new Store_BasketController();
             $BackURL = $Store_BasketController->link();
             break;
             /* Order Step 1 */
         /* Order Step 1 */
         case "placeorder":
             $Store_OrderController = new Store_OrderController();
             $BackURL = $Store_OrderController->link() . "/place/one";
             break;
             /* Storefront */
         /* Storefront */
         default:
             $BackURL = self::get_link();
             break;
     }
     return $security->Link('logout') . "?BackURL=" . $BackURL;
 }