init
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\FileManager\Ui\Component\Listing\Column;
|
||||
|
||||
use Magento\Framework\AuthorizationInterface;
|
||||
use Magento\Framework\UrlInterface;
|
||||
use Magento\Framework\View\Element\UiComponent\ContextInterface;
|
||||
use Magento\Framework\View\Element\UiComponentFactory;
|
||||
use Magento\Ui\Component\Listing\Columns\Column;
|
||||
|
||||
class Actions extends Column
|
||||
{
|
||||
public function __construct(
|
||||
ContextInterface $context,
|
||||
UiComponentFactory $uiComponentFactory,
|
||||
private readonly UrlInterface $urlBuilder,
|
||||
private readonly AuthorizationInterface $authorization,
|
||||
array $components = [],
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $uiComponentFactory, $components, $data);
|
||||
}
|
||||
|
||||
public function prepareDataSource(array $dataSource): array
|
||||
{
|
||||
if (!isset($dataSource['data']['items'])) {
|
||||
return $dataSource;
|
||||
}
|
||||
|
||||
$fieldName = (string) $this->getData('name');
|
||||
foreach ($dataSource['data']['items'] as &$item) {
|
||||
if (!isset($item['id'], $item['url'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$item[$fieldName]['copy'] = [
|
||||
'label' => __('Copy URL'),
|
||||
'url' => (string) $item['url'],
|
||||
];
|
||||
$item[$fieldName]['open'] = [
|
||||
'href' => (string) $item['url'],
|
||||
'label' => __('Open'),
|
||||
'target' => '_blank',
|
||||
];
|
||||
if ($this->authorization->isAllowed('Esun_FileManager::delete')) {
|
||||
$item[$fieldName]['delete'] = [
|
||||
'href' => $this->urlBuilder->getUrl(
|
||||
'esun_filemanager/file/delete',
|
||||
['id' => (int) $item['id']]
|
||||
),
|
||||
'label' => __('Delete'),
|
||||
'post' => true,
|
||||
'confirm' => [
|
||||
'title' => __('Delete %1', (string) ($item['filename'] ?? '')),
|
||||
'message' => __('Are you sure you want to delete this file?'),
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return $dataSource;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\FileManager\Ui\Component\Listing\Column;
|
||||
|
||||
use Magento\Framework\View\Element\UiComponent\ContextInterface;
|
||||
use Magento\Framework\View\Element\UiComponentFactory;
|
||||
use Magento\Ui\Component\Listing\Columns\Column;
|
||||
|
||||
class Size extends Column
|
||||
{
|
||||
public function __construct(
|
||||
ContextInterface $context,
|
||||
UiComponentFactory $uiComponentFactory,
|
||||
array $components = [],
|
||||
array $data = []
|
||||
) {
|
||||
parent::__construct($context, $uiComponentFactory, $components, $data);
|
||||
}
|
||||
|
||||
public function prepareDataSource(array $dataSource): array
|
||||
{
|
||||
if (!isset($dataSource['data']['items'])) {
|
||||
return $dataSource;
|
||||
}
|
||||
|
||||
$fieldName = (string) $this->getData('name');
|
||||
foreach ($dataSource['data']['items'] as &$item) {
|
||||
$item[$fieldName] = $this->formatBytes((int) ($item[$fieldName] ?? 0));
|
||||
}
|
||||
|
||||
return $dataSource;
|
||||
}
|
||||
|
||||
private function formatBytes(int $bytes): string
|
||||
{
|
||||
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
|
||||
$value = (float) $bytes;
|
||||
$unit = 0;
|
||||
while ($value >= 1024 && $unit < count($units) - 1) {
|
||||
$value /= 1024;
|
||||
++$unit;
|
||||
}
|
||||
|
||||
return number_format($value, $unit === 0 ? 0 : 2) . ' ' . $units[$unit];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\FileManager\Ui\DataProvider\File;
|
||||
|
||||
use Esun\FileManager\Model\ResourceModel\File\CollectionFactory;
|
||||
use Magento\Framework\Api\Filter;
|
||||
use Magento\Ui\DataProvider\AbstractDataProvider;
|
||||
|
||||
class ListingDataProvider extends AbstractDataProvider
|
||||
{
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $primaryFieldName,
|
||||
string $requestFieldName,
|
||||
CollectionFactory $collectionFactory,
|
||||
array $meta = [],
|
||||
array $data = []
|
||||
) {
|
||||
$this->collection = $collectionFactory->create();
|
||||
parent::__construct($name, $primaryFieldName, $requestFieldName, $meta, $data);
|
||||
}
|
||||
|
||||
public function getData(): array
|
||||
{
|
||||
$data = parent::getData();
|
||||
if (!isset($data['items'])) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
foreach ($data['items'] as &$item) {
|
||||
$item['media_directive'] = sprintf(
|
||||
'{{media url="%s"}}',
|
||||
(string) ($item['filepath'] ?? '')
|
||||
);
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function addFilter(Filter $filter): void
|
||||
{
|
||||
if ($filter->getField() === 'fulltext') {
|
||||
$this->collection->addFieldToFilter(
|
||||
'filename',
|
||||
['like' => '%' . (string) $filter->getValue() . '%']
|
||||
);
|
||||
|
||||
return;
|
||||
}
|
||||
parent::addFilter($filter);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user