Passed
Pull Request — 4.0 (#7773)
by Damian
07:41
created

GridFieldVersionedState   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 175
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 175
rs 10
c 1
b 0
f 0
wmc 21

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getColumnContent() 0 16 4
A __construct() 0 4 2
B augmentColumns() 0 22 5
A getColumnAttributes() 0 3 1
A getColumnMetadata() 0 3 1
A getColumnsHandled() 0 3 2
B getStatusFlags() 0 30 6
1
<?php
2
3
namespace SilverStripe\Forms\GridField;
4
5
use SilverStripe\ORM\DataObject;
6
use SilverStripe\Versioned\Versioned;
7
use SilverStripe\Core\Convert;
8
9
/**
10
 * @todo Move to siverstripe/versioned module
11
 */
12
class GridFieldVersionedState implements GridField_ColumnProvider
13
{
14
    /**
15
     * Column name for versioned state
16
     *
17
     * @var string
18
     */
19
    protected $column = null;
20
21
    /**
22
     * Fields/columns to display version states. We can specifies more than one
23
     * field but states only show in the first column found.
24
     */
25
    protected $versionedLabelFields = ['Title'];
26
27
    public function __construct($versionedLabelFields = null)
28
    {
29
        if ($versionedLabelFields) {
30
            $this->versionedLabelFields = $versionedLabelFields;
31
        }
32
    }
33
34
    /**
35
     * Modify the list of columns displayed in the table.
36
     *
37
     * @see {@link GridFieldDataColumns->getDisplayFields()}
38
     * @see {@link GridFieldDataColumns}.
39
     *
40
     * @param GridField $gridField
41
     * @param array $columns List reference of all column names.
42
     */
43
    public function augmentColumns($gridField, &$columns)
44
    {
45
        if (!class_exists(Versioned::class)) {
46
            return;
47
        }
48
49
        $model = $gridField->getModelClass();
50
        $isModelVersioned = $model::has_extension(Versioned::class);
51
        if (!$isModelVersioned) {
52
            return;
53
        }
54
55
        $matchedVersionedFields = array_intersect(
56
            $columns,
57
            $this->versionedLabelFields
58
        );
59
60
        if (count($matchedVersionedFields) > 0) {
61
            $this->column = array_values($matchedVersionedFields)[0];
62
        } elseif ($columns) {
0 ignored issues
show
introduced by
The condition $columns can never be true.
Loading history...
Bug Best Practice introduced by
The expression $columns 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...
63
            // Use first column
64
            $this->column = $columns[0];
65
        }
66
    }
67
68
    /**
69
     * Names of all columns which are affected by this component.
70
     *
71
     * @param GridField $gridField
72
     * @return array
73
     */
74
    public function getColumnsHandled($gridField)
75
    {
76
        return $this->column ? [$this->column] : [];
77
    }
78
79
    /**
80
     * HTML for the column, content of the <td> element.
81
     *
82
     * @param  GridField $gridField
83
     * @param  DataObject $record - Record displayed in this row
84
     * @param  string $columnName
85
     * @return string - HTML for the column. Return NULL to skip.
86
     */
87
    public function getColumnContent($gridField, $record, $columnName)
88
    {
89
        $flagContent = '';
90
        $flags = $this->getStatusFlags($record);
91
        foreach ($flags as $class => $data) {
92
            if (is_string($data)) {
93
                $data = array('text' => $data);
94
            }
95
            $flagContent .= sprintf(
96
                " <span class=\"ss-gridfield-badge badge %s\"%s>%s</span>",
97
                'status-' . Convert::raw2xml($class),
98
                (isset($data['title'])) ? sprintf(' title="%s"', Convert::raw2xml($data['title'])) : '',
99
                Convert::raw2xml($data['text'])
100
            );
101
        }
102
        return $flagContent;
103
    }
104
105
    /**
106
     * Attributes for the element containing the content returned by {@link getColumnContent()}.
107
     *
108
     * @param  GridField $gridField
109
     * @param  DataObject $record displayed in this row
110
     * @param  string $columnName
111
     * @return array
112
     */
113
    public function getColumnAttributes($gridField, $record, $columnName)
114
    {
115
        return [];
116
    }
117
118
    /**
119
     * Additional metadata about the column which can be used by other components,
120
     * e.g. to set a title for a search column header.
121
     *
122
     * @param GridField $gridField
123
     * @param string $columnName
124
     * @return array - Map of arbitrary metadata identifiers to their values.
125
     */
126
    public function getColumnMetadata($gridField, $columnName)
127
    {
128
        return [];
129
    }
130
131
132
    /**
133
     * A flag provides the user with additional data about the current item
134
     * status, for example a "removed from draft" status. Each item can have
135
     * more than one status flag. Returns a map of a unique key to a
136
     * (localized) title for the flag. The unique key can be reused as a CSS
137
     * class.
138
     *
139
     * Example (simple):
140
     *
141
     * ```php
142
     *   "deletedonlive" => "Deleted"
143
     * ```
144
     *
145
     * Example (with optional title attribute):
146
     *
147
     * ```php
148
     *   "deletedonlive" => array(
149
     *      'text' => "Deleted",
150
     *      'title' => 'This page has been deleted'
151
     *   )
152
     * ```
153
     *
154
     * @param Versioned|DataObject $record - the record to check status for
155
     * @return array
156
     */
157
    protected function getStatusFlags($record)
158
    {
159
        if (!$record->hasExtension(Versioned::class)) {
0 ignored issues
show
Bug introduced by
The method hasExtension() does not exist on SilverStripe\Versioned\Versioned. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

159
        if (!$record->/** @scrutinizer ignore-call */ hasExtension(Versioned::class)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
160
            return [];
161
        }
162
163
        $flags = [];
164
        if ($record->isOnLiveOnly()) {
0 ignored issues
show
Bug introduced by
The method isOnLiveOnly() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

164
        if ($record->/** @scrutinizer ignore-call */ isOnLiveOnly()) {
Loading history...
165
            $flags['removedfromdraft'] = array(
166
                'text' => _t(__CLASS__ . '.ONLIVEONLYSHORT', 'On live only'),
167
                'title' => _t(__CLASS__ . '.ONLIVEONLYSHORTHELP', 'Item is published, but has been deleted from draft'),
168
            );
169
        } elseif ($record->isArchived()) {
0 ignored issues
show
Bug introduced by
The method isArchived() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
        } elseif ($record->/** @scrutinizer ignore-call */ isArchived()) {
Loading history...
170
            $flags['archived'] = array(
171
                'text' => _t(__CLASS__ . '.ARCHIVEDPAGESHORT', 'Archived'),
172
                'title' => _t(__CLASS__ . '.ARCHIVEDPAGEHELP', 'Item is removed from draft and live'),
173
            );
174
        } elseif ($record->isOnDraftOnly()) {
0 ignored issues
show
Bug introduced by
The method isOnDraftOnly() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

174
        } elseif ($record->/** @scrutinizer ignore-call */ isOnDraftOnly()) {
Loading history...
175
            $flags['addedtodraft'] = array(
176
                'text' => _t(__CLASS__ . '.ADDEDTODRAFTSHORT', 'Draft'),
177
                'title' => _t(__CLASS__ . '.ADDEDTODRAFTHELP', "Item has not been published yet")
178
            );
179
        } elseif ($record->isModifiedOnDraft()) {
0 ignored issues
show
Bug introduced by
The method isModifiedOnDraft() does not exist on SilverStripe\ORM\DataObject. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

179
        } elseif ($record->/** @scrutinizer ignore-call */ isModifiedOnDraft()) {
Loading history...
180
            $flags['modified'] = array(
181
                'text' => _t(__CLASS__ . '.MODIFIEDONDRAFTSHORT', 'Modified'),
182
                'title' => _t(__CLASS__ . '.MODIFIEDONDRAFTHELP', 'Item has unpublished changes'),
183
            );
184
        }
185
186
        return $flags;
187
    }
188
}
189