1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Checkout\Component; |
4
|
|
|
|
5
|
|
|
use SilverShop\Model\Order; |
6
|
|
|
use SilverShop\ShopUserInfo; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Forms\CompositeField; |
9
|
|
|
use SilverStripe\Forms\FieldList; |
10
|
|
|
use SilverStripe\ORM\DataObject; |
11
|
|
|
use SilverStripe\Security\Security; |
12
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
13
|
|
|
|
14
|
|
|
abstract class Address extends CheckoutComponent |
15
|
|
|
{ |
16
|
|
|
protected $formfielddescriptions = true; |
17
|
|
|
|
18
|
|
|
protected $addresstype; |
19
|
|
|
|
20
|
|
|
protected $addtoaddressbook = false; |
21
|
|
|
|
22
|
|
|
private static $composite_field_tag = 'div'; |
23
|
|
|
|
24
|
|
|
public function getFormFields(Order $order) |
25
|
|
|
{ |
26
|
|
|
$fields = $this->getAddress($order)->getFrontEndFields([ |
27
|
|
|
'addfielddescriptions' => $this->formfielddescriptions |
28
|
|
|
]); |
29
|
|
|
|
30
|
|
|
$label = _t( |
31
|
|
|
"SilverShop\Model\Address.{$this->addresstype}Address", |
32
|
|
|
"{$this->addresstype} Address" |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
return FieldList::create( |
36
|
|
|
CompositeField::create($fields) |
37
|
|
|
->addExtraClass($this->addresstype) |
38
|
|
|
->setLegend($label) |
39
|
|
|
->setTag(Config::inst()->get(self::class, 'composite_field_tag')) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getRequiredFields(Order $order) |
44
|
|
|
{ |
45
|
|
|
return $this->getAddress($order)->getRequiredFields(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function validateData(Order $order, array $data) |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getData(Order $order) |
53
|
|
|
{ |
54
|
|
|
$data = $this->getAddress($order)->toMap(); |
55
|
|
|
|
56
|
|
|
//merge data from multiple sources |
57
|
|
|
$data = array_merge( |
58
|
|
|
ShopUserInfo::singleton()->getLocation(), |
59
|
|
|
$data, |
60
|
|
|
[$this->addresstype . 'AddressID' => $order->{$this->addresstype . 'AddressID'}] |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
//merge in default address if an address isn't available |
64
|
|
|
$member = Security::getCurrentUser(); |
65
|
|
|
if (!$order->{$this->addresstype . 'AddressID'}) { |
66
|
|
|
$data = array_merge( |
67
|
|
|
ShopUserInfo::singleton()->getLocation(), |
68
|
|
|
$member ? $member->{'Default' . $this->addresstype . 'Address'}()->toMap() : [], |
|
|
|
|
69
|
|
|
[$this->addresstype . 'AddressID' => $order->{$this->addresstype . 'AddressID'}] |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
unset($data['ID']); |
74
|
|
|
unset($data['ClassName']); |
75
|
|
|
unset($data['RecordClassName']); |
76
|
|
|
|
77
|
|
|
//ensure country is restricted if there is only one allowed country |
78
|
|
|
if ($country = SiteConfig::current_site_config()->getSingleCountry()) { |
79
|
|
|
$data['Country'] = $country; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return $data; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create a new address if the existing address has changed, or is not yet |
87
|
|
|
* created. |
88
|
|
|
* |
89
|
|
|
* @param Order $order order to get addresses from |
90
|
|
|
* @param array $data data to set |
91
|
|
|
* @throws \SilverStripe\ORM\ValidationException |
92
|
|
|
*/ |
93
|
|
|
public function setData(Order $order, array $data) |
94
|
|
|
{ |
95
|
|
|
$address = $this->getAddress($order); |
96
|
|
|
//if the value matches the current address then unset |
97
|
|
|
//this is to fix issues with blank fields & the readonly Country field |
98
|
|
|
$addressFields = DataObject::getSchema()->databaseFields(\SilverShop\Model\Address::class); |
99
|
|
|
foreach ($data as $key => $value) { |
100
|
|
|
if (!isset($addressFields[$key]) || (!$value && !$address->{$key})) { |
101
|
|
|
unset($data[$key]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
$address->update($data); |
105
|
|
|
//if only one country is available, then set it |
106
|
|
|
if ($country = SiteConfig::current_site_config()->getSingleCountry()) { |
107
|
|
|
$address->Country = $country; |
108
|
|
|
} |
109
|
|
|
//write new address, or duplicate if changed |
110
|
|
|
if (!$address->isInDB()) { |
111
|
|
|
$address->write(); |
112
|
|
|
} elseif ($address->isChanged()) { |
113
|
|
|
$address = $address->duplicate(); |
114
|
|
|
} |
115
|
|
|
//set billing address, if not already set |
116
|
|
|
$order->{$this->addresstype . 'AddressID'} = $address->ID; |
117
|
|
|
if (!$order->BillingAddressID) { |
118
|
|
|
$order->BillingAddressID = $address->ID; |
119
|
|
|
} |
120
|
|
|
$order->write(); |
121
|
|
|
|
122
|
|
|
if ($this->addresstype === 'Shipping') { |
123
|
|
|
ShopUserInfo::singleton()->setAddress($address); |
124
|
|
|
|
125
|
|
|
$order->extend('onSetShippingAddress', $address); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
//associate member to address |
129
|
|
|
if ($member = Security::getCurrentUser()) { |
130
|
|
|
$default = $member->{'Default' . $this->addresstype . 'Address'}(); |
131
|
|
|
//set default address |
132
|
|
|
if (!$default->exists()) { |
133
|
|
|
$member->{'Default' . $this->addresstype . 'AddressID'} = $address->ID; |
134
|
|
|
$member->write(); |
135
|
|
|
} |
136
|
|
|
if ($this->addtoaddressbook) { |
137
|
|
|
$member->AddressBook()->add($address); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
//extension hooks |
141
|
|
|
$order->extend('onSet' . $this->addresstype . 'Address', $address); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Enable adding form field descriptions |
146
|
|
|
*/ |
147
|
|
|
public function setShowFormFieldDescriptions($show = true) |
148
|
|
|
{ |
149
|
|
|
$this->formfielddescriptions = $show; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Add new addresses to the address book. |
154
|
|
|
*/ |
155
|
|
|
public function setAddToAddressBook($add = true) |
156
|
|
|
{ |
157
|
|
|
$this->addtoaddressbook = $add; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param Order $order |
162
|
|
|
* @return \SilverShop\Model\Address |
163
|
|
|
*/ |
164
|
|
|
public function getAddress(Order $order) |
165
|
|
|
{ |
166
|
|
|
return $order->{$this->addresstype . 'Address'}(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|