1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace tonisormisson\addressform; |
4
|
|
|
|
5
|
|
|
use tonisormisson\addressform\models\Address; |
6
|
|
|
use tonisormisson\addressform\traits\TranslationsTrait; |
7
|
|
|
use yii\base\ErrorException; |
8
|
|
|
use yii\base\Widget; |
9
|
|
|
use Yii; |
10
|
|
|
use Rinvex\Country\Country; |
11
|
|
|
use yii\widgets\ActiveForm; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class AddressForm |
16
|
|
|
* @property array $countryList |
17
|
|
|
* @property Country[] $countries |
18
|
|
|
* @property boolean $isFormInjected |
19
|
|
|
* |
20
|
|
|
* |
21
|
|
|
* @package tonisormisson\addressform |
22
|
|
|
* @author Tõnis Ormisson <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class AddressForm extends Widget |
25
|
|
|
{ |
26
|
|
|
use TranslationsTrait; |
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array $allowedCountries list of allowed countries. If list is empty, all countries are used. |
30
|
|
|
*/ |
31
|
|
|
public $allowedCountries = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Country |
35
|
|
|
*/ |
36
|
|
|
private $country; |
37
|
|
|
|
38
|
|
|
public $id = "address-form"; |
39
|
|
|
|
40
|
|
|
/** @var array $placeHolders Form placeholders */ |
41
|
|
|
public $placeHolders; |
42
|
|
|
|
43
|
|
|
/** @var Module */ |
44
|
|
|
public $module; |
45
|
|
|
|
46
|
|
|
/** @var ActiveForm */ |
47
|
|
|
public $form; |
48
|
|
|
|
49
|
|
|
/** @var array Fields to disable */ |
50
|
|
|
public $disabledFields = []; |
51
|
|
|
|
52
|
|
|
/** @var Address */ |
53
|
|
|
public $address; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @var array config yii\bootstrap\ActiveForm |
57
|
|
|
*/ |
58
|
|
|
public $formOptions = []; |
59
|
|
|
|
60
|
|
|
/** @var string */ |
61
|
|
|
public $submitText; |
62
|
|
|
|
63
|
|
|
|
64
|
|
|
/** @var array $submitOptions the HTML options for submit button */ |
65
|
|
|
public $submitOptions = [ |
66
|
|
|
'class' => 'btn btn-primary', |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
public $htmlOptions = []; |
70
|
|
|
|
71
|
|
|
public $fieldIdPrefix = 'address-'; |
72
|
|
|
|
73
|
|
|
/** @var string $defaultCountry */ |
74
|
|
|
public $defaultCountry = ""; |
75
|
|
|
|
76
|
|
|
public function init() |
77
|
|
|
{ |
78
|
|
|
parent::init(); |
79
|
|
|
$this->registerTranslations(); |
80
|
|
|
$this->module = Yii::$app->getModule('addressform'); |
81
|
|
|
|
82
|
|
|
$this->setDefaults(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
|
87
|
|
|
|
88
|
|
|
public function run() |
89
|
|
|
{ |
90
|
|
|
return $this->render('form', [ |
91
|
|
|
'widget' => $this |
92
|
|
|
]); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string[] |
97
|
|
|
*/ |
98
|
|
|
public function getCountryList() |
99
|
|
|
{ |
100
|
|
|
|
101
|
|
|
$countryList = []; |
102
|
|
|
foreach ($this->countries as $country) { |
103
|
|
|
$countryList[$country->getIsoAlpha2()] = $country->getEmoji(). " " . $country->getName(); |
104
|
|
|
} |
105
|
|
|
return $countryList; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return Country |
110
|
|
|
*/ |
111
|
|
|
public function getCountry() |
112
|
|
|
{ |
113
|
|
|
return $this->country; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return Country[] |
118
|
|
|
*/ |
119
|
|
|
public function getCountries() |
120
|
|
|
{ |
121
|
|
|
$countries = []; |
122
|
|
|
if (empty($this->allowedCountries)) { |
123
|
|
|
foreach (\countries() as $countryCode => $countryAttributes) { |
124
|
|
|
$country = country($countryCode); |
125
|
|
|
$countries[$country->getIsoAlpha2()] = $country; |
126
|
|
|
} |
127
|
|
|
return $countries; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
foreach ($this->allowedCountries as $countryCode) { |
131
|
|
|
$countries[$countryCode] = country($countryCode); |
132
|
|
|
} |
133
|
|
|
return $countries; |
134
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
private function setDefaults() |
138
|
|
|
{ |
139
|
|
|
if (empty($this->address)) { |
140
|
|
|
$params = []; |
141
|
|
|
if (!empty($this->disabledFields)) { |
142
|
|
|
$requiredFields = array_diff(Address::$defaultRequiredFields, $this->disabledFields); |
143
|
|
|
$params['requiredFields'] = $requiredFields; |
144
|
|
|
} |
145
|
|
|
$this->address = new Address($params); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (count($this->allowedCountries) === 1) { |
149
|
|
|
$this->country = country($this->allowedCountries[0]); |
150
|
|
|
$this->address->country = $this->country->getIsoAlpha2(); |
151
|
|
|
} |
152
|
|
|
if(!empty($this->defaultCountry)) { |
153
|
|
|
if(!in_array($this->defaultCountry, $this->allowedCountries) && !empty($this->allowedCountries)) { |
154
|
|
|
throw new ErrorException("the set defaultCounty '{$this->defaultCountry}' must be one of the configured allowedCountries"); |
155
|
|
|
} |
156
|
|
|
$this->country = country($this->defaultCountry); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
if (empty($this->submitText)) { |
160
|
|
|
$this->submitText = Yii::t("addressform", "Send"); |
161
|
|
|
} |
162
|
|
|
$this->htmlOptions['id'] = $this->id; |
163
|
|
|
if (!empty($this->htmlOptions)) { |
164
|
|
|
foreach ($this->htmlOptions as $key => $value) { |
165
|
|
|
$this->htmlOptions[$key] = $value; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$this->defaultPlaceHolders(); |
170
|
|
|
|
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
private function defaultPlaceHolders() |
174
|
|
|
{ |
175
|
|
|
$defaults = [ |
176
|
|
|
'name' => Yii::t("addressform", "John Doe"), |
177
|
|
|
'country' => Yii::t("addressform", "Select country"), |
178
|
|
|
'state' => Yii::t("addressform", "Select state"), |
179
|
|
|
'city' => Yii::t("addressform", "New York City"), |
180
|
|
|
'postCode' => Yii::t("addressform", "10001"), |
181
|
|
|
'addressLine1' => Yii::t("addressform", "350 Fifth Avenue"), |
182
|
|
|
'addressLine2' => Yii::t("addressform", "2nd floor, room 203"), |
183
|
|
|
]; |
184
|
|
|
foreach ($defaults as $key => $value) { |
185
|
|
|
if(!isset($this->placeHolders[$key])) { |
186
|
|
|
$this->placeHolders[$key] = $value; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|