-
Notifications
You must be signed in to change notification settings - Fork 0
/
InputfieldSimpleAddress.module
295 lines (255 loc) · 12.1 KB
/
InputfieldSimpleAddress.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php namespace ProcessWire;
/**
* Inputfield to enter address data and coordinates
*
* @author Jürgen Kern
* @license Licensed under MIT
*/
class InputfieldSimpleAddress extends Inputfield
{
public static function getModuleInfo()
{
return [
'title' => 'Inputfield SimpleAddress',
'summary' => 'Renders an inputfield for entering various data including coordinates of an address.',
'version' => 1,
'permanent' => false,
'href' => 'https://github.com/juergenweb/FieldtypeSimpleAddress',
'icon' => 'home',
'requires' => [
'FieldtypeSimpleAddress',
'PHP>=7.0.0',
'ProcessWire>=3'
],
'author' => 'Jürgen Kern'
];
}
public function __construct()
{
// set default values for inputfield properties
$this->set('input_street', '');
$this->set('input_number', '');
$this->set('input_postalcode', '');
$this->set('input_city', '');
$this->set('input_state', '');
$this->set('input_country', '');
$this->set('input_lat', '');
$this->set('input_lng', '');
// set default values for field configuration
$this->set('input_coordinates', false);
parent::__construct();
}
/**
* Per the Module interface, init() is called when the system is ready for API usage
*/
public function init()
{
$info = $this->getModuleInfo();
$version = $info['version'];
//Add CSS and JS files
$this->config->styles->add(config()->urls->{$this->className} . 'simpleaddress.css?v='.$version);
$this->config->scripts->add(config()->urls->{$this->className} . 'simpleaddress.js?v='.$version);
//Set default values for field configuration properties to make them reachable
$this->set('input_required', []);
return parent::init();
}
/* Method to get the labels of the inputfields */
public function getLabels(): array
{
return [
'street' => $this->_('Street'),
'number' => $this->_('Number'),
'postalcode' => $this->_('Postalcode'),
'city' => $this->_('City'),
'state' => $this->_('State'),
'country' => $this->_('Country'),
'lat' => $this->_('Latitude'),
'lng' => $this->_('Longitude'),
];
}
/**
* Method to add required class on inputfields depending on configuration
* @param string $fieldname
* @return string
*/
private function renderRequired(string $fieldname): string
{
if(in_array($fieldname, $this->input_required)){
return ' InputfieldStateRequired';
}
return '';
}
/**
* Render the inputs in the backend
* @return string
*/
public function ___render(): string
{
$name = $this->attr('name'); // name of the inputfield
$out = '';
$value = $this->attr('value') ? $this->attr('value') : new SimpleAddress();
$out .= '<div class="addressline">';
$out .= '<div class="inputfield_street'.$this->renderRequired('street').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['street'].'</label>';
$out .= '<input id="'.$name.'_street" type="text" name="'.$name.'_street" max="255" value="'.$value->street.'">';
$out .= '</div>';
$out .= '<div class="inputfield_number'.$this->renderRequired('number').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['number'].'</label>';
$out .= '<input id="'.$name.'_number" type="text" name="'.$name.'_number" max="255" value="'.$value->number.'">';
$out .= '</div>';
$out .= '</div>';
$out .= '<div class="addressline">';
$out .= '<div class="inputfield_postalcode'.$this->renderRequired('postalcode').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['postalcode'].'</label>';
$out .= '<input id="'.$name.'_postalcode" type="text" name="'.$name.'_postalcode" max="255" value="'.$value->postalcode.'">';
$out .= '</div>';
$out .= '<div class="inputfield_city'.$this->renderRequired('city').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['city'].'</label>';
$out .= '<input id="'.$name.'_city" type="text" name="'.$name.'_city" max="255" value="'.$value->city.'">';
$out .= '</div>';
$out .= '</div>';
$out .= '<div class="addressline">';
$out .= '<div class="inputfield_state'.$this->renderRequired('state').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['state'].'</label>';
$out .= '<input id="'.$name.'_state" type="text" name="'.$name.'_state" max="255" value="'.$value->state.'">';
$out .= '</div>';
$out .= '</div>';
$out .= '<div class="addressline">';
$out .= '<div class="inputfield_country'.$this->renderRequired('country').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['country'].'</label>';
$out .= '<input id="'.$name.'_country" type="text" name="'.$name.'_country" max="255" value="'.$value->country.'">';
$out .= '</div>';
$out .= '</div>';
if($this->get('input_coordinates')){
$out .= '<div id="latlng" class="addressline">';
$out .= '<div class="inputfield_lat'.$this->renderRequired('lat').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['lat'].'</label>';
$out .= '<input id="'.$name.'_lat" type="text" name="'.$name.'_lat" max="255" value="'.$value->lat.'">';
$out .= '</div>';
$out .= '<div class="inputfield_lng'.$this->renderRequired('lng').'">';
$out .= '<label class="InputfieldHeader uk-form-label InputfieldStateToggle">'.$this->getLabels()['lng'].'</label>';
$out .= '<input id="'.$name.'_lat" type="text" name="'.$name.'_lng" max="255" value="'.$value->lng.'">';
$out .= '</div>';
$out .= '<p>'.sprintf($this->_('You can use fe %s to get your coordinates.'), '<a href="https://www.gps-coordinates.net/" target="_blank">www.gps-coordinates.net</a>').'</p>';
$out .= '</div>';
}
return $out;
}
/**
* Process the input after a form submission($_POST or $_GET data) took place. Sanitize/validate it, and populate the value back to this inputfield.
* First line of defense for values coming directly from a form
* That function literally takes the value from $input and populates it to the 'value' attribute of the Inputfield object.
* @param WireInputData $input
* @return this
*
*/
public function ___processInput(WireInputData $input)
{
$name = $this->attr('name'); // self choosen name of the inputfield
$value = $this->attr('value');
$coordinates = $this->input_coordinates;
//create an array of the inputfield names to set processed values back afterwards
$inputfields = [ // name in db => name input element
'street' => $name . '_street',
'number' => $name . '_number',
'postalcode' => $name . '_postalcode',
'city' => $name . '_city',
'country' => $name . '_country',
'state' => $name . '_state',
'lat' => $name . '_lat',
'lng' => $name . '_lng',
];
foreach ($inputfields as $key => $name) {
//check for required inputs
$label = $this->getLabels()[$key];
//sanitize all values as string and put them back to the input
$value->set($key, (string) trim($input->$name));
//track changes in the values
$this->trackChange('value');
//start validation
if(in_array($key, $this->input_required)){
if(empty($value[$key])){
$this->error(sprintf($this->_('Field "%s" is required'), $label));
}
}
//validate geocodes if they are numbers
if(($key === 'lat') || ($key === 'lng')){
$coordinate = $value[$key];
if($coordinate){
//sanitize comma to dot if present
if (strpos($coordinate, ',') !== false) {
$coordinate = str_replace(',', '.', $coordinate);
$value->set($key, (string) $coordinate);
}
if (!is_numeric($coordinate)) { // take care here if you expect 0 to be valid
$this->error(sprintf($this->_('Field "%s" must be a number'), $label));
}
}
}
}
return $this;
}
/**
* Add additional configuration fields
*
* @return InputfieldWrapper
*
*/
// Add additional configuration fields
public function ___getConfigInputfields(): InputfieldWrapper
{
//get all parent input fields
$inputfields = parent::___getConfigInputfields();
// get rid of required config field, because we cannot use it here
$inputfields->remove('required');
// checkbox field to configure if coordinate fields should be displayed or not
$f = $this->modules->get('InputfieldCheckbox');
$f->label = $this->_('Show coordinate fields (latitude and longitude)');
$f->checkboxLabel = $this->_('Show fields');
$f->attr('name', 'input_coordinates');
$f->attr('checked', $this->input_coordinates == '1' ? 'checked' : '');
$f->description = $this->_('If checked 2 additional fields for entering longitude and latitude will be displayed.', 'dimensions');
$inputfields->append($f);
// checkbox field to make specific fields required
$f = $this->modules->get('InputfieldCheckboxes');
$f->label = $this->_('Required fields');
$f->attr('name', 'input_required');
foreach($this->getLabels() as $value => $label) {
$checked = (in_array($value,$this->input_required)) ? 'checked' : '';
$f->addOption($value, $label, ['checked' => $checked]);
}
$f->description = $this->_('If checked the input is required.');
$inputfields->append($f);
// add inputfield markup for api notes
$f = $this->modules->get('InputfieldMarkup');
$f->label = $this->_('API Notes');
$f->description = $this->_('The following can be used in your templates:');
$f->value .= '<pre>';
$f->value .= '$page->' . $this->name . '->street' . PHP_EOL;
$f->value .= '$page->' . $this->name . '->number' . PHP_EOL;
$f->value .= '$page->' . $this->name . '->postalcode' . PHP_EOL;
$f->value .= '$page->' . $this->name . '->city' . PHP_EOL;
$f->value .= '$page->' . $this->name . '->state' . PHP_EOL;
$f->value .= '$page->' . $this->name . '->country' . PHP_EOL;
$f->value .= '$page->' . $this->name . '->lat' . PHP_EOL;
$f->value .= '$page->' . $this->name . '->lng' . PHP_EOL;
$f->value .= '$page->' . $this->name . PHP_EOL;
$f->value .= '</pre>';
$inputfields->add($f);
// output all input fields (parent and custom)
return $inputfields;
}
/**
* Allow these fields to get overwritten by user on per template base
* @param \ProcessWire\Field $field
* @return array
*
*/
public function getConfigAllowContext(Field $field): array
{
return [
'input_coordinates',
'input_required',
];
}
}