Update 2024-09-13 05:04:13

This commit is contained in:
Robot 2024-09-13 05:05:03 +02:00
parent 216bd102ed
commit e78ebe5eeb
Signed by untrusted user: Robot
GPG Key ID: 14DECD44E7E1BB95
4 changed files with 279 additions and 5 deletions

View File

@ -13,8 +13,15 @@
@startuml @startuml
class Display << (F,LightGreen) >> #RoyalBlue { class Display << (F,LightGreen) >> #RoyalBlue {
# Items $items # Items $items
+ __construct(Items $items) # Item $item
# array $fileTypes
# array $fileTypeTasks
+ __construct(Items $items, Item $item)
+ get(string $entity, string $target) : ?array + 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 note right of Display::__construct
@ -30,6 +37,34 @@ note right of Display::get
return: ?array return: ?array
end note 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 @enduml
``` ```

View File

@ -12,7 +12,10 @@
namespace VDM\Joomla\Componentbuilder\File; 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\ItemsInterface as Items;
use VDM\Joomla\Interfaces\Data\ItemInterface as Item;
/** /**
@ -30,16 +33,42 @@ final class Display
*/ */
protected Items $items; 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. * Constructor.
* *
* @param Items $items The Items Class. * @param Items $items The Items Class.
* @param Item $item The Item Class.
* *
* @since 5.0.2 * @since 5.0.2
*/ */
public function __construct(Items $items) public function __construct(Items $items, Item $item)
{ {
$this->items = $items; $this->items = $items;
$this->item = $item;
} }
/** /**
@ -60,7 +89,12 @@ final class Display
if ($file->entity_type !== $target) if ($file->entity_type !== $target)
{ {
unset($files[$n]); 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 // If the $files array is empty, return null
@ -69,5 +103,90 @@ final class Display
return null; 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];
}
} }

View File

@ -6,16 +6,42 @@
*/ */
protected Items $items; 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. * Constructor.
* *
* @param Items $items The Items Class. * @param Items $items The Items Class.
* @param Item $item The Item Class.
* *
* @since 5.0.2 * @since 5.0.2
*/ */
public function __construct(Items $items) public function __construct(Items $items, Item $item)
{ {
$this->items = $items; $this->items = $items;
$this->item = $item;
} }
/** /**
@ -36,7 +62,12 @@
if ($file->entity_type !== $target) if ($file->entity_type !== $target)
{ {
unset($files[$n]); 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 // If the $files array is empty, return null
@ -45,3 +76,88 @@
return null; 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];
}

View File

@ -1,5 +1,5 @@
{ {
"add_head": "0", "add_head": "1",
"add_licensing_template": "2", "add_licensing_template": "2",
"extends": "", "extends": "",
"guid": "d46c4667-378b-49e8-9782-ffb28d92415a", "guid": "d46c4667-378b-49e8-9782-ffb28d92415a",
@ -13,12 +13,16 @@
"use_selection0": { "use_selection0": {
"use": "7212e4db-371f-4cfd-8122-32e9bb100d83", "use": "7212e4db-371f-4cfd-8122-32e9bb100d83",
"as": "Items" "as": "Items"
},
"use_selection1": {
"use": "05744dd3-4030-4cf8-8dda-a93ab809b473",
"as": "Item"
} }
}, },
"extendsinterfaces": null, "extendsinterfaces": null,
"namespace": "[[[NamespacePrefix]]]\\Joomla\\[[[ComponentNamespace]]].File.Display", "namespace": "[[[NamespacePrefix]]]\\Joomla\\[[[ComponentNamespace]]].File.Display",
"description": "File Display Class\r\n\r\n@since 5.0.2", "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 <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/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", "licensing_template": "\/**\r\n * @package Joomla.Component.Builder\r\n *\r\n * @created 3rd September, 2020\r\n * @author Llewellyn van der Merwe <https:\/\/dev.vdm.io>\r\n * @git Joomla Component Builder <https:\/\/git.vdm.dev\/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": "" "composer": ""
} }