|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This component creates a dropdown of possible data object classes and a button to create a new instance. |
|
5
|
|
|
* |
|
6
|
|
|
*/ |
|
7
|
|
|
class GridFieldDropdownAddNewButton extends GridFieldAddNewButton |
|
|
|
|
|
|
8
|
|
|
implements GridField_ActionProvider { |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class names |
|
12
|
|
|
* |
|
13
|
|
|
* @var array |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $modelClasses = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This is because this doesn't extend Object |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function create($baseClass, $classes, $targetFragment = 'buttons-before-left') |
|
22
|
|
|
{ |
|
23
|
|
|
return new GridFieldDropdownAddNewButton($baseClass, $classes, $targetFragment); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $baseClass |
|
28
|
|
|
* @param array $classes Class or list of classes to create. |
|
29
|
|
|
* @param string $targetFragment The fragment to render the button into |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct($baseClass, $classes, $targetFragment = 'buttons-before-left') |
|
32
|
|
|
{ |
|
33
|
|
|
if (!is_array($classes)) { |
|
34
|
|
|
user_error('$classes is not an array', E_USER_ERROR); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$this->setClasses($classes); |
|
38
|
|
|
|
|
39
|
|
|
foreach($this->getClasses() as $class => $nice){ |
|
40
|
|
|
if(!is_subclass_of($class, $baseClass)){ |
|
41
|
|
|
user_error(sprintf('%s is not a subclass of %s', $class, $baseClass), E_USER_ERROR); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
parent::__construct($targetFragment); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Specify the classes to create |
|
50
|
|
|
* |
|
51
|
|
|
* @param array $classes |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setClasses($classes) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->modelClasses = $classes; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Get the classes of the objects to create |
|
60
|
|
|
* |
|
61
|
|
|
* @return array |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getClasses() |
|
64
|
|
|
{ |
|
65
|
|
|
return $this->modelClasses; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Abstract method to fill out. Gets the HTML for this component |
|
70
|
|
|
* @param $gridField GridField |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getHTMLFragments($gridField) |
|
73
|
|
|
{ |
|
74
|
|
|
$state = $gridField->State->GridFieldDropdownAddNewButton; |
|
75
|
|
|
$classesSource = $this->getClasses(); |
|
76
|
|
|
|
|
77
|
|
|
if(empty($classesSource)) { |
|
78
|
|
|
return []; |
|
79
|
|
|
} else if(count($classesSource) > 1) { |
|
80
|
|
|
$dropdown = DropdownField::create( |
|
81
|
|
|
"Class", |
|
82
|
|
|
"Class", |
|
83
|
|
|
$classesSource |
|
84
|
|
|
)->setFieldHolderTemplate("GridFieldDropdownAddNewButton_holder") |
|
85
|
|
|
->addExtraClass("gridfield-dropdown no-change-track"); |
|
86
|
|
|
|
|
87
|
|
|
if (!$this->buttonName) { |
|
88
|
|
|
$this->buttonName = 'Add new'; |
|
89
|
|
|
} |
|
90
|
|
|
} else { |
|
91
|
|
|
$class = key($classesSource); |
|
92
|
|
|
$dropdown = HiddenField::create( |
|
93
|
|
|
"Class", |
|
94
|
|
|
"Class", |
|
95
|
|
|
$class |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
if (!$this->buttonName) { |
|
99
|
|
|
$this->buttonName = sprintf('Add new %s', $class); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$state->class = key($classesSource); |
|
104
|
|
|
|
|
105
|
|
|
$action = GridField_FormAction::create( |
|
106
|
|
|
$gridField, |
|
107
|
|
|
'add', |
|
108
|
|
|
$this->buttonName, |
|
109
|
|
|
'add', |
|
110
|
|
|
'add' |
|
111
|
|
|
)->setAttribute( |
|
112
|
|
|
'data-icon', |
|
113
|
|
|
'add' |
|
114
|
|
|
)->addExtraClass("no-ajax ss-ui-action-constructive dropdown-action"); |
|
115
|
|
|
|
|
116
|
|
|
Requirements::css(CONTENTBLOCKS_DIR . "/css/GridFieldDropdownAddNewButton.css"); |
|
117
|
|
|
Requirements::javascript(CONTENTBLOCKS_DIR . "/javascript/GridFieldDropdownAddNewButton.js"); |
|
118
|
|
|
|
|
119
|
|
|
return [ |
|
120
|
|
|
$this->targetFragment => ArrayData::create([ |
|
121
|
|
|
'Fields' => ArrayList::create([ |
|
122
|
|
|
$dropdown, |
|
123
|
|
|
$action, |
|
124
|
|
|
]), |
|
125
|
|
|
])->renderWith("GridFieldDropdownAddNewButton"), |
|
126
|
|
|
]; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Provide actions to this component. |
|
131
|
|
|
* |
|
132
|
|
|
* @param GridField $gridField |
|
133
|
|
|
* @return array |
|
134
|
|
|
**/ |
|
135
|
|
|
public function getActions($gridField) { |
|
136
|
|
|
return ["add"]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Handles the add action |
|
141
|
|
|
* |
|
142
|
|
|
* @param GridField $gridField |
|
143
|
|
|
* @param string $actionName |
|
144
|
|
|
* @param mixed $arguments |
|
145
|
|
|
* @param array $data |
|
146
|
|
|
**/ |
|
147
|
|
|
public function handleAction(GridField $gridField, $actionName, $arguments, $data) |
|
148
|
|
|
{ |
|
149
|
|
|
$response = $gridField->getForm()->controller->response; |
|
150
|
|
|
|
|
151
|
|
|
if(in_array(strtolower($actionName), $this->getActions($gridField))) { |
|
152
|
|
|
$class = null; |
|
153
|
|
|
$name = $gridField->getName(); |
|
154
|
|
|
if (isset($data[$name]) && isset($data[$name]['GridState'])){ |
|
155
|
|
|
$state = json_decode($data[$name]['GridState'], true); |
|
156
|
|
|
// Debug::dump($state); |
|
|
|
|
|
|
157
|
|
|
if (isset($state['GridFieldDropdownAddNewButton']) |
|
158
|
|
|
&& isset($state['GridFieldDropdownAddNewButton']['class'])) { |
|
159
|
|
|
$class = $state['GridFieldDropdownAddNewButton']['class']; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if(!$class || !$this->isValidClass($class)) { |
|
164
|
|
|
return Controller::curr()->redirectBack(); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$list = $gridField->getList(); |
|
168
|
|
|
$object = $class::create(); |
|
169
|
|
|
$object->write(); |
|
170
|
|
|
$list->add($object); |
|
171
|
|
|
|
|
172
|
|
|
$url = Controller::join_links($gridField->link('item'), $object->ID); |
|
173
|
|
|
|
|
174
|
|
|
$response->redirect(Director::absoluteBaseURL() . $url); |
|
175
|
|
|
return $response; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return Controller::curr()->redirectBack(); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* Validates that a class is okay for creation |
|
183
|
|
|
* |
|
184
|
|
|
* @param string |
|
185
|
|
|
* @return bool |
|
186
|
|
|
*/ |
|
187
|
|
|
public function isValidClass($class) |
|
188
|
|
|
{ |
|
189
|
|
|
if (!array_key_exists($class, $this->getClasses())) { |
|
190
|
|
|
return false; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return singleton($class)->canCreate(); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.