<?php
namespace App\Document\Areabrick;
use Pimcore\Extension\Document\Areabrick\AbstractTemplateAreabrick;
use Pimcore\Model\Document\Editable\Area\Info;
class Downloads extends AbstractTemplateAreabrick
{
public function action(Info $info)
{
$dateien = [];
if ( $info->getDocumentElement('downloads') ) {
$downies = $info->getDocumentElement('downloads')->getData();
if ( $downies ) {
foreach ( $downies as $key => $val ) {
$tmp = \Pimcore\Model\Asset::getByPath($val);
//dd($tmp);
$title = $tmp->getMetadata("title");
if ( $title == '' ) $title = $tmp->getFilename();
$mime = explode('.',$tmp->getFilename());
$mime = strtoupper(end($mime));
$type = $tmp->getType();
$dateien[$key]['title'] = $title;
$dateien[$key]['type'] = $type;
$dateien[$key]['mimetype'] = $mime;
$dateien[$key]['bytes'] = $this->format_filesize($tmp->getFilesize());
$dateien[$key]['path'] = $tmp;
}
}
}
return $info->setParam('downloads', $dateien);
}
private function format_filesize($B, $D=2){
$S = 'kMGTPEZY';
$F = floor((strlen($B) - 1) / 3);
return sprintf("%.{$D}f", $B/pow(1024, $F)).' '.@$S[$F-1].'B';
}
public function getName()
{
return 'Downloads';
}
public function getDescription()
{
return '';
}
public function getTemplateLocation()
{
return static::TEMPLATE_LOCATION_GLOBAL;
}
public function needsReload(): bool
{
// optional
// here you can decide whether adding this bricks should trigger a reload
// in the editing interface, this could be necessary in some cases. default=false
return false;
}
// returns a custom html wrapper element (return an empty string if you don't want a wrapper element)
public function getHtmlTagOpen(Info $info)
{
return '';
}
public function getHtmlTagClose(Info $info)
{
return '';
}
}