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 | private static $owns = [ |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
43 | 'DefaultProductImage' |
||
44 | ]; |
||
45 | |||
46 | /** |
||
47 | * Email address where shop emails should be sent from |
||
48 | * |
||
49 | * @config |
||
50 | * @var |
||
51 | */ |
||
52 | private static $email_from; |
||
53 | |||
54 | /** |
||
55 | * The shop base currency |
||
56 | * |
||
57 | * @config |
||
58 | * @var |
||
59 | */ |
||
60 | private static $base_currency = 'NZD'; |
||
61 | |||
62 | private static $forms_use_button_tag = false; |
||
63 | |||
64 | public static function current() |
||
65 | { |
||
66 | return SiteConfig::current_site_config(); |
||
67 | } |
||
68 | |||
69 | public static function get_site_currency() |
||
70 | { |
||
71 | return self::config()->base_currency; |
||
72 | } |
||
73 | |||
74 | public function updateCMSFields(FieldList $fields) |
||
75 | { |
||
76 | $fields->insertBefore('Access', $shoptab = Tab::create('Shop', 'Shop')); |
||
77 | $fields->addFieldToTab( |
||
78 | 'Root.Shop', |
||
79 | TabSet::create( |
||
80 | 'ShopTabs', |
||
81 | $maintab = Tab::create( |
||
82 | 'Main', |
||
83 | TreeDropdownField::create( |
||
84 | 'TermsPageID', |
||
85 | _t(__CLASS__ . '.TermsPage', 'Terms and Conditions Page'), |
||
86 | SiteTree::class |
||
87 | ), |
||
88 | TreeDropdownField::create( |
||
89 | 'CustomerGroupID', |
||
90 | _t(__CLASS__ . '.CustomerGroup', 'Group to add new customers to'), |
||
91 | Group::class |
||
92 | ), |
||
93 | UploadField::create('DefaultProductImage', _t(__CLASS__ . '.DefaultImage', 'Default Product Image')) |
||
94 | ), |
||
95 | $countriesTab = Tab::create( |
||
96 | 'Countries', |
||
97 | CheckboxSetField::create( |
||
98 | 'AllowedCountries', |
||
99 | _t(__CLASS__ . '.AllowedCountries', 'Allowed Ordering and Shipping Countries'), |
||
100 | self::config()->iso_3166_country_codes |
||
101 | ) |
||
102 | ) |
||
103 | ) |
||
104 | ); |
||
105 | $fields->removeByName('CreateTopLevelGroups'); |
||
106 | $countriesTab->setTitle(_t(__CLASS__ . '.AllowedCountriesTabTitle', 'Allowed Countries')); |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Get list of allowed countries |
||
111 | * |
||
112 | * @param boolean $prefixisocode - prefix the country code |
||
113 | * |
||
114 | * @return array |
||
115 | */ |
||
116 | public function getCountriesList($prefixisocode = false) |
||
117 | { |
||
118 | $countries = self::config()->iso_3166_country_codes; |
||
119 | asort($countries); |
||
120 | if ($allowed = $this->owner->AllowedCountries) { |
||
121 | $allowed = json_decode($allowed); |
||
122 | if (!empty($allowed)) { |
||
123 | $countries = array_intersect_key($countries, array_flip($allowed)); |
||
124 | } |
||
125 | } |
||
126 | if ($prefixisocode) { |
||
127 | foreach ($countries as $key => $value) { |
||
128 | $countries[$key] = "$key - $value"; |
||
129 | } |
||
130 | } |
||
131 | return $countries; |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * For shops that only sell to a single country, |
||
136 | * this will return the country code, otherwise null. |
||
137 | * |
||
138 | * @param string fullname get long form name of country |
||
139 | * |
||
140 | * @return string country code |
||
141 | */ |
||
142 | public function getSingleCountry($fullname = false) |
||
143 | { |
||
144 | $countries = $this->getCountriesList(); |
||
145 | if (count($countries) == 1) { |
||
146 | if ($fullname) { |
||
147 | return array_pop($countries); |
||
148 | } else { |
||
149 | reset($countries); |
||
150 | return key($countries); |
||
151 | } |
||
152 | } |
||
153 | return null; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Convert iso country code to English country name |
||
158 | * |
||
159 | * @return string - name of country |
||
160 | */ |
||
161 | public static function countryCode2name($code) |
||
162 | { |
||
163 | $codes = self::config()->iso_3166_country_codes; |
||
164 | if (isset($codes[$code])) { |
||
165 | return $codes[$code]; |
||
166 | } |
||
167 | return $code; |
||
168 | } |
||
169 | } |
||
170 |