Permalink
Browse files

Merge branch 'master' of github.com:frankmullenger/silverstripe-swipe…

…stripe
  • Loading branch information...
2 parents f257071 + 58e6595 commit 4d018091be299d97819725968a70f166fa037b9f @frankmullenger committed Apr 20, 2012
View
@@ -40,3 +40,13 @@ Issues
------
[Please log bugs here](https://github.com/frankmullenger/silverstripe-swipestripe/issues).
+Attribution
+-----------
+Big thanks to:
+
+* [Emogrifier](http://www.pelagodesign.com/sidecar/emogrifier/)
+* [Twitter Bootstrap](http://twitter.github.com/bootstrap/) [Licence](http://www.apache.org/licenses/LICENSE-2.0)
+* [jQuery](http://jquery.com)
+
+
+
@@ -244,7 +244,14 @@ private function addShippingAddressFields(&$fields, &$validator) {
$countryField = new DropdownField('Shipping[Country]', 'Country', Shipping::supported_countries());
$countryField->setCustomValidationMessage('Please enter a country.');
if (!Member::currentUserID() && Geoip::$default_country_code) $countryField->setValue(Geoip::$default_country_code); //Should probably do a default country in Shipping
-
+
+ $regions = Shipping::supported_regions();
+ $regionField = null;
+ if (!empty($regions)) {
+ $regionField = new RegionField('Shipping[Region]', 'Region');
+ $regionField->setCustomValidationMessage('Please enter a region.');
+ }
+
$sameAddressField = new CheckboxField('ShipToBillingAddress', 'to same address?');
$sameAddressField->addExtraClass('shipping-same-address');
@@ -259,7 +266,8 @@ private function addShippingAddressFields(&$fields, &$validator) {
$cityField,
new TextField('Shipping[PostalCode]', 'Postal Code'),
new TextField('Shipping[State]', 'State'),
- $countryField
+ $countryField,
+ $regionField
);
$shippingAddressFields->setID('ShippingAddress');
@@ -346,7 +354,14 @@ private function addItemFields(&$fields, &$validator, $order) {
private function addModifierFields(&$fields, &$validator, $order) {
foreach (Modifier::combined_form_fields($order) as $field) {
- $fields['Modifiers'][] = $field;
+
+ if ($field->modifiesSubTotal()) {
+ $fields['SubTotalModifiers'][] = $field;
+ }
+ else {
+ $fields['Modifiers'][] = $field;
+ }
+
}
}
@@ -536,7 +551,7 @@ function ProcessOrder($data, $form) {
function updateOrderFormCart(SS_HTTPRequest $data) {
if ($data->isPOST()) {
-
+
$fields = array();
$validator = new OrderFormValidator();
$member = Customer::currentUser() ? Customer::currentUser() : singleton('Customer');
@@ -554,7 +569,11 @@ function updateOrderFormCart(SS_HTTPRequest $data) {
//TODO This should be constructed for non-dropdown fields as well
//Update modifier form fields so that the dropdown values are correct
$newModifierData = array();
- foreach ($fields['Modifiers'] as $field) {
+ $subTotalModifiers = (isset($fields['SubTotalModifiers'])) ? $fields['SubTotalModifiers'] : array();
+ $totalModifiers = (isset($fields['Modifiers'])) ? $fields['Modifiers'] : array();
+ $modifierFields = array_merge($subTotalModifiers, $totalModifiers);
+
+ foreach ($modifierFields as $field) {
if (method_exists($field, 'updateValue')) {
$field->updateValue($order);
@@ -20,13 +20,15 @@ class AttributeValidator extends RequiredFields {
function php($data) {
$valid = parent::php($data);
-
+
$newTitle = (isset($data['Title'])) ? $data['Title'] : null;
if ($newTitle) {
$existingTitles = DataObject::get('Attribute');
$existingTitles = $existingTitles->map('ID', 'Title');
+ if (isset($data['ID'])) unset($existingTitles[$data['ID']]);
+
if (in_array($newTitle, $existingTitles)) {
$valid = false;
$this->validationError("Title", "Title already exists, please choose a different one.", 'bad');
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Field for picking categories for a {@link Product}.
+ *
+ * @author Frank Mullenger <frankmullenger@gmail.com>
+ * @copyright Copyright (c) 2012, Frank Mullenger
+ * @package swipestripe
+ * @subpackage form
+ * @version 1.0
+ */
+class CategoriesField extends TreeMultiselectField {
+
+ /**
+ * Set dummy callback so that filterMarking() is triggered.
+ *
+ * @param String $name
+ * @param String $title
+ * @param String $sourceObject
+ * @param String $keyField
+ * @param String $labelField
+ */
+ function __construct($name, $title, $sourceObject = "Group", $keyField = "ID", $labelField = "Title") {
+ parent::__construct($name, $title, $sourceObject, $keyField, $labelField);
+ $this->value = 'unchanged';
+ $this->filterCallback = 'dummyTrigger';
+ }
+
+ /**
+ * Overriding to display only {@link ProductCategory}s in the dropdown.
+ *
+ * @see TreeDropdownField::filterMarking()
+ */
+ function filterMarking($node) {
+
+ if ($node->ClassName === 'ProductCategory') return true;
+
+ //If node has children that are categories then include it in the tree
+ if ($node->ClassName != 'ProductCategory') {
+
+ $children = $node->AllChildren();
+ if ($children && $children->exists()) {
+ foreach ($children as $child) {
+ if ($child->ClassName === 'ProductCategory') {
+ return true;
+ }
+ else {
+ return false;
+ }
+ }
+ }
+ else {
+ return false;
+ }
+ }
+
+ //Ignore the callback
+ //if ($this->filterCallback && !call_user_func($this->filterCallback, $node)) return false;
+
+ if ($this->sourceObject == "Folder" && $node->ClassName != 'Folder') return false;
+ if ($this->search != "") {
+ return isset($this->searchIds[$node->ID]) && $this->searchIds[$node->ID] ? true : false;
+ }
+
+ return true;
+ }
+}
@@ -38,7 +38,6 @@ class ModifierHiddenField extends HiddenField {
* @param value The current value
* @param form The parent form
*/
-
function __construct($modifier, $title = null, $value = "", $maxLength = null, $form = null) {
$name = "Modifiers[$modifier->ClassName]";
@@ -85,4 +84,13 @@ function Description() {
return;
}
+ /**
+ * Does not modify {@link Order} sub total by default.
+ *
+ * @return Boolean False
+ */
+ function modifiesSubTotal() {
+ return false;
+ }
+
}
@@ -72,4 +72,13 @@ function getModifier() {
return $this->modifier;
}
+ /**
+ * Does not modify {@link Order} sub total by default.
+ *
+ * @return Boolean False
+ */
+ function modifiesSubTotal() {
+ return false;
+ }
+
}
View
@@ -0,0 +1,43 @@
+<?php
+/**
+ * For {@link Product} {@link Option} fields to be displayed on the {@link Product} page.
+ *
+ * @author Frank Mullenger <frankmullenger@gmail.com>
+ * @copyright Copyright (c) 2011, Frank Mullenger
+ * @package swipestripe
+ * @subpackage form
+ * @version 1.0
+ */
+class RegionField extends DropdownField {
+
+ /**
+ * Create drop down field for a product option, just ensures name of field
+ * is in the format Options[OptionClassName].
+ *
+ * @param String $optionClass Class name of the product option
+ * @param String $title
+ * @param Array $source
+ * @param String $value
+ * @param Form $form
+ * @param String $emptyString
+ */
+ function __construct($name, $title = null, $source = array(), $value = "", $form = null, $emptyString = null) {
+
+ $this->addExtraClass('dropdown');
+ parent::__construct($name, $title, $source, $value, $form, $emptyString);
+ }
+
+ function FieldHolder() {
+
+ $regions = Shipping::supported_regions();
+ $jsonRegions = json_encode($regions);
+
+ Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
+ Requirements::javascriptTemplate('swipestripe/javascript/RegionField.js', array(
+ "regions" => $jsonRegions,
+ 'defaultValue' => $this->Value()
+ ));
+ return parent::FieldHolder();
+ }
+
+}
@@ -144,6 +144,7 @@ function addToOrder($order, $value) {
$modification = new Modification();
$modification->ModifierClass = get_class($this);
$modification->ModifierOptionID = $value;
+ $modification->SubTotalModifier = true;
$modification->Amount = $this->Amount($order, $value);
$modification->Description = $this->Description($order, $value);
@@ -30,10 +30,7 @@ function extraStatics() {
*/
function updateCMSFields(FieldSet &$fields) {
- //$fields->addFieldToTab("Root", new TabSet('Shop'));
- $fields->addFieldToTab("Root.Shop",
- new TabSet('Shipping')
- );
+ $fields->findOrMakeTabSet('Root.Shop.Shipping');
$fields->addFieldToTab("Root.Shop.Shipping",
new Tab('FlatFeeShipping')
);
@@ -139,4 +139,14 @@ function validate($validator){
return $valid;
}
+
+ /**
+ * Shipping field modifies {@link Order} sub total by default.
+ *
+ * @see ModifierSetField::modifiesSubTotal()
+ * @return Boolean True
+ */
+ function modifiesSubTotal() {
+ return true;
+ }
}
@@ -20,6 +20,9 @@ class Shipping extends DataObject {
protected static $supported_countries = array(
);
+ protected static $supported_regions = array(
+ );
+
/**
* Set countries that are supported for shipping to.
*
@@ -51,5 +54,13 @@ public static function set_supported_countries(Array $countries) {
public static function supported_countries() {
return self::$supported_countries;
}
+
+ public static function set_supported_regions(Array $regions) {
+ self::$supported_regions = $regions;
+ }
+
+ public static function supported_regions() {
+ return self::$supported_regions;
+ }
}
View
@@ -26,6 +26,9 @@ class Address extends DataObject {
'PostalCode' => 'Varchar(30)',
'State' => 'Varchar(100)',
'Country' => 'Varchar',
+ 'CountryName' => 'Varchar',
+ 'Region' => 'Varchar',
+ 'RegionName' => 'Varchar'
);
/**
@@ -25,7 +25,8 @@ class Modification extends DataObject {
'ModifierClass' => 'Varchar',
'ModifierOptionID' => 'Int',
'Amount' => 'Money',
- 'Description' => 'Text'
+ 'Description' => 'Text',
+ 'SubTotalModifier' => 'Boolean'
);
/**
@@ -80,5 +81,5 @@ public static function currency() {
function validate() {
return parent::validate();
}
-
+
}
Oops, something went wrong.

0 comments on commit 4d01809

Please sign in to comment.