ModelAdminSnippet   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 42
c 3
b 0
f 1
dl 0
loc 202
rs 10
wmc 15

11 Methods

Rating   Name   Duplication   Size   Complexity  
A addExtraClasses() 0 12 2
A removeExtraClasses() 0 13 4
A getGridfield() 0 3 1
A __construct() 0 3 1
A getSnippet() 0 3 1
A getOrder() 0 3 1
A getTitle() 0 3 1
A getHTMLFragments() 0 6 1
A getTextColour() 0 3 1
A getExtraClasses() 0 3 1
A getBackgroundColour() 0 3 1
1
<?php
2
3
namespace ilateral\SilverStripe\ModelAdminPlus;
4
5
use SilverStripe\View\ViewableData;
6
use SilverStripe\Forms\GridField\GridField;
7
use SilverStripe\Forms\GridField\GridField_HTMLProvider;
8
9
/**
10
 * Object representing a snippet of generic data that can be loaded at the top of a
11
 * ModelAdminPlus interface
12
 */
13
abstract class ModelAdminSnippet extends ViewableData implements GridField_HTMLProvider
14
{
15
    const PRIMARY = "primary";
16
17
    const SECONDARY = "secondary";
18
19
    const SUCCESS = "success";
20
21
    const INFO = "info";
22
23
    const WARNING = "warning";
24
25
    const DANGER = "danger";
26
27
    const LIGHT = "light";
28
29
    const DARK = "dark";
30
31
    const WHITE = "white";
32
33
    /**
34
     * @var string placement indicator for this control
35
     */
36
    protected $targetFragment;
37
38
    /**
39
     * The current parent gridfield
40
     *
41
     * @var GridField
42
     */
43
    protected $gridfield;
44
45
    /**
46
     * The name/title of the current snippet.
47
     *
48
     * @var string
49
     */
50
    private static $title;
51
52
    /**
53
     * The order in which this snippet will be loaded
54
     *
55
     * @var int
56
     */
57
    private static $priority = 0;
58
59
    /**
60
     * Default background colour
61
     *
62
     * @var string
63
     */
64
    private static $background = self::INFO;
65
66
    /**
67
     * Default text colour
68
     *
69
     * @var string
70
     */
71
    private static $text = self::WHITE;
72
73
    /**
74
     * List of extra CSS classes applied to this snippet
75
     *
76
     * @var array
77
     */
78
    protected $extra_classes = [];
79
80
    private $casting = [
0 ignored issues
show
introduced by
The private property $casting is not used, and could be removed.
Loading history...
81
        "Order" => "Float"
82
    ];
83
84
    /**
85
     * @param string $targetFragment The HTML fragment to write the button into
86
     */
87
    public function __construct($targetFragment = "after")
88
    {
89
        $this->targetFragment = $targetFragment;
90
    }
91
92
    public function getHTMLFragments($gridField)
93
    {
94
        $this->gridfield = $gridField;
95
96
        return [
97
            $this->targetFragment => $this->getSnippet()
98
        ];
99
    }
100
101
    /**
102
     * Return an i18n friendly version of the title.
103
     *
104
     * @return string
105
     */
106
    public function getTitle()
107
    {
108
        return _t(__CLASS__ . "Title", $this->config()->title);
109
    }
110
111
    public function getOrder()
112
    {
113
        return $this->config()->priority;
114
    }
115
116
    /**
117
     * Render the current snippet
118
     *
119
     * @return string
120
     */
121
    public function getSnippet()
122
    {
123
        return $this->renderWith(__CLASS__);
124
    }
125
126
    /**
127
     * Return the background colour suitable for a template
128
     *
129
     * @return string
130
     */
131
    public function getBackgroundColour()
132
    {
133
        return $this->config()->background;
134
    }
135
136
    /**
137
     * Return the background colour suitable for a template
138
     *
139
     * @return string
140
     */
141
    public function getTextColour()
142
    {
143
        return $this->config()->text;
144
    }
145
146
    /**
147
     * The content of this snippet that will be rendered below
148
     * the title.
149
     *
150
     * @return string
151
     */
152
    abstract public function getContent();
153
154
    /**
155
     * Get extra CSS classes as a string
156
     *
157
     * @return string
158
     */
159
    public function getExtraClasses()
160
    {
161
        return implode(" ", $this->extra_classes);
162
    }
163
164
    /**
165
     * Add additional css classes
166
     *
167
     * @param  array|string  $extra_classes  extra CSS classes
168
     *
169
     * @return  self
170
     */
171
    public function addExtraClasses($classes)
172
    {
173
        if (!is_array($classes)) {
174
            $classes = explode(" ", $classes);
175
        }
176
177
        $this->extra_classes = array_merge(
178
            $this->extra_classes,
179
            $classes
180
        );
181
182
        return $this;
183
    }
184
185
    /**
186
     * Remove provided CSS classes
187
     *
188
     * @param  array|string  $extra_classes  extra CSS classes
189
     *
190
     * @return  self
191
     */
192
    public function removeExtraClasses($classes)
193
    {
194
        if (!is_array($classes)) {
195
            $classes = explode(" ", $classes);
196
        }
197
198
        foreach ($classes as $class) {
199
            if (isset($this->extra_classes[$class])) {
200
                unset($this->extra_classes[$class]);
201
            }
202
        }
203
204
        return $this;
205
    }
206
207
    /**
208
     * Get the current parent gridfield
209
     *
210
     * @return  GridField
211
     */ 
212
    public function getGridfield()
213
    {
214
        return $this->gridfield;
215
    }
216
}
217