|
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
|
|
|
use SilverStripe\ORM\Versioning\Versioned; |
|
9
|
|
|
use SilverStripe\ORM\Versioning\DataDifferencer; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Update File dataobjects to be editable in this asset admin |
|
13
|
|
|
* |
|
14
|
|
|
* @property File $owner |
|
15
|
|
|
*/ |
|
16
|
|
|
class AssetAdminFile extends DataExtension |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Max width for inserted images |
|
20
|
|
|
* |
|
21
|
|
|
* @config |
|
22
|
|
|
* @var int |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $insert_width = 600; |
|
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Max height for inserted images |
|
28
|
|
|
* |
|
29
|
|
|
* @config |
|
30
|
|
|
* @var int |
|
31
|
|
|
*/ |
|
32
|
|
|
private static $insert_height = 400; |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function updateCMSEditLink(&$link) |
|
35
|
|
|
{ |
|
36
|
|
|
// Update edit link for this file to point to the new asset admin |
|
37
|
|
|
$controller = AssetAdmin::singleton(); |
|
38
|
|
|
$link = Director::absoluteURL($controller->getFileEditLink($this->owner)); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Calculate width to insert into html area |
|
43
|
|
|
* |
|
44
|
|
|
* @return int|null |
|
45
|
|
|
*/ |
|
46
|
|
|
public function getInsertWidth() { |
|
47
|
|
|
$size = $this->getInsertDimensions(); |
|
48
|
|
|
return $size ? $size['width'] : null; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Calculate width to insert into html area |
|
53
|
|
|
* |
|
54
|
|
|
* @return int |
|
55
|
|
|
*/ |
|
56
|
|
|
public function getInsertHeight() { |
|
57
|
|
|
$size = $this->getInsertDimensions(); |
|
58
|
|
|
return $size ? $size['height'] : null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get dimensions of this image sized within insert_width x insert_height |
|
63
|
|
|
* |
|
64
|
|
|
* @return array|null |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function getInsertDimensions() { |
|
67
|
|
|
$dimensions = $this->owner->getDimensions('array'); |
|
68
|
|
|
if (!$dimensions) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
list ($width, $height) = $dimensions; |
|
73
|
|
|
$maxWidth = $this->owner->config()->get('insert_width'); |
|
74
|
|
|
$maxHeight = $this->owner->config()->get('insert_height'); |
|
75
|
|
|
|
|
76
|
|
|
// Within bounds |
|
77
|
|
|
if ($width < $maxWidth && $height < $maxHeight) { |
|
78
|
|
|
return [ |
|
79
|
|
|
'width' => $width, |
|
80
|
|
|
'height' => $height, |
|
81
|
|
|
]; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// Check if sizing by height or width |
|
85
|
|
|
if( ($width * $maxHeight) < ($height * $maxWidth) ) { |
|
86
|
|
|
// Size by height |
|
87
|
|
|
return [ |
|
88
|
|
|
'width' => intval(($width * $maxHeight) / $height + 0.5), |
|
89
|
|
|
'height' => $maxHeight, |
|
90
|
|
|
]; |
|
91
|
|
|
|
|
92
|
|
|
} else { |
|
93
|
|
|
// Size by width |
|
94
|
|
|
return [ |
|
95
|
|
|
'width' => $maxWidth, |
|
96
|
|
|
'height' => intval(($height * $maxWidth) / $width + 0.5), |
|
97
|
|
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param int $from |
|
103
|
|
|
* @param int $to |
|
104
|
|
|
* |
|
105
|
|
|
* @return string |
|
106
|
|
|
*/ |
|
107
|
|
|
public function humanizedChanges($from, $to) { |
|
108
|
|
|
if(!$from) { |
|
109
|
|
|
return _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdmin.UPLOADEDFILE', "Uploaded file"); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
$fromRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $from); |
|
113
|
|
|
$toRecord = Versioned::get_version($this->owner->class, $this->owner->ID, $to); |
|
114
|
|
|
|
|
115
|
|
|
$diff = new DataDifferencer($fromRecord, $toRecord); |
|
116
|
|
|
$changes = $diff->changedFieldNames(); |
|
117
|
|
|
|
|
118
|
|
|
$k = array_search('LastEdited', $changes); |
|
119
|
|
|
|
|
120
|
|
|
if($k !== false) { |
|
121
|
|
|
unset($changes[$k]); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$output = array(); |
|
125
|
|
|
|
|
126
|
|
|
foreach($changes as $change) { |
|
127
|
|
|
$human = $change; |
|
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
if($change == "ParentID") { |
|
130
|
|
|
// updated folder ID |
|
131
|
|
|
$human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.MOVEDFOLDER', "Moved file"); |
|
132
|
|
|
} else if($change == 'Title') { |
|
133
|
|
|
$human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDTITLE', "Updated title to ") . $fromRecord->Title; |
|
134
|
|
|
} else if($change == 'Name') { |
|
135
|
|
|
$human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDFILE', "Renamed file to ") . $fromRecord->Filename; |
|
136
|
|
|
} else if($change == 'File') { |
|
137
|
|
|
// check to make sure the files are actually different |
|
138
|
|
|
if($fromRecord->getHash() != $toRecord->getHash()) { |
|
139
|
|
|
$human = _t('SilverStripe\\AssetAdmin\\Controller\\AssetAdminFile.RENAMEDFILE', "Replaced file"); |
|
140
|
|
|
} else { |
|
141
|
|
|
$human = false; |
|
142
|
|
|
} |
|
143
|
|
|
} else { |
|
144
|
|
|
$human = false; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if($human) { |
|
|
|
|
|
|
148
|
|
|
$output[] = $human; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return implode(", ", $output); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.