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