-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.php
executable file
·90 lines (78 loc) · 2.8 KB
/
index.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
<?php
/*
* kirby 3 kirbytag - download
* return a beautiful download link for all/ specific/ first/ last file
*
* copyright: Jannik Beyerstedt | http://jannikbeyerstedt.de | [email protected]
* license: http://www.gnu.org/licenses/gpl-3.0.txt GPLv3 License
*/
Kirby::plugin('jbeyerstedt/download', [
'options' => [
'class' => 'dl',
'warnings' => 'true'
],
'tags' => [
'download' => [
'attr' => [
'text',
'type',
'ext'
],
'html' => function($tag) {
$types = array('image',
'document',
'archive',
'code',
'video',
'audio');
$files = $tag->parent()->files();
$class = option('jbeyerstedt.download.class');
$warnings = option('jbeyerstedt.download.warnings');
if ($tag->attr('type') != "") {
if(in_array($tag->attrs['type'], $types)) {
$files = $files->filterBy('type', $tag->attrs['type']);
} else {
return $warnings ? '<b>ERROR: download - no valid type defined</b>' : '';
}
}
if ($tag->ext != "") {
$files = $files->filterBy('extension', $tag->ext);
}
if (!$files || $files->count() == 0) {
return $warnings ? '<b>WARNING</b>: no file(s) selected' : '';
}
if ($tag->value == 'first') {
$files = $files->first();
} else if ($tag->value == 'last') {
$files = $files->last();
} else if ($tag->value == 'all') {
$files = $files;
} else{
$files = $files->find($tag->value);
}
if (!$files) {
return $warnings ? '<b>WARNING</b>: file(s) could not be found.' : '';
}
if ($tag->value == 'all') {
$html = '<ul>';
foreach ($files as $file) {
$ext = $file->extension();
$classes = $class . " " . $class . "--" . $ext; // i.e. "dl dl--pdf"
$text = $file->filename();
$html .= '<li>';
$html .= '<a class="'. $classes .'" href="'.$file->url().'" data-ext="'. $ext .'" target="_blank" download>'.$text.'</a> <small>('.$file->niceSize().')</small>';
$html .= '</li>';
}
$html .= '</ul>';
return $html;
} else {
// switch link text: filename or custom text
$ext = $files->extension();
$classes = $class . " " . $class . "--" . $ext; // i.e. "dl dl--pdf"
(empty($tag->attrs['text'])) ? $text = $files->filename() : $text = $tag->attrs['text'];
return '<a class="'. $classes .'" href="'.$files->url().'" data-ext="'. $ext .'" target="_blank" download>'.$text.'</a> <small>('.$files->niceSize().')</small>';
}
}
]
]
]);