/**
  * Action to add a product to cart.
  * 
  * @param SS_HTTPRequest $request Request to check for product data
  * 
  * @return void
  *
  * @author Sebastian Diel <*****@*****.**>
  * @since 12.03.2013
  */
 public function addToCart(SS_HTTPRequest $request)
 {
     $isValidRequest = false;
     $backLink = null;
     $postVars = $request->postVars();
     $params = $request->allParams();
     $productID = $params['ID'];
     $quantity = $params['OtherID'];
     if (is_null($productID) || is_null($quantity)) {
         if (array_key_exists('productID', $postVars) && array_key_exists('productQuantity', $postVars)) {
             $isValidRequest = true;
             $productID = $postVars['productID'];
             $quantity = $postVars['productQuantity'];
         }
     } else {
         $isValidRequest = true;
     }
     if ($isValidRequest) {
         $postVars['productID'] = $productID;
         $postVars['productQuantity'] = $quantity;
         if ($quantity == 0) {
             SilvercartShoppingCart::removeProduct($postVars);
         } else {
             SilvercartShoppingCart::addProduct($postVars);
         }
         if (SilvercartConfig::getRedirectToCartAfterAddToCartAction()) {
             $backLink = SilvercartTools::PageByIdentifierCodeLink('SilvercartCartPage');
         }
     }
     $this->redirectBack($backLink, '#product' . $productID);
 }
 /**
  * Returns the tax rate for this fee.
  * 
  * @return int
  *
  * @author Sebastian Diel <*****@*****.**>
  * @since 16.11.2013
  */
 public function getTaxRate()
 {
     $taxRate = SilvercartShoppingCart::get_most_valuable_tax_rate();
     if ($taxRate === false) {
         $taxRate = $this->SilvercartTax()->getTaxRate();
     }
     return $taxRate;
 }
 /**
  * creates a form object with a free configurable markup
  *
  * @param ContentController $controller  the calling controller instance
  * @param array             $params      optional parameters
  * @param array             $preferences optional preferences
  * @param bool              $barebone    defines if a form should only be instanciated or be used too
  *
  * @return CustomHtmlForm
  *
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 13.01.2015
  */
 public function __construct($controller, $params = null, $preferences = null, $barebone = false)
 {
     $this->extend('onBeforeConstruct', $controller, $params, $preferences, $barebone);
     global $project;
     $this->barebone = $barebone;
     $this->controller = $controller;
     if (is_array($params)) {
         $this->customParameters = $params;
     }
     // Hook for setting preferences via a method call
     $this->preferences();
     if (is_array($preferences)) {
         foreach ($preferences as $title => $setting) {
             if (!empty($title)) {
                 $this->basePreferences[$title] = $setting;
             }
         }
     }
     $name = $this->getSubmitAction();
     if (!$barebone) {
         $this->getFormFields();
     }
     if ($this->securityTokenEnabled) {
         SecurityToken::enable();
     } else {
         SecurityToken::disable();
     }
     parent::__construct($this->getFormController($controller, $preferences), $name, new FieldList(), new FieldList());
     if (!$barebone) {
         $this->getFormFields();
         $this->fillInFieldValues();
     }
     // Hook for setting preferences via a method call; we need to do this
     // a second time so that the standard Silverstripe mechanism can take
     // influence, too (i.e. _config.php files, init methods, etc).
     $this->preferences();
     if (is_array($preferences)) {
         foreach ($preferences as $title => $setting) {
             if (!empty($title)) {
                 $this->basePreferences[$title] = $setting;
             }
         }
     }
     // Counter for the form class, init or increment
     if (!isset(self::$classInstanceCounter[$this->class])) {
         self::$classInstanceCounter[$this->class] = 0;
     }
     if (!$barebone) {
         self::$classInstanceCounter[$this->class]++;
     }
     // new assignment required, because the controller will be overwritten in the form class
     $this->controller = $controller;
     // create group structure
     if (isset($this->formFields)) {
         $this->fieldGroups['formFields'] = $this->getFormFields();
     } else {
         $this->fieldGroups['formFields'] = array();
     }
     $this->name = str_replace('/', '', $this->class . '_' . $name . '_' . self::$classInstanceCounter[$this->class]);
     $this->jsName = $this->name;
     $this->SSformFields = $this->getForm();
     $this->SSformFields['fields']->setForm($this);
     $this->SSformFields['actions']->setForm($this);
     parent::setFields($this->SSformFields['fields']);
     parent::setActions($this->SSformFields['actions']);
     // define form action
     $this->setFormAction($this->buildFormAction());
     $this->setHTMLID($this->getName());
     /*
      * load and init JS validators
      * form integration via FormAttributes()
      */
     if (!$barebone) {
         $javascriptSnippets = $this->getJavascriptValidatorInitialisation();
         if (!$this->getLoadShoppingCartModules()) {
             SilvercartShoppingCart::setLoadShoppingCartModules(false);
         }
         if ($this->getCreateShoppingCartForms() && class_exists('SilvercartShoppingCart')) {
             SilvercartShoppingCart::setCreateShoppingCartForms(false);
         }
         $this->controller->addJavascriptSnippet($javascriptSnippets['javascriptSnippets']);
         $this->controller->addJavascriptOnloadSnippet($javascriptSnippets['javascriptOnloadSnippets']);
         $this->controller->addJavascriptOnloadSnippet($this->getJavascriptFieldInitialisations());
     }
     // Register the default module directory from mysite/_config.php
     self::registerModule($project);
     $this->extend('onAfterConstruct', $controller, $params, $preferences, $barebone);
 }
 /**
  * Set wether the shopping cart forms should be drawn.
  *
  * @param boolean $doCreate set wether to create the forms or not
  *
  * @return void
  */
 public static function setCreateShoppingCartForms($doCreate)
 {
     self::$createForms = $doCreate;
 }
 /**
  * Returns allowed payment methods.
  * 
  * @param string                 $shippingCountry                  The SilvercartCountry to check the payment methods for.
  * @param SilvercartShoppingCart $shoppingCart                     The shopping cart object
  * @param Boolean                $forceAnonymousCustomerIfNotExist When true, an anonymous customer will be created when no customer exists
  * 
  * @return ArrayList
  * 
  * @author Sebastian Diel <*****@*****.**>,
  *         Sascha Koehler <*****@*****.**>
  * @since 15.11.2014
  */
 public static function getAllowedPaymentMethodsFor($shippingCountry, $shoppingCart, $forceAnonymousCustomerIfNotExist = false)
 {
     $allowedPaymentMethods = array();
     if (!$shippingCountry) {
         return $allowedPaymentMethods;
     }
     $paymentMethods = $shippingCountry->SilvercartPaymentMethods('isActive = 1');
     $member = SilvercartCustomer::currentUser();
     if (!$member && $forceAnonymousCustomerIfNotExist) {
         $member = new Member();
         $anonymousGroup = Group::get()->filter('Code', 'anonymous')->first();
         $memberGroups = new ArrayList();
         $memberGroups->push($anonymousGroup);
     } else {
         $memberGroups = $member->Groups();
     }
     $shippingMethodID = null;
     if (Controller::curr() instanceof SilvercartCheckoutStep_Controller) {
         $checkoutData = Controller::curr()->getCombinedStepData();
         if (array_key_exists('ShippingMethod', $checkoutData)) {
             $shippingMethodID = $checkoutData['ShippingMethod'];
         }
     }
     if ($paymentMethods) {
         foreach ($paymentMethods as $paymentMethod) {
             $assumePaymentMethod = true;
             $containedInGroup = false;
             $containedInUsers = false;
             $doAccessChecks = true;
             // ------------------------------------------------------------
             // Basic checks
             // ------------------------------------------------------------
             if ($paymentMethod->enableActivationByOrderRestrictions) {
                 $assumePaymentMethod = $paymentMethod->isActivationByOrderRestrictionsPossible($member);
                 $doAccessChecks = false;
             }
             $checkAmount = $shoppingCart->getAmountTotalWithoutFees()->getAmount();
             if (!$paymentMethod->isAvailableForAmount($checkAmount)) {
                 $assumePaymentMethod = false;
                 $doAccessChecks = false;
             }
             // ------------------------------------------------------------
             // Shipping method check
             // ------------------------------------------------------------
             if (!is_null($shippingMethodID) && $paymentMethod->SilvercartShippingMethods()->exists() && !$paymentMethod->SilvercartShippingMethods()->find('ID', $shippingMethodID)) {
                 $assumePaymentMethod = false;
                 $doAccessChecks = false;
             }
             // ------------------------------------------------------------
             // Access checks
             // ------------------------------------------------------------
             if ($doAccessChecks) {
                 // Check if access for groups or is set positively
                 if ($paymentMethod->ShowOnlyForGroups()->exists()) {
                     foreach ($paymentMethod->ShowOnlyForGroups() as $paymentGroup) {
                         if ($memberGroups->find('ID', $paymentGroup->ID)) {
                             $containedInGroup = true;
                             break;
                         }
                     }
                     if ($containedInGroup) {
                         $assumePaymentMethod = true;
                     } else {
                         $assumePaymentMethod = false;
                     }
                 }
                 // Check if access for users or is set positively
                 if ($paymentMethod->ShowOnlyForUsers()->exists()) {
                     if ($paymentMethod->ShowOnlyForUsers()->find('ID', $member->ID)) {
                         $containedInUsers = true;
                     }
                     if ($containedInUsers) {
                         $assumePaymentMethod = true;
                     } else {
                         if (!$containedInGroup) {
                             $assumePaymentMethod = false;
                         }
                     }
                 }
                 // Check if access for groups is set negatively
                 if ($paymentMethod->ShowNotForGroups()->exists()) {
                     foreach ($paymentMethod->ShowNotForGroups() as $paymentGroup) {
                         if ($memberGroups->find('ID', $paymentGroup->ID)) {
                             if (!$containedInUsers) {
                                 $assumePaymentMethod = false;
                             }
                         }
                     }
                 }
                 // Check if access for users is set negatively
                 if ($paymentMethod->ShowNotForUsers()->exists()) {
                     if ($paymentMethod->ShowNotForUsers()->find('ID', $member->ID)) {
                         if (!$containedInUsers) {
                             $assumePaymentMethod = false;
                         }
                     }
                 }
             }
             if ($assumePaymentMethod) {
                 $allowedPaymentMethods[] = $paymentMethod;
             }
         }
     }
     $allowedPaymentMethods = new ArrayList($allowedPaymentMethods);
     return $allowedPaymentMethods;
 }
 /**
  * Attributes a shopping cart to the Member if none is attributed yet.
  *
  * @return void
  *
  * @author Sascha Koehler <*****@*****.**>
  * @since 10.10.2011
  */
 public function onAfterWrite()
 {
     parent::onAfterWrite();
     if ($this->owner->SilvercartShoppingCartID === null) {
         $cart = new SilvercartShoppingCart();
         $cart->write();
         $this->owner->SilvercartShoppingCartID = $cart->ID;
         $this->owner->write();
     }
     // check whether to add a member to an administrative group
     if (SilvercartCustomer::currentUser() && SilvercartCustomer::currentUser()->inGroup('administrators') && array_key_exists('Groups', $_POST)) {
         $groups = explode(',', $_POST['Groups']);
         if (count($groups) > 0) {
             foreach ($groups as $group) {
                 if (!$this->owner->Groups()->find('ID', $group)) {
                     $groupToAdd = DataObject::get_by_id('Group', $group);
                     if ($groupToAdd) {
                         $groupToAdd->Members()->add($this->owner);
                     }
                 }
             }
             if ($this->owner->Groups()->count() > count($groups)) {
                 foreach ($this->owner->Groups() as $group) {
                     if (!in_array($group->ID, $groups)) {
                         $group->Members()->remove($this->owner);
                     }
                 }
             }
         }
     }
 }