diff --git a/src/12a2a8de-a893-4dbb-a53d-b52de4f6cb0e/code.power b/src/12a2a8de-a893-4dbb-a53d-b52de4f6cb0e/code.power new file mode 100644 index 0000000..747601c --- /dev/null +++ b/src/12a2a8de-a893-4dbb-a53d-b52de4f6cb0e/code.power @@ -0,0 +1,147 @@ + /** + * The Item Class. + * + * @var Item + * @since 5.0.2 + */ + protected Item $item; + + /** + * The File Types + * + * @var string + * @since 5.0.2 + */ + protected string $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. + * + * @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): ?array + { + if (($fileType = $this->details($guid)) !== null) + { + 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 + * + * @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); + } + + /** + * Retrieves the field name + * + * @param array $data The type data array + * + * @return string The field name + * @since 5.0.2 + */ + protected function getFieldName(array $data): string + { + $type = data->type ?? 4; + if (isset($this->fileTypes[$type])) + { + return $this->fileTypes[$type]; + } + return 'file'; + } + + /** + * Retrieves the allow formats (for script) + * + * @param array $data The type data array + * + * @return string The allow values + * @since 5.0.2 + */ + protected function getAllow(array $data): string + { + $formats = $this->getAllowFormats($data); + if (!empty($formats)) + { + return '*.(' . implode('|', $formats) . ')'; + } + return ''; + } + + /** + * Retrieves the allow formats (for span) + * + * @param array $data The type data array + * + * @return string The allow values + * @since 5.0.2 + */ + protected function getAllowSpan(array $data): string + { + $formats = $this->getAllowFormats($data); + if (!empty($formats)) + { + return '(formats allowed: ' . implode(', ', $formats) . ')'; + } + return ''; + } + + /** + * Retrieves the allow formats + * + * @param array|null $data The type data array + * + * @return array|null The allow values + * @since 5.0.2 + */ + protected function getAllowFormats(array $data): ?array + { + $type = data->type ?? 4; + switch ($type) + { + case 1: + return $data->image_formats ?? null; + break; + case 2: + return $data->document_formats ?? null; + break; + case 3: + return $data->media_formats ?? null; + break; + default: + return $data->file_formats ?? null; + break; + } + } \ No newline at end of file