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