1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\Commerce_CountryModel; |
7
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Commerce Countries Service. |
11
|
|
|
* |
12
|
|
|
* Sync Craft Setups. |
13
|
|
|
* |
14
|
|
|
* @author Nerds & Company |
15
|
|
|
* @copyright Copyright (c) 2015-2017, Nerds & Company |
16
|
|
|
* @license MIT |
17
|
|
|
* |
18
|
|
|
* @see http://www.nerds.company |
19
|
|
|
*/ |
20
|
|
|
class Countries extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Export countries. |
24
|
|
|
* |
25
|
|
|
* @param CountryModel[] $countries |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function export(array $countries = []) |
30
|
|
|
{ |
31
|
|
|
if (!count($countries)) { |
32
|
|
|
$countries = Craft::app()->commerce_countries->getAllCountries(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
Craft::log(Craft::t('Exporting Commerce Countries')); |
36
|
|
|
|
37
|
|
|
$countryDefinitions = []; |
38
|
|
|
|
39
|
|
|
foreach ($countries as $country) { |
40
|
|
|
$countryDefinitions[$country->iso] = $this->getCountryDefinition($country); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $countryDefinitions; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get countries definition. |
48
|
|
|
* |
49
|
|
|
* @param Commerce_CountryModel $country |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
private function getCountryDefinition(Commerce_CountryModel $country) |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'name' => $country->name, |
57
|
|
|
'stateRequired' => $country->stateRequired, |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Attempt to import countries. |
63
|
|
|
* |
64
|
|
|
* @param array $countryDefinitions |
65
|
|
|
* @param bool $force If set to true countries not included in the import will be deleted |
66
|
|
|
* |
67
|
|
|
* @return Result |
68
|
|
|
*/ |
69
|
|
|
public function import(array $countryDefinitions, $force = false) |
70
|
|
|
{ |
71
|
|
|
Craft::log(Craft::t('Importing Commerce Countries')); |
72
|
|
|
|
73
|
|
|
$countries = array(); |
74
|
|
|
foreach (Craft::app()->commerce_countries->getAllCountries() as $country) { |
75
|
|
|
$countries[$country->iso] = $country; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($countryDefinitions as $countryHandle => $countryDefinition) { |
79
|
|
|
$country = array_key_exists($countryHandle, $countries) |
80
|
|
|
? $countries[$countryHandle] |
81
|
|
|
: new Commerce_CountryModel(); |
82
|
|
|
|
83
|
|
|
unset($countries[$countryHandle]); |
84
|
|
|
|
85
|
|
|
$this->populateCountry($country, $countryDefinition, $countryHandle); |
86
|
|
|
|
87
|
|
|
if (!Craft::app()->commerce_countries->saveCountry($country)) { // Save country via craft |
88
|
|
|
$this->addErrors($country->getAllErrors()); |
89
|
|
|
|
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($force) { |
95
|
|
|
foreach ($countries as $country) { |
96
|
|
|
Craft::app()->commerce_countries->deleteCountryById($country->id); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->getResultModel(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Populate country. |
105
|
|
|
* |
106
|
|
|
* @param Commerce_CountryModel $country |
107
|
|
|
* @param array $countryDefinition |
108
|
|
|
* @param string $countryHandle |
109
|
|
|
*/ |
110
|
|
|
private function populateCountry(Commerce_CountryModel $country, array $countryDefinition, $countryHandle) |
111
|
|
|
{ |
112
|
|
|
$country->setAttributes([ |
113
|
|
|
'iso' => $countryHandle, |
114
|
|
|
'name' => $countryDefinition['name'], |
115
|
|
|
'stateRequired' => $countryDefinition['stateRequired'], |
116
|
|
|
]); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|