1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\AssetAdmin\Controller; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\Control\Director; |
7
|
|
|
use SilverStripe\ORM\DataExtension; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Update File dataobjects to be editable in this asset admin |
11
|
|
|
* |
12
|
|
|
* @property File $owner |
13
|
|
|
*/ |
14
|
|
|
class AssetAdminFile extends DataExtension |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Max width for inserted images |
18
|
|
|
* |
19
|
|
|
* @config |
20
|
|
|
* @var int |
21
|
|
|
*/ |
22
|
|
|
private static $insert_width = 600; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Max height for inserted images |
26
|
|
|
* |
27
|
|
|
* @config |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private static $insert_height = 400; |
|
|
|
|
31
|
|
|
|
32
|
|
|
public function updateCMSEditLink(&$link) |
33
|
|
|
{ |
34
|
|
|
// Update edit link for this file to point to the new asset admin |
35
|
|
|
$controller = AssetAdmin::singleton(); |
36
|
|
|
$link = Director::absoluteURL($controller->getFileEditLink($this->owner)); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Calculate width to insert into html area |
41
|
|
|
* |
42
|
|
|
* @return int|null |
43
|
|
|
*/ |
44
|
|
|
public function getInsertWidth() { |
45
|
|
|
$size = $this->getInsertDimensions(); |
46
|
|
|
return $size ? $size['width'] : null; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Calculate width to insert into html area |
51
|
|
|
* |
52
|
|
|
* @return int |
53
|
|
|
*/ |
54
|
|
|
public function getInsertHeight() { |
55
|
|
|
$size = $this->getInsertDimensions(); |
56
|
|
|
return $size ? $size['height'] : null; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get dimensions of this image sized within insert_width x insert_height |
61
|
|
|
* |
62
|
|
|
* @return array|null |
63
|
|
|
*/ |
64
|
|
|
protected function getInsertDimensions() { |
65
|
|
|
$dimensions = $this->owner->getDimensions('array'); |
66
|
|
|
if (!$dimensions) { |
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
list ($width, $height) = $dimensions; |
71
|
|
|
$maxWidth = $this->owner->config()->get('insert_width'); |
72
|
|
|
$maxHeight = $this->owner->config()->get('insert_height'); |
73
|
|
|
|
74
|
|
|
// Within bounds |
75
|
|
|
if ($width < $maxWidth && $height < $maxHeight) { |
76
|
|
|
return [ |
77
|
|
|
'width' => $width, |
78
|
|
|
'height' => $height, |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Check if sizing by height or width |
83
|
|
|
if( ($width * $maxHeight) < ($height * $maxWidth) ) { |
84
|
|
|
// Size by height |
85
|
|
|
return [ |
86
|
|
|
'width' => intval(($width * $maxHeight) / $height + 0.5), |
87
|
|
|
'height' => $maxHeight, |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
} else { |
91
|
|
|
// Size by width |
92
|
|
|
return [ |
93
|
|
|
'width' => $maxWidth, |
94
|
|
|
'height' => intval(($height * $maxWidth) / $width + 0.5), |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.