Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Trigger the version diff within the main file (see #5220)
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Mar 18, 2013
1 parent e3ca87e commit 352b1fb
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 5 deletions.
218 changes: 218 additions & 0 deletions contao/classes/Versions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2013 Leo Feyer
*
* @package Core
* @link https://contao.org
* @license http://www.gnu.org/licenses/lgpl-3.0.html LGPL
*/


/**
* Run in a custom namespace, so the class can be replaced
*/
namespace Contao;


/**
* Class Versions
*
* Provide methods to handle versioning.
* @copyright Leo Feyer 2005-2013
* @author Leo Feyer <https://contao.org>
* @package Core
*/
class Versions extends \Backend
{

/**
* Compare versions
* @param string
* @param integer
* @return string
*/
public function compare($strTable, $intPid)
{
$strBuffer = '';
$arrVersions = array();
$intTo = 0;
$intFrom = 0;

$objVersions = $this->Database->prepare("SELECT * FROM tl_version WHERE pid=? AND fromTable=? ORDER BY version DESC")
->execute($intPid, $strTable);

if ($objVersions->numRows < 1)
{
$strBuffer = 'There are no versions of ' . $strTable . '.id=' . $intPid;
}
else
{
$intIndex = 0;
$from = array();

// Store the versions and mark the active one
while ($objVersions->next())
{
if ($objVersions->active)
{
$intIndex = $objVersions->version;
}

$arrVersions[$objVersions->version] = $objVersions->row();
$arrVersions[$objVersions->version]['info'] = $GLOBALS['TL_LANG']['MSC']['version'].' '.$objVersions->version.' ('.\Date::parse($GLOBALS['TL_CONFIG']['datimFormat'], $objVersions->tstamp).') '.$objVersions->username;
}

// To
if (\Input::post('to') && isset($arrVersions[\Input::post('to')]))
{
$intTo = \Input::post('to');
$to = deserialize($arrVersions[\Input::post('to')]['data']);
}
elseif (\Input::get('to') && isset($arrVersions[\Input::get('to')]))
{
$intTo = \Input::get('to');
$to = deserialize($arrVersions[\Input::get('to')]['data']);
}
else
{
$intTo = $intIndex;
$to = deserialize($arrVersions[$intTo]['data']);
}

// From
if (\Input::post('from') && isset($arrVersions[\Input::post('from')]))
{
$intFrom = \Input::post('from');
$from = deserialize($arrVersions[\Input::post('from')]['data']);
}
elseif (\Input::get('from') && isset($arrVersions[\Input::get('from')]))
{
$intFrom = \Input::get('from');
$from = deserialize($arrVersions[\Input::get('from')]['data']);
}
elseif ($intIndex > 1)
{
$intFrom = $intIndex-1;
$from = deserialize($arrVersions[$intFrom]['data']);
}

\System::loadLanguageFile($strTable);
$this->loadDataContainer($strTable);

// Include the PhpDiff library
require_once TL_ROOT . '/system/modules/core/vendor/phpdiff/Diff.php';
require_once TL_ROOT . '/system/modules/core/vendor/phpdiff/Diff/Renderer/Html/Contao.php';

$arrFields = $GLOBALS['TL_DCA'][$strTable]['fields'];

// Find the changed fields and highlight the changes
foreach ($to as $k=>$v)
{
if ($from[$k] != $to[$k])
{
if (!isset($arrFields[$k]['inputType']) || $arrFields[$k]['inputType'] == 'password' || $arrFields[$k]['eval']['doNotShow'] || $arrFields[$k]['eval']['hideInput'])
{
continue;
}

// Convert serialized arrays into strings
if (is_array(($tmp = deserialize($to[$k]))) && !is_array($to[$k]))
{
$to[$k] = $this->implodeRecursive($tmp);
}
if (is_array(($tmp = deserialize($from[$k]))) && !is_array($from[$k]))
{
$from[$k] = $this->implodeRecursive($tmp);
}
unset($tmp);

// Convert date fields
if ($arrFields[$k]['eval']['rgxp'] == 'date')
{
$to[$k] = \Date::parse($GLOBALS['TL_CONFIG']['dateFormat'], $to[$k] ?: '');
$from[$k] = \Date::parse($GLOBALS['TL_CONFIG']['dateFormat'], $from[$k] ?: '');
}
elseif ($arrFields[$k]['eval']['rgxp'] == 'time')
{
$to[$k] = \Date::parse($GLOBALS['TL_CONFIG']['timeFormat'], $to[$k] ?: '');
$from[$k] = \Date::parse($GLOBALS['TL_CONFIG']['timeFormat'], $from[$k] ?: '');
}
elseif ($arrFields[$k]['eval']['rgxp'] == 'datim')
{
$to[$k] = \Date::parse($GLOBALS['TL_CONFIG']['datimFormat'], $to[$k] ?: '');
$from[$k] = \Date::parse($GLOBALS['TL_CONFIG']['datimFormat'], $from[$k] ?: '');
}

// Convert strings into arrays
if (!is_array($to[$k]))
{
$to[$k] = explode("\n", $to[$k]);
}
if (!is_array($from[$k]))
{
$from[$k] = explode("\n", $from[$k]);
}

$objDiff = new \Diff($from[$k], $to[$k]);
$strBuffer .= $objDiff->Render(new \Diff_Renderer_Html_Contao(array('field'=>($arrFields[$k]['label'][0] ?: $k))));
}
}
}

// Identical versions
if ($strBuffer == '')
{
$strBuffer = '<p>'.$GLOBALS['TL_LANG']['MSC']['identicalVersions'].'</p>';
}

$objTemplate = new \BackendTemplate('be_diff');

// Template variables
$objTemplate->content = $strBuffer;
$objTemplate->versions = $arrVersions;
$objTemplate->to = $intTo;
$objTemplate->from = $intFrom;
$objTemplate->showLabel = specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']);
$objTemplate->theme = \Backend::getTheme();
$objTemplate->base = \Environment::get('base');
$objTemplate->language = $GLOBALS['TL_LANGUAGE'];
$objTemplate->title = specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']);
$objTemplate->charset = $GLOBALS['TL_CONFIG']['characterSet'];
$objTemplate->action = ampersand(\Environment::get('request'));

$GLOBALS['TL_CONFIG']['debugMode'] = false;
$objTemplate->output();
}


/**
* Implode a multi-dimensional array recursively
* @param mixed
* @return string
*/
protected function implodeRecursive($var)
{
if (!is_array($var))
{
return $var;
}
elseif (!is_array(next($var)))
{
return implode(', ', $var);
}
else
{
$buffer = '';

foreach ($var as $k=>$v)
{
$buffer .= $k . ": " . $this->implodeRecursive($v) . "\n";
}

return trim($buffer);
}
}
}
1 change: 1 addition & 0 deletions contao/config/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
'Contao\RebuildIndex' => 'system/modules/core/classes/RebuildIndex.php',
'Contao\StyleSheets' => 'system/modules/core/classes/StyleSheets.php',
'Contao\Theme' => 'system/modules/core/classes/Theme.php',
'Contao\Versions' => 'system/modules/core/classes/Versions.php',

// Drivers
'Contao\DC_File' => 'system/modules/core/drivers/DC_File.php',
Expand Down
9 changes: 8 additions & 1 deletion contao/drivers/DC_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,13 @@ public function edit($intID=null, $ajaxId=null)
$this->procedure[] = 'id=?';
$this->blnCreateNewVersion = false;

// Compare versions
if (\Input::get('versions'))
{
$this->import('Versions');
$this->Versions->compare($this->strTable, $this->intId);
}

// Change version
if ($GLOBALS['TL_DCA'][$this->strTable]['config']['enableVersioning'] && \Input::post('FORM_SUBMIT') == 'tl_version' && \Input::post('version') != '')
{
Expand Down Expand Up @@ -1862,7 +1869,7 @@ public function edit($intID=null, $ajaxId=null)
<select name="version" class="tl_select">'.$versions.'
</select>
<input type="submit" name="showVersion" id="showVersion" class="tl_submit" value="'.specialchars($GLOBALS['TL_LANG']['MSC']['restore']).'">
<a href="contao/diff.php?table='.$this->strTable.'&amp;pid='.$this->intId.'" title="'.specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']).'" onclick="Backend.openModalIframe({\'width\':860,\'title\':\''.specialchars(str_replace("'", "\\'", $GLOBALS['TL_LANG']['MSC']['showDifferences'])).'\',\'url\':this.href});return false">'.\Image::getHtml('diff.gif').'</a>
<a href="'.$this->addToUrl('versions=1&popup=1').'" title="'.specialchars($GLOBALS['TL_LANG']['MSC']['showDifferences']).'" onclick="Backend.openModalIframe({\'width\':765,\'title\':\''.specialchars(str_replace("'", "\\'", $GLOBALS['TL_LANG']['MSC']['showDifferences'])).'\',\'url\':this.href});return false">'.\Image::getHtml('diff.gif').'</a>
</div>
</form>
Expand Down
5 changes: 2 additions & 3 deletions contao/templates/backend/be_diff.html5
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@
<div id="container">

<div id="main">
<form action="<?php echo $this->action; ?>" method="get">
<form action="<?php echo $this->action; ?>" method="post">
<div class="formbody">
<input type="hidden" name="table" value="<?php echo $this->table; ?>">
<input type="hidden" name="pid" value="<?php echo $this->pid; ?>">
<input type="hidden" name="REQUEST_TOKEN" value="<?php echo REQUEST_TOKEN; ?>">
<select name="from" id="ctrl_from" class="tl_select">
<?php foreach ($this->versions as $k=>$v): ?>
<option value="<?php echo $k; ?>"<?php if ($v['version'] == $this->from): ?> selected="selected"<?php endif; ?>><?php echo $v['info']; ?></option>
Expand Down
2 changes: 1 addition & 1 deletion contao/templates/backend/be_welcome.html5
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<td><?php echo $version['pid']; ?></td>
<td><?php echo $version['description'] ?: '-'; ?></td>
<td><?php echo $version['version']; ?></td>
<td><?php if ($version['deleted']): ?><a href="contao/main.php?do=undo" title="<?php echo specialchars($GLOBALS['TL_LANG']['MSC']['restore']); ?>"><?php echo Image::getHtml('undo.gif'); ?></a><?php elseif ($version['editUrl'] != ''): ?><a href="<?php echo $version['editUrl']; ?>" title="<?php echo $this->editElement; ?>"><?php echo Image::getHtml('edit.gif', '', 'style="padding:0 2px"'); ?></a><?php else: ?><?php echo Image::getHtml('edit_.gif', '', 'style="padding:0 2px"'); ?><?php endif; ?> <a href="contao/diff.php?table=<?php echo $version['fromTable']; ?>&amp;pid=<?php echo $version['pid']; ?>&amp;from=<?php echo $version['from']; ?>&amp;to=<?php echo $version['to']; ?>" title="<?php echo $this->showDifferences; ?>" onclick="Backend.openModalIframe({'width':860,'title':'<?php echo $this->showDifferences; ?>','url':this.href});return false"><?php echo Image::getHtml('diff.gif'); ?></a></td>
<td><?php if ($version['deleted']): ?><a href="contao/main.php?do=undo" title="<?php echo specialchars($GLOBALS['TL_LANG']['MSC']['restore']); ?>"><?php echo Image::getHtml('undo.gif'); ?></a><?php elseif ($version['editUrl'] != ''): ?><a href="<?php echo $version['editUrl']; ?>" title="<?php echo $this->editElement; ?>"><?php echo Image::getHtml('edit.gif', '', 'style="padding:0 2px"'); ?></a><?php else: ?><?php echo Image::getHtml('edit_.gif', '', 'style="padding:0 2px"'); ?><?php endif; ?> <a href="<?php echo $version['editUrl']; ?>&amp;from=<?php echo $version['from']; ?>&amp;to=<?php echo $version['to']; ?>&amp;versions=1&amp;popup=1" title="<?php echo $this->showDifferences; ?>" onclick="Backend.openModalIframe({'width':765,'title':'<?php echo $this->showDifferences; ?>','url':this.href});return false"><?php echo Image::getHtml('diff.gif'); ?></a></td>
</tr>
<?php endforeach; ?>
</tbody>
Expand Down

0 comments on commit 352b1fb

Please sign in to comment.