Skip to content

Commit

Permalink
Show the version conflict message on a separate page (see #809).
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed May 19, 2017
1 parent b659d4d commit 7e9fa2a
Show file tree
Hide file tree
Showing 11 changed files with 621 additions and 8 deletions.
121 changes: 121 additions & 0 deletions src/Controller/VersionConflictController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

/*
* This file is part of Contao.
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/

namespace Contao\CoreBundle\Controller;

use Contao\Backend;
use Contao\BackendTemplate;
use Contao\Config;
use Contao\Controller as ContaoController;
use Contao\CoreBundle\Framework\ContaoFrameworkInterface;
use Contao\Environment;
use Contao\StringUtil;
use Contao\System;
use Contao\Versions;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* Shows an information screen if there is a version conflict.
*
* @author Leo Feyer <https://github.com/leofeyer>
*/
class VersionConflictController extends Controller
{
/**
* @var ContaoFrameworkInterface
*/
private $framework;

/**
* Adds the diff view and referer URL.
*
* @param Request $request
*
* @return Response
*
* @Route("/contao/conflict", name="contao_backend_conflict")
*/
public function indexAction(Request $request)
{
$this->framework = $this->get('contao.framework');
$this->framework->initialize();

$template = $this->getTemplate();

$template->explain1 = sprintf(
$GLOBALS['TL_LANG']['MSC']['versionConflict1'],
(int) $request->query->get('theirs'),
(int) $request->query->get('mine')
);

$template->explain2 = sprintf(
$GLOBALS['TL_LANG']['MSC']['versionConflict2'],
(int) $request->query->get('theirs') + 1,
(int) $request->query->get('theirs')
);

$session = $this->get('session');

if ($session->has('versionConflictUrl')) {
$template->href = $session->get('versionConflictUrl');
} else {
$template->href = $this->get('router')->generate('contao_backend');
}

$versions = new Versions($request->query->get('table'), (int) $request->query->get('id'));

$template->diff = $versions->compare(true);

return $template->getResponse();
}

/**
* Returns the template object.
*
* @return BackendTemplate|object
*/
private function getTemplate()
{
/** @var System $system */
$system = $this->framework->getAdapter(System::class);
$system->loadLanguageFile('default');

/** @var ContaoController $controller */
$controller = $this->framework->getAdapter(ContaoController::class);
$controller->setStaticUrls();

/** @var BackendTemplate|object $template */
$template = $this->framework->createInstance(BackendTemplate::class, ['be_conflict']);
$template->language = $GLOBALS['TL_LANGUAGE'];
$template->h1 = $GLOBALS['TL_LANG']['MSC']['versionConflict'];
$template->back = $GLOBALS['TL_LANG']['MSC']['backBT'];
$template->title = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['versionConflict']);

/** @var Backend $backend */
$backend = $this->framework->getAdapter(Backend::class);

$template->theme = $backend->getTheme();

/** @var Config $config */
$config = $this->framework->getAdapter(Config::class);

$template->charset = $config->get('characterSet');

/** @var Environment $environment */
$environment = $this->framework->getAdapter(Environment::class);

$template->base = $environment->get('base');

return $template;
}
}
23 changes: 21 additions & 2 deletions src/Resources/contao/classes/Versions.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,22 @@ class Versions extends \Controller
*
* @param string $strTable
* @param integer $intPid
*
* @throws \InvalidArgumentException
*/
public function __construct($strTable, $intPid)
{
$this->import('Database');
parent::__construct();

$this->loadDataContainer($strTable);

if (!isset($GLOBALS['TL_DCA'][$strTable])) {
throw new \InvalidArgumentException(sprintf('"%s" is not a valid table', StringUtil::specialchars($strTable)));
}

$this->strTable = $strTable;
$this->intPid = $intPid;
$this->intPid = (int) $intPid;
}


Expand Down Expand Up @@ -378,8 +386,14 @@ public function restore($intVersion)

/**
* Compare versions
*
* @param bool $blnReturnBuffer
*
* @return string
*
* @throws ResponseException
*/
public function compare()
public function compare($blnReturnBuffer=false)
{
$strBuffer = '';
$arrVersions = array();
Expand Down Expand Up @@ -541,6 +555,11 @@ public function compare()
$strBuffer = '<p>'.$GLOBALS['TL_LANG']['MSC']['identicalVersions'].'</p>';
}

if ($blnReturnBuffer)
{
return $strBuffer;
}

/** @var BackendTemplate|object $objTemplate */
$objTemplate = new \BackendTemplate('be_diff');

Expand Down
8 changes: 6 additions & 2 deletions src/Resources/contao/drivers/DC_Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2212,8 +2212,12 @@ public function edit($intId=null, $ajaxId=null)
// Show a warning if the record has been saved by another user (see #8412)
if ($intLatestVersion !== null && isset($_POST['VERSION_NUMBER']) && $intLatestVersion > \Input::post('VERSION_NUMBER'))
{
\Message::addError(sprintf($GLOBALS['TL_LANG']['ERR']['versionWarning'], $intLatestVersion, \Input::post('VERSION_NUMBER')));
$this->reload();
/** @var SessionInterface $objSession */
$objSession = \System::getContainer()->get('session');

$objSession->set('versionConflictUrl', \Environment::get('request'));

$this->redirect(\System::getContainer()->get('router')->generate('contao_backend_conflict', array('table'=>$this->strTable, 'id'=>$this->intId, 'mine'=>\Input::post('VERSION_NUMBER'), 'theirs'=>$intLatestVersion)));
}

