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