Completed
Push — master ( 3c0a16...506e36 )
by Robbie
10s
created

ElementalEditor   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 118
rs 10
c 0
b 0
f 0
wmc 7

6 Methods

Rating   Name   Duplication   Size   Complexity  
B getField() 0 33 2
A setTitle() 0 5 1
A __construct() 0 4 1
A getArea() 0 3 1
A getTypes() 0 7 1
A setTypes() 0 5 1
1
<?php
2
3
namespace DNADesign\Elemental;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use SilverStripe\Core\Extensible;
7
use SilverStripe\Core\Injector\Injectable;
8
use SilverStripe\Core\Injector\Injector;
9
use SilverStripe\Forms\GridField\GridField;
10
use SilverStripe\Forms\GridField\GridFieldAddExistingAutocompleter;
11
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
12
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
13
use SilverStripe\Forms\GridField\GridFieldDeleteAction;
14
use SilverStripe\Forms\GridField\GridFieldPageCount;
15
use SilverStripe\Forms\GridField\GridFieldPaginator;
16
use SilverStripe\Forms\GridField\GridFieldSortableHeader;
17
use SilverStripe\Versioned\VersionedGridFieldState\VersionedGridFieldState;
18
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
19
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
20
21
class ElementalEditor
22
{
23
    use Extensible;
24
    use Injectable;
25
26
    /**
27
     * @var ElementalArea $area
28
     */
29
    protected $area;
30
31
    /**
32
     * @var string $name
33
     */
34
    protected $name;
35
36
    /**
37
     * By default, no need for a title on the editor. If there is more than one
38
     * area then use `setTitle` to describe.
39
     *
40
     * @var string $title
41
     */
42
    protected $title = '';
43
44
    /**
45
     * @var array $type
46
     */
47
    protected $types = [];
48
49
    /**
50
     * @param string $name
51
     * @param ElementalArea $area
52
     */
53
    public function __construct($name, ElementalArea $area)
54
    {
55
        $this->name = $name;
56
        $this->area = $area;
57
    }
58
59
    /**
60
     * @param array $types
61
     *
62
     * @return $this
63
     */
64
    public function setTypes($types)
65
    {
66
        $this->types = $types;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @return array
73
     */
74
    public function getTypes()
75
    {
76
        $types = $this->types;
77
78
        $this->extend('updateGetTypes', $types);
79
80
        return $types;
81
    }
82
83
    /**
84
     * @return ElementalArea
85
     */
86
    public function getArea()
87
    {
88
        return $this->area;
89
    }
90
91
    /**
92
     * @param string $title
93
     *
94
     * @return $this
95
     */
96
    public function setTitle($title)
97
    {
98
        $this->title = $title;
99
100
        return $this;
101
    }
102
103
    /**
104
     * @return GridField
105
     */
106
    public function getField()
107
    {
108
        $gridField = GridField::create(
109
            $this->name,
110
            $this->title,
111
            $this->getArea()->Elements(),
112
            $config = GridFieldConfig_RelationEditor::create()
113
                ->removeComponentsByType(array(
114
                    GridFieldAddNewButton::class,
115
                    GridFieldSortableHeader::class,
116
                    GridFieldDeleteAction::class,
117
                    GridFieldPaginator::class,
118
                    GridFieldPageCount::class,
119
                    VersionedGridFieldState::class,
120
                    GridFieldAddExistingAutocompleter::class,
121
                ))
122
                ->addComponent(new GridFieldOrderableRows('Sort'))
123
                // delete elements rather than unlinking them
124
                ->addComponent(new GridFieldDeleteAction(false))
125
        );
126
127
        $gridField->addExtraClass('elemental-editor');
128
129
        if ($this->types) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->types of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
130
            $adder = Injector::inst()->create(GridFieldAddNewMultiClass::class, 'toolbar-header-left');
131
            $adder->setClasses($this->getTypes());
132
133
            $config->addComponent($adder);
134
        }
135
136
        $this->extend('updateField', $gridField);
137
138
        return $gridField;
139
    }
140
}
141