1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\ElementalBlocks\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
|
|
|
|
10
|
|
|
class FileBlock extends BaseElement |
11
|
|
|
{ |
12
|
|
|
private static $has_one = [ |
|
|
|
|
13
|
|
|
'File' => File::class, |
14
|
|
|
]; |
15
|
|
|
|
16
|
|
|
private static $owns = [ |
|
|
|
|
17
|
|
|
'File', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
private static $singular_name = 'file'; |
|
|
|
|
21
|
|
|
|
22
|
|
|
private static $plural_name = 'files'; |
|
|
|
|
23
|
|
|
|
24
|
|
|
private static $icon = 'font-icon-block-file'; |
|
|
|
|
25
|
|
|
|
26
|
|
|
public function getType() |
27
|
|
|
{ |
28
|
|
|
return _t(__CLASS__ . '.BlockType', 'File'); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function getSummary() |
32
|
|
|
{ |
33
|
|
|
if ($this->File() && $this->File()->exists()) { |
|
|
|
|
34
|
|
|
return $this->getSummaryThumbnail() . $this->File()->Title; |
35
|
|
|
} |
36
|
|
|
return ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Return a thumbnail of the file, if it's an image. Used in GridField preview summaries. |
41
|
|
|
* |
42
|
|
|
* @return string|null |
43
|
|
|
*/ |
44
|
|
|
public function getSummaryThumbnail() |
45
|
|
|
{ |
46
|
|
|
if ($this->File() && $this->File()->exists()) { |
47
|
|
|
$template = '<span class="elemental-preview__thumbnail-image">%s</span>'; |
48
|
|
|
|
49
|
|
|
if ($this->File()->getIsImage()) { |
50
|
|
|
// Stretch to maximum of 36px either way then trim the extra off |
51
|
|
|
if ($this->File()->getOrientation() === Image_Backend::ORIENTATION_PORTRAIT) { |
52
|
|
|
$image = $this->File()->ScaleWidth(36)->CropHeight(36); |
53
|
|
|
} else { |
54
|
|
|
$image = $this->File()->ScaleHeight(36)->CropWidth(36); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return sprintf($template, $image); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$placeholderFilename = ModuleResourceLoader::resourceURL( |
61
|
|
|
'silverstripe/framework:client/images/app_icons/document_92.png' |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return sprintf( |
65
|
|
|
$template, |
66
|
|
|
'<img alt="' . _t(__CLASS__ . '.BlockType', 'File') . '" src="' . $placeholderFilename . '"' |
67
|
|
|
. ' class="elemental-preview__thumbnail-image--placeholder" />' |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.