Completed
Push — master ( 3b23c0...1ceb95 )
by Damian
9s
created

FileHistoryFormFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 7
dl 0
loc 66
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getForm() 0 6 1
B getSpecsMarkup() 0 29 4
A getFormFields() 0 16 1
A getFormActions() 0 8 1
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Forms;
4
5
use SilverStripe\Assets\File;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Core\Convert;
8
use SilverStripe\Forms\FieldList;
9
use SilverStripe\Forms\FormFactory;
10
use SilverStripe\Forms\LiteralField;
11
use SilverStripe\Forms\ReadonlyField;
12
13
class FileHistoryFormFactory extends FileFormFactory
14
{
15
    public function getForm(Controller $controller, $name = FormFactory::DEFAULT_NAME, $context = [])
16
    {
17
        $form = parent::getForm($controller, $name, $context);
18
        $form->makeReadonly();
19
        return $form;
20
    }
21
22
    protected function getSpecsMarkup($record)
23
    {
24
        if (!$record || !$record->isInDB()) {
25
            return null;
26
        }
27
        /**
28
         * Can remove .label and .label-info when Bootstrap has been updated to BS4 Beta
29
         * .label is being replaced with .tag
30
         */
31
        $versionTag = sprintf(
32
            '<span class="label label-info tag tag-info">v.%s</span>',
33
            $record->Version
34
        );
35
        $agoTag = sprintf(
36
            '%s <time class="relative-time" title="%s">%s</time>',
37
            $record->WasPublished
38
                ? _t('SilverStripe\\AssetAdmin\\Forms\\FileHistoryFormFactory.PUBLISHED', 'Published')
39
                : _t('SilverStripe\\AssetAdmin\\Forms\\FileHistoryFormFactory.SAVED', 'Saved'),
40
            Convert::raw2att($record->LastEdited),
41
            Convert::raw2xml($record->dbObject('LastEdited')->Ago())
42
        );
43
        return sprintf(
44
            '<div class="editor__specs">%s %s, %s %s</div>',
45
            $versionTag,
46
            $agoTag,
47
            $record->getSize(),
48
            $this->getStatusFlagMarkup($record)
49
        );
50
    }
51
52
    protected function getFormFields(Controller $controller, $name, $context = [])
53
    {
54
        $record = $context['Record'];
55
56
        $fields = new FieldList(
57
            LiteralField::create('Thumbnail', $this->getIconMarkup($record)),
58
            LiteralField::create('FileSpecs', $this->getSpecsMarkup($record)),
59
            ReadonlyField::create("Title", File::singleton()->fieldLabel('Title')),
60
            ReadonlyField::create('Name', File::singleton()->fieldLabel('Filename')),
61
            ReadonlyField::create("Path", _t('AssetTableField.PATH', 'Path'), $this->getPath($record))
62
        );
63
64
        $this->invokeWithExtensions('updateFormFields', $fields, $controller, $name, $context);
65
66
        return $fields;
67
    }
68
69
70
    protected function getFormActions(Controller $controller, $name, $context = [])
71
    {
72
        $actions = new FieldList();
73
        // Update
74
        $this->invokeWithExtensions('updateFormActions', $actions, $controller, $name, $context);
75
76
        return $actions;
77
    }
78
}
79