From 498010ad8f795b4bafb3547dd27a8e2ba31e254f Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 8 Jul 2021 11:36:19 -0400 Subject: [PATCH] Add a new FieldtypeHasFiles interface and update FieldtypeFile and FieldtypeImage to implement it. This interface is intended to be implemented by any Fieldtype that works with files to provide a common interface among them. --- wire/core/Interfaces.php | 36 ++++++++++++ .../FieldtypeFile/FieldtypeFile.module | 55 ++++++++++++++++++- .../FieldtypeImage/FieldtypeImage.module | 37 ++++++++++++- 3 files changed, 126 insertions(+), 2 deletions(-) diff --git a/wire/core/Interfaces.php b/wire/core/Interfaces.php index d19215cd..961c3ea8 100644 --- a/wire/core/Interfaces.php +++ b/wire/core/Interfaces.php @@ -166,6 +166,42 @@ interface WireTrackable { */ interface FieldtypePageTitleCompatible { } +/** + * Indicates Fieldtype manages files + * + */ +interface FieldtypeHasFiles { + /** + * Whether or not given Page/Field has any files connected with it + * + * @param Page $page + * @param Field $field + * @return bool + * + */ + public function hasFiles(Page $page, Field $field); + + /** + * Get array of full path/file for all files managed by given page and field + * + * @param Page $page + * @param Field $field + * @return array + * + */ + public function getFiles(Page $page, Field $field); + + /** + * Get path where files are (or would be) stored + * + * @param Page $page + * @param Field $field + * @return string + * + */ + public function getFilesPath(Page $page, Field $field); +} + /** * Indicates that an Inputfield provides tree selection capabilities diff --git a/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module b/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module index 633b1879..58057408 100644 --- a/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module +++ b/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module @@ -18,7 +18,7 @@ * */ -class FieldtypeFile extends FieldtypeMulti implements ConfigurableModule { +class FieldtypeFile extends FieldtypeMulti implements ConfigurableModule, FieldtypeHasFiles { public static function getModuleInfo() { return array( @@ -1580,6 +1580,59 @@ class FieldtypeFile extends FieldtypeMulti implements ConfigurableModule { 'validators' => $validators, // file validators in use indexed by file extension ); } + + /** + * Whether or not given Page/Field has any files connected with it + * + * #pw-internal For FieldtypeHasFiles interface + * + * @param Page $page + * @param Field $field + * @return bool + * @since 3.0.181 + * + */ + public function hasFiles(Page $page, Field $field) { + if(!$field->type instanceof FieldtypeFile) return false; + $value = $page->get($field->name); + return ($value instanceof Pagefile || ($value instanceof Pagefiles && $value->count() > 0)); + } + + /** + * Get array of full path/file for all files managed by given page and field + * + * #pw-internal For FieldtypeHasFiles interface + * + * @param Page $page + * @param Field $field + * @return array + * @since 3.0.181 + * + */ + public function getFiles(Page $page, Field $field) { + $value = $page->get($field->name); + if($value instanceof Pagefile) { + /** @var Pagefile $value */ + return array($value->filename()); + } + if($value instanceof Pagefiles && $value->count()) { + /** @var Pagefiles $value */ + return $value->explode('filename'); + } + return array(); + } + + /** + * Get path where files are (or would be) stored + * + * @param Page $page + * @param Field $field + * @return string + * + */ + public function getFilesPath(Page $page, Field $field) { + return PagefilesManager::_path($page); + } /** * Field config diff --git a/wire/modules/Fieldtype/FieldtypeImage/FieldtypeImage.module b/wire/modules/Fieldtype/FieldtypeImage/FieldtypeImage.module index 3c981109..3aa312db 100644 --- a/wire/modules/Fieldtype/FieldtypeImage/FieldtypeImage.module +++ b/wire/modules/Fieldtype/FieldtypeImage/FieldtypeImage.module @@ -15,7 +15,7 @@ * */ -class FieldtypeImage extends FieldtypeFile { +class FieldtypeImage extends FieldtypeFile implements FieldtypeHasFiles { /** * File schema is configured to store dimensions for image files 'width', 'height', 'ratio' (flag) @@ -228,6 +228,41 @@ class FieldtypeImage extends FieldtypeFile { return $value; } + /** + * Get array of full path/file for all files managed by given page and field (also includes image variations) + * + * #pw-internal For FieldtypeHasFiles interface + * + * @param Page $page + * @param Field $field + * @return array + * @since 3.0.181 + * + */ + public function getFiles(Page $page, Field $field) { + + $value = $page->get($field->name); + $files = array(); + + if($value instanceof Pageimage) { + $value = array($value); + } else if(!$value instanceof Pageimages) { + return array(); + } + + foreach($value as $pageimage) { + $file = $pageimage->filename(); + $path = dirname($file) . '/'; + $files[] = $file; + $variations = $pageimage->getVariations(array('info' => true, 'verbose' => false)); + foreach($variations as $basename) { + $files[] = $path . $basename; + } + } + + return $files; + } + /** * Get Inputfields to configure fields using this Fieldtype *