|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Moo_EditableFieldAdmin is an admin class for managing the editable fields in the system. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
*/ |
|
8
|
|
|
class Moo_EditableFieldAdmin extends ModelAdmin |
|
9
|
|
|
{ |
|
10
|
|
|
private static $url_segment = 'editablefield'; |
|
11
|
|
|
private static $menu_title = 'Editable fields'; |
|
12
|
|
|
private static $managed_models = [ |
|
|
|
|
|
|
13
|
|
|
'Moo_EditableField', |
|
14
|
|
|
'Moo_EditableFieldGroup', |
|
15
|
|
|
]; |
|
16
|
|
|
private static $menu_icon = 'editablefield/images/icon.png'; |
|
17
|
|
|
|
|
18
|
|
|
public function getEditForm($id = null, $fields = null) |
|
19
|
|
|
{ |
|
20
|
|
|
$form = parent::getEditForm($id, $fields); |
|
21
|
|
|
|
|
22
|
|
|
// Customise the grid field to show icon and title |
|
23
|
|
|
$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass)); |
|
24
|
|
|
$config = $gridField |
|
25
|
|
|
->getConfig(); |
|
26
|
|
|
|
|
27
|
|
|
$config->getComponentByType('GridFieldDataColumns') |
|
28
|
|
|
->setDisplayFields([ |
|
29
|
|
|
'Type' => 'Type', |
|
30
|
|
|
'Title' => 'Title', |
|
31
|
|
|
])->setFieldFormatting([ |
|
32
|
|
|
'Type' => function ($_, Moo_EditableField $field) { |
|
33
|
|
|
return $field->getIconTag(); |
|
34
|
|
|
}, |
|
35
|
|
|
]); |
|
36
|
|
|
|
|
37
|
|
|
$adder = new GridFieldAddNewMultiClass(); |
|
38
|
|
|
$adder->setClasses($this->getCreatableFields()); |
|
39
|
|
|
$config->removeComponentsByType('GridFieldAddNewButton') |
|
40
|
|
|
->addComponent($adder); |
|
41
|
|
|
|
|
42
|
|
|
return $form; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Return a {@link ArrayList} of all the addable fields to populate the add |
|
47
|
|
|
* field menu. |
|
48
|
|
|
* |
|
49
|
|
|
* @return array |
|
50
|
|
|
*/ |
|
51
|
|
|
private function getCreatableFields() |
|
52
|
|
|
{ |
|
53
|
|
|
$fields = ClassInfo::subclassesFor('Moo_EditableField'); |
|
54
|
|
|
$output = []; |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($fields)) { |
|
57
|
|
|
array_shift($fields); // get rid of subclass 0 |
|
58
|
|
|
asort($fields); // get in order |
|
59
|
|
|
|
|
60
|
|
|
foreach ($fields as $field => $title) { |
|
61
|
|
|
// Skip an abstract class |
|
62
|
|
|
if ($field == 'Moo_EditableFieldMultipleOption') { |
|
63
|
|
|
continue; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Get the nice title and strip out field |
|
67
|
|
|
$niceTitle = _t($field . '.SINGULARNAME', $title); |
|
68
|
|
|
if ($niceTitle) { |
|
69
|
|
|
$output[$field] = $niceTitle; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $output; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
} |
|
78
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.