1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package silvershop-shipping |
5
|
|
|
*/ |
6
|
|
|
class ShippingEstimateForm extends Form |
7
|
|
|
{ |
8
|
|
|
public function __construct($controller, $name = "ShippingEstimateForm") |
9
|
|
|
{ |
10
|
|
|
$address = new Address(); // get address to access it's getCountryField method |
11
|
|
|
$fields = new FieldList( |
12
|
|
|
$address->getCountryField(), |
13
|
|
|
TextField::create('State', _t('Address.db_State', 'State')), |
14
|
|
|
TextField::create('City', _t('Address.db_City', 'City')), |
15
|
|
|
TextField::create('PostalCode', _t('Address.db_PostalCode', 'Postal Code')) |
16
|
|
|
); |
17
|
|
|
$actions = new FieldList( |
18
|
|
|
FormAction::create( |
19
|
|
|
"submit", |
20
|
|
|
_t('ShippingEstimateForm.FormActionTitle', 'Estimate') |
21
|
|
|
) |
22
|
|
|
); |
23
|
|
|
$validator = new RequiredFields(array( |
24
|
|
|
'Country' |
25
|
|
|
)); |
26
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
27
|
|
|
$this->extend('updateForm'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function submit($data, $form) |
31
|
|
|
{ |
32
|
|
|
if ($country = SiteConfig::current_site_config()->getSingleCountry()) { // Add Country if missing due to ReadonlyField in form |
33
|
|
|
$data['Country'] = $country; |
34
|
|
|
} |
35
|
|
|
if ($order = ShoppingCart::singleton()->current()) { |
36
|
|
|
$estimator = new ShippingEstimator( |
37
|
|
|
$order, |
38
|
|
|
new Address(Convert::raw2sql($data)) |
|
|
|
|
39
|
|
|
); |
40
|
|
|
$estimates = $estimator->getEstimates(); |
41
|
|
|
if (!$estimates->exists()) { |
42
|
|
|
$form->sessionMessage( |
43
|
|
|
_t('ShippingEstimateForm.FormActionWarningMessage', 'No estimates could be found for that location.'), |
44
|
|
|
_t('ShippingEstimateForm.FormActionWarningCode', "warning") |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
Session::set("ShippingEstimates", $estimates); |
48
|
|
|
if (Director::is_ajax()) { |
49
|
|
|
//TODO: replace with an AJAXResponse class that can output to different formats |
50
|
|
|
return json_encode($estimates->toNestedArray()); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
$this->controller->redirectBack(); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.