|
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 $name = "address-form"; |
|
38
|
|
|
|
|
39
|
|
|
/** @var array $placeHolders Form placeholders */ |
|
40
|
|
|
public $placeHolders; |
|
41
|
|
|
|
|
42
|
|
|
public $fieldIdPrefix = "address-form-"; |
|
43
|
|
|
|
|
44
|
|
|
/** @var Module */ |
|
45
|
|
|
public $module; |
|
46
|
|
|
|
|
47
|
|
|
/** @var ActiveForm */ |
|
48
|
|
|
public $form; |
|
49
|
|
|
|
|
50
|
|
|
/** @var array Fields to disable */ |
|
51
|
|
|
public $disabledFields = []; |
|
52
|
|
|
|
|
53
|
|
|
/** @var Address */ |
|
54
|
|
|
public $address; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var array config yii\bootstrap\ActiveForm |
|
58
|
|
|
*/ |
|
59
|
|
|
public $formOptions = []; |
|
60
|
|
|
|
|
61
|
|
|
/** @var string */ |
|
62
|
|
|
public $submitText; |
|
63
|
|
|
|
|
64
|
|
|
/** @var array $submitOptions the HTML options for submit button */ |
|
65
|
|
|
public $submitOptions = [ |
|
66
|
|
|
'class' => 'btn btn-primary', |
|
67
|
|
|
]; |
|
68
|
|
|
|
|
69
|
|
|
public function init() |
|
70
|
|
|
{ |
|
71
|
|
|
parent::init(); |
|
72
|
|
|
$this->registerTranslations(); |
|
73
|
|
|
$this->module = Yii::$app->getModule('addressform'); |
|
74
|
|
|
|
|
75
|
|
|
$this->setDefaults(); |
|
76
|
|
|
|
|
77
|
|
|
$this->placeHolders = [ |
|
78
|
|
|
'name' => Yii::t("addressform", "John Doe"), |
|
79
|
|
|
'country' => Yii::t("addressform", "Select country"), |
|
80
|
|
|
'state' => Yii::t("addressform", "Select state"), |
|
81
|
|
|
'city' => Yii::t("addressform", "New York City"), |
|
82
|
|
|
'postCode' => Yii::t("addressform", "10001"), |
|
83
|
|
|
'addressLine1' => Yii::t("addressform", "350 Fifth Avenue"), |
|
84
|
|
|
'addressLine2' => Yii::t("addressform", "2nd floor, room 203"), |
|
85
|
|
|
]; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
public function run() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->render('form', [ |
|
94
|
|
|
'widget' => $this |
|
95
|
|
|
]); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string[] |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getCountryList() |
|
102
|
|
|
{ |
|
103
|
|
|
|
|
104
|
|
|
$countryList = []; |
|
105
|
|
|
foreach ($this->countries as $country) { |
|
106
|
|
|
$countryList[$country->getIsoAlpha2()] = $country->getEmoji(). " " . $country->getName(); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
return $countryList; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Country |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getCountry() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->country; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return Country[] |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getCountries() |
|
123
|
|
|
{ |
|
124
|
|
|
$countries = []; |
|
125
|
|
|
if (empty($this->allowedCountries)) { |
|
126
|
|
|
foreach (\countries() as $countryCode => $countryAttributes) { |
|
127
|
|
|
$country = country($countryCode); |
|
128
|
|
|
$countries[$country->getIsoAlpha2()] = $country; |
|
129
|
|
|
} |
|
130
|
|
|
return $countries; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
foreach ($this->allowedCountries as $countryCode) { |
|
134
|
|
|
$countries[$countryCode] = country($countryCode); |
|
135
|
|
|
} |
|
136
|
|
|
return $countries; |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function setDefaults() |
|
141
|
|
|
{ |
|
142
|
|
|
if (empty($this->address)) { |
|
143
|
|
|
$this->address = new Address(); |
|
144
|
|
|
} else { |
|
145
|
|
|
$this->address->validate(); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
if (count($this->allowedCountries) === 1) { |
|
149
|
|
|
$this->country = country($this->allowedCountries[0]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if (empty($this->submitText)) { |
|
153
|
|
|
$this->submitText = Yii::t("addressform", "Send"); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
|
|
159
|
|
|
} |