Passed
Push — hans/core-specific-configs ( 7670cf )
by Simon
09:28
created

SolrLog::getCMSFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * class SolrLog|Firesphere\SolrSearch\Models\SolrLog Solr logging to be read from the CMS
4
 *
5
 * @package Firesphere\SolrSearch\Models
6
 * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo
7
 * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy
8
 */
9
10
namespace Firesphere\SolrSearch\Models;
11
12
use SilverStripe\Control\Director;
13
use SilverStripe\ORM\DataObject;
14
use SilverStripe\ORM\FieldType\DBDatetime;
15
use SilverStripe\Security\Member;
16
use SilverStripe\Security\PermissionProvider;
17
18
/**
19
 * Class \Firesphere\SolrSearch\Models\SolrError
20
 *
21
 * @package Firesphere\SolrSearch\Models
22
 * @property string $Timestamp
23
 * @property string $Index
24
 * @property string $Type
25
 * @property string $Level
26
 * @property string $Message
27
 */
28
class SolrLog extends DataObject implements PermissionProvider
29
{
30
    /**
31
     * Used to give the Gridfield rows a corresponding colour
32
     *
33
     * @var array
34
     */
35
    protected static $row_color = [
36
        'ERROR' => 'alert alert-danger',
37
        'WARN'  => 'alert alert-warning',
38
        'INFO'  => 'alert alert-info',
39
    ];
40
    /**
41
     * @var string Database table name
42
     */
43
    private static $table_name = 'SolrLog';
44
    /**
45
     * @var array Database columns
46
     */
47
    private static $db = [
48
        'Timestamp' => 'Datetime',
49
        'Index'     => 'Varchar(255)',
50
        'Type'      => 'Enum("Config,Index,Query")',
51
        'Level'     => 'Varchar(10)',
52
        'Message'   => 'Text',
53
    ];
54
    /**
55
     * @var array Core this log entry belongs too
56
     */
57
    private static $has_one = [
58
        'SolrCore' => SolrCore::class
59
    ];
60
    /**
61
     * @var array Summary fields
62
     */
63
    private static $summary_fields = [
64
        'Timestamp',
65
        'Index',
66
        'Type',
67
        'Level',
68
    ];
69
    /**
70
     * @var array Searchable fields
71
     */
72
    private static $searchable_fields = [
73
        'Created',
74
        'Timestamp',
75
        'Index',
76
        'Type',
77
        'Level',
78
    ];
79
    /**
80
     * @var array Timestamp is indexed
81
     */
82
    private static $indexes = [
83
        'Timestamp' => true,
84
    ];
85
    /**
86
     * @var string Default sort
87
     */
88
    private static $default_sort = 'Timestamp DESC';
89
90
    public function getCMSFields()
91
    {
92
        $fields = parent::getCMSFields();
93
        $fields->removeByName(['SolrCoreID', 'SolrCore']);
94
95
        return $fields;
96
    }
97
98
    /**
99
     * Convert the Timestamp to a DBDatetime for compatibility
100
     */
101 35
    public function onBeforeWrite()
102
    {
103 35
        parent::onBeforeWrite();
104
105 35
        $this->Timestamp = DBDatetime::create()->setValue(strtotime($this->Timestamp));
106 35
    }
107
108
    /**
109
     * Return the first line of this log item error
110
     *
111
     * @return string
112
     */
113 1
    public function getLastErrorLine()
114
    {
115 1
        $lines = explode(PHP_EOL, $this->Message);
116
117 1
        return $lines[0];
118
    }
119
120
    /**
121
     * Not creatable by users
122
     *
123
     * @param null|Member $member
124
     * @param array $context
125
     * @return bool|mixed
126
     */
127 1
    public function canCreate($member = null, $context = [])
128
    {
129 1
        return false;
130
    }
131
132
    /**
133
     * Not editable by users
134
     *
135
     * @param null|Member $member
136
     * @return bool|mixed
137
     */
138 1
    public function canEdit($member = null)
139
    {
140 1
        return false;
141
    }
142
143
    /**
144
     * Member has view access?
145
     *
146
     * @param null|Member $member
147
     * @return bool|mixed
148
     */
149 36
    public function canView($member = null)
150
    {
151 36
        return parent::canView($member);
152
    }
153
154
    /**
155
     * Only deleteable by admins or when in dev mode to clean up
156
     *
157
     * @param null|Member $member
158
     * @return bool|mixed
159
     */
160 1
    public function canDelete($member = null)
161
    {
162 1
        if ($member) {
163 1
            return $member->inGroup('administrators') || Director::isDev();
164
        }
165
166 1
        return parent::canDelete($member) || Director::isDev();
167
    }
168
169
    /**
170
     * Get the extra classes to colour the gridfield rows
171
     *
172
     * @return mixed|string
173
     */
174 1
    public function getExtraClass()
175
    {
176 1
        $classMap = static::$row_color;
177
178 1
        return $classMap[$this->Level] ?? 'alert alert-info';
179
    }
180
181
182
    /**
183
     * Return a map of permission codes to add to the dropdown shown in the Security section of the CMS.
184
     * array(
185
     *   'VIEW_SITE' => 'View the site',
186
     * );
187
     *
188
     * @return array
189
     */
190 1
    public function providePermissions()
191
    {
192
        return [
193
            'DELETE_LOG' => [
194 1
                'name'     => _t(self::class . '.PERMISSION_DELETE_DESCRIPTION', 'Delete Solr logs'),
195 1
                'category' => _t('Permissions.LOGS_CATEGORIES', 'Solr logs permissions'),
196 1
                'help'     => _t(
197 1
                    self::class . '.PERMISSION_DELETE_HELP',
198 1
                    'Permission required to delete existing Solr logs.'
199
                ),
200
            ],
201
            'VIEW_LOG'   => [
202 1
                'name'     => _t(self::class . '.PERMISSION_VIEW_DESCRIPTION', 'View Solr logs'),
203 1
                'category' => _t('Permissions.LOGS_CATEGORIES', 'Solr logs permissions'),
204 1
                'help'     => _t(
205 1
                    self::class . '.PERMISSION_VIEW_HELP',
206 1
                    'Permission required to view existing Solr logs.'
207
                ),
208
            ],
209
        ];
210
    }
211
}
212