1 <?php
2 3 4 5 6
7 class TreeSelectorField extends FormField {
8 protected $sourceObject;
9
10 function __construct($name, $title, $sourceObject = "Group") {
11 $this->sourceObject = $sourceObject;
12 parent::__construct($name, $title);
13 }
14
15 function Field() {
16 Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/prototype/prototype.js");
17 Requirements::javascript(SAPPHIRE_DIR . "/thirdparty/behaviour/behaviour.js");
18 Requirements::javascript(SAPPHIRE_DIR . "/javascript/prototype_improvements.js");
19
20 Requirements::add_i18n_javascript(SAPPHIRE_DIR . '/javascript/lang');
21 Requirements::javascript(SAPPHIRE_DIR . "/javascript/TreeSelectorField.js");
22
23 $fieldName = $this->name;
24 if($this->form) {
25 $record = $this->form->getRecord();
26 if($record && $record->hasMethod($fieldName)) $items = $record->$fieldName();
27 }
28 if($items) {
29 foreach($items as $item) {
30 $titleArray[] =$item->Title;
31 $idArray[] = $item->ID;
32 }
33 if($titleArray) {
34 $itemList = implode(", ", $titleArray);
35 $value = implode(",", $idArray);
36 }
37 }
38
39 $id = $this->id();
40
41 return <<<HTML
42 <div class="TreeSelectorField">
43 <input type="hidden" name="$this->name" value="$value" />
44 <input type="button" class="edit" value="edit" />
45 <span class="items">$itemList</span>
46 </div>
47 HTML;
48 }
49
50 51 52
53 function saveInto(DataObject $record) {
54 $fieldName = $this->name;
55 $saveDest = $record->$fieldName();
56
57 if($this->value) {
58 $items = preg_split("/ *, */", trim($this->value));
59 }
60
61 $saveDest->setByIDList($items);
62 }
63
64
65 66 67
68 function gettree() {
69 echo "<div class=\"actions\">
70 <input type=\"button\" name=\"save\" value=\""._t('TreeSelectorField.SAVE', 'save')."\" />
71 <input type=\"button\" name=\"cancel\" value=\""._t('TreeSelectorField.CANCEL', 'cancel')."\" />
72 </div>";
73
74
75 $obj = singleton($this->sourceObject);
76 $obj->markPartialTree(10);
77
78 $eval = '"<li id=\"selector-' . $this->name . '-$child->ID\" class=\"$child->class closed" . ($child->isExpanded() ? "" : " unexpanded") . "\"><a>" . $child->Title . "</a>"';
79 echo $obj->getChildrenAsUL("class=\"tree\"", $eval, null, true);
80
81 }
82
83 }
84
85 ?>
86
[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the
Silverstripe Forums to ask development related questions.
-