/**
  * Automatically create an AccountPage if one is not found
  * on the site at the time the database is built (dev/build).
  */
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     if (!DataObject::get_one('RepeatOrdersPage')) {
         $page = new RepeatOrdersPage();
         $page->Title = 'Repeat Orders';
         $page->Content = '<p>This is the Repeat orders account page. It is used for shop users to login and create or change their Repeat orders.</p>';
         $page->URLSegment = 'repeat-orders';
         $page->WhatAreRepeatOrders = '<p>Repeat Orders allow you to regularly repeat an order.</p>';
         $page->OnceLoggedInYouCanCreateRepeatOrder = '<p>Once logged in you can setup a repeating order.</p>';
         $page->ShowInMenus = 0;
         $page->ShowInSearch = 0;
         $page->writeToStage('Stage');
         $page->publish('Stage', 'Live');
         if (method_exists('DB', 'alteration_message')) {
             DB::alteration_message('Repeat Order page \'Repeat Orders\' created', 'created');
         }
     }
 }
 /**
  * retursn and array like this: array(Title => "bla", Link => "/doit/now/");
  * @return Array
  */
 public function PostSubmitAction()
 {
     if (Member::currentUser()) {
         return array("Title" => _t("RepeatOrder.CREATEREPEATEORDER", "Turn this Order into a Repeat Order"), "Link" => RepeatOrdersPage::get_repeat_order_link("createorder", $this->Order()->ID));
     }
 }
 /**
  * Save the changes
  */
 public function doSave($data, $form, $request)
 {
     $data = Convert::raw2sql($data);
     $member = Member::currentUser();
     if (!$member) {
         $form->sessionMessage('Could not find customer details.', 'bad');
         Director::redirectBack();
         return false;
     }
     if ($member->IsShopAdmin()) {
         $form->sessionMessage('Repeat orders can not be created by Shop Administrators.  Only customers can create repeat orders.', 'bad');
         Director::redirectBack();
         return false;
     }
     if (isset($data['OrderID'])) {
         $order = DataObject::get_one('Order', 'Order.ID = \'' . $data['OrderID'] . '\' AND MemberID = \'' . $member->ID . '\'');
         if ($order) {
             $repeatOrder = RepeatOrder::create_repeat_order_from_order($order);
         } else {
             $form->sessionMessage('Could not find originating order.', 'bad');
             Director::redirectBack();
             return false;
         }
     } else {
         $repeatOrderID = intval($data['RepeatOrderID']);
         $repeatOrder = DataObject::get_one('RepeatOrder', 'RepeatOrder.ID = \'' . $repeatOrderID . '\' AND MemberID = \'' . $member->ID . '\'');
     }
     if ($repeatOrder) {
         if ($repeatOrderItems = $repeatOrder->OrderItems()) {
             foreach ($repeatOrderItems as $repeatOrderItem) {
                 $repeatOrderItem->ProductID = $data["Product"]["ID"][$repeatOrderItem->ProductID];
                 $repeatOrderItem->Quantity = $data["Product"]["Quantity"][$repeatOrderItem->ProductID];
                 for ($i = 1; $i < 6; $i++) {
                     $alternativeField = "Alternative" . $i . "ID";
                     $repeatOrderItem->{$alternativeField} = $data["Product"][$alternativeField][$repeatOrderItem->ProductID];
                 }
                 $repeatOrderItem->write();
             }
         }
         $params = array();
         if (isset($data['Start']) && strtotime($data['Start']) > strtotime(Date("Y-m-d"))) {
             $params['Start'] = $data['Start'];
         } else {
             $params["Start"] = Date("Y-m-d");
         }
         if (isset($data['End']) && strtotime($data['End']) > strtotime($params["Start"])) {
             $params['End'] = $data['End'];
         } else {
             $params["End"] = Date("Y-m-d", strtotime("+1 year"));
         }
         if (isset($data['Period'])) {
             $params['Period'] = $data['Period'];
         } else {
             $data['Period'] = RepeatOrder::default_period_key();
         }
         if (isset($data['DeliveryDay'])) {
             $params['DeliveryDay'] = $data['DeliveryDay'];
         } else {
             $data['DeliveryDay'] = RepeatOrder::default_delivery_day_key();
         }
         if (isset($data['PaymentMethod'])) {
             $params['PaymentMethod'] = $data['PaymentMethod'];
         } else {
             $data['PaymentMethod'] = RepeatOrder::default_payment_method_key();
         }
         if (isset($data['Notes'])) {
             $params['Notes'] = $data['Notes'];
         }
         $repeatOrder->update($params);
         $repeatOrder->Status = 'Pending';
         $repeatOrder->write();
     } else {
         $form->sessionMessage('Could not find repeat order.', 'bad');
         Director::redirectBack();
         return false;
     }
     Director::redirect(RepeatOrdersPage::get_repeat_order_link('view', $repeatOrder->ID));
     return true;
 }
 /**
  * Link for cancelling
  * @return String
  */
 public function CancelLink()
 {
     return RepeatOrdersPage::get_repeat_order_link('cancel', $this->ID);
 }