1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StoutLogic\AcfBuilder; |
4
|
|
|
|
5
|
|
|
class FieldsBuilder extends Builder |
6
|
|
|
{ |
7
|
|
|
protected $config = []; |
8
|
|
|
protected $fields = []; |
9
|
|
|
protected $location = null; |
10
|
|
|
protected $name; |
11
|
|
|
|
12
|
|
|
public function __construct($name, $groupConfig = []) |
13
|
|
|
{ |
14
|
|
|
$this->name = $name; |
15
|
|
|
$this->setGroupConfig('key', 'group_'.$name); |
16
|
|
|
$this->setGroupConfig('title', $this->generateLabel($name)); |
17
|
|
|
|
18
|
|
|
$this->config = array_merge($this->config, $groupConfig); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setGroupConfig($key, $value) |
22
|
|
|
{ |
23
|
|
|
$this->config[$key] = $value; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getName() |
29
|
|
|
{ |
30
|
|
|
return $this->name; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Build the final config array. Build any other builders that may exist |
35
|
|
|
* in the config. |
36
|
|
|
* @return array final field config |
37
|
|
|
*/ |
38
|
|
|
public function build() |
39
|
|
|
{ |
40
|
|
|
$fields = $this->getFields(); |
41
|
|
|
|
42
|
|
|
$fields = $this->buildFields($fields); |
43
|
|
|
|
44
|
|
|
array_walk_recursive($fields, function(&$value, $key) { |
45
|
|
|
|
46
|
|
|
switch ($key) { |
47
|
|
|
case 'conditional_logic': |
48
|
|
|
$value = $value->build(); |
49
|
|
|
$value = $this->transformConditionalConfig($value); |
50
|
|
|
break; |
51
|
|
|
} |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
$location = $this->getLocation(); |
55
|
|
|
if (is_subclass_of($location, Builder::class)) { |
|
|
|
|
56
|
|
|
$location = $location->build(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return array_merge($this->config, [ |
60
|
|
|
'fields' => $fields, |
61
|
|
|
'location' => $location, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function buildFields($fields) |
66
|
|
|
{ |
67
|
|
|
$builtFields = []; |
68
|
|
|
|
69
|
|
|
foreach($fields as $i => $field) { |
70
|
|
|
if (is_subclass_of($field, Builder::class)) { |
|
|
|
|
71
|
|
|
$builtFields[] = $field->build(); |
72
|
|
|
} |
73
|
|
|
else { |
74
|
|
|
$builtFields[] = $field; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $builtFields; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Replace field values with the field's respective key |
83
|
|
|
* @param array $config |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
protected function transformConditionalConfig($config) |
87
|
|
|
{ |
88
|
|
|
// Replace field name with the field's key, default: field_$name |
89
|
|
|
array_walk_recursive($config, function(&$value, $key) { |
90
|
|
|
switch ($key) { |
91
|
|
|
case 'field': |
92
|
|
|
if ($field = $this->getFieldByName($value)) { |
93
|
|
|
$value = $field['key']; |
94
|
|
|
} else { |
95
|
|
|
$value = 'field_'.$value; |
96
|
|
|
} |
97
|
|
|
break; |
98
|
|
|
} |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
return $config; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Add multiple fields either via an array or from another builder |
106
|
|
|
* @param mixed $fields array of fields or a FieldBuilder |
107
|
|
|
*/ |
108
|
|
|
public function addFields($fields) |
109
|
|
|
{ |
110
|
|
|
if (is_a($fields, FieldsBuilder::class)) { |
111
|
|
|
$fields = clone $fields; |
112
|
|
|
foreach ($fields->getFields() as $field) { |
113
|
|
|
$this->pushField($field); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
else { |
117
|
|
|
foreach ($fields as $field) { |
118
|
|
|
$this->pushField($field); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function addField($name, $args = []) |
126
|
|
|
{ |
127
|
|
|
$field = array_merge([ |
128
|
|
|
'key' => 'field_'.$name, |
129
|
|
|
'name' => $name, |
130
|
|
|
'label' => $this->generateLabel($name), |
131
|
|
|
], $args); |
132
|
|
|
|
133
|
|
|
$this->pushField($field); |
134
|
|
|
|
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function addFieldType($name, $type, $args = []) |
139
|
|
|
{ |
140
|
|
|
return $this->addField($name, array_merge([ |
141
|
|
|
'type' => $type, |
142
|
|
|
], $args)); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function addText($name, $args = []) |
146
|
|
|
{ |
147
|
|
|
return $this->addFieldType($name, 'text', $args); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function addTextarea($name, $args = []) |
151
|
|
|
{ |
152
|
|
|
return $this->addFieldType($name, 'textarea', $args); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function addNumber($name, $args = []) |
156
|
|
|
{ |
157
|
|
|
return $this->addFieldType($name, 'number', $args); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function addEmail($name, $args = []) |
161
|
|
|
{ |
162
|
|
|
return $this->addFieldType($name, 'email', $args); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function addUrl($name, $args = []) |
166
|
|
|
{ |
167
|
|
|
return $this->addFieldType($name, 'url', $args); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function addPassword($name, $args = []) |
171
|
|
|
{ |
172
|
|
|
return $this->addFieldType($name, 'password', $args); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function addWysiwyg($name, $args = []) |
176
|
|
|
{ |
177
|
|
|
return $this->addFieldType($name, 'wysiwyg', $args); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function addOembed($name, $args = []) |
181
|
|
|
{ |
182
|
|
|
return $this->addFieldType($name, 'oembed', $args); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function addImage($name, $args = []) |
186
|
|
|
{ |
187
|
|
|
return $this->addFieldType($name, 'image', $args); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function addFile($name, $args = []) |
191
|
|
|
{ |
192
|
|
|
return $this->addFieldType($name, 'file', $args); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function addGallery($name, $args = []) |
196
|
|
|
{ |
197
|
|
|
return $this->addFieldType($name, 'gallery', $args); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function addTrueFalse($name, $args = []) |
201
|
|
|
{ |
202
|
|
|
return $this->addFieldType($name, 'true_false', $args); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function addSelect($name, $args = []) |
206
|
|
|
{ |
207
|
|
|
return $this->addFieldType($name, 'select', $args); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function addRadio($name, $args = []) |
211
|
|
|
{ |
212
|
|
|
return $this->addFieldType($name, 'radio', $args); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function addCheckbox($name, $args = []) |
216
|
|
|
{ |
217
|
|
|
return $this->addFieldType($name, 'checkbox', $args); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function addPostObject($name, $args = []) |
221
|
|
|
{ |
222
|
|
|
return $this->addFieldType($name, 'post_object', $args); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function addPostLink($name, $args = []) |
226
|
|
|
{ |
227
|
|
|
return $this->addFieldType($name, 'post_link', $args); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function addRelationship($name, $args = []) |
231
|
|
|
{ |
232
|
|
|
return $this->addFieldType($name, 'relationship', $args); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function addTaxonomy($name, $args = []) |
236
|
|
|
{ |
237
|
|
|
return $this->addFieldType($name, 'taxonomy', $args); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function addUser($name, $args = []) |
241
|
|
|
{ |
242
|
|
|
return $this->addFieldType($name, 'user', $args); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function addDatePicker($name, $args = []) |
246
|
|
|
{ |
247
|
|
|
return $this->addFieldType($name, 'date_picker', $args); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
public function addTimePicker($name, $args = []) |
251
|
|
|
{ |
252
|
|
|
return $this->addFieldType($name, 'time_picker', $args); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
public function addDateTimePicker($name, $args = []) |
256
|
|
|
{ |
257
|
|
|
return $this->addFieldType($name, 'date_time_picker', $args); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function addColorPicker($name, $args = []) |
261
|
|
|
{ |
262
|
|
|
return $this->addFieldType($name, 'color_picker', $args); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
View Code Duplication |
public function addTab($label, $args = []) |
|
|
|
|
266
|
|
|
{ |
267
|
|
|
$name = $this->generateName($label).'_tab'; |
268
|
|
|
$args = array_merge([ |
269
|
|
|
'label' => $label, |
270
|
|
|
], $args); |
271
|
|
|
|
272
|
|
|
return $this->addFieldType($name, 'tab', $args); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function endpoint($value = 1) |
276
|
|
|
{ |
277
|
|
|
return $this->setConfig('endpoint', $value); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
View Code Duplication |
public function addMessage($label, $message, $args = []) |
|
|
|
|
281
|
|
|
{ |
282
|
|
|
$name = $this->generateName($label).'_message'; |
283
|
|
|
$args = array_merge([ |
284
|
|
|
'label' => $label, |
285
|
|
|
'message' => $message, |
286
|
|
|
], $args); |
287
|
|
|
|
288
|
|
|
return $this->addFieldType($name, 'message', $args); |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function addRepeater($name, $args = []) |
292
|
|
|
{ |
293
|
|
|
$repeaterBuilder = new RepeaterBuilder($name, $args); |
294
|
|
|
$repeaterBuilder->setParentContext($this); |
295
|
|
|
$this->pushField($repeaterBuilder); |
296
|
|
|
|
297
|
|
|
return $repeaterBuilder; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
public function addFlexibleContent($name, $args = []) |
301
|
|
|
{ |
302
|
|
|
$flexibleContentBuilder = new FlexibleContentBuilder($name, $args); |
303
|
|
|
$flexibleContentBuilder->setParentContext($this); |
304
|
|
|
$this->pushField($flexibleContentBuilder); |
305
|
|
|
|
306
|
|
|
return $flexibleContentBuilder; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function addChoice($choice, $label = null) |
310
|
|
|
{ |
311
|
|
|
$field = $this->popLastField(); |
312
|
|
|
|
313
|
|
|
array_key_exists('choices', $field) ?: $field['choices'] = []; |
314
|
|
|
$label ?: $label = $choice; |
315
|
|
|
|
316
|
|
|
$field['choices'][$choice] = $label; |
317
|
|
|
$this->pushField($field); |
318
|
|
|
|
319
|
|
|
return $this; |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function addChoices() |
323
|
|
|
{ |
324
|
|
|
foreach (func_get_args() as $choice) { |
325
|
|
|
if (is_array($choice)) { |
326
|
|
|
$values = each($choice); |
327
|
|
|
$this->addChoice($values['key'], $values['value']); |
328
|
|
|
} else { |
329
|
|
|
$this->addChoice($choice); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return $this; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
public function conditional($name, $operator, $value) |
337
|
|
|
{ |
338
|
|
|
$field = $this->popLastField(); |
339
|
|
|
$conditionalBuilder = new ConditionalBuilder($name, $operator, $value); |
340
|
|
|
$conditionalBuilder->setParentContext($this); |
341
|
|
|
|
342
|
|
|
$field['conditional_logic'] = $conditionalBuilder; |
343
|
|
|
$this->pushField($field); |
344
|
|
|
|
345
|
|
|
return $conditionalBuilder; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function getFields() |
349
|
|
|
{ |
350
|
|
|
return $this->fields; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
protected function getFieldByName($name) |
354
|
|
|
{ |
355
|
|
|
foreach ($this->fields as $field) { |
356
|
|
|
if ($field['name'] === $name) { |
357
|
|
|
return $field; |
358
|
|
|
} |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
public function defaultValue($value) |
365
|
|
|
{ |
366
|
|
|
return $this->setConfig('default_value', $value); |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function required($value = true) |
370
|
|
|
{ |
371
|
|
|
return $this->setConfig('required', $value ? 1 : 0); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
public function instructions($value) |
375
|
|
|
{ |
376
|
|
|
return $this->setConfig('instructions', $value); |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
public function setConfig($key, $value) |
380
|
|
|
{ |
381
|
|
|
$field = $this->popLastField(); |
382
|
|
|
$field[$key] = $value; |
383
|
|
|
$this->pushField($field); |
384
|
|
|
|
385
|
|
|
return $this; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function setLocation($param, $operator, $value) |
389
|
|
|
{ |
390
|
|
|
if ($this->getParentContext()) { |
391
|
|
|
return $this->getParentContext()->setLocation($param, $operator, $value); |
|
|
|
|
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
$this->location = new LocationBuilder($param, $operator, $value); |
395
|
|
|
$this->location->setParentContext($this); |
396
|
|
|
|
397
|
|
|
return $this->location; |
398
|
|
|
} |
399
|
|
|
|
400
|
|
|
public function getLocation() |
401
|
|
|
{ |
402
|
|
|
return $this->location; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
protected function popLastField() |
406
|
|
|
{ |
407
|
|
|
return array_pop($this->fields); |
408
|
|
|
} |
409
|
|
|
|
410
|
|
|
protected function pushField($field) |
411
|
|
|
{ |
412
|
|
|
$this->fields[] = $field; |
413
|
|
|
} |
414
|
|
|
|
415
|
|
|
protected function generateLabel($name) |
416
|
|
|
{ |
417
|
|
|
return ucwords(str_replace("_", " ", $name)); |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
protected function generateName($name) |
421
|
|
|
{ |
422
|
|
|
return strtolower(str_replace(" ", "_", $name)); |
423
|
|
|
} |
424
|
|
|
} |
425
|
|
|
|