Skip to content

Commit

Permalink
Use the version callbacks to backup and restore file contents (see #1128
Browse files Browse the repository at this point in the history
)

Description
-----------

Closes #788

Commits
-------

8552ad5 Use the version callbacks to backup and restore file contents (see #788)
fb88195 Fix the coding style
  • Loading branch information
leofeyer committed Dec 23, 2019
1 parent 385fc89 commit 84eede8
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 42 deletions.
42 changes: 0 additions & 42 deletions core-bundle/src/Resources/contao/classes/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,26 +181,6 @@ public function create($blnHideUser=false)
}
}

// Store the content if it is an editable file
if ($this->strTable == 'tl_files')
{
$objModel = FilesModel::findByPk($this->intPid);

if ($objModel !== null && \in_array($objModel->extension, StringUtil::trimsplit(',', strtolower(Config::get('editableFiles')))))
{
$objFile = new File($objModel->path);

if ($objFile->extension == 'svgz')
{
$data['content'] = gzdecode($objFile->getContent());
}
else
{
$data['content'] = $objFile->getContent();
}
}
}

$intVersion = 1;

$objVersion = $this->Database->prepare("SELECT MAX(version) AS version FROM tl_version WHERE pid=? AND fromTable=?")
Expand Down Expand Up @@ -301,28 +281,6 @@ public function restore($intVersion)
return;
}

// Restore the content if it is an editable file
if ($this->strTable == 'tl_files')
{
$objModel = FilesModel::findByPk($this->intPid);

if ($objModel !== null && \in_array($objModel->extension, StringUtil::trimsplit(',', strtolower(Config::get('editableFiles')))))
{
$objFile = new File($objModel->path);

if ($objFile->extension == 'svgz')
{
$objFile->write(gzencode($data['content']));
}
else
{
$objFile->write($data['content']);
}

$objFile->close();
}
}

// Get the currently available fields
$arrFields = array_flip($this->Database->getFieldNames($this->strTable));

Expand Down
88 changes: 88 additions & 0 deletions core-bundle/src/Resources/contao/dca/tl_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@
array('tl_files', 'addBreadcrumb'),
array('tl_files', 'adjustPalettes')
),
'oncreate_version_callback' => array
(
array('tl_files', 'createVersion')
),
'onrestore_version_callback' => array
(
array('tl_files', 'restoreVersion')
),
'sql' => array
(
'keys' => array
Expand Down Expand Up @@ -459,6 +467,86 @@ public function adjustPalettes(Contao\DataContainer $dc)
}
}

/**
* Store the content if it is an editable file
*
* @param string $table
* @param integer $pid
* @param integer $version
* @param array $data
*/
public function createVersion($table, $pid, $version, $data)
{
$model = Contao\FilesModel::findByPk($pid);

if ($model === null || !in_array($model->extension, Contao\StringUtil::trimsplit(',', strtolower(Contao\Config::get('editableFiles')))))
{
return;
}

$file = new Contao\File($model->path);

if ($file->extension == 'svgz')
{
$data['content'] = gzdecode($file->getContent());
}
else
{
$data['content'] = $file->getContent();
}

$this->Database->prepare("UPDATE tl_version SET data=? WHERE pid=? AND version=? AND fromTable=?")
->execute(serialize($data), $pid, $version, $table);
}

/**
* Restore the content if it is an editable file
*
* @param string $table
* @param integer $pid
* @param integer $version
* @param array $data
*/
public function restoreVersion($table, $pid, $version, $data)
{
$model = Contao\FilesModel::findByPk($pid);

if ($model === null || !in_array($model->extension, Contao\StringUtil::trimsplit(',', strtolower(Contao\Config::get('editableFiles')))))
{
return;
}

// Refetch the data, because not existing field have been unset
$objData = $this->Database->prepare("SELECT data FROM tl_version WHERE fromTable=? AND pid=? AND version=?")
->limit(1)
->execute($table, $pid, $version);

if ($objData->numRows < 1)
{
return;
}

$arrData = Contao\StringUtil::deserialize($objData->data);

if (!is_array($arrData))
{
return;
}

$file = new Contao\File($model->path);

if ($file->extension == 'svgz')
{
$file->write(gzencode($arrData['content']));
}
else
{
$file->write($arrData['content']);
}

$file->close();
}

/**
* Add the file location instead of the help text (see #6503)
*
Expand Down

0 comments on commit 84eede8

Please sign in to comment.