super-powers/src/12a2a8de-a893-4dbb-a53d-b52de4f6cb0e/code.power

203 lines
4.7 KiB
Plaintext
Raw Normal View History

2024-09-12 01:45:11 +00:00
/**
* The Item Class.
*
* @var Item
* @since 5.0.2
*/
protected Item $item;
/**
* The File Types
*
* @var array
* @since 5.0.2
*/
protected array $fileTypes = [1 => 'image' , 2 => 'document' , 3 => 'media', 4 => 'file'];
/**
* Constructor.
*
* @param Item $item The Item Class.
*
* @since 5.0.2
*/
public function __construct(Item $item)
{
$this->item = $item;
}
/**
* Retrieves the file type details (ajax)
*
* @param string $guid The GUID (Globally Unique Identifier) used as the key to retrieve the file type
* @param string $target The entity target name.
*
* @return array|null The item object if found, or null if the item does not exist.
* @since 5.0.2
*/
public function get(string $guid, string $target): ?array
{
if (($fileType = $this->details($guid)) !== null &&
$this->validTarget($fileType, $target))
{
return [
'name' => $this->getFieldName($fileType),
'allow' => $this->getAllow($fileType),
'allow_span' => $this->getAllowSpan($fileType),
'file_type_span' => $fileType->name ?? 'file',
'display_fields' => $fileType->display_fields ?? null,
'param_fields' => $fileType->param_fields ?? null,
];
}
return null;
}
/**
* Retrieves the file type details (upload)
*
* @param string $guid The GUID (Globally Unique Identifier) used as the key to retrieve the file type
* @param string $target The entity target name.
*
* @return array|null The item object if found, or null if the item does not exist.
* @since 5.0.2
*/
public function load(string $guid, string $target): ?array
{
if (($fileType = $this->details($guid)) !== null &&
$this->validTarget($fileType, $target))
{
return [
'field' => $this->getFieldName($fileType),
'type' => $this->getFieldName($fileType),
'formats' => $this->getAllowFormats($fileType) ?? [],
'filter' => $fileType->filter ?? null,
'path' => $fileType->path ?? null
];
}
return null;
}
/**
* Retrieves the file type details
*
* @param string $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
*/
public function details(string $guid): ?object
{
return $this->item->table('file_type')->get($guid);
}
/**
* Valid if this is a correct target trying to call this file type
*
* @param object $data The type data array
* @param string $target The entity target name.
*
* @return bool True if valid target
* @since 5.0.2
*/
protected function validTarget(object $data, string $target): bool
{
$targets = $data->target ?? null;
if (!empty($targets))
{
$targets = (array) $targets;
return in_array($target, $targets);
}
return false;
}
/**
* Retrieves the field name
*
* @param object $data The type data array
*
* @return string The field name
* @since 5.0.2
*/
protected function getFieldName(object $data): string
{
$type = $data->type ?? 4;
if (isset($this->fileTypes[$type]))
{
return $this->fileTypes[$type];
}
return 'file';
}
/**
* Retrieves the allow formats (for script)
*
* @param object $data The type data array
*
* @return string The allow values
* @since 5.0.2
*/
protected function getAllow(object $data): string
{
$formats = $this->getAllowFormats($data);
if (!empty($formats))
{
return '*.(' . implode('|', $formats) . ')';
}
return '';
}
/**
* Retrieves the allow formats (for span)
*
* @param object $data The type data array
*
* @return string The allow values
* @since 5.0.2
*/
protected function getAllowSpan(object $data): string
{
$formats = $this->getAllowFormats($data);
if (!empty($formats))
{
return '(formats allowed: <b>' . implode(', ', $formats) . '</b>)';
}
return '';
}
/**
* Retrieves the allow formats
*
* @param object|null $data The type data array
*
* @return array|null The allow values
* @since 5.0.2
*/
protected function getAllowFormats(object $data): ?array
{
$type = $data->type ?? 4;
switch ($type)
{
case 1:
$formats = $data->image_formats ?? null;
break;
case 2:
$formats = $data->document_formats ?? null;
break;
case 3:
$formats = $data->media_formats ?? null;
break;
default:
$formats = $data->file_formats ?? null;
break;
}
if ($formats)
{
return (array) $formats;
}
return null;
}