Completed
Pull Request — master (#14)
by Simon
02:03
created

LogEntry::getCMSFields()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 0
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
12
/**
13
 * A LogEntry is a set of data provided from Monolog via the DataObjectHandler
14
 *
15
 * @package silverstripe-logviewer
16
 * @author  Robbie Averill <[email protected]>
17
 * @author  Simon Erkelens <[email protected]>
18
 *
19
 * @property string $Entry
20
 * @property string $Level
21
 */
22
class LogEntry extends DataObject
23
{
24
    /**
25
     * {@inheritDoc}
26
     */
27
    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...
28
29
30
    /**
31
     * @var bool
32
     */
33
    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...
34
35
    /**
36
     * {@inheritDoc}
37
     */
38
    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...
39
        'Entry' => 'Text',
40
        'Level' => 'Varchar'
41
    ];
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    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...
47
        'GridfieldSummary' => 'Entry',
48
        'Created'          => 'Created',
49
        'Level'            => 'Level'
50
    ];
51
52
    /**
53
     * We should never need to edit log entries
54
     *
55
     * {@inheritDoc}
56
     */
57
    public function canEdit($member = false, $context = [])
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
58
    {
59
        return false;
60
    }
61
62
    /**
63
     * @inheritdoc
64
     * @return \SilverStripe\Forms\FieldList
65
     */
66
    public function getCMSFields()
67
    {
68
        $config = self::config()->get('format_entry');
69
        $fields = parent::getCMSFields();
70
        if ($config) { // Default setting, format the entry
71
            $fields->addFieldToTab('Root.Main', HeaderField::create('EntryHeading', 'Entry', 3));
72
            $fields->removeByName(array('Entry'));
73
            $entry = $this->buildEntry();
74
            $fields->addFieldToTab('Root.Main', LiteralField::create('FormattedEntry', $entry));
75
        } else { // Just move the field after the Level
76
            $entryField = $fields->dataFieldByName('Entry');
77
            if ($entryField !== null) {
78
                $fields->insertAfter('Level', $entryField);
79
            }
80
        }
81
82
        return $fields;
83
    }
84
85
    private function buildEntry()
86
    {
87
        $text = $this->Entry;
88
        $asArray = Convert::json2array($text);
89
        // Inline styling for now
90
        $entry = "<pre class='form-control-static logentry-entry' style='white-space: normal; max-width: 85%;'>";
91
        if (!is_array($asArray)) {
92
            /** @deprecated We're falling back to the legacy text-only error */
93
            $entry .= DeprecatedLogFormatter::formatLegacyEntry($text);
94
        } else {
95
            $entry .= LogFormatter::entryToUl($asArray, false);
96
        }
97
        $entry .= '</pre>';
98
        $entry = nl2br($entry);
99
100
        return $entry;
101
    }
102
103
    /**
104
     * @param int $length Length of the summary
105
     * @return string shortened string of the entry if too long.
106
     */
107
    public function getGridfieldSummary($length = 300)
108
    {
109
        $elipsis = strlen($this->Entry) > $length ? '...' : '';
110
111
        return substr($this->Entry, 0, $length) . $elipsis;
112
    }
113
}
114