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