TreeDropdownField.php
15.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
<?php
/**
* Dropdown-like field that allows you to select an item from a hierarchical
* AJAX-expandable tree.
*
* Creates a field which opens a dropdown (actually a div via javascript
* included for you) which contains a tree with the ability to select a singular
* item for the value of the field. This field has the ability to store one-to-one
* joins related to hierarchy or a hierarchy based filter.
*
* **Note:** your source object must use an implementation of hierarchy for this
* field to generate the tree correctly, e.g. {@link Group}, {@link SiteTree} etc.
*
* All operations are carried out through javascript and provides no fallback
* to non JS.
*
* <b>Usage</b>.
*
* <code>
* static $has_one = array(
* 'RightContent' => 'SiteTree'
* );
*
* function getCMSFields() {
* ...
* $treedropdownfield = new TreeDropdownField("RightContentID", "Choose a page to show on the right:", "SiteTree");
* ..
* }
* </code>
*
* This will generate a tree allowing the user to expand and contract subsections
* to find the appropriate page to save to the field.
*
* @see TreeMultiselectField for the same implementation allowing multiple selections
* @see DropdownField for a simple dropdown field.
* @see CheckboxSetField for multiple selections through checkboxes.
* @see OptionsetField for single selections via radiobuttons.
*
* @package forms
* @subpackage fields-relational
*/
class TreeDropdownField extends FormField {
private static $url_handlers = array(
'$Action!/$ID' => '$Action'
);
private static $allowed_actions = array(
'tree'
);
/**
* @ignore
*/
protected $sourceObject, $keyField, $labelField, $filterCallback,
$disableCallback, $searchCallback, $baseID = 0;
/**
* @var string default child method in Hierarcy->getChildrenAsUL
*/
protected $childrenMethod = 'AllChildrenIncludingDeleted';
/**
* Used by field search to leave only the relevant entries
*/
protected $searchIds = null, $showSearch, $searchExpanded = array();
/**
* CAVEAT: for search to work properly $labelField must be a database field,
* or you need to setSearchFunction.
*
* @param string $name the field name
* @param string $title the field label
* @param string|array $sourceObject The object-type to list in the tree. This could
* be one of the following:
* - A DataObject class name with the {@link Hierarchy} extension.
* - An array of key/value pairs, like a {@link DropdownField} source. In
* this case, the field will act like show a flat list of tree items,
* without any hierarchy. This is most useful in conjunction with
* {@link TreeMultiselectField}, for presenting a set of checkboxes in
* a compact view. Note, that all value strings must be XML encoded
* safely prior to being passed in.
*
* @param string $keyField to field on the source class to save as the
* field value (default ID).
* @param string $labelField the field name to show as the human-readable
* value on the tree (default Title).
* @param bool $showSearch enable the ability to search the tree by
* entering the text in the input field.
*/
public function __construct($name, $title = null, $sourceObject = 'Group', $keyField = 'ID',
$labelField = 'TreeTitle', $showSearch = true
) {
$this->sourceObject = $sourceObject;
$this->keyField = $keyField;
$this->labelField = $labelField;
$this->showSearch = $showSearch;
$this->addExtraClass('single');
parent::__construct($name, $title);
}
/**
* Set the ID of the root node of the tree. This defaults to 0 - i.e.
* displays the whole tree.
*
* @param int $ID
*/
public function setTreeBaseID($ID) {
$this->baseID = (int) $ID;
return $this;
}
/**
* Set a callback used to filter the values of the tree before
* displaying to the user.
*
* @param callback $callback
*/
public function setFilterFunction($callback) {
if(!is_callable($callback, true)) {
throw new InvalidArgumentException('TreeDropdownField->setFilterCallback(): not passed a valid callback');
}
$this->filterCallback = $callback;
return $this;
}
/**
* Set a callback used to disable checkboxes for some items in the tree
*
* @param callback $callback
*/
public function setDisableFunction($callback) {
if(!is_callable($callback, true)) {
throw new InvalidArgumentException('TreeDropdownField->setDisableFunction(): not passed a valid callback');
}
$this->disableCallback = $callback;
return $this;
}
/**
* Set a callback used to search the hierarchy globally, even before
* applying the filter.
*
* @param callback $callback
*/
public function setSearchFunction($callback) {
if(!is_callable($callback, true)) {
throw new InvalidArgumentException('TreeDropdownField->setSearchFunction(): not passed a valid callback');
}
$this->searchCallback = $callback;
return $this;
}
public function getShowSearch() {
return $this->showSearch;
}
/**
* @param Boolean
*/
public function setShowSearch($bool) {
$this->showSearch = $bool;
return $this;
}
/**
* @param $method The parameter to ChildrenMethod to use when calling Hierarchy->getChildrenAsUL in
* {@link Hierarchy}. The method specified determined the structure of the returned list. Use "ChildFolders"
* in place of the default to get a drop-down listing with only folders, i.e. not including the child elements in
* the currently selected folder.
*
* See {@link Hierarchy} for a complete list of possible methods.
*/
public function setChildrenMethod($method) {
$this->childrenMethod = $method;
return $this;
}
/**
* @return string
*/
public function Field($properties = array()) {
Requirements::add_i18n_javascript(FRAMEWORK_DIR . '/javascript/lang');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery/jquery.js');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jquery-entwine/dist/jquery.entwine-dist.js');
Requirements::javascript(FRAMEWORK_DIR . '/thirdparty/jstree/jquery.jstree.js');
Requirements::javascript(FRAMEWORK_DIR . '/javascript/TreeDropdownField.js');
Requirements::css(FRAMEWORK_DIR . '/thirdparty/jquery-ui-themes/smoothness/jquery-ui.css');
Requirements::css(FRAMEWORK_DIR . '/css/TreeDropdownField.css');
$record = $this->Value() ? $this->objectForKey($this->Value()) : null;
if($record instanceof ViewableData) {
$title = $record->obj($this->labelField)->forTemplate();
} elseif($record) {
$title = Convert::raw2xml($record->{$this->labelField});
} else if($this->showSearch) {
$title = _t('DropdownField.CHOOSESEARCH', '(Choose or Search)', 'start value of a dropdown');
} else {
$title = _t('DropdownField.CHOOSE', '(Choose)', 'start value of a dropdown');
}
// TODO Implement for TreeMultiSelectField
$metadata = array(
'id' => $record ? $record->ID : null,
'ClassName' => $record ? $record->ClassName : $this->sourceObject
);
$properties = array_merge(
$properties,
array(
'Title' => $title,
'Metadata' => ($metadata) ? Convert::raw2json($metadata) : null,
)
);
return $this->customise($properties)->renderWith('TreeDropdownField');
}
public function extraClass() {
return implode(' ', array(parent::extraClass(), ($this->showSearch ? "searchable" : null)));
}
/**
* Get the whole tree of a part of the tree via an AJAX request.
*
* @param SS_HTTPRequest $request
* @return string
*/
public function tree(SS_HTTPRequest $request) {
// Array sourceObject is an explicit list of values - construct a "flat tree"
if(is_array($this->sourceObject)) {
$output = "<ul class=\"tree\">\n";
foreach($this->sourceObject as $k => $v) {
$output .= '<li id="selector-' . $this->name . '-' . $k . '"><a>' . $v . '</a>';
}
$output .= "</ul>";
return $output;
}
// Regular source specification
$isSubTree = false;
$this->search = Convert::Raw2SQL($request->requestVar('search'));
$ID = (is_numeric($request->latestparam('ID')))
? (int)$request->latestparam('ID')
: (int)$request->requestVar('ID');
if($ID && !$request->requestVar('forceFullTree')) {
$obj = DataObject::get_by_id($this->sourceObject, $ID);
$isSubTree = true;
if(!$obj) {
throw new Exception (
"TreeDropdownField->tree(): the object #$ID of type $this->sourceObject could not be found"
);
}
} else {
if($this->baseID) {
$obj = DataObject::get_by_id($this->sourceObject, $this->baseID);
}
if(!$this->baseID || !$obj) $obj = singleton($this->sourceObject);
}
// pre-process the tree - search needs to operate globally, not locally as marking filter does
if ( $this->search != "" )
$this->populateIDs();
if ($this->filterCallback || $this->sourceObject == 'Folder' || $this->search != "" )
$obj->setMarkingFilterFunction(array($this, "filterMarking"));
$obj->markPartialTree();
// allow to pass values to be selected within the ajax request
if( isset($_REQUEST['forceValue']) || $this->value ) {
$forceValue = ( isset($_REQUEST['forceValue']) ? $_REQUEST['forceValue'] : $this->value);
if(($values = preg_split('/,\s*/', $forceValue)) && count($values)) foreach($values as $value) {
if(!$value || $value == 'unchanged') continue;
$obj->markToExpose($this->objectForKey($value));
}
}
$self = $this;
$titleFn = function(&$child) use(&$self) {
$keyField = $self->keyField;
$labelField = $self->labelField;
return sprintf(
'<li id="selector-%s-%s" data-id="%s" class="class-%s %s %s"><a rel="%d">%s</a>',
Convert::raw2xml($self->getName()),
Convert::raw2xml($child->$keyField),
Convert::raw2xml($child->$keyField),
Convert::raw2xml($child->class),
Convert::raw2xml($child->markingClasses()),
($self->nodeIsDisabled($child)) ? 'disabled' : '',
(int)$child->ID,
$child->obj($labelField)->forTemplate()
);
};
// Limit the amount of nodes shown for performance reasons.
// Skip the check if we're filtering the tree, since its not clear how many children will
// match the filter criteria until they're queried (and matched up with previously marked nodes).
$nodeThresholdLeaf = Config::inst()->get('Hierarchy', 'node_threshold_leaf');
if($nodeThresholdLeaf && !$this->filterCallback && !$this->search) {
$className = $this->sourceObject;
$nodeCountCallback = function($parent, $numChildren) use($className, $nodeThresholdLeaf) {
if($className == 'SiteTree' && $parent->ID && $numChildren > $nodeThresholdLeaf) {
return sprintf(
'<ul><li><span class="item">%s</span></li></ul>',
_t('LeftAndMain.TooManyPages', 'Too many pages')
);
}
};
} else {
$nodeCountCallback = null;
}
if($isSubTree) {
$html = $obj->getChildrenAsUL(
"",
$titleFn,
null,
true,
$this->childrenMethod,
'numChildren',
true, // root call
null,
$nodeCountCallback
);
return substr(trim($html), 4, -5);
} else {
$html = $obj->getChildrenAsUL(
'class="tree"',
$titleFn,
null,
true,
$this->childrenMethod,
'numChildren',
true, // root call
null,
$nodeCountCallback
);
return $html;
}
}
/**
* Marking public function for the tree, which combines different filters sensibly. If a filter function has been
* set, that will be called. If the source is a folder, automatically filter folder. And if search text is set,
* filter on that too. Return true if all applicable conditions are true, false otherwise.
* @param $node
* @return unknown_type
*/
public function filterMarking($node) {
if ($this->filterCallback && !call_user_func($this->filterCallback, $node)) return false;
if ($this->sourceObject == "Folder" && $node->ClassName != 'Folder') return false;
if ($this->search != "") {
return isset($this->searchIds[$node->ID]) && $this->searchIds[$node->ID] ? true : false;
}
return true;
}
/**
* Marking a specific node in the tree as disabled
* @param $node
* @return boolean
*/
public function nodeIsDisabled($node) {
return ($this->disableCallback && call_user_func($this->disableCallback, $node));
}
/**
* @param String $field
*/
public function setLabelField($field) {
$this->labelField = $field;
return $this;
}
/**
* @return String
*/
public function getLabelField() {
return $this->labelField;
}
/**
* @param String $field
*/
public function setKeyField($field) {
$this->keyField = $field;
return $this;
}
/**
* @return String
*/
public function getKeyField() {
return $this->keyField;
}
/**
* @param String $field
*/
public function setSourceObject($class) {
$this->sourceObject = $class;
return $this;
}
/**
* @return String
*/
public function getSourceObject() {
return $this->sourceObject;
}
/**
* Populate $this->searchIds with the IDs of the pages matching the searched parameter and their parents.
* Reverse-constructs the tree starting from the leaves. Initially taken from CMSSiteTreeFilter, but modified
* with pluggable search function.
*/
protected function populateIDs() {
// get all the leaves to be displayed
if ($this->searchCallback) {
$res = call_user_func($this->searchCallback, $this->sourceObject, $this->labelField, $this->search);
} else {
$sourceObject = $this->sourceObject;
$filters = array();
if(singleton($sourceObject)->hasDatabaseField($this->labelField)) {
$filters["{$this->labelField}:PartialMatch"] = $this->search;
} else {
if(singleton($sourceObject)->hasDatabaseField('Title')) {
$filters["Title:PartialMatch"] = $this->search;
}
if(singleton($sourceObject)->hasDatabaseField('Name')) {
$filters["Name:PartialMatch"] = $this->search;
}
}
if(empty($filters)) {
throw new InvalidArgumentException(sprintf(
'Cannot query by %s.%s, not a valid database column',
$sourceObject,
$this->labelField
));
}
$res = DataObject::get($this->sourceObject)->filterAny($filters);
}
if( $res ) {
// iteratively fetch the parents in bulk, until all the leaves can be accessed using the tree control
foreach($res as $row) {
if ($row->ParentID) $parents[$row->ParentID] = true;
$this->searchIds[$row->ID] = true;
}
$sourceObject = $this->sourceObject;
while (!empty($parents)) {
$items = $sourceObject::get()
->filter("ID",array_keys($parents));
$parents = array();
foreach($items as $item) {
if ($item->ParentID) $parents[$item->ParentID] = true;
$this->searchIds[$item->ID] = true;
$this->searchExpanded[$item->ID] = true;
}
}
}
}
/**
* Get the object where the $keyField is equal to a certain value
*
* @param string|int $key
* @return DataObject
*/
protected function objectForKey($key) {
if($this->keyField == 'ID') {
return DataObject::get_by_id($this->sourceObject, $key);
} else {
return DataObject::get_one($this->sourceObject, "\"{$this->keyField}\" = '".Convert::raw2sql($key)."'");
}
}
/**
* Changes this field to the readonly field.
*/
public function performReadonlyTransformation() {
$copy = $this->castedCopy('TreeDropdownField_Readonly');
$copy->setKeyField($this->keyField);
$copy->setLabelField($this->labelField);
$copy->setSourceObject($this->sourceObject);
return $copy;
}
}
/**
* @package forms
* @subpackage fields-relational
*/
class TreeDropdownField_Readonly extends TreeDropdownField {
protected $readonly = true;
public function Field($properties = array()) {
$fieldName = $this->labelField;
if($this->value) {
$keyObj = $this->objectForKey($this->value);
$obj = $keyObj ? $keyObj->$fieldName : '';
} else {
$obj = null;
}
$source = array(
$this->value => $obj
);
$field = new LookupField($this->name, $this->title, $source);
$field->setValue($this->value);
$field->setForm($this->form);
$field->dontEscape = true;
return $field->Field();
}
}