/**
  * work out the options for the user
  **/
 protected function workOutMessagesAndActions()
 {
     if (!$this->workedOutMessagesAndActions) {
         $this->actionLinks = new ArrayList(array());
         //what order are we viewing?
         $viewingRealCurrentOrder = $this->CurrentOrderIsInCart();
         $currentUserID = Member::currentUserID();
         //Continue Shopping
         if (isset($this->ContinueShoppingLabel) && $this->ContinueShoppingLabel) {
             if ($viewingRealCurrentOrder) {
                 if ($this->isCartPage()) {
                     $continueLink = $this->ContinueShoppingLink();
                     if ($continueLink) {
                         $this->actionLinks->push(new ArrayData(array("Title" => $this->ContinueShoppingLabel, "Link" => $continueLink)));
                     }
                 }
             }
         }
         //Proceed To CheckoutLabel
         if (isset($this->ProceedToCheckoutLabel) && $this->ProceedToCheckoutLabel) {
             if ($viewingRealCurrentOrder) {
                 if ($this->isCartPage()) {
                     $checkoutPageLink = CheckoutPage::find_link();
                     if ($checkoutPageLink && $this->currentOrder && $this->currentOrder->getTotalItems()) {
                         $this->actionLinks->push(new ArrayData(array("Title" => $this->ProceedToCheckoutLabel, "Link" => $checkoutPageLink)));
                     }
                 }
             }
         }
         //view account details
         if (isset($this->ShowAccountLabel) && $this->ShowAccountLabel) {
             if ($this->isOrderConfirmationPage() || $this->isCartPage()) {
                 if (AccountPage::find_link()) {
                     if ($currentUserID) {
                         $this->actionLinks->push(new ArrayData(array("Title" => $this->ShowAccountLabel, "Link" => AccountPage::find_link())));
                     }
                 }
             }
         }
         //go to current order
         if (isset($this->CurrentOrderLinkLabel) && $this->CurrentOrderLinkLabel) {
             if ($this->isCartPage()) {
                 if (!$viewingRealCurrentOrder) {
                     $this->actionLinks->push(new ArrayData(array("Title" => $this->CurrentOrderLinkLabel, "Link" => ShoppingCart::current_order()->Link())));
                 }
             }
         }
         //Save order - we assume only current ones can be saved.
         if (isset($this->SaveOrderLinkLabel) && $this->SaveOrderLinkLabel) {
             if ($viewingRealCurrentOrder) {
                 if ($currentUserID && $this->currentOrder->MemberID == $currentUserID) {
                     if ($this->isCartPage()) {
                         if ($this->currentOrder && $this->currentOrder->getTotalItems() && !$this->currentOrder->IsSubmitted()) {
                             $this->actionLinks->push(new ArrayData(array("Title" => $this->SaveOrderLinkLabel, "Link" => $this->Link("saveorder") . "/" . $this->currentOrder->ID . "/")));
                         }
                     }
                 }
             }
         }
         //load order
         if (isset($this->LoadOrderLinkLabel) && $this->LoadOrderLinkLabel) {
             if ($this->isCartPage() && $this->currentOrder) {
                 if (!$viewingRealCurrentOrder) {
                     $this->actionLinks->push(new ArrayData(array("Title" => $this->LoadOrderLinkLabel, "Link" => $this->Link("loadorder") . "/" . $this->currentOrder->ID . "/")));
                 }
             }
         }
         //delete order
         if (isset($this->DeleteOrderLinkLabel) && $this->DeleteOrderLinkLabel) {
             if ($this->isCartPage() && $this->currentOrder) {
                 if (!$viewingRealCurrentOrder) {
                     $this->actionLinks->push(new ArrayData(array("Title" => $this->DeleteOrderLinkLabel, "Link" => $this->Link("deleteorder") . "/" . $this->currentOrder->ID . "/")));
                 }
             }
         }
         //Start new order
         //Strictly speaking this is only part of the
         //OrderConfirmationPage but we put it here for simplicity's sake
         if (isset($this->StartNewOrderLinkLabel) && $this->StartNewOrderLinkLabel) {
             if ($this->isOrderConfirmationPage()) {
                 $this->actionLinks->push(new ArrayData(array("Title" => $this->StartNewOrderLinkLabel, "Link" => CartPage::new_order_link($this->currentOrder->ID))));
             }
         }
         //copy order
         //Strictly speaking this is only part of the
         //OrderConfirmationPage but we put it here for simplicity's sake
         if (isset($this->CopyOrderLinkLabel) && $this->CopyOrderLinkLabel) {
             if ($this->isOrderConfirmationPage() && $this->currentOrder->ID) {
                 $this->actionLinks->push(new ArrayData(array("Title" => $this->CopyOrderLinkLabel, "Link" => OrderConfirmationPage::copy_order_link($this->currentOrder->ID))));
             }
         }
         //actions from modifiers
         if ($this->isOrderConfirmationPage() && $this->currentOrder->ID) {
             $modifiers = $this->currentOrder->Modifiers();
             if ($modifiers->count()) {
                 foreach ($modifiers as $modifier) {
                     $array = $modifier->PostSubmitAction();
                     if (is_array($array) && count($array)) {
                         $this->actionLinks->push(new ArrayData($array));
                     }
                 }
             }
         }
         //log out
         //Strictly speaking this is only part of the
         //OrderConfirmationPage but we put it here for simplicity's sake
         if (Member::currentUser()) {
             if ($this->isOrderConfirmationPage()) {
                 $this->actionLinks->push(new ArrayData(array("Title" => _t("CartPage.LOGOUT", "log out"), "Link" => "/Security/logout/")));
             }
         }
         //no items
         if ($this->currentOrder) {
             if (!$this->currentOrder->getTotalItems()) {
                 $this->message = $this->NoItemsInOrderMessage;
             }
         } else {
             $this->message = $this->NonExistingOrderMessage;
         }
         $this->workedOutMessagesAndActions = true;
         //does nothing at present....
     }
 }
 /**
  * returns the link to view the Order
  * WHY NOT CHECKOUT PAGE: first we check for cart page.
  * @return CartPage | Null
  */
 function DisplayPage()
 {
     if ($this->MyStep() && $this->MyStep()->AlternativeDisplayPage()) {
         $page = $this->MyStep()->AlternativeDisplayPage();
     } elseif ($this->IsSubmitted()) {
         $page = OrderConfirmationPage::get()->First();
     } else {
         $page = CartPage::get()->Filter(array("ClassName" => 'CartPage'))->First();
         if (!$page) {
             $page = CheckoutPage::get()->First();
         }
     }
     return $page;
 }
 function getRetrieveLink()
 {
     if ($this->IsSubmitted()) {
         if (!$this->SessionID) {
             $this->SessionID = session_id();
             $this->write();
         }
         return Director::AbsoluteURL(OrderConfirmationPage::find_link()) . "retrieveorder/" . $this->SessionID . "/" . $this->ID . "/";
     } else {
         return Director::AbsoluteURL("/shoppingcart/loadorder/" . $this->ID . "/");
     }
 }
 protected function addPages()
 {
     if ($checkoutPage = CheckoutPage::get()->First()) {
         $this->getPageDefinitions($checkoutPage);
         $this->definitions["Pages"]["CheckoutPage"] = "Page where customers finalise (checkout) their order. This page is required.<br />" . ($checkoutPage ? "<a href=\"/admin/pages/edit/show/" . $checkoutPage->ID . "/\">edit</a>" : "Create one in the <a href=\"/admin/pages/add/\">CMS</a>");
         $this->configs["Pages"]["CheckoutPage"] = $checkoutPage ? "view: <a href=\"" . $checkoutPage->Link() . "\">" . $checkoutPage->Title . "</a><br />" . $checkoutPage->configArray : " NOT CREATED!";
         $this->defaults["Pages"]["CheckoutPage"] = $checkoutPage ? $checkoutPage->defaultsArray : "[add page first to see defaults]";
         $this->databaseValues["Pages"]["CheckoutPage"] = true;
     }
     if ($orderConfirmationPage = OrderConfirmationPage::get()->First()) {
         $this->getPageDefinitions($orderConfirmationPage);
         $this->definitions["Pages"]["OrderConfirmationPage"] = "Page where customers review their order after it has been placed. This page is required.<br />" . ($orderConfirmationPage ? "<a href=\"/admin/pages/edit/show/" . $orderConfirmationPage->ID . "/\">edit</a>" : "Create one in the <a href=\"/admin/pages/add/\">CMS</a>");
         $this->configs["Pages"]["OrderConfirmationPage"] = $orderConfirmationPage ? "view: <a href=\"" . $orderConfirmationPage->Link() . "\">" . $orderConfirmationPage->Title . "</a><br />" . $orderConfirmationPage->configArray : " NOT CREATED!";
         $this->defaults["Pages"]["OrderConfirmationPage"] = $orderConfirmationPage ? $orderConfirmationPage->defaultsArray : "[add page first to see defaults]";
         $this->databaseValues["Pages"]["OrderConfirmationPage"] = true;
     }
     if ($accountPage = AccountPage::get()->First()) {
         $this->getPageDefinitions($accountPage);
         $this->definitions["Pages"]["AccountPage"] = "Page where customers can review their account. This page is required.<br />" . ($accountPage ? "<a href=\"/admin/pages/edit/show/" . $accountPage->ID . "/\">edit</a>" : "Create one in the <a href=\"/admin/pages/add/\">CMS</a>");
         $this->configs["Pages"]["AccountPage"] = $accountPage ? "view: <a href=\"" . $accountPage->Link() . "\">" . $accountPage->Title . "</a><br />" . $accountPage->configArray : " NOT CREATED!";
         $this->defaults["Pages"]["AccountPage"] = $accountPage ? $accountPage->defaultsArray : "[add page first to see defaults]";
         $this->databaseValues["Pages"]["AccountPage"] = true;
     }
     if ($cartPage = CartPage::get()->Filter(array("ClassName" => "CartPage"))->First()) {
         $this->getPageDefinitions($cartPage);
         $this->definitions["Pages"]["CartPage"] = "Page where customers review their cart while shopping. This page is optional.<br />" . ($cartPage ? "<a href=\"/admin/pages/edit/show/" . $cartPage->ID . "/\">edit</a>" : "Create one in the <a href=\"/admin/pages/add/\">CMS</a>");
         $this->configs["Pages"]["CartPage"] = $cartPage ? "view: <a href=\"" . $cartPage->Link() . "\">" . $cartPage->Title . "</a>, <a href=\"/admin/pages/edit/show/" . $cartPage->ID . "/\">edit</a><br />" . $cartPage->configArray : " NOT CREATED!";
         $this->defaults["Pages"]["CartPage"] = $cartPage ? $cartPage->defaultsArray : "[add page first to see defaults]";
         $this->defaults["Pages"]["CartPage"] = $cartPage ? $cartPage->defaultsArray : "[add page first to see defaults]";
         $this->databaseValues["Pages"]["CartPage"] = true;
     }
 }
 /**
  * returns a link that can be used to test
  * the email being sent during this step
  * this method returns NULL if no email
  * is being sent OR if there is no suitable Order
  * to test with...
  * @return String
  */
 protected function testEmailLink()
 {
     if ($this->getEmailClassName()) {
         $orders = Order::get()->where("\"OrderStep\".\"Sort\" >= " . $this->Sort)->sort("IF(\"OrderStep\".\"Sort\" > " . $this->Sort . ", 0, 1) ASC, \"OrderStep\".\"Sort\" ASC, RAND() ASC")->innerJoin("OrderStep", "\"OrderStep\".\"ID\" = \"Order\".\"StatusID\"");
         if ($orders->count()) {
             if ($order = $orders->First()) {
                 return OrderConfirmationPage::get_email_link($order->ID, $this->getEmailClassName(), $actuallySendEmail = false, $alternativeOrderStepID = $this->ID);
             }
         }
     }
 }
    private function collateexamplepages()
    {
        $this->addExamplePages(0, "Checkout page", CheckoutPage::get()->First());
        $this->addExamplePages(0, "Order Confirmation page", OrderConfirmationPage::get()->First());
        $this->addExamplePages(0, "Cart page (review cart without checkout)", CartPage::get()->where("ClassName = 'CartPage'")->First());
        $this->addExamplePages(0, "Account page", AccountPage::get()->First());
        //$this->addExamplePages(1, "Donation page", AnyPriceProductPage::get()->First());
        $this->addExamplePages(1, "Products that can not be sold", Product::get()->where("\"AllowPurchase\" = 0 AND ClassName = 'Product'")->First());
        $this->addExamplePages(1, "Product group with short product display template", ProductGroup::get()->where("\"DisplayStyle\" = 'Short'")->First());
        $this->addExamplePages(1, "Product group with medium length product display template", ProductGroup::get()->where("\"DisplayStyle\" = ''")->First());
        $this->addExamplePages(1, "Product group with more detail product display template", ProductGroup::get()->where("\"DisplayStyle\" = 'MoreDetail'")->First());
        //$this->addExamplePages(1, "Quick Add page", AddToCartPage::get()->first());
        //$this->addExamplePages(1, "Shop by Tag page ", ProductGroupWithTags::get()->first());
        $this->addExamplePages(2, "Delivery options (add product to cart first)", CheckoutPage::get()->First());
        $this->addExamplePages(2, "Taxes (NZ based GST - add product to cart first)", CheckoutPage::get()->first());
        $this->addExamplePages(2, "Discount Coupon (try <i>AAA</i>)", CheckoutPage::get()->First());
        $this->addExamplePages(4, "Products with zero price", Product::get()->where("\"Price\" = 0 AND ClassName = 'Product'")->First());
        //$this->addExamplePages(5, "Corporate Account Order page", AddUpProductsToOrderPage::get()->First());
        $html = '
		<h2>Some Interesting Features</h2>
		<p>
			Below are some features of this e-commerce application that may be of interest to you:
		</p>
		<ul>
			<li>customised search for users with search history graphs for admins</li>
			<li>ability to check-out with or without adding a password (creating an account)</li>
			<li>easy to use CMS</li>
			<li>very fast product listings, making extensive use of caching</li>
			<li>many ways to display products, allowing the content editor to set things like <i>products per page</i>, <i>product selctions</i>, <i>sorting orders</i></li>
			<li>multi-currency options and currency conversions</li>
			<li>step-by-step system for completed orders leading them from being submitted to archived via very steps.  This allows the admin to review orders where needed, add extra information, such as tracking codes for delivery, etc...</li>
			<li>code that is very easy to customise and adjust for your needs</li>
			<li>a ton of additional modules are available - you can add them directly to your e-commece install or use these as examples for building your own extensions </li>
			<li>geo-coding for addresses</li>
			<li>extensive developer assistance through various tools and personalised help</li>
		</ul>
		<h2>examples shown on this demo site</h2>';
        foreach ($this->examplePages as $key => $exampleGroups) {
            $html .= "<h3>" . $exampleGroups["Title"] . "</h3><ul>";
            foreach ($exampleGroups["List"] as $examplePages) {
                $html .= '<li><span class="exampleTitle">' . $examplePages["Title"] . '</span>' . $examplePages["List"] . '</li>';
            }
            $html .= "</ul>";
        }
        $html .= '
		<h2>API Access</h2>
		<p>
			E-commerce allows you to access its model using the built-in Silverstripe API.
			This is great for communication with third party applications.
			Access examples are listed below:
		</p>
		<ul>
			<li><a href="/api/v1/Order/">view all orders</a></li>
			<li><a href="/api/v1/Order/1">view order with ID = 1</a></li>
		</ul>
		<p>
			For more information on the restful server API, you can visit the modules home: <a href="https://github.com/silverstripe/silverstripe-restfulserver">https://github.com/silverstripe/silverstripe-restfulserver</a> to find out more on this topic.
		</p>
		';
        $featuresPage = Page::get()->where("URLSegment = 'features'")->First();
        $featuresPage->Content .= $html;
        $featuresPage->write();
        $featuresPage->Publish('Stage', 'Live');
        $featuresPage->flushCache();
    }
 function run($request)
 {
     $update = array();
     // ACCOUNT PAGE
     $accountPage = DataObject::get_one('AccountPage');
     if (!$accountPage) {
         $accountPage = new AccountPage();
         $accountPage->Title = 'Account';
         $accountPage->MenuTitle = 'Account';
         $accountPage->MetaTitle = 'Account';
         $accountPage->Content = '<p>This is the account page. It is used for shop users to login and change their member details if they have an account.</p>';
         $accountPage->URLSegment = 'account';
         $accountPage->ShowInMenus = 0;
         $accountPage->writeToStage('Stage');
         $accountPage->publish('Stage', 'Live');
         DB::alteration_message('Account page \'Account\' created', 'created');
     } else {
         DB::alteration_message('No need to create an account page, it already exists.');
     }
     //CHECKOUT PAGE
     //CHECKOUT PAGE
     $checkoutPage = DataObject::get_one('CheckoutPage');
     if (!$checkoutPage) {
         $checkoutPage = new CheckoutPage();
         $checkoutPage->Content = '<p>This is the checkout page. You can edit all the messages in the Content Management System.</p>';
         $checkoutPage->Title = 'Checkout';
         $checkoutPage->TermsAndConditionsMessage = 'You must agree with the terms and conditions to proceed. ';
         $checkoutPage->MetaTitle = 'Checkout';
         $checkoutPage->MenuTitle = 'Checkout';
         $checkoutPage->URLSegment = 'checkout';
         $update[] = 'Checkout page \'Checkout\' created';
         $checkoutPage->ShowInMenus = 0;
         DB::alteration_message('new checkout page created.', 'created');
     } else {
         DB::alteration_message('No need to create an checkout page, it already exists.');
     }
     if ($checkoutPage) {
         if ($checkoutPage->TermsPageID == 0 && ($termsPage = DataObject::get_one('Page', "\"URLSegment\" = 'terms-and-conditions'"))) {
             $checkoutPage->TermsPageID = $termsPage->ID;
             DB::alteration_message('terms and conditions page linked.', "created");
         } else {
             DB::alteration_message('No terms and conditions page linked.');
         }
         $checkoutPage->writeToStage('Stage');
         $checkoutPage->publish('Stage', 'Live');
         DB::alteration_message('Checkout page saved');
         if (!DataObject::get_one('OrderConfirmationPage')) {
             $orderConfirmationPage = new OrderConfirmationPage();
             $orderConfirmationPage->ParentID = $checkoutPage->ID;
             $orderConfirmationPage->Title = 'Order confirmation';
             $orderConfirmationPage->MenuTitle = 'Order confirmation';
             $orderConfirmationPage->MetaTitle = 'Order confirmation';
             $orderConfirmationPage->Content = '<p>This is the order confirmation page. It is used to confirm orders after they have been placed in the checkout page.</p>';
             $orderConfirmationPage->URLSegment = 'order-confirmation';
             $orderConfirmationPage->ShowInMenus = 0;
             $orderConfirmationPage->writeToStage('Stage');
             $orderConfirmationPage->publish('Stage', 'Live');
             DB::alteration_message('Order Confirmation created', 'created');
         } else {
             DB::alteration_message('No need to create an Order Confirmation Page. It already exists.');
         }
     }
     $update = array();
     $ecommerceConfig = EcommerceDBConfig::current_ecommerce_db_config();
     if ($ecommerceConfig) {
         if (!$ecommerceConfig->ReceiptEmail) {
             $ecommerceConfig->ReceiptEmail = Email::getAdminEmail();
             if (!$ecommerceConfig->ReceiptEmail) {
                 user_error("you must set an AdminEmail (Email::setAdminEmail)", E_USER_NOTICE);
             }
             $update[] = "created default entry for ReceiptEmail";
         }
         if (!$ecommerceConfig->NumberOfProductsPerPage) {
             $ecommerceConfig->NumberOfProductsPerPage = 12;
             $update[] = "created default entry for NumberOfProductsPerPage";
         }
         if (count($update)) {
             $ecommerceConfig->write();
             DB::alteration_message($ecommerceConfig->ClassName . " created/updated: " . implode(" --- ", $update), 'created');
         }
     }
 }
 function addConfirmationPage_250()
 {
     $explanation = "\r\n\t\t\t<h1>250. Add Confirmation Page</h1>\r\n\t\t\t<p>Creates a checkout page and order confirmation page in case they do not exist.</p>\r\n\t\t";
     if ($this->retrieveInfoOnly) {
         return $explanation;
     } else {
         echo $explanation;
     }
     $checkoutPage = CheckoutPage::get()->First();
     if (!$checkoutPage) {
         $checkoutPage = new CheckoutPage();
         $this->DBAlterationMessageNow("Creating a CheckoutPage", "created");
     } else {
         $this->DBAlterationMessageNow("No need to create a CheckoutPage Page");
     }
     if ($checkoutPage) {
         $checkoutPage->HasCheckoutSteps = 1;
         $checkoutPage->writeToStage('Stage');
         $checkoutPage->publish('Stage', 'Live');
         $orderConfirmationPage = OrderConfirmationPage::get()->First();
         if ($orderConfirmationPage) {
             $this->DBAlterationMessageNow("No need to create an Order Confirmation Page");
         } else {
             $orderConfirmationPage = new OrderConfirmationPage();
             $orderConfirmationPage->ParentID = $checkoutPage->ID;
             $orderConfirmationPage->writeToStage('Stage');
             $orderConfirmationPage->publish('Stage', 'Live');
             $this->DBAlterationMessageNow("Creating an Order Confirmation Page", "created");
         }
     } else {
         $this->DBAlterationMessageNow("There is no CheckoutPage available", "deleted");
     }
     return 0;
 }
 /**
  *@return String (URLSegment)
  **/
 public function OrderConfirmationPageLink()
 {
     return OrderConfirmationPage::find_link();
 }
 function addConfirmationPage_250()
 {
     DB::alteration_message("\r\n\t\t\t<h1>250. Add Confirmation Page</h1>\r\n\t\t\t<p>Creates a checkout page and order confirmation page in case they do not exist.</p>\r\n\t\t");
     $checkoutPage = DataObject::get_one("CheckoutPage");
     if ($checkoutPage) {
         if (!DataObject::get_one("OrderConfirmationPage")) {
             $orderConfirmationPage = new OrderConfirmationPage();
             $orderConfirmationPage->ParentID = $checkoutPage->ID;
             $orderConfirmationPage->writeToStage('Stage');
             $orderConfirmationPage->publish('Stage', 'Live');
             DB::alteration_message("Creating an Order Confirmation Page", "created");
         } else {
             DB::alteration_message("No need to create an Order Confirmation Page");
         }
     } else {
         DB::alteration_message("There is no CheckoutPage available", "deleted");
     }
     return 0;
 }
 /**
  * returns a dataobject set of the steps.
  * Or just one step if that is more relevant.
  * @param Int $number - if set, it returns that one step.
  * @return Null | DataObject (CheckoutPage_Description) | ArrayList (CheckoutPage_Description)
  */
 function CheckoutSteps($number = 0)
 {
     $where = '';
     $dos = CheckoutPage_StepDescription::get()->Sort("ID", "ASC");
     if ($number) {
         $dos = $dos->Filter(array("ID" => $number));
     }
     if ($number) {
         if ($dos->count()) {
             return $dos->First();
         }
     }
     $returnData = new ArrayList(array());
     $completed = 1;
     $completedClass = "completed";
     foreach ($dos as $do) {
         if ($this->currentStep && $do->Code() == $this->currentStep) {
             $do->LinkingMode = "current";
             $completed = 0;
             $completedClass = "notCompleted";
         } else {
             if ($completed) {
                 $do->Link = $this->Link("checkoutstep") . "/" . $do->Code . "/";
             }
             $do->LinkingMode = "link {$completedClass}";
         }
         $do->Completed = $completed;
         $returnData->push($do);
     }
     if (EcommerceConfig::get("OrderConfirmationPage_Controller", "include_as_checkout_step")) {
         $orderConfirmationPage = OrderConfirmationPage::get()->First();
         if ($orderConfirmationPage) {
             $do = $orderConfirmationPage->CurrentCheckoutStep(false);
             if ($do) {
                 $returnData->push($do);
             }
         }
     }
     return $returnData;
 }
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     $checkoutPage = CheckoutPage::get()->first();
     if ($checkoutPage) {
         $orderConfirmationPage = OrderConfirmationPage::get()->first();
         if (!$orderConfirmationPage) {
             $orderConfirmationPage = OrderConfirmationPage::create();
             $orderConfirmationPage->Title = "Order Confirmation";
             $orderConfirmationPage->MenuTitle = "Order Confirmation";
             $orderConfirmationPage->URLSegment = "order-confirmation";
             $orderConfirmationPage->writeToStage("Stage");
             $orderConfirmationPage->publish("Stage", "Live");
         }
     }
 }