Skip to content

Commit

Permalink
[CODE] BREAKING CHANGES: config options renamed, new version number
Browse files Browse the repository at this point in the history
* config options according to the standard
* cleanup code format
* v2.0 to reflect the breaking changes and incompatibility with Kirby 2
  • Loading branch information
jbeyerstedt committed Apr 14, 2019
1 parent 8f87155 commit 285f145
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 69 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
by Jannik Beyerstedt from Hamburg, Germany
[jannikbeyerstedt.de](http://jannikbeyerstedt.de) | [Github](https://github.com/jbeyerstedt)
**license:** GNU GPL v3
**version:** v1.0.0
**version:** v2.0.0

## Notice About Branches and Kirby 2
Kirby 3 support was added in the `kirby3` branch, because Kirby 2 and Kirby 3 plugins are quite different.
Now that Kirby 3 is released, I changed the default branch to `kirby3` but will keep the `master`, because old Kirby 2 projects might break, if I rename the branches.

**Updating from the Kirby 2 version:** The configuration options are now prefixed by `jbeyerstedt.download` instead of `tags.download`.
This is caused by the new way, how plugins work within Kirby 3.


## Introduction
**Kirbytag to generate beautiful download links for a specific file**
Expand Down Expand Up @@ -71,8 +74,8 @@ With the `text` attribute (replace `$someLinkText`) you can set a custom text, w

### Options

* `tags.download.class`: The class appended to the anchor-element (Default: `dl`)
* `tags.download.warnings`: Inlines warnings if for example the given file couldn’t be found or the set is empty. (Default: `true`)
* `jbeyerstedt.download.class`: The class appended to the anchor-element (Default: `dl`)
* `jbeyerstedt.download.warnings`: Inlines warnings if for example the given file couldn’t be found or the set is empty. (Default: `true`)

You can set these in your `config.php` like this:

Expand All @@ -92,9 +95,9 @@ A look like this can be achieved with the following CSS:
```CSS
a.dl {
@mixin colored-badge($color: gray) {
border-color: rgba($color, .3);
background: rgba($color, .1);
color: $color;
border-color: rgba($color, .3);
background: rgba($color, .1);
color: $color;
}

&:before {
Expand Down
123 changes: 61 additions & 62 deletions index.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
<?php
/*
* kirby 3 tag - download
* 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
*
* sample usage:
* (download: $keyword type: $filetype ext: $extension text: $someLinkText)
* $keyword: type "all" for all files in collection, "first" or "last" for first/ last of collection, or specify the filename
* $filetype: filetype to choose: document/ code/ images/ videos
* $extension: filename extension to filter
*/

Kirby::plugin('jbeyerstedt/download', [
'options' => [
'class' => 'dl',
'warnings' => 'true'
],
'tags' => [
'download' => [
'attr' => [
Expand All @@ -22,68 +20,69 @@
'ext'
],
'html' => function($tag) {

$types = array('image',
'document',
'archive',
'code',
'video',
'audio');

$files = $tag->parent()->files();
$class = option('tags.download.class', 'dl');
$warnings = option('tags.download.warnings', true);

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>' : '';
}
}
'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->ext != "") {
$files = $files->filterBy('extension', $tag->ext);
}
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 || $files->count() == 0) {
return $warnings ? '<b>WARNING</b>: no file(s) selected' : '';
}
if (!$files) {
return $warnings ? '<b>WARNING</b>: file(s) could not be found.' : '';
}

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 ($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>';
}

if (!$files) {
return $warnings ? '<b>WARNING</b>: file(s) could not be found.' : '';
}
$html .= '</ul>';

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>';
}
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'];

$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>';
}
return '<a class="'. $classes .'" href="'.$files->url().'" data-ext="'. $ext .'" target="_blank" download>'.$text.'</a> <small>('.$files->niceSize().')</small>';
}

}
]
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jbeyerstedt/download",
"description": "Kirbytag to generate beautiful download links for a specific file",
"author": "Jannik Beyerstedt <[email protected]>",
"version": "1.0.0",
"version": "2.0.0",
"license": "GNU GPLv3",
"autoload": {
"files": ["config.php"],
Expand Down

0 comments on commit 285f145

Please sign in to comment.