Completed
Push — master ( eaf16e...6f8e07 )
by Mohamed
11:01
created

Moo_EditableFieldAdmin   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 4
dl 0
loc 69
ccs 34
cts 34
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B getEditForm() 0 26 1
B getCreatableFields() 0 25 5
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 = [
0 ignored issues
show
Unused Code introduced by
The property $managed_models is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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