diff --git a/src/MadeYourDay/Contao/CustomElements.php b/src/MadeYourDay/Contao/CustomElements.php index f9059c6..b5ebb9c 100644 --- a/src/MadeYourDay/Contao/CustomElements.php +++ b/src/MadeYourDay/Contao/CustomElements.php @@ -84,7 +84,22 @@ public function onloadCallback($dc) */ public function loadCallback($value, $dc) { - return $this->getNestedValue($dc->field); + $value = $this->getNestedValue($dc->field); + + if ( + version_compare(VERSION, '3.2', '>=') && + $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['inputType'] === 'rsce_file_tree' && + $value + ) { + if (strlen($value) === 36) { + $value = \String::uuidToBin($value); + } + else { + $value = serialize(array_map('String::uuidToBin', deserialize($value))); + } + } + + return $value; } /** @@ -204,6 +219,23 @@ public function saveCallback($value, $dc) return; } + if ( + version_compare(VERSION, '3.2', '>=') && + $GLOBALS['TL_DCA'][$dc->table]['fields'][$dc->field]['inputType'] === 'rsce_file_tree' + ) { + if (trim($value)) { + if (strlen($value) === 16) { + $value = \String::binToUuid($value); + } + else { + $value = serialize(array_map('String::binToUuid', deserialize($value))); + } + } + else { + $value = ''; + } + } + $field = preg_split('(__([0-9]+)__)', substr($dc->field, 11), -1, PREG_SPLIT_DELIM_CAPTURE); $data =& $this->saveData[$field[0]]; diff --git a/src/MadeYourDay/Contao/CustomElementsAjax.php b/src/MadeYourDay/Contao/CustomElementsAjax.php index 5b27d2c..7d0c659 100644 --- a/src/MadeYourDay/Contao/CustomElementsAjax.php +++ b/src/MadeYourDay/Contao/CustomElementsAjax.php @@ -44,7 +44,12 @@ public function executePostActionsHook($action, $dc) // Automatically add resources to the DBAFS if ($widgetKey == 'rsce_file_tree') { foreach ($value as $k => $v) { - $value[$k] = \Dbafs::addResource($v)->id; + if (version_compare(VERSION, '3.2', '<')) { + $value[$k] = \Dbafs::addResource($v)->id; + } + else { + $value[$k] = \Dbafs::addResource($v)->uuid; + } } } $value = serialize($value); diff --git a/src/MadeYourDay/Contao/Element/CustomElement.php b/src/MadeYourDay/Contao/Element/CustomElement.php index f4cbd61..24d7d2b 100644 --- a/src/MadeYourDay/Contao/Element/CustomElement.php +++ b/src/MadeYourDay/Contao/Element/CustomElement.php @@ -79,6 +79,12 @@ public function compile() foreach ($data as $key => $value) { $this->Template->$key = $value; } + + $self = $this; + + $this->Template->getImageObject = function() use($self) { + return call_user_func_array(array($self, 'getImageObject'), func_get_args()); + }; } /** @@ -110,4 +116,65 @@ protected function deserializeDataRecursive($data) return $data; } + + /** + * Get an image object from id/uuid and an optional size configuration + * + * @param int|string $id ID, UUID string or binary + * @param string|array $size [width, height, mode] optionally serialized + * @return object Image object (similar as addImageToTemplate) + */ + public function getImageObject($id, $size = null) + { + global $objPage; + + if (!$id) { + return null; + } + + if (version_compare(VERSION, '3.2', '<')) { + $image = \FilesModel::findByPk($id); + } + else { + if (strlen($id) === 36) { + $id = \String::uuidToBin($id); + } + $image = \FilesModel::findByUuid($id); + } + if (!$image) { + return null; + } + + $file = new \File($image->path, true); + if (!$file->isGdImage) { + return null; + } + + $imageMeta = $this->getMetaData($image->meta, $objPage->language); + + if (is_string($size) && trim($size)) { + $size = deserialize($size); + } + if (!is_array($size)) { + $size = array(0, 0, 'center_center'); + } + $size[0] = isset($size[0]) ? $size[0] : 0; + $size[1] = isset($size[1]) ? $size[1] : 0; + $size[2] = isset($size[2]) ? $size[2] : 'center_center'; + + $image = array( + 'id' => $image->id, + 'uuid' => isset($image->uuid) ? $image->uuid : null, + 'name' => $file->basename, + 'singleSRC' => $image->path, + 'size' => serialize($size), + 'alt' => $imageMeta['title'], + 'imageUrl' => $imageMeta['link'], + 'caption' => $imageMeta['caption'], + ); + + $imageObject = new \stdClass(); + $this->addImageToTemplate($imageObject, $image); + return $imageObject; + } }