-
Notifications
You must be signed in to change notification settings - Fork 0
/
FieldtypeFields.module
371 lines (321 loc) · 9.84 KB
/
FieldtypeFields.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php namespace ProcessWire;
/**
* ProcessWire Select Fields Fieldtype
*
* Copyright 2016 by Martijn Geerts
*
* ProcessWire 2.x
* Copyright (C) 2014 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://processwire.com
*
*/
class FieldtypeFields extends FieldtypeMulti implements Module {
/**
* Return array of module information
*
* @return array
*
*/
public static function getModuleInfo() {
return array(
'title' => __('Select Fields', __FILE__),
'summary' => __('Field storing field references.', __FILE__),
'version' => '0.0.2',
'author' => 'Martijn Geerts',
'icon' => 'cube',
'requiresVersions' => array(
'ProcessWire' => array('>=', '3.0.39'),
),
);
}
/**
* Set defaults
*
*/
public function __construct() {
$this->set('inputfieldClass', 'InputfieldSelect');
}
/**
* Is the Inputfield a single select?
*
* @param Field $field
* @return boolean
*
*/
protected function isSingle(Field $field) {
$selects = $this->getInputfieldSelects();
$className = $this->getInputfieldClass($field);
return $selects[$className]['single'];
}
/**
* Return class name of the used inputfield type
*
* @param Field $field
* @return string
*
*/
protected function getInputfieldClass(Field $field) {
return $field->inputfieldClass ? $field->inputfieldClass : $this->inputfieldClass;
}
/**
* Get Fieldtypes that are known compatible with this one.
*
* @param Field $field
* @return Fieldtypes
* @throws WireException
*
*/
public function ___getCompatibleFieldtypes(Field $field) {
$fieldtypes = $this->wire(new Fieldtypes());
foreach($this->wire('fieldtypes') as $fieldtype) {
if($fieldtype instanceof FieldtypeFields) $fieldtypes->add($fieldtype);
}
return $fieldtypes;
}
/**
* Get a blank array
*
* @param Page $page
* @param Field $field
* @return array
*
*/
public function getBlankValue(Page $page, Field $field) {
return array();
}
/**
* Sanitize value for storage in a page
*
* @param Page $page
* @param Field $field
* @param mixed $value
* @return array
*
*/
public function sanitizeValue(Page $page, Field $field, $value) {
$cleanValue = $this->getBlankValue($page, $field);
if ($value instanceof FieldsArray) {
$value = $value->explode('id');
}
$value = (array) $value;
// Return blank value
if (!$value || empty($value)) return $cleanValue;
$value = array_map('intval', $value);
foreach ($value as $key => $id) {
if (!$id) continue;
$item = $this->wire('fields')->get($id);
if ($item && $item instanceof Field) {
$cleanValue[] = $id;
// Only allow first found field id when Inputfield is single
if ($this->isSingle($field)) break;
}
}
return $cleanValue;
}
/**
* Get the Inputfield that provides input for this Fieldtype
*
* @param Page $page
* @param Field $field
* @return FieldsArray
*
*/
private function getFields(Page $page, Field $field) {
$fields = $this->wire(new FieldsArray());
if ($field->filterPage) $fieldsFromPage = $page->template->fields;
// Get per selected template
if (is_array($field->filterTypes) && count($field->filterTemplates)) {
foreach ($field->filterTemplates as $id) {
$fields->import($this->wire('templates')->get($id)->fields);
}
}
if (0 === $fields->count()) {
$fields = $this->wire('fields')->getAll();
}
foreach ($fields as $f) {
// Reduce on type
if (is_array($field->filterTypes) && count($field->filterTypes) && !in_array($f->type->className(), $field->filterTypes)) {
$fields->remove($f->id);
}
// Remove fieldsets
if ($field->filterFieldsets && $f->type instanceof FieldtypeFieldsetOpen) {
$fields->remove($f->id);
}
// Reduce from page
if ($field->filterPage && !$fieldsFromPage->has($f)) {
$fields->remove($f->id);
}
if ($field->filterText) {
$words = array_map('trim', explode(PHP_EOL, $field->filterText));
$remove = true;
foreach($words as $word) {
if (strpos($f->name, $word) !== false) {
$remove = false;
break;
}
}
if ($remove) $fields->remove($f->id);
}
if ($field->filterFields && count($field->filterFields)) {
if (!in_array($f->id, $field->filterFields)) {
$fields->remove($f->id);
}
}
}
return $fields;
}
/**
* Get the Inputfield that provides input for this Fieldtype
*
* @param Page $page
* @param Field $field
* @return Inputfield
*
*/
public function getInputfield(Page $page, Field $field) {
// Filtered fields
$fields = $this->getFields($page, $field);
if ($field->sortFields) $fields->sort($field->sortFields);
$inputfield = $this->wire('modules')->get($this->getInputfieldClass($field));
$inputfield->attr('name', $field->name);
foreach($fields as $option) {
$inputfield->addOption($option->id, $option->get('label|name'));
}
return $inputfield;
}
/**
* Get the Inputfield that provides input for this Fieldtype
*
* @return array
*
*/
public function getInputfieldSelects() {
$array = array();
$modules = $this->wire('modules');
foreach($modules as $module) {
if(strpos($module->className(), 'Inputfield') !== 0) continue;
if($module instanceof ModulePlaceholder) $module = $modules->getModule($module->className(), array('noInit' => true));
if($module instanceof InputfieldSelect) {
$name = str_replace('Inputfield', '', $module->className());
$label = $name;
if($module instanceof InputfieldSelectMultiple) $label .= " *";
$array[$module->className()] = array(
'label' => $label,
'single' => $label === $name,
);
}
}
return $array;
}
/**
* Get regular array with Fieldtypes
*
* @return array
*
*/
private function getFieldTypes() {
$types = array();
foreach($this->wire('modules') as $module) {
if(strpos($module->className(), 'Fieldtype') === 0) {
// if($module instanceof ModulePlaceholder) $module = $this->wire('modules')->get($module->className());
$types[] = $module->className();
}
}
return $types;
}
/**
* Return the selected fields in a FieldsArray
*
* @param Page $page
* @param Field $field
* @param array $value
* @return FieldsArray
*
*/
public function ___formatValue(Page $page, Field $field, $value) {
$fields = $this->wire(new FieldsArray());
foreach ($value as $id) {
$fields->add($this->wire('fields')->get($id));
}
return $fields;
}
/**
* Get Inputfields needed to configure this Fieldtype
*
* @param Field $field
* @return InputfieldWrapper
*
*/
public function ___getConfigInputfields(Field $field) {
$modules = $this->wire('modules');
$inputfields = parent::___getConfigInputfields($field);
$selects = $this->getInputfieldSelects();
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'inputfieldClass');
$f->set('label', $this->_('Which Inputfield to use?'));
$f->set('description', $this->_('Returns a FieldsArray when outputformatting is true when false a regular PHP array containing the field ids.'));
$f->set('notes', $this->_('Types indicated with an asterisk are for multiple field selection.'));
foreach ($selects as $moduleName => $prop) $f->addOption($moduleName, $prop['label']);
$f->attr('value', $this->getInputfieldClass($field));
$inputfields->add($f);
$fs = $modules->get('InputfieldFieldset');
$fs->set('label', $this->_('Reduce the amount of selectable fields.'));
$f = $modules->get('InputfieldAsmSelect');
$f->attr('name', 'filterFields');
$f->set('label', $this->_('Only selected fields'));
$f->set('description', $this->_('Return only the selected fields'));
foreach ($this->wire('fields')->getAll() as $o) $f->addOption($o->id, $o->get('label|name'));
$f->attr('value', $field->get('filterFields'));
$fs->add($f);
$f = $modules->get('InputfieldAsmSelect');
$f->attr('name', 'filterTemplates');
$f->set('columnWidth', 50);
$f->set('label', $this->_('Filter by template'));
$f->set('description', $this->_('Return only fields for selected template(s).'));
foreach ($this->wire('templates') as $template) $f->addOption($template->id, $template->get('label|name'));
$f->attr('value', $field->get('filterTemplates'));
$fs->add($f);
$f = $modules->get('InputfieldAsmSelect');
$f->attr('name', 'filterTypes');
$f->set('columnWidth', 50);
$f->set('label', $this->_('Filter by type'));
$f->set('description', $this->_('Return only fields for selected fieldtypes.'));
foreach ($this->getFieldTypes() as $type) $f->addOption($type, $type);
$f->attr('value', $field->get('filterTypes'));
$fs->add($f);
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'filterFieldsets');
$f->set('columnWidth', 50);
$f->set('label', $this->_('Remove fieldset fields'));
$f->set('label2', $this->_('Reduce fields'));
$f->attr('value', $field->get('filterFieldsets'));
$f->attr('checked', $field->get('filterFieldsets'));
$fs->add($f);
$f = $modules->get('InputfieldCheckbox');
$f->attr('name', 'filterPage');
$f->set('columnWidth', 50);
$f->set('label', $this->_('Only fields from page'));
$f->set('label2', $this->_('Reduce fields'));
$f->attr('value', $field->get('filterPage'));
$f->attr('checked', $field->get('filterPage'));
$fs->add($f);
$f = $modules->get('InputfieldTextarea');
$f->attr('name', 'filterText');
$f->set('label', $this->_('Reduce on text.'));
$f->set('description', $this->_('Allow only field names containing text.'));
$f->set('notes', $this->_('Each text filter on it\'s own line.'));
$f->attr('value', $field->get('filterText'));
$fs->add($f);
$inputfields->add($fs);
$f = $modules->get('InputfieldSelect');
$f->attr('name', 'sortFields');
$f->set('columnWidth', 50);
$f->set('label', $this->_('Sort options in Inputfield'));
foreach (array('', 'type', 'name', 'label') as $sort) $f->addOption($sort, $sort);
$f->attr('value', $field->get('sortFields'));
$inputfields->add($f);
return $inputfields;
}
}