1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverShop\Extension; |
4
|
|
|
|
5
|
|
|
use SilverStripe\AssetAdmin\Forms\UploadField; |
6
|
|
|
use SilverStripe\Assets\Image; |
7
|
|
|
use SilverStripe\CMS\Model\SiteTree; |
8
|
|
|
use SilverStripe\Core\Config\Configurable; |
9
|
|
|
use SilverStripe\Forms\CheckboxSetField; |
10
|
|
|
use SilverStripe\Forms\FieldList; |
11
|
|
|
use SilverStripe\Forms\Tab; |
12
|
|
|
use SilverStripe\Forms\TabSet; |
13
|
|
|
use SilverStripe\Forms\TreeDropdownField; |
14
|
|
|
use SilverStripe\ORM\DataExtension; |
15
|
|
|
use SilverStripe\Security\Group; |
16
|
|
|
use SilverStripe\SiteConfig\SiteConfig; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @property string $AllowedCountries |
20
|
|
|
* @property int $TermsPageID |
21
|
|
|
* @property int $CustomerGroupID |
22
|
|
|
* @property int $DefaultProductImageID |
23
|
|
|
* |
24
|
|
|
* @method SiteTree TermsPage() |
25
|
|
|
* @method Group CustomerGroup() |
26
|
|
|
* @method Image DefaultProductImage() |
27
|
|
|
*/ |
28
|
|
|
class ShopConfigExtension extends DataExtension |
29
|
|
|
{ |
30
|
|
|
use Configurable; |
31
|
|
|
|
32
|
|
|
private static $db = [ |
|
|
|
|
33
|
|
|
'AllowedCountries' => 'Text', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
private static $has_one = [ |
|
|
|
|
37
|
|
|
'TermsPage' => SiteTree::class, |
38
|
|
|
'CustomerGroup' => Group::class, |
39
|
|
|
'DefaultProductImage' => Image::class, |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Email address where shop emails should be sent from |
44
|
|
|
* |
45
|
|
|
* @config |
46
|
|
|
* @var |
47
|
|
|
*/ |
48
|
|
|
private static $email_from; |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The shop base currency |
52
|
|
|
* |
53
|
|
|
* @config |
54
|
|
|
* @var |
55
|
|
|
*/ |
56
|
|
|
private static $base_currency = 'NZD'; |
57
|
|
|
|
58
|
|
|
public static function current() |
59
|
|
|
{ |
60
|
|
|
return SiteConfig::current_site_config(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public static function get_site_currency() |
64
|
|
|
{ |
65
|
|
|
return self::config()->base_currency; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function updateCMSFields(FieldList $fields) |
69
|
|
|
{ |
70
|
|
|
$fields->insertBefore('Access', $shoptab = Tab::create('Shop', 'Shop')); |
71
|
|
|
$fields->addFieldToTab( |
72
|
|
|
'Root.Shop', |
73
|
|
|
TabSet::create( |
74
|
|
|
'ShopTabs', |
75
|
|
|
$maintab = Tab::create( |
76
|
|
|
'Main', |
77
|
|
|
TreeDropdownField::create( |
78
|
|
|
'TermsPageID', |
79
|
|
|
_t(__CLASS__ . '.TermsPage', 'Terms and Conditions Page'), |
80
|
|
|
SiteTree::class |
81
|
|
|
), |
82
|
|
|
TreeDropdownField::create( |
83
|
|
|
'CustomerGroupID', |
84
|
|
|
_t(__CLASS__ . '.CustomerGroup', 'Group to add new customers to'), |
85
|
|
|
Group::class |
86
|
|
|
), |
87
|
|
|
UploadField::create('DefaultProductImage', _t(__CLASS__ . '.DefaultImage', 'Default Product Image')) |
88
|
|
|
), |
89
|
|
|
$countriesTab = Tab::create( |
90
|
|
|
'Countries', |
91
|
|
|
CheckboxSetField::create( |
92
|
|
|
'AllowedCountries', |
93
|
|
|
_t(__CLASS__ . '.AllowedCountries', 'Allowed Ordering and Shipping Countries'), |
94
|
|
|
self::config()->iso_3166_country_codes |
95
|
|
|
) |
96
|
|
|
) |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
$fields->removeByName('CreateTopLevelGroups'); |
100
|
|
|
$countriesTab->setTitle(_t(__CLASS__ . '.AllowedCountriesTabTitle', 'Allowed Countries')); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get list of allowed countries |
105
|
|
|
* |
106
|
|
|
* @param boolean $prefixisocode - prefix the country code |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function getCountriesList($prefixisocode = false) |
111
|
|
|
{ |
112
|
|
|
$countries = self::config()->iso_3166_country_codes; |
113
|
|
|
asort($countries); |
114
|
|
|
if ($allowed = $this->owner->AllowedCountries) { |
115
|
|
|
$allowed = json_decode($allowed); |
116
|
|
|
if (!empty($allowed)) { |
117
|
|
|
$countries = array_intersect_key($countries, array_flip($allowed)); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
if ($prefixisocode) { |
121
|
|
|
foreach ($countries as $key => $value) { |
122
|
|
|
$countries[$key] = "$key - $value"; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
return $countries; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* For shops that only sell to a single country, |
130
|
|
|
* this will return the country code, otherwise null. |
131
|
|
|
* |
132
|
|
|
* @param string fullname get long form name of country |
|
|
|
|
133
|
|
|
* |
134
|
|
|
* @return string country code |
135
|
|
|
*/ |
136
|
|
|
public function getSingleCountry($fullname = false) |
137
|
|
|
{ |
138
|
|
|
$countries = $this->getCountriesList(); |
139
|
|
|
if (count($countries) == 1) { |
140
|
|
|
if ($fullname) { |
141
|
|
|
return array_pop($countries); |
142
|
|
|
} else { |
143
|
|
|
reset($countries); |
144
|
|
|
return key($countries); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return null; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Convert iso country code to English country name |
152
|
|
|
* |
153
|
|
|
* @return string - name of country |
154
|
|
|
*/ |
155
|
|
|
public static function countryCode2name($code) |
156
|
|
|
{ |
157
|
|
|
$codes = self::config()->iso_3166_country_codes; |
158
|
|
|
if (isset($codes[$code])) { |
159
|
|
|
return $codes[$code]; |
160
|
|
|
} |
161
|
|
|
return $code; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|