Completed
Pull Request — master (#14)
by Simon
01:57
created

LogEntry::canView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SilverLeague\LogViewer\Model;
4
5
use SilverLeague\LogViewer\Helper\DeprecatedLogFormatter;
6
use SilverLeague\LogViewer\Helper\LogFormatter;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\Forms\HeaderField;
9
use SilverStripe\Forms\LiteralField;
10
use SilverStripe\ORM\DataObject;
11
use SilverStripe\Security\Permission;
12
use SilverStripe\Security\PermissionProvider;
13
14
/**
15
 * A LogEntry is a set of data provided from Monolog via the DataObjectHandler
16
 *
17
 * @package silverstripe-logviewer
18
 * @author  Robbie Averill <[email protected]>
19
 * @author  Simon Erkelens <[email protected]>
20
 *
21
 * @property string $Entry
22
 * @property string $Level
23
 */
24
class LogEntry extends DataObject implements PermissionProvider
25
{
26
    /**
27
     * {@inheritDoc}
28
     */
29
    private static $table_name = 'LogEntry';
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $table_name is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
30
31
32
    /**
33
     * @var bool
34
     */
35
    private static $format_entry = true;
0 ignored issues
show
Unused Code introduced by
The property $format_entry is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    private static $db = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
41
        'Entry' => 'Text',
42
        'Level' => 'Varchar'
43
    ];
44
45
    /**
46
     * {@inheritDoc}
47
     */
48
    private static $summary_fields = [
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $summary_fields is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
49
        'GridfieldSummary' => 'Entry',
50
        'Created'          => 'Created',
51
        'Level'            => 'Level'
52
    ];
53
54
    /**
55
     * @inheritdoc
56
     * @return \SilverStripe\Forms\FieldList
57
     */
58
    public function getCMSFields()
59
    {
60
        $config = self::config()->get('format_entry');
61
        $fields = parent::getCMSFields();
62
        if ($config) { // Default setting, format the entry
63
            $fields->addFieldToTab('Root.Main', HeaderField::create('EntryHeading', 'Entry', 3));
64
            $fields->removeByName(array('Entry'));
65
            $entry = $this->buildEntry();
66
            $fields->addFieldToTab('Root.Main', LiteralField::create('FormattedEntry', $entry));
67
        } else { // Just move the field after the Level
68
            $entryField = $fields->dataFieldByName('Entry');
69
            if ($entryField !== null) {
70
                $fields->insertAfter('Level', $entryField);
71
            }
72
        }
73
74
        return $fields;
75
    }
76
77
    private function buildEntry()
78
    {
79
        $text = $this->Entry;
80
        $asArray = Convert::json2array($text);
81
        // Inline styling for now
82
        $entry = "<pre class='form-control-static logentry-entry' style='white-space: normal; max-width: 85%;'>";
83
        if (!is_array($asArray)) {
84
            /** @deprecated We're falling back to the legacy text-only error */
85
            $entry .= DeprecatedLogFormatter::formatLegacyEntry($text);
86
        } else {
87
            $entry .= LogFormatter::entryToUl($asArray, false);
88
        }
89
        $entry .= '</pre>';
90
        $entry = nl2br($entry);
91
92
        return $entry;
93
    }
94
95
    /**
96
     * @param int $length Length of the summary
97
     * @return string shortened string of the entry if too long.
98
     */
99
    public function getGridfieldSummary($length = 300)
100
    {
101
        $elipsis = strlen($this->Entry) > $length ? '...' : '';
102
103
        return substr($this->Entry, 0, $length) . $elipsis;
104
    }
105
106
    /**
107
     * Permissions
108
     */
109
    public function providePermissions()
110
    {
111
        return array(
112
            'DELETE_ENTRY' => array(
113
                'name'     => _t('LogEntry.PERMISSION_DELETE_DESCRIPTION', 'Delete log entries'),
114
                'category' => _t('Permissions.LOGENTRY_CATEGORY', 'Log entry permissions'),
115
                'help'     => _t('LogEntry.PERMISSION_DELETE_HELP', 'Permission required to delete existing log entries.')
116
            ),
117
            'VIEW_ENTRY'   => array(
118
                'name'     => _t('LogEntry.PERMISSION_VIEW_DESCRIPTION', 'View log entries'),
119
                'category' => _t('Permissions.LOGENTRY_CATEGORY', 'Log entry permissions'),
120
                'help'     => _t('LogEntry.PERMISSION_VIEW_HELP', 'Permission required to view existing log entries.')
121
            ),
122
        );
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function canCreate($member = null, $context = [])
129
    {
130
        return false;
131
    }
132
133
    /**
134
     * We should never edit log entries
135
     *
136
     * {@inheritDoc}
137
     */
138
    public function canEdit($member = null)
139
    {
140
        return false;
141
    }
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function canDelete($member = null)
146
    {
147
        return Permission::checkMember($member, array('DELETE_ENTRY', 'CMS_ACCESS_LogViewerAdmin'));
148
    }
149
150
    /**
151
     * {@inheritdoc}
152
     */
153
    public function canView($member = null)
154
    {
155
        return Permission::checkMember($member, array('VIEW_ENTRY', 'CMS_ACCESS_LogViewerAdmin'));
156
    }
157
158
}
159