Completed
Push — 4.0 ( b59aea...80f83b )
by Loz
52s queued 21s
created

GridFieldVersionedState::getColumnAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
use SilverStripe\View\HTML;
9
10
/**
11
 * @todo Move to siverstripe/versioned module
12
 */
13
class GridFieldVersionedState implements GridField_ColumnProvider
14
{
15
    /**
16
     * Column name for versioned state
17
     *
18
     * @var string
19
     */
20
    protected $column = null;
21
22
    /**
23
     * Fields/columns to display version states. We can specifies more than one
24
     * field but states only show in the first column found.
25
     */
26
    protected $versionedLabelFields = ['Title'];
27
28
    public function __construct($versionedLabelFields = null)
29
    {
30
        if ($versionedLabelFields) {
31
            $this->versionedLabelFields = $versionedLabelFields;
32
        }
33
    }
34
35
    /**
36
     * Modify the list of columns displayed in the table.
37
     *
38
     * @see {@link GridFieldDataColumns->getDisplayFields()}
39
     * @see {@link GridFieldDataColumns}.
40
     *
41
     * @param GridField $gridField
42
     * @param array $columns List reference of all column names.
43
     */
44
    public function augmentColumns($gridField, &$columns)
45
    {
46
        if (!class_exists(Versioned::class)) {
47
            return;
48
        }
49
50
        /** @var Versioned|DataObject $model */
51
        $model = DataObject::singleton($gridField->getModelClass());
52
        $isModelVersioned = $model->hasExtension(Versioned::class) && $model->hasStages();
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

52
        $isModelVersioned = $model->/** @scrutinizer ignore-call */ hasExtension(Versioned::class) && $model->hasStages();

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...
Bug introduced by
The method hasStages() 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

52
        $isModelVersioned = $model->hasExtension(Versioned::class) && $model->/** @scrutinizer ignore-call */ hasStages();
Loading history...
53
        if (!$isModelVersioned) {
54
            return;
55
        }
56
57
        $matchedVersionedFields = array_intersect(
58
            $columns,
59
            $this->versionedLabelFields
60
        );
61
62
        if (count($matchedVersionedFields) > 0) {
63
            $this->column = array_values($matchedVersionedFields)[0];
64
        } elseif ($columns) {
0 ignored issues
show
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...
65
            // Use first column
66
            $this->column = $columns[0];
67
        }
68
    }
69
70
    /**
71
     * Names of all columns which are affected by this component.
72
     *
73
     * @param GridField $gridField
74
     * @return array
75
     */
76
    public function getColumnsHandled($gridField)
77
    {
78
        return $this->column ? [$this->column] : [];
79
    }
80
81
    /**
82
     * HTML for the column, content of the <td> element.
83
     *
84
     * @param  GridField $gridField
85
     * @param  DataObject $record - Record displayed in this row
86
     * @param  string $columnName
87
     * @return string - HTML for the column. Return NULL to skip.
88
     */
89
    public function getColumnContent($gridField, $record, $columnName)
90
    {
91
        $flagContent = '';
92
        $flags = $this->getStatusFlags($record);
93
        foreach ($flags as $class => $data) {
94
            if (is_string($data)) {
95
                $data = array('text' => $data);
96
            }
97
            $flagContent .= HTML::createTag(
98
                'span',
99
                array_merge(
100
                    [ 'class' => 'ss-gridfield-badge badge status-' . $class ],
101
                    array_intersect_key($data, ['title' => true])
102
                ),
103
                Convert::raw2xml($data['text'])
104
            );
105
        }
106
        return $flagContent;
107
    }
108
109
    /**
110
     * Attributes for the element containing the content returned by {@link getColumnContent()}.
111
     *
112
     * @param  GridField $gridField
113
     * @param  DataObject $record displayed in this row
114
     * @param  string $columnName
115
     * @return array
116
     */
117
    public function getColumnAttributes($gridField, $record, $columnName)
118
    {
119
        return [];
120
    }
121
122
    /**
123
     * Additional metadata about the column which can be used by other components,
124
     * e.g. to set a title for a search column header.
125
     *
126
     * @param GridField $gridField
127
     * @param string $columnName
128
     * @return array - Map of arbitrary metadata identifiers to their values.
129
     */
130
    public function getColumnMetadata($gridField, $columnName)
131
    {
132
        return [];
133
    }
134
135
136
    /**
137
     * A flag provides the user with additional data about the current item
138
     * status, for example a "removed from draft" status. Each item can have
139
     * more than one status flag. Returns a map of a unique key to a
140
     * (localized) title for the flag. The unique key can be reused as a CSS
141
     * class.
142
     *
143
     * Example (simple):
144
     *
145
     * ```php
146
     *   "deletedonlive" => "Deleted"
147
     * ```
148
     *
149
     * Example (with optional title attribute):
150
     *
151
     * ```php
152
     *   "deletedonlive" => array(
153
     *      'text' => "Deleted",
154
     *      'title' => 'This page has been deleted'
155
     *   )
156
     * ```
157
     *
158
     * @param Versioned|DataObject $record - the record to check status for
159
     * @return array
160
     */
161
    protected function getStatusFlags($record)
162
    {
163
        if (!$record->hasExtension(Versioned::class) || !$record->hasStages()) {
164
            return [];
165
        }
166
167
        $flags = [];
168
        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

168
        if ($record->/** @scrutinizer ignore-call */ isOnLiveOnly()) {
Loading history...
169
            $flags['removedfromdraft'] = array(
170
                'text' => _t(__CLASS__ . '.ONLIVEONLYSHORT', 'On live only'),
171
                'title' => _t(__CLASS__ . '.ONLIVEONLYSHORTHELP', 'Item is published, but has been deleted from draft'),
172
            );
173
        } 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

173
        } elseif ($record->/** @scrutinizer ignore-call */ isArchived()) {
Loading history...
174
            $flags['archived'] = array(
175
                'text' => _t(__CLASS__ . '.ARCHIVEDPAGESHORT', 'Archived'),
176
                'title' => _t(__CLASS__ . '.ARCHIVEDPAGEHELP', 'Item is removed from draft and live'),
177
            );
178
        } 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

178
        } elseif ($record->/** @scrutinizer ignore-call */ isOnDraftOnly()) {
Loading history...
179
            $flags['addedtodraft'] = array(
180
                'text' => _t(__CLASS__ . '.ADDEDTODRAFTSHORT', 'Draft'),
181
                'title' => _t(__CLASS__ . '.ADDEDTODRAFTHELP', "Item has not been published yet")
182
            );
183
        } 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

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