|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This class represents a single version of a file. Each file can have many |
|
4
|
|
|
* versions, and one of these is currently active at any one time. |
|
5
|
|
|
* |
|
6
|
|
|
* @package silverstripe-versionedfiles |
|
7
|
|
|
*/ |
|
8
|
|
|
class FileVersion extends DataObject |
|
9
|
|
|
{ |
|
10
|
|
|
const VERSION_FOLDER = '_versions'; |
|
11
|
|
|
|
|
12
|
|
|
private static $db = array( |
|
|
|
|
|
|
13
|
|
|
'VersionNumber' => 'Int', |
|
14
|
|
|
'Filename' => 'Varchar(255)' |
|
15
|
|
|
); |
|
16
|
|
|
|
|
17
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
18
|
|
|
'Creator' => 'Member', |
|
19
|
|
|
'File' => 'File' |
|
20
|
|
|
); |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Saves version meta-data, and generates the saved file version on first |
|
24
|
|
|
* write. |
|
25
|
|
|
*/ |
|
26
|
|
|
public function onBeforeWrite() |
|
27
|
|
|
{ |
|
28
|
|
|
if (!$this->isInDB()) { |
|
29
|
|
|
$this->CreatorID = Member::currentUserID(); |
|
|
|
|
|
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (!$this->VersionNumber) { |
|
|
|
|
|
|
33
|
|
|
$versions = DataObject::get( |
|
34
|
|
|
'FileVersion', sprintf('"FileID" = %d', $this->FileID) |
|
|
|
|
|
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
if ($versions) { |
|
38
|
|
|
$this->VersionNumber = $versions->Count() + 1; |
|
|
|
|
|
|
39
|
|
|
} else { |
|
40
|
|
|
$this->VersionNumber = 1; |
|
|
|
|
|
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if (!$this->Filename) { |
|
|
|
|
|
|
45
|
|
|
$this->Filename = $this->saveCurrentVersion(); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
parent::onBeforeWrite(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
public function onBeforeDelete() |
|
52
|
|
|
{ |
|
53
|
|
|
if (file_exists($this->getFullPath())) { |
|
54
|
|
|
unlink($this->getFullPath()); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
parent::onBeforeDelete(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
public function getTitle() |
|
64
|
|
|
{ |
|
65
|
|
|
return sprintf( |
|
66
|
|
|
_t('VersionNumber.VERSIONNUM', "Version %d"), $this->VersionNumber |
|
|
|
|
|
|
67
|
|
|
); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getName() |
|
74
|
|
|
{ |
|
75
|
|
|
return basename($this->Filename); |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getURL() |
|
82
|
|
|
{ |
|
83
|
|
|
return Controller::join_links(Director::baseURL(), $this->Filename); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return string |
|
88
|
|
|
*/ |
|
89
|
|
|
public function getFullPath() |
|
90
|
|
|
{ |
|
91
|
|
|
return Director::baseFolder() . $this->Filename; |
|
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Returns a Boolean object indicating if this version is currently active. |
|
96
|
|
|
* |
|
97
|
|
|
* @return Boolean |
|
98
|
|
|
*/ |
|
99
|
|
|
public function IsCurrent() |
|
100
|
|
|
{ |
|
101
|
|
|
return DBField::create_field( |
|
102
|
|
|
'Boolean', ($this->File()->CurrentVersionID == $this->ID) |
|
|
|
|
|
|
103
|
|
|
); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Saves the current version of the linked File object in a versions |
|
108
|
|
|
* directory, then returns the relative path to where it is stored. |
|
109
|
|
|
* |
|
110
|
|
|
* @return string |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function saveCurrentVersion() |
|
113
|
|
|
{ |
|
114
|
|
|
// Ensure we re-fetch fresh record from database (in case it was recently renamed / moved) |
|
115
|
|
|
$file = File::get()->byID($this->FileID); |
|
|
|
|
|
|
116
|
|
|
if ($file->ParentID) { |
|
117
|
|
|
$base = Controller::join_links( |
|
118
|
|
|
$file->Parent()->getFullPath(), |
|
|
|
|
|
|
119
|
|
|
self::VERSION_FOLDER, |
|
120
|
|
|
$this->FileID |
|
|
|
|
|
|
121
|
|
|
); |
|
122
|
|
|
} else { |
|
123
|
|
|
$base = Controller::join_links( |
|
124
|
|
|
Director::baseFolder(), |
|
125
|
|
|
ASSETS_DIR, |
|
126
|
|
|
self::VERSION_FOLDER, |
|
127
|
|
|
$this->FileID |
|
|
|
|
|
|
128
|
|
|
); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
Filesystem::makeFolder($base); |
|
132
|
|
|
|
|
133
|
|
|
$extension = $file->getExtension(); |
|
|
|
|
|
|
134
|
|
|
$basename = basename($file->Name, $extension); |
|
135
|
|
|
$version = $this->VersionNumber; |
|
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
$cachedPath = Controller::join_links( |
|
138
|
|
|
$base, "{$basename}$version.$extension" |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
if (!copy($file->getFullPath(), $cachedPath)) { |
|
142
|
|
|
throw new Exception( |
|
143
|
|
|
"Unable to copy version #$version of file #$this->FileID from:\n \"{$file->getFullPath()}\"\nto:\n\"$cachedPath\"" |
|
|
|
|
|
|
144
|
|
|
); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return Director::makeRelative($cachedPath); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|