Test Setup Failed
Push — master ( 210134...c17796 )
by Damian
03:18
created

src/Forms/GridField/GridFieldVersionedState.php (1 issue)

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