1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NerdsAndCompany\Schematic\Commerce\Services; |
4
|
|
|
|
5
|
|
|
use Craft\Craft; |
6
|
|
|
use Craft\Commerce_StateModel; |
7
|
|
|
use NerdsAndCompany\Schematic\Services\Base; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Schematic Commerce States 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 States extends Base |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Export states. |
24
|
|
|
* |
25
|
|
|
* @param StateModel[] $states |
26
|
|
|
* |
27
|
|
|
* @return array |
28
|
|
|
*/ |
29
|
|
|
public function export(array $states = []) |
30
|
|
|
{ |
31
|
|
|
if (!count($states)) { |
32
|
|
|
$states = Craft::app()->commerce_states->getAllStates(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
Craft::log(Craft::t('Exporting Commerce States')); |
36
|
|
|
|
37
|
|
|
$stateDefinitions = []; |
38
|
|
|
|
39
|
|
|
foreach ($states as $state) { |
40
|
|
|
$stateDefinitions[$state->abbreviation] = $this->getStateDefinition($state); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $stateDefinitions; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get states definition. |
48
|
|
|
* |
49
|
|
|
* @param Commerce_StateModel $state |
50
|
|
|
* |
51
|
|
|
* @return array |
52
|
|
|
*/ |
53
|
|
|
private function getStateDefinition(Commerce_StateModel $state) |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
'name' => $state->name, |
57
|
|
|
'country' => $state->getCountry()->iso, |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Attempt to import states. |
63
|
|
|
* |
64
|
|
|
* @param array $stateDefinitions |
65
|
|
|
* @param bool $force If set to true states not included in the import will be deleted |
66
|
|
|
* |
67
|
|
|
* @return Result |
68
|
|
|
*/ |
69
|
|
|
public function import(array $stateDefinitions, $force = false) |
70
|
|
|
{ |
71
|
|
|
Craft::log(Craft::t('Importing Commerce States')); |
72
|
|
|
|
73
|
|
|
$states = array(); |
74
|
|
|
foreach (Craft::app()->commerce_states->getAllStates() as $state) { |
75
|
|
|
$states[$state->abbreviation] = $state; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
foreach ($stateDefinitions as $stateHandle => $stateDefinition) { |
79
|
|
|
$state = array_key_exists($stateHandle, $states) |
80
|
|
|
? $states[$stateHandle] |
81
|
|
|
: new Commerce_StateModel(); |
82
|
|
|
|
83
|
|
|
unset($states[$stateHandle]); |
84
|
|
|
|
85
|
|
|
$this->populateState($state, $stateDefinition, $stateHandle); |
86
|
|
|
|
87
|
|
|
if (!Craft::app()->commerce_states->saveState($state)) { // Save state via craft |
88
|
|
|
$this->addErrors($state->getAllErrors()); |
89
|
|
|
|
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ($force) { |
95
|
|
|
foreach ($states as $state) { |
96
|
|
|
Craft::app()->commerce_states->deleteStateById($state->id); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->getResultModel(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Populate state. |
105
|
|
|
* |
106
|
|
|
* @param Commerce_StateModel $state |
107
|
|
|
* @param array $stateDefinition |
108
|
|
|
* @param string $stateHandle |
109
|
|
|
*/ |
110
|
|
|
private function populateState(Commerce_StateModel $state, array $stateDefinition, $stateHandle) |
111
|
|
|
{ |
112
|
|
|
$country = Craft::app()->commerce_countries->getCountryByAttributes(array('iso' => $stateDefinition['country'])); |
113
|
|
|
|
114
|
|
|
$state->setAttributes([ |
115
|
|
|
'abbreviation' => $stateHandle, |
116
|
|
|
'name' => $stateDefinition['name'], |
117
|
|
|
'countryId' => $country ? $country->id : null, |
118
|
|
|
]); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|