Completed
Pull Request — master (#4)
by
unknown
03:09
created

FileBlock::getSummaryData()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ElementalFileBlock\Block;
4
5
use DNADesign\Elemental\Models\BaseElement;
6
use SilverStripe\Assets\File;
7
use SilverStripe\Assets\Image_Backend;
8
use SilverStripe\Core\Manifest\ModuleResourceLoader;
9
use SilverStripe\ORM\FieldType\DBHTMLText;
10
11
class FileBlock extends BaseElement
12
{
13
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
14
        'File' => File::class,
15
    ];
16
17
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
18
        'File',
19
    ];
20
21
    private static $singular_name = 'file';
0 ignored issues
show
introduced by
The private property $singular_name is not used, and could be removed.
Loading history...
22
23
    private static $plural_name = 'files';
0 ignored issues
show
introduced by
The private property $plural_name is not used, and could be removed.
Loading history...
24
25
    private static $icon = 'font-icon-block-file';
0 ignored issues
show
introduced by
The private property $icon is not used, and could be removed.
Loading history...
26
27
    private static $table_name = 'S_EB_FileBlock';
0 ignored issues
show
introduced by
The private property $table_name is not used, and could be removed.
Loading history...
28
29
    public function getType()
30
    {
31
        return _t(__CLASS__ . '.BlockType', 'File');
32
    }
33
34
    public function getSummary()
35
    {
36
        if ($this->File() && $this->File()->exists()) {
0 ignored issues
show
Bug introduced by
The method File() does not exist on SilverStripe\ElementalFileBlock\Block\FileBlock. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        if ($this->/** @scrutinizer ignore-call */ File() && $this->File()->exists()) {
Loading history...
37
            return $this->getSummaryThumbnail() . $this->File()->Title;
38
        }
39
        return '';
40
    }
41
42
    /**
43
     * Return file title and thumbnail for summary section of ElementEditor
44
     *
45
     * @return array
46
     */
47
    public function getSummaryData()
48
    {
49
        $summaryData = [];
50
        if ($this->File() && $this->File()->exists() && $this->File()->getIsImage()) {
51
            $summaryData["fileURL"] = $this->File()->CMSThumbnail()->getURL();
52
            $summaryData["fileTitle"] = $this->File()->getTitle();
53
        }
54
        return $summaryData;
55
    }
56
57
    /**
58
     * Return a thumbnail of the file, if it's an image. Used in GridField preview summaries.
59
     *
60
     * @return DBHTMLText
61
     */
62
    public function getSummaryThumbnail()
63
    {
64
        $data = [];
65
66
        if ($this->File() && $this->File()->exists()) {
67
            if ($this->File()->getIsImage()) {
68
                // Stretch to maximum of 36px either way then trim the extra off
69
                if ($this->File()->getOrientation() === Image_Backend::ORIENTATION_PORTRAIT) {
70
                    $data['Image'] = $this->File()->ScaleWidth(36)->CropHeight(36);
71
                } else {
72
                    $data['Image'] = $this->File()->ScaleHeight(36)->CropWidth(36);
73
                }
74
            } else {
75
                $data = [
76
                    'Image' => ModuleResourceLoader::resourceURL(
77
                        'silverstripe/framework:client/images/app_icons/document_92.png'
78
                    ),
79
                    'IsPlaceholder' => true
80
                ];
81
            }
82
        }
83
84
        return $this->customise($data)->renderWith(__CLASS__ . '/SummaryThumbnail');
85
    }
86
}
87