-
Notifications
You must be signed in to change notification settings - Fork 2
/
klpbcserverfilevideoinputtype.php
261 lines (231 loc) · 7.36 KB
/
klpbcserverfilevideoinputtype.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
<?php
/**
* File containing the klpBcServerFileVideoInputType class
*
* @copyright Copyright (C) 1999-2013 by Kaliop. All rights reserved. http://www.kaliop.com
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
*/
/**
* Class for supporting videos via arbitrary ServerFiles
*/
class klpBcServerFileVideoInputType implements klpBcVideoInputTypeInterface
{
/**
* Creates a new instance of this class
*
* @param string $identifier String that identifies this input type
*/
public function __construct( $identifier, $config = null )
{
$this->tr = 'extension/klpbc/inputtype/serverfile';
$this->identifier = $identifier;
$this->lastError = '';
$this->serverfileInstance = new klpBcServerFile( false );
if ( !$config )
$config = klpDiContainer::getInstance()->getServerFileConfig();
$this->config = $config;
}
/**
* Validates input data (typically form data)
*
* @param array $data Array of POST data
* @param bool $isRequired Whether data is required to be present or not
* @param bool $hasOriginalData True if we've already have stored some data
* @return bool
*/
public function isInputDataValid( $data, $isRequired, $hasOriginalData )
{
if ( empty( $data ) && $isRequired )
return $this->error( ezpI18n::tr( $this->tr, "An URL is required" ) );
if ( empty( $data ) && !$isRequired )
return true;
if ( !isset( $data['serverfile'] ) && $isRequired )
return $this->error( ezpI18n::tr( $this->tr, "Missing URL" ) );
if ( empty( $data['serverfile'] ) && $isRequired )
return $this->error( ezpI18n::tr( $this->tr, "Missing URL" ) );
return true;
}
/**
* Returns the last validation error
*
* @return string Validation error or '' if no error
*/
public function lastError()
{
return $this->lastError;
}
/**
* Fetch binary file by content object attribute id and version
*
* @param int $id Content object attribute id
* @param int $version Content object attribute version
* @return object
*/
public function fetch( $id, $version )
{
return $this->runServerFileMethod( 'fetch', $id, $version );
}
/**
* Initializes new value for a new version
*
* @param int $id Content object attribute id
* @param int $currentVersion Current content object attribute version
* @param int $newVersion New content object attribute version
*/
public function initialize( $id, $currentVersion, $newVersion )
{
$oldServerFile = $this->runServerFileMethod( 'fetch', $id, $currentVersion );
if ( $oldServerFile )
{
$oldServerFile->setAttribute( 'contentobject_attribute_id', $id );
$oldServerFile->setAttribute( "version", $newVersion );
$oldServerFile->store();
}
}
/**
* Stores the uploaded file
*
* @param array $data
* @param int $id Content object attribute id
* @param int $version Content object attribute version
* @return bool True if file was stored, false if not.
*/
public function store( $data, $id, $version )
{
if ( !isset( $data['serverfile'] ) )
return false;
$serverfile = $this->fetch( $id, $version );
if ( !$serverfile )
$serverfile = $this->serverfileInstance;
$serverfile->setAttribute( 'contentobject_attribute_id', $id );
$serverfile->setAttribute( 'version', $version );
$serverfile->setAttribute( 'filepath', $data['serverfile'] );
$serverfile->store();
return true;
}
/**
* Deletes the uploaded video and any references to it
*
* @param int $id Content object attribute id
* @param int|null $version Content object attribute version
**/
public function delete( $id, $version )
{
$this->runServerFileMethod( 'delete', $id, $version );
}
/**
* Stores a serverfile
*
* @param int $id Id of attribute to associate the file with
* @param int $version Which version to associate the file with
* @param string $serverfile ServerFile to video
* @return bool False if file could not be stored
*/
public function fromString( $id, $version, $serverfile )
{
$this->store( array( 'serverfile' => $serverfile ), $id, $version );
}
/**
* Returns path to the original video file
*
* @param int $id Content object attribute id
* @param int $version Content object attribute version
* @return string or null if there's no video file
*/
public function getFileUrl( $id, $version )
{
$file = $this->fetch( $id, $version );
if ( !$file )
return null;
$rootDir = $this->config->rootDirectory;
$rootDir = rtrim( $rootDir, DIRECTORY_SEPARATOR );
$filePath = ltrim( $file->attribute( 'filepath' ), DIRECTORY_SEPARATOR );
$path = $rootDir . DIRECTORY_SEPARATOR . $filePath;
return $path;
}
/**
* Returns array of file information
*
* @param int $id Content object attribute id
* @param int $version Content object attribute version
* @return array ( mime_type => string,
* filename => string,
* original_filename => string,
* filepath => string )
**/
public function getFileInfo( $id, $version )
{
return false;
}
/**
* We support file download
*
* @return bool True
**/
public function canDownload()
{
return false;
}
/**
* Indicates that this video input type requires processing
*
* @return true
*/
public function requiresProcessing()
{
return true;
}
/**
* Implements eZPersistentObjet attribute interface to allow this object to
* be embedded in eZ templates.
*/
public function attributes()
{
return array( 'is_enabled' );
}
/**
* Implements eZPersistentObjet attribute interface to allow this object to
* be embedded in eZ templates.
*/
public function attribute( $name )
{
return $this->config->isEnabled;
}
/**
* Implements eZPersistentObjet attribute interface to allow this object to
* be embedded in eZ templates.
*/
public function hasAttribute( $name )
{
return true;
}
/**
* Sets an error message and returns false
*
* @param string $message Error message
* @return false
* @author Me
*/
protected function error( $message )
{
$this->lastError = $message;
return false;
}
/**
* Runs a method on the ServerFile persistence class
*
* This method takes a variable number or arguments. Any arguments after
* $method will be passed on to the ServerFile class
*
* @param string $method Name of method to run
* @return mixed Return value of the api method
*/
protected function runServerFileMethod( $method /* ... */ )
{
$args = func_get_args();
array_shift( $args ); // Get rid of first arg which is $method
return call_user_func_array(
array( $this->serverfileInstance, $method ), $args
);
}
}