From e78ebe5eebb65c400c37b2bbadb38cee0bef5bb2 Mon Sep 17 00:00:00 2001 From: aB0t Date: Fri, 13 Sep 2024 05:05:03 +0200 Subject: [PATCH] Update 2024-09-13 05:04:13 --- .../README.md | 37 +++++- .../code.php | 121 +++++++++++++++++- .../code.power | 118 ++++++++++++++++- .../settings.json | 8 +- 4 files changed, 279 insertions(+), 5 deletions(-) diff --git a/src/d46c4667-378b-49e8-9782-ffb28d92415a/README.md b/src/d46c4667-378b-49e8-9782-ffb28d92415a/README.md index 69d8f3f..f63c838 100644 --- a/src/d46c4667-378b-49e8-9782-ffb28d92415a/README.md +++ b/src/d46c4667-378b-49e8-9782-ffb28d92415a/README.md @@ -13,8 +13,15 @@ @startuml class Display << (F,LightGreen) >> #RoyalBlue { # Items $items - + __construct(Items $items) + # Item $item + # array $fileTypes + # array $fileTypeTasks + + __construct(Items $items, Item $item) + get(string $entity, string $target) : ?array + # setFileTypeTask(object $file) : void + # setFileDownloadLink(object $file) : void + # getFileTypeTask(object $data) : string + # getFileType(?string $guid) : ?object } note right of Display::__construct @@ -29,6 +36,34 @@ note right of Display::get since: 5.0.2 return: ?array end note + +note right of Display::setFileTypeTask + Add the file type details to this file + + since: 5.0.2 + return: void +end note + +note right of Display::setFileDownloadLink + Add the file download link + + since: 5.0.2 + return: void +end note + +note right of Display::getFileTypeTask + Retrieves the file type task name + + since: 5.0.2 + return: string +end note + +note right of Display::getFileType + Retrieves the file type details + + since: 5.0.2 + return: ?object +end note @enduml ``` diff --git a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php index e87f382..3f5a3b0 100644 --- a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php +++ b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.php @@ -12,7 +12,10 @@ namespace VDM\Joomla\Componentbuilder\File; +use Joomla\CMS\Uri\Uri; +use Joomla\CMS\Router\Route; use VDM\Joomla\Interfaces\Data\ItemsInterface as Items; +use VDM\Joomla\Interfaces\Data\ItemInterface as Item; /** @@ -30,16 +33,42 @@ final class Display */ protected Items $items; + /** + * The Item Class. + * + * @var Item + * @since 5.0.2 + */ + protected Item $item; + + /** + * The file types + * + * @var array + * @since 5.0.2 + */ + protected array $fileTypes; + + /** + * The File Type Task + * + * @var array + * @since 5.0.2 + */ + protected array $fileTypeTasks = [1 => 'image' , 2 => 'file' , 3 => 'media', 4 => 'file']; + /** * Constructor. * * @param Items $items The Items Class. + * @param Item $item The Item Class. * * @since 5.0.2 */ - public function __construct(Items $items) + public function __construct(Items $items, Item $item) { $this->items = $items; + $this->item = $item; } /** @@ -60,7 +89,12 @@ final class Display if ($file->entity_type !== $target) { unset($files[$n]); + continue; } + // set the file type task + $this->setFileTypeTask($file); + // set the file download link + $this->setFileDownloadLink($file); } // If the $files array is empty, return null @@ -68,6 +102,91 @@ final class Display } return null; + } + + /** + * Add the file type details to this file + * + * @param object $file The file being updated + * + * @return void + * @since 5.0.2 + */ + protected function setFileTypeTask(object &$file): void + { + if (($fileType = $this->getFileType($file->file_type ?? null)) !== null) + { + $file->task = $this->getFileTypeTask($fileType); + } + } + + /** + * Add the file download link + * + * @param object $file The file being updated + * + * @return void + * @since 5.0.2 + */ + protected function setFileDownloadLink(object &$file): void + { + if (isset($file->task)) + { + // Build the query parameters + $queryParams = [ + 'option' => 'com_componentbuilder', + 'controller' => 'download', + 'task' => 'download.' . $file->task, + 'file' => $file->guid, + 'name' => $file->name + ]; + + // Build the full URL + $file->link = Uri::root() . Route::_('index.php?' . http_build_query($queryParams)); + } + } + + /** + * Retrieves the file type task name + * + * @param object $data The type data array + * + * @return string The field name + * @since 5.0.2 + */ + protected function getFileTypeTask(object $data): string + { + $type = $data->type ?? 4; + if (isset($this->fileTypeTasks[$type])) + { + return $this->fileTypeTasks[$type]; + } + return 'file'; + } + + /** + * Retrieves the file type details + * + * @param string|null $guid The GUID (Globally Unique Identifier) used as the key to retrieve the file type. + * + * @return object|null The item object if found, or null if the item does not exist. + * @since 5.0.2 + */ + protected function getFileType(?string $guid): ?object + { + if ($guid === null) + { + return null; + } + + if (isset($this->fileTypes[$guid])) + { + return $this->fileTypes[$guid]; + } + + $this->fileTypes[$guid] = $this->item->table('file_type')->get($guid); + + return $this->fileTypes[$guid]; } } diff --git a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power index 07f81cd..a9f26d8 100644 --- a/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power +++ b/src/d46c4667-378b-49e8-9782-ffb28d92415a/code.power @@ -6,16 +6,42 @@ */ protected Items $items; + /** + * The Item Class. + * + * @var Item + * @since 5.0.2 + */ + protected Item $item; + + /** + * The file types + * + * @var array + * @since 5.0.2 + */ + protected array $fileTypes; + + /** + * The File Type Task + * + * @var array + * @since 5.0.2 + */ + protected array $fileTypeTasks = [1 => 'image' , 2 => 'file' , 3 => 'media', 4 => 'file']; + /** * Constructor. * * @param Items $items The Items Class. + * @param Item $item The Item Class. * * @since 5.0.2 */ - public function __construct(Items $items) + public function __construct(Items $items, Item $item) { $this->items = $items; + $this->item = $item; } /** @@ -36,7 +62,12 @@ if ($file->entity_type !== $target) { unset($files[$n]); + continue; } + // set the file type task + $this->setFileTypeTask($file); + // set the file download link + $this->setFileDownloadLink($file); } // If the $files array is empty, return null @@ -44,4 +75,89 @@ } return null; + } + + /** + * Add the file type details to this file + * + * @param object $file The file being updated + * + * @return void + * @since 5.0.2 + */ + protected function setFileTypeTask(object &$file): void + { + if (($fileType = $this->getFileType($file->file_type ?? null)) !== null) + { + $file->task = $this->getFileTypeTask($fileType); + } + } + + /** + * Add the file download link + * + * @param object $file The file being updated + * + * @return void + * @since 5.0.2 + */ + protected function setFileDownloadLink(object &$file): void + { + if (isset($file->task)) + { + // Build the query parameters + $queryParams = [ + 'option' => 'com_[[[component]]]', + 'controller' => 'download', + 'task' => 'download.' . $file->task, + 'file' => $file->guid, + 'name' => $file->name + ]; + + // Build the full URL + $file->link = Uri::root() . Route::_('index.php?' . http_build_query($queryParams)); + } + } + + /** + * Retrieves the file type task name + * + * @param object $data The type data array + * + * @return string The field name + * @since 5.0.2 + */ + protected function getFileTypeTask(object $data): string + { + $type = $data->type ?? 4; + if (isset($this->fileTypeTasks[$type])) + { + return $this->fileTypeTasks[$type]; + } + return 'file'; + } + + /** + * Retrieves the file type details + * + * @param string|null $guid The GUID (Globally Unique Identifier) used as the key to retrieve the file type. + * + * @return object|null The item object if found, or null if the item does not exist. + * @since 5.0.2 + */ + protected function getFileType(?string $guid): ?object + { + if ($guid === null) + { + return null; + } + + if (isset($this->fileTypes[$guid])) + { + return $this->fileTypes[$guid]; + } + + $this->fileTypes[$guid] = $this->item->table('file_type')->get($guid); + + return $this->fileTypes[$guid]; } \ No newline at end of file diff --git a/src/d46c4667-378b-49e8-9782-ffb28d92415a/settings.json b/src/d46c4667-378b-49e8-9782-ffb28d92415a/settings.json index 4fea6d7..a4f5035 100644 --- a/src/d46c4667-378b-49e8-9782-ffb28d92415a/settings.json +++ b/src/d46c4667-378b-49e8-9782-ffb28d92415a/settings.json @@ -1,5 +1,5 @@ { - "add_head": "0", + "add_head": "1", "add_licensing_template": "2", "extends": "", "guid": "d46c4667-378b-49e8-9782-ffb28d92415a", @@ -13,12 +13,16 @@ "use_selection0": { "use": "7212e4db-371f-4cfd-8122-32e9bb100d83", "as": "Items" + }, + "use_selection1": { + "use": "05744dd3-4030-4cf8-8dda-a93ab809b473", + "as": "Item" } }, "extendsinterfaces": null, "namespace": "[[[NamespacePrefix]]]\\Joomla\\[[[ComponentNamespace]]].File.Display", "description": "File Display Class\r\n\r\n@since 5.0.2", "licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 3rd September, 2020\r\n * @author Llewellyn van der Merwe \r\n * @git Joomla Component Builder \r\n * @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.\r\n * @license GNU General Public License version 2 or later; see LICENSE.txt\r\n *\/\r\n", - "head": "", + "head": "use Joomla\\CMS\\Uri\\Uri;\r\nuse Joomla\\CMS\\Router\\Route;", "composer": "" } \ No newline at end of file