1 <?php
2
3 4 5 6 7 8 9 10 11 12 13 14
15
16 class EditableMultipleOptionField extends EditableFormField {
17
18 static $has_many = array(
19 "Options" => "EditableOption"
20 );
21
22 23 24 25 26 27 28 29
30 public function doPublish($fromStage, $toStage, $createNewVersion = false) {
31 $live = Versioned::get_by_stage("EditableOption", "Live", "\"EditableOption\".\"ParentID\" = $this->ID");
32
33 if($live) {
34 foreach($live as $option) {
35 $option->delete();
36 }
37 }
38
39 if($this->Options()) {
40 foreach($this->Options() as $option) {
41 $option->publish($fromStage, $toStage, $createNewVersion);
42 }
43 }
44
45 $this->publish($fromStage, $toStage, $createNewVersion);
46 }
47
48 49 50 51 52 53 54
55 public function doDeleteFromStage($stage) {
56 if($this->Options()) {
57 foreach($this->Options() as $option) {
58 $option->deleteFromStage($stage);
59 }
60 }
61
62 $this->deleteFromStage($stage);
63 }
64
65 66 67 68 69 70
71 public function delete() {
72 $options = $this->Options();
73
74 if($options) {
75 foreach($options as $option) {
76 $option->delete();
77 }
78 }
79
80 parent::delete();
81 }
82
83 84 85 86 87 88
89 public function duplicate() {
90 $clonedNode = parent::duplicate();
91
92 if($this->Options()) {
93 foreach($this->Options() as $field) {
94 $newField = $field->duplicate();
95 $newField->ParentID = $clonedNode->ID;
96 $newField->write();
97 }
98 }
99
100 return $clonedNode;
101 }
102
103 104 105 106 107 108
109 public function populateFromPostData($data) {
110 parent::populateFromPostData($data);
111
112
113 $fieldSet = $this->Options();
114
115
116 foreach($fieldSet as $option) {
117 if(isset($data[$option->ID]) && isset($data[$option->ID]['Title']) && $data[$option->ID]['Title'] != "field-node-deleted") {
118 $option->populateFromPostData($data[$option->ID]);
119 }
120 else {
121 $option->delete();
122 }
123 }
124 }
125
126 127 128 129 130 131
132 public function getHasAddableOptions() {
133 return true;
134 }
135
136 137 138 139 140
141 public function getFormField() {
142 return user_error('Please implement getFormField() on '. $this->class, E_USER_ERROR);
143 }
144 }
[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.
-