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
|
1 |
|
public function getEditForm($id = null, $fields = null) |
19
|
|
|
{ |
20
|
1 |
|
$form = parent::getEditForm($id, $fields); |
21
|
|
|
|
22
|
|
|
// Customise the grid field to show icon and title |
23
|
1 |
|
$gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->modelClass)); |
24
|
|
|
$config = $gridField |
25
|
1 |
|
->getConfig(); |
26
|
|
|
|
27
|
1 |
|
$config->getComponentByType('GridFieldDataColumns') |
28
|
1 |
|
->setDisplayFields([ |
29
|
1 |
|
'Type' => 'Type', |
30
|
1 |
|
'Title' => 'Title', |
31
|
1 |
|
])->setFieldFormatting([ |
32
|
1 |
|
'Type' => function ($_, Moo_EditableField $field) { |
33
|
1 |
|
return $field->getIconTag(); |
34
|
1 |
|
}, |
35
|
1 |
|
]); |
36
|
|
|
|
37
|
1 |
|
$adder = new GridFieldAddNewMultiClass(); |
38
|
1 |
|
$adder->setClasses($this->getCreatableFields()); |
39
|
1 |
|
$config->removeComponentsByType('GridFieldAddNewButton') |
40
|
1 |
|
->addComponent($adder); |
41
|
|
|
|
42
|
1 |
|
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
|
1 |
|
private function getCreatableFields() |
52
|
|
|
{ |
53
|
1 |
|
$fields = ClassInfo::subclassesFor('Moo_EditableField'); |
54
|
1 |
|
$output = []; |
55
|
|
|
|
56
|
1 |
|
if (!empty($fields)) { |
57
|
1 |
|
array_shift($fields); // get rid of subclass 0 |
58
|
1 |
|
asort($fields); // get in order |
59
|
|
|
|
60
|
1 |
|
foreach ($fields as $field => $title) { |
61
|
|
|
// Skip an abstract class |
62
|
1 |
|
if ($field == 'Moo_EditableFieldMultipleOption') { |
63
|
1 |
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Get the nice title and strip out field |
67
|
1 |
|
$niceTitle = _t($field . '.SINGULARNAME', $title); |
68
|
1 |
|
if ($niceTitle) { |
69
|
1 |
|
$output[$field] = $niceTitle; |
70
|
1 |
|
} |
71
|
1 |
|
} |
72
|
1 |
|
} |
73
|
|
|
|
74
|
1 |
|
return $output; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.