// Redirect
Expand Down
12 changes: 9 additions & 3 deletions src/Resources/contao/languages/en/default.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,6 @@
<trans-unit id="ERR.invalidFieldName">
<source>Please enter only the following characters: A-Z0-9[]_-</source>
</trans-unit>
<trans-unit id="ERR.versionWarning">
<source>Another user has created version %s while you were editing version %s of this record.</source>
</trans-unit>
<trans-unit id="SEC.question1">
<source>Please add %d and %d.</source>
</trans-unit>
Expand Down Expand Up @@ -1688,6 +1685,15 @@ This e-mail has been generated by Contao. You can not reply to it directly.
<trans-unit id="MSC.invalidTokenUrl">
<source>Invalid token</source>
</trans-unit>
<trans-unit id="MSC.versionConflict">
<source>Version conflict</source>
</trans-unit>
<trans-unit id="MSC.versionConflict1">
<source>Another user has created version %s while you were editing version %s of this record.</source>
</trans-unit>
<trans-unit id="MSC.versionConflict2">
<source>Your changes have been now been saved as version %s and have potentially overwritten the changes of version %s, therefore please compare the versions below:</source>
</trans-unit>
<trans-unit id="MSC.dragItemsHint">
<source>Drag the items to re-order them</source>
</trans-unit>
Expand Down
39 changes: 39 additions & 0 deletions src/Resources/contao/templates/backend/be_conflict.html5
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<!DOCTYPE html>
<html lang="<?= $this->language ?>">
<head>

<meta charset="<?= $this->charset ?>">
<title><?= $this->title ?> - Contao Open Source CMS</title>
<base href="<?= $this->base ?>">
<meta name="generator" content="Contao Open Source CMS">
<meta name="viewport" content="width=device-width,initial-scale=1.0,shrink-to-fit=no">
<meta name="referrer" content="origin">

<link rel="stylesheet" href="<?= TL_ASSETS_URL ?>system/themes/<?= $this->theme ?>/fonts.css">
<link rel="stylesheet" href="<?= TL_ASSETS_URL ?>system/themes/<?= $this->theme ?>/basic.css">
<link rel="stylesheet" href="<?= TL_ASSETS_URL ?>system/themes/<?= $this->theme ?>/diff.css">
<link rel="stylesheet" href="<?= TL_ASSETS_URL ?>system/themes/<?= $this->theme ?>/conflict.css">
<?= $this->stylesheets ?>

<script><?= $this->getLocaleString() ?></script>
<script src="<?= TL_ASSETS_URL ?>assets/mootools/js/mootools.min.js"></script>
<script src="<?= TL_ASSETS_URL ?>bundles/contaocore/mootao.min.js"></script>
<script src="<?= TL_ASSETS_URL ?>bundles/contaocore/core.min.js"></script>
<script><?= $this->getDateString() ?></script>
<?= $this->javascripts ?>

</head>
<body class="<?= $this->ua ?>">

<div id="container" class="cf">
<div id="main">
<h1><?= $this->h1 ?></h1>
<p><?= $this->explain1 ?></p>
<p><?= $this->explain2 ?></p>
<div id="diff"><?= $this->diff ?></div>
<p><a href="<?= $this->href ?>" class="tl_submit"><?= $this->back ?></a></p>
</div>
</div>

</body>
</html>
2 changes: 2 additions & 0 deletions src/Resources/contao/themes/flexible/conflict.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 54 additions & 0 deletions src/Resources/contao/themes/flexible/src/conflict.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Contao Open Source CMS
*
* Copyright (c) 2005-2017 Leo Feyer
*
* @license LGPL-3.0+
*/

/* Body */
body {
background:#eaeaea url("icons/stop.svg") center 3em no-repeat;
}

/* Container */
#container {
width:640px;
margin:14em auto 0;
background:#fff;
}
#main {
padding-bottom:6px;
}

/* Elements */
h1 {
padding:14px 18px 12px;
line-height:22px;
border-bottom:1px solid #ddd;
}
p {
line-height:1.3;
}
p a {
vertical-align:middle;
}
#diff {
margin:18px 0;
}
#diff,#main > p {
padding-left:18px;
padding-right:18px;
}
#main > p:first-of-type {
margin:18px 0 9px;
}

/* Handheld */
@media (max-width:767px) {
#container {
width:auto;
margin-left:10px;
margin-right:10px;
}
}
2 changes: 1 addition & 1 deletion tests/Controller/BackendCsvImportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function setUpBeforeClass()
*/
public static function tearDownAfterClass()
{
unset($GLOBALS['TL_LANG']['MSC']);
unset($GLOBALS['TL_LANG']);
}

/**
Expand Down
Loading

0 comments on commit 7e9fa2a

Please sign in to comment.