-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldtypeSimpleAddress.module
More file actions
213 lines (179 loc) · 7 KB
/
FieldtypeSimpleAddress.module
File metadata and controls
213 lines (179 loc) · 7 KB
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
<?php namespace ProcessWire;
/**
* Fieldtype SimpleAddress stores address data in the database
*
* @author Jürgen Kern
* @license Licensed under MIT
*/
class FieldtypeSimpleAddress extends Fieldtype
{
public function __construct()
{
include_once(__DIR__ . '/SimpleAddress.php');
parent::__construct();
}
public static function getModuleInfo()
{
return array(
'title' => 'Fieldtype SimpleAddress',
'summary' => 'Fieldtype that stores various elements of an address.',
'version' => 1,
'href' => 'https://github.com/juergenweb/FieldtypeSimpleAddress',
'icon' => 'home',
'installs' => 'InputfieldSimpleAddress',
'requires' => [
'PHP>=7.0.0',
'ProcessWire>=3'
],
'author' => 'Jürgen Kern'
);
}
public function init()
{
$this->addHookBefore('ProcessField::fieldSaved', $this, 'removeCoordinates');
}
/**
* Before ProcessField::fieldSaved
* Hook method to uncheck checkboxes of latitude and longitude before field save if they should not be displayed in the inputfield
*
* @param HookEvent $event
*/
public function removeCoordinates(HookEvent $event) {
$field = $event->arguments(0);
if ($field->type instanceof FieldtypeSimpleAddress) {
$coordinates = $field->input_coordinates;
$req = $field->input_required;
// Uncheck checkboxes of lat and lng if coordinates are disabled
if (!$coordinates) {
if(in_array('lat', $req) || in_array('lng', $req)){
//get keys of lat and lng
$keyLat = array_search('lat', $req);
$keyLng = array_search('lng', $req);
//remove them from the required array
unset($req[$keyLat]);
unset($req[$keyLng]);
//set the new array as value of field input_required
$field->input_required = $req;
// Populate modified arguments back
$event->arguments(0, $field);
$field->save(); //save the changes in inputfield configuration
}
}
}
}
/**
* @param \ProcessWire\Page $page
* @param \ProcessWire\Field $field
* @return null|\ProcessWire\_Module|\ProcessWire\Inputfield|\ProcessWire\Module
* @throws \ProcessWire\WirePermissionException
* link the core inputfield to this fieldtype
*/
public function getInputfield(Page $page, Field $field)
{
return $this->modules->get('InputfieldSimpleAddress');
}
/**
* @param \ProcessWire\Field $field
* @return null|\ProcessWire\_Module|\ProcessWire\Inputfield|\ProcessWire\Module
* User is not allowed to switch from this fieldtype to another
*/
public function ___getCompatibleFieldtypes(Field $field)
{
return null;
}
/**
* Return the blank value for this fieldtype, whether that is a blank string, zero value, blank object or array
* In this case create a new instance of this fieldtype for wakeupValue
* @param Page $page
* @param Field $field
* @return object
*
*/
public function getBlankValue(Page $page, Field $field)
{
return new SimpleAddress();
}
/**
* @param \ProcessWire\Page $page
* @param \ProcessWire\Field $field
* @param int|object|\ProcessWire\WireArray|string $value
* @return int|null|object|\ProcessWire\ObjectDimensions|\ProcessWire\WireArray|string
*
* Fieldtype sanitization/validation -> every value from this fieldtype that will be displayed on the page runs through this method
*/
public function sanitizeValue(Page $page, Field $field, $value)
{
if (!$value instanceof SimpleAddress) {
$value = $this->getBlankValue($page, $field);
}
return $value;
}
/**
* Convert from DB storage to API value.
*
* @param Page $page
* @param Field $field
* @param string|int|array $value
* @return object $address
*
*/
public function wakeupValue(Page $page, Field $field, $value)
{
$address = $this->getBlankValue($page, $field);
// sanitize and populate the address object for usage on frontend
$address->street = (string)$value['street'];
$address->number = (string)$value['number'];
$address->postalcode = (string)$value['postalcode'];
$address->city = (string)$value['city'];
$address->state = (string)$value['state'];
$address->country = (string)$value['country'];
$address->lat = (string)$value['lat'];
$address->lng = (string)$value['lng'];
return $address;
}
/**
* Convert from API to DB storage value (opposition of wakeupValue).
*
* @param Page $page
* @param Field $field
* @param string|int|array|object $value
* @return array
*
*/
public function sleepValue(Page $page, Field $field, $value): array
{
// check if value is object of the correct class type and throw error if value is not of the right type
if (!$value instanceof SimpleAddress) {
throw new \Exception($this->_('Expecting an instance of SimpleAddress'));
}
// sanitzize values and prepare it for storage into the DB
$sleepValue = [
'street' => (string) $value->street ? $value->street : NULL,
'number' => (string) $value->number ? $value->number : NULL,
'postalcode' => (string) $value->postalcode ? $value->postalcode : NULL,
'city' => (string) $value->city ? $value->city : NULL,
'state' => (string) $value->state ? $value->state : NULL,
'country' => (string) $value->country ? $value->country : NULL,
'lat' => (float) $value->lat ? $value->lat : NULL,
'lng' => (float) $value->lng ? $value->lng : NULL,
];
return $sleepValue;
}
/**
* @param \ProcessWire\Field $field
* @return null|\ProcessWire\_Module|\ProcessWire\Inputfield|\ProcessWire\Module
*/
public function getDatabaseSchema(Field $field)
{
$schema = parent::getDatabaseSchema($field);
$schema['street'] = 'TINYTEXT DEFAULT NULL';
$schema['number'] = 'TINYTEXT DEFAULT NULL';
$schema['postalcode'] = 'TINYTEXT DEFAULT NULL';
$schema['city'] = 'TINYTEXT DEFAULT NULL';
$schema['state'] = 'TINYTEXT DEFAULT NULL';
$schema['country'] = 'TINYTEXT DEFAULT NULL';
$schema['lat'] = 'DOUBLE DEFAULT NULL';
$schema['lng'] = 'DOUBLE DEFAULT NULL';
return $schema;
}
}