|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package org.openpsa.helpers |
|
4
|
|
|
* @author CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
5
|
|
|
* @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/ |
|
6
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
use midcom\datamanager\storage\blobs; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Helper class for file info |
|
13
|
|
|
* |
|
14
|
|
|
* @package org.openpsa.helpers |
|
15
|
|
|
*/ |
|
16
|
|
|
class org_openpsa_helpers_fileinfo |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Try to generate a human-readable file type by doing some educated guessing based on mimetypes |
|
20
|
|
|
*/ |
|
21
|
|
|
public static function render_type(string $mimetype) : string |
|
22
|
|
|
{ |
|
23
|
|
|
if (!preg_match('/\//', $mimetype)) { |
|
24
|
|
|
return $mimetype; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
//first, try if there is a direct translation |
|
28
|
|
|
if ($mimetype != midcom::get()->i18n->get_string($mimetype, 'org.openpsa.helpers')) { |
|
29
|
|
|
return midcom::get()->i18n->get_string($mimetype, 'org.openpsa.helpers'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
//if nothing is found, do some heuristics |
|
33
|
|
|
[$type, $subtype] = explode('/', $mimetype); |
|
34
|
|
|
$st_orig = $subtype; |
|
35
|
|
|
|
|
36
|
|
|
switch ($type) { |
|
37
|
|
|
case 'image': |
|
38
|
|
|
$subtype = strtoupper($subtype); |
|
39
|
|
|
break; |
|
40
|
|
|
case 'text': |
|
41
|
|
|
$type = 'document'; |
|
42
|
|
|
break; |
|
43
|
|
|
case 'application': |
|
44
|
|
|
$type = 'document'; |
|
45
|
|
|
|
|
46
|
|
|
if (preg_match('/^vnd\.oasis\.opendocument/', $subtype)) { |
|
47
|
|
|
$type = str_replace('vnd.oasis.opendocument.', '', $subtype); |
|
48
|
|
|
$subtype = 'OpenDocument'; |
|
49
|
|
|
} elseif (preg_match('/^vnd\.ms/', $subtype)) { |
|
50
|
|
|
$subtype = ucfirst(str_replace('vnd.ms-', '', $subtype)); |
|
51
|
|
|
} elseif (preg_match('/^vnd\.openxmlformats/', $subtype)) { |
|
52
|
|
|
$type = str_replace('vnd.openxmlformats-officedocument.', '', $subtype); |
|
53
|
|
|
$type = str_replace('ml.', ' ', $type); |
|
54
|
|
|
$subtype = 'OOXML'; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$subtype = preg_replace('/^vnd\./', '', $subtype); |
|
58
|
|
|
$subtype = preg_replace('/^x-/', '', $subtype); |
|
59
|
|
|
|
|
60
|
|
|
break; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/* |
|
64
|
|
|
* if nothing matched so far and the subtype is alphanumeric, uppercase it on the theory |
|
65
|
|
|
* that it's probably a file extension |
|
66
|
|
|
*/ |
|
67
|
|
|
if ( $st_orig == $subtype |
|
68
|
|
|
&& preg_match('/^[a-z0-9]+$/', $subtype)) { |
|
69
|
|
|
$subtype = strtoupper($subtype); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return sprintf(midcom::get()->i18n->get_string('%s ' . $type, 'org.openpsa.helpers'), $subtype); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public static function render(midcom_core_dbaobject $object, string $field) : string |
|
76
|
|
|
{ |
|
77
|
|
|
$output = ''; |
|
78
|
|
|
$attachments = blobs::get_attachments($object, $field); |
|
79
|
|
|
foreach ($attachments as $attachment) { |
|
80
|
|
|
$stat = $attachment->stat(); |
|
81
|
|
|
$filesize = midcom_helper_misc::filesize_to_string($stat[7]); |
|
82
|
|
|
$url = midcom::get()->permalinks->create_attachment_link($attachment->guid, $attachment->name); |
|
83
|
|
|
$type = self::render_type($attachment->mimetype); |
|
84
|
|
|
$parts = explode('.', $attachment->name); |
|
85
|
|
|
$ext = ''; |
|
86
|
|
|
if (count($parts) > 1) { |
|
87
|
|
|
$ext = end($parts); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$output .= '<span class="org_openpsa_helpers_fileinfo">'; |
|
91
|
|
|
$output .= '<a href="' . $url . '" class="icon" title="' . $attachment->name . '">'; |
|
92
|
|
|
$output .= '<i class="fa fa-file-o"></i><span class="extension">' . $ext . '</span></a>'; |
|
93
|
|
|
$output .= '<a href="' . $url . '" class="filename">' . $attachment->name . '</a>'; |
|
94
|
|
|
$output .= '<span class="mimetype">' . $type . '</span>'; |
|
95
|
|
|
$output .= '<span class="filesize">' . $filesize . '</span>'; |
|
96
|
|
|
$output .= "</span>\n"; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $output; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|