Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Copy feature #96

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions src/Contao/Widgets/MultiColumnWizard.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class MultiColumnWizard extends Widget
*/
protected $arrButtons = [
'new' => 'new.gif',
'copy' => 'copy.gif',
'delete' => 'delete.gif',
'move' => 'drag.gif'
];
Expand Down Expand Up @@ -594,6 +595,11 @@ protected function validator($varInput)
if ($blnHasError) {
$this->blnSubmitInput = false;
$this->addError($GLOBALS['TL_LANG']['ERR']['general']);
foreach ($this->arrWidgetErrors as $key => $rows) {
foreach ($rows as $rowNumber => $msg) {
$this->addError(sprintf('Row: %s | Widget: %s | Error: %s', $rowNumber, $key, $msg));
}
}
}

// Rebuild the order.
Expand Down Expand Up @@ -626,21 +632,26 @@ protected function validator($varInput)
*
* @param bool $onlyRows If true, only row's will be output.
*
* @return string The HTML code of the widget.
* @param bool $copyRow Flag to use the first row in the list for copy. If u use the validator
* function and have only one row, the validate function will set it as
* first value e.g. on key zero.
*
* @throws \Exception If something went wrong.
* @return string The HTML code of the widget.
*
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @SuppressWarnings(PHPMD.NPathComplexity)
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
* @SuppressWarnings(PHPMD.Superglobals)
* @SuppressWarnings(PHPMD.ShortVariable)
*/
public function generate($overwriteRowCurrentRow = null, $onlyRows = false)
{
public function generate(
$overwriteRowCurrentRow = null,
$onlyRows = false,
$copyRow = false
) {
/*
* Load the callback data if there's any
* (do not do this in __set() already because then we don't have access to currentRecord)
* (do not do this in __set() already, because then we don't have access to currentRecord)
*/

if (is_array($this->arrCallback)) {
Expand Down Expand Up @@ -726,7 +737,12 @@ public function generate($overwriteRowCurrentRow = null, $onlyRows = false)
$arrField = array_merge($arrField, $this->arrRowSpecificData[$i][$strKey]);
}

$objWidget = $this->initializeWidget($arrField, $i, $strKey, $this->varValue[$i][$strKey]);
// Switch to use a valid copy row or the current data.
if ($overwriteRowCurrentRow !== null && $copyRow === true) {
$objWidget = $this->initializeWidget($arrField, $i, $strKey, $this->varValue[0][$strKey]);
} else {
$objWidget = $this->initializeWidget($arrField, $i, $strKey, $this->varValue[$i][$strKey]);
}

// load errors if there are any
if (!empty($this->arrWidgetErrors[$strKey][$i])) {
Expand Down
69 changes: 65 additions & 4 deletions src/EventListener/Contao/ExecutePostActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,45 @@ public function __construct(EventDispatcherInterface $eventDispatcher)
}

/**
* Create a new row.
* We have to clear the data, because contao set for some widgets a default hidden input with a empty value.
* This force the system to set a empty value in the post and the controller knows there is an empty value.
* But since we use mootools to get the post data out of the form, this input makes some trouble.
*
* So we must check if the widget data are an array and have more than one value. If yes and the first value
* is a empty string we have to remove it and reset the key count.
*
* @param string $widgetName The name of the widget. Used for the post lookup.
*
* @return void
*/
private function clearPostForCopyCommand($widgetName)
{
if (empty($widgetName)) {
return;
}

$widgetData = Input::post($widgetName);

foreach ($widgetData as $rowId => $row) {
foreach ($row as $fieldName => $value) {
// See the php function doc, why this is okay.
if (!is_array($value) || 1 === count($value)) {
continue;
}

if ($value[0] === '') {
unset($value[0]);
$widgetData[$rowId][$fieldName] = array_values($value);
}
}
}

Input::setPost($widgetName, $widgetData);
}

/**
* Create a new row or make a copy from the given post data.
*
* Will call the event men-at-work.multi-column-wizard-bundle.create-widget to get the widget.
*
* @param string $action The action.
Expand All @@ -81,7 +119,8 @@ public function __construct(EventDispatcherInterface $eventDispatcher)
public function handleRowCreation($action, $container)
{
// Check the context.
if ('mcwCreateNewRow' != $action) {
if ('mcwCreateNewRow' !== $action
&& 'mcwCopyNewRow' !== $action) {
return;
}

Expand All @@ -90,7 +129,8 @@ public function handleRowCreation($action, $container)
if (!$container instanceof General) {
$container->inputName = $fieldName;
}
if (Input::get('act') == 'editAll') {

if ('editAll' === Input::get('act')) {
$fieldName = \preg_replace('/(.*)_[0-9a-zA-Z]+$/', '$1', $fieldName);
}

Expand Down Expand Up @@ -126,7 +166,28 @@ public function handleRowCreation($action, $container)
$maxRowCount = 0;
}

throw new ResponseException($this->convertToResponse($widget->generate(($maxRowCount + 1), true)));
// Handle the run. Since we check at the start which action it is, we don't need a else part.
$return = null;
if ('mcwCreateNewRow' === $action) {
$return = $widget->generate(($maxRowCount + 1), true);
}

if ('mcwCopyNewRow' === $action) {
$name = Input::post('name');
$this->clearPostForCopyCommand($name);
$widget->validate();
if ($widget->hasErrors()) {
// ToDo handle them.
}

$return = $widget->generate(($maxRowCount + 1), true, true);
}

if ($return === null) {
throw new BadRequestHttpException('Bad request');
}

throw new ResponseException($this->convertToResponse($return));
}

/**
Expand Down
Loading