Skip to content

Commit

Permalink
Added compatibility for Contao 3.2
Browse files Browse the repository at this point in the history
  • Loading branch information
ausi committed Oct 4, 2013
1 parent a9f5173 commit b4fa082
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 2 deletions.
34 changes: 33 additions & 1 deletion src/MadeYourDay/Contao/CustomElements.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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]];
Expand Down
7 changes: 6 additions & 1 deletion src/MadeYourDay/Contao/CustomElementsAjax.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
67 changes: 67 additions & 0 deletions src/MadeYourDay/Contao/Element/CustomElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
};
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit b4fa082

Please sign in to comment.