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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
<?php
/**
* This file is part of Stud.IP.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Moritz Strohm <strohm@data-quest.de>
* @copyright 2020
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
* @since 4.6
*/
class LibraryFile extends StandardFile
{
public $library_document = null;
public function __construct($fileref, $file = null)
{
parent::__construct($fileref, $file);
if (is_object($this->file->metadata)) {
$this->library_document = LibraryDocument::createFromArray($this->file->metadata->getArrayCopy());
}
}
public function getFile()
{
return $this->file;
}
public function getFileRef(): ?FileRef
{
return $this->fileref;
}
public static function createFromLibraryDocument(LibraryDocument $document, $folder_id = null, $user_id = null)
{
$file = new File();
$file->name = '';
$file->size = '0';
$file->mime_type = '';
$file->metadata = $document->toJson();
$file->user_id = $user_id ?: $GLOBALS['user']->id;
$file->filetype = get_called_class();
if (!empty($document->csl_data['URL']) || $document->opac_link) {
$file->metadata['url'] = $document->opac_link ?: $document->csl_data['URL'];
$file->metadata['access_type'] = 'redirect';
}
$file->store();
$file_ref = new FileRef();
$file_ref->file_id = $file->id;
$file_ref->folder_id = $folder_id ?: '';
$file_ref->name = $file->name;
$file_ref->downloads = 0;
$file_ref->description = trim($document->csl_data['description'] ?? '');
$file_ref->content_terms_of_use_id = ContentTermsOfUse::findDefault()->id;
$file_ref->user_id = $file->user_id;
$file_ref->store();
return new LibraryFile($file_ref);
}
/**
* Updates the LibraryFile by using a LibraryDocument instance.
*
* @param LibraryDocument $document The document containing the new data.
*
* @returns bool True, if the update was successful, false otherwise.
*/
public function updateFromLibraryDocument(LibraryDocument $document) : bool
{
if ($this->file instanceof File) {
$file_name = self::getFileNameFromDocument($document);
$this->file->name = $file_name;
$this->file->metadata = $document->toJson();
if ($this->file->isDirty()) {
if (!$this->file->store()) {
return false;
}
}
$this->fileref->name = $this->file->name;
if ($this->fileref->isDirty()) {
return $this->fileref->store();
}
return true;
} else {
return false;
}
}
/**
* Extracts a file name from a LibraryDocument instance.
* This is an internal helper method.
*
* @param LibraryDocument $document The document from which a file name
* shall be extracted.
*
* @returns string The extracted file name.
*/
protected static function getFileNameFromDocument(LibraryDocument $document) : string
{
$file_name = $document->getTitle();
if (!$file_name) {
//The document hasn't got a title. We can still generate a file name
//using the search parameters of the document.
if ($document->search_params[LibrarySearch::TITLE]) {
$file_name = $document->search_params[LibrarySearch::TITLE];
} elseif ($document->search_params[LibrarySearch::AUTHOR]) {
if ($document->search_params[LibrarySearch::YEAR]) {
$file_name = sprintf(
'%1$s (%2$s)',
$document->search_params[LibrarySearch::AUTHOR],
$document->search_params[LibrarySearch::YEAR]
);
} else {
$file_name = $document->search_params[LibrarySearch::AUTHOR];
}
} elseif ($document->search_params[LibrarySearch::NUMBER]) {
$file_name = $document->search_params[LibrarySearch::NUMBER];
} elseif ($document->search_params[LibrarySearch::SIGNATURE]) {
$file_name = $document->search_params[LibrarySearch::SIGNATURE];
} else {
$file_name = _('unbekannt');
}
}
return $file_name;
}
public function getIcon($role)
{
if ($this->library_document instanceof LibraryDocument) {
$icon = $this->library_document->getIcon();
}
return $icon && $icon->getShape() !== 'literature-request' ? $icon : Icon::create('literature', $role);
}
public function getFilename()
{
$file_name = '';
if ($this->library_document instanceof LibraryDocument) {
$file_name = $this->library_document->getTitle('long');
}
if (!$file_name && $this->library_document->search_params) {
$formatted_params = $this->library_document->getSearchDescription();
$file_name = _('Suche in der Bibliothek');
if ($formatted_params) {
$file_name .= ': ' . implode(', ', $formatted_params);
}
}
if (!$file_name && ($this->fileref instanceof FileRef)) {
$file_name = $this->fileref->name;
}
return $file_name;
}
public function getSize()
{
if ($this->fileref instanceof FileRef) {
return $this->fileref['size'];
}
return null;
}
public function getDownloadURL()
{
if ($this->hasFileAttached() || $this->hasURL()) {
if ($this->fileref instanceof FileRef) {
return $this->fileref->getDownloadURL();
}
}
return '';
}
public function getPath() : string
{
return $this->file instanceof File ? $this->file->getPath() : '';
}
public function getDownloads()
{
if ($this->fileref instanceof FileRef) {
return $this->fileref['downloads'];
}
return 0;
}
public function getActionmenu()
{
$action_menu = ActionMenu::get();
$action_menu->addLink(
URLHelper::getURL(sprintf('dispatch.php/file/details/%s/1', $this->fileref->id)),
_('Info'),
Icon::create('info-circle'),
['data-dialog' => ''],
'file-display-info'
);
if (Config::get()->LITERATURE_ENABLE && Context::get() && $GLOBALS['perm']->have_studip_perm('tutor', Context::getId())) {
$plugin_manager = PluginManager::getInstance();
$library_plugins = $plugin_manager->getPlugins(LibraryPlugin::class);
if (count($library_plugins)) {
$plugin = $library_plugins[0];
$action_menu->addLink(
$plugin->getRequestURL($this->getId()),
$plugin->getRequestTitle(),
$plugin->getRequestIcon(),
['data-dialog' => 'size=auto;reload-on-close;id=' . $plugin->getPluginId()]
);
}
}
if (parent::isWritable($GLOBALS['user']->id)) {
//Before the edit action can be displayed, we must make sure
//the library document is one that is defined.
$known_document_type = false;
foreach ($GLOBALS['LIBRARY_DOCUMENT_TYPES'] as $defined_type) {
if ($defined_type['name'] == $this->library_document->type) {
$known_document_type = true;
break;
}
}
if ($known_document_type) {
$action_menu->addLink(
URLHelper::getURL('dispatch.php/library_file/edit/' . $this->fileref->id),
_('Bearbeiten'),
Icon::create('edit'),
['data-dialog' => 'size=auto']
);
}
$action_menu->addButton(
'delete',
_('Datei löschen'),
Icon::create('trash'),
[
'formaction' => URLHelper::getURL("dispatch.php/file/delete/{$this->fileref->id}"),
'data-confirm' => sprintf(_('Soll die Datei "%s" wirklich gelöscht werden?'), $this->fileref->name),
]
);
}
return $action_menu;
}
public function getInfoDialogButtons(array $extra_link_params = []) : array
{
$buttons = [];
if ($this->isEditable($GLOBALS['user']->id)) {
$known_document_type = false;
foreach ($GLOBALS['LIBRARY_DOCUMENT_TYPES'] as $defined_type) {
if ($defined_type['name'] == $this->library_document->type) {
$known_document_type = true;
break;
}
}
if ($known_document_type) {
$buttons[] = Studip\LinkButton::create(
_('Bearbeiten'),
URLHelper::getURL('dispatch.php/library_file/edit/' . $this->fileref->id),
['data-dialog' => 'size=auto']
);
}
}
if ($this->isDownloadable($GLOBALS['user']->id) && $this->getDownloadURL()) {
$buttons[] = Studip\LinkButton::create(
$this->hasFileAttached() ? _('Herunterladen') : _('Öffnen'),
$this->getDownloadURL(),
['target' => '_blank']
);
}
return $buttons;
}
public function isEditable($user_id = null)
{
if ($this->fileref instanceof FileRef) {
return parent::isEditable($user_id);
}
return false;
}
public function getInfoTemplate(bool $include_downloadable_infos = false)
{
if (!$this->library_document) {
return null;
}
return $this->library_document->getInfoTemplate('full');
}
/**
* This is a method special to the LibraryFile class.
* It handles the attachment of a (real) File object to this LibraryFile.
* In that case, the library metadata must be transferred from the (virtual)
* file that is attached to the LibraryFile to the specified file that
* shall be attached.
*
* @param File $file The file to attach to this LibraryFile.
*
* @throws Exception In case of an error, an exception with an error message
* is thrown.
*/
public function attachFile(File $file) : bool
{
if ($file->isNew()) {
//The file must be stored in the database.
if (!$file->store()) {
throw new Exception(_('Fehler beim Speichern der Datei!'));
}
}
if ($this->fileref instanceof FileRef) {
$old_file = $this->fileref->file;
if (!($old_file instanceof File)) {
throw new Exception(_('Es gibt kein Dateiobjekt zum Bibliothekseintrag!'));
}
if ($old_file->id == $file->id) {
//The file is already attached.
return true;
}
$metadata = $old_file->metadata;
if (!$metadata) {
//Something went wrong.
throw new Exception(_('Das Dateiobjekt zum Bibliothekseintrag hat keine Metadaten!'));
}
$file->metadata = $metadata;
$file->filetype = get_class($this);
if ($file->isDirty()) {
if (!$file->store()) {
throw new Exception(_('Die neue Datei konnte nicht gespeichert werden!'));
}
}
$this->fileref->file_id = $file->id;
$this->fileref->name = $file->name;
if ($this->fileref->isDirty()) {
if ($this->fileref->store()) {
$old_file->delete();
} else {
throw new Exception(_('Die Verknüfpung der Datei mit dem Bibliothekseintrag konnte nicht gespeichert werden!'));
}
}
$this->file = $file;
} else {
throw new Exception(_('Der Bibliothekseintrag ist nicht mit einer virtuellen Datei verknüpft!'));
}
return true;
}
/**
* This method checks if a real file is attached to this library file.
*
* @returns bool True, if a real file is attached, false otherwise.
*/
public function hasFileAttached() : bool
{
return file_exists($this->file->path);
}
public function hasURL()
{
if (isset($this->file->metadata['csl_data']['URL']) && !isset($this->file->metadata['url'])) {
$this->file->metadata['url'] = $this->file->metadata['csl_data']['URL'];
$this->file->metadata['access_type'] = 'redirect';
$this->file->store();
}
return isset($this->file->metadata['url']);
}
public function removeDataFile()
{
if ($this->hasFileAttached()) {
$this->file->deleteDataFile();
$this->file->mime_type = '';
$this->file->name = '';
$this->file->size = 0;
$this->file->metadata['url'] = null;
$this->file->metadata['access_type'] = null;
$this->fileref->name = '';
$this->fileref->store();
return $this->file->store();
}
}
}
|