Commit b6f342a8 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Implements #7639. Check for id/_id and ord/_ord. Use the given columname....

Implements #7639. Check for id/_id and ord/_ord. Use the given columname. Throw meaningful exception if missing.
parent 70ddd6cb
Pipeline #2749 passed with stages
in 3 minutes and 20 seconds
......@@ -122,28 +122,35 @@ class DragAndDrop {
$ord = $orderInterval;
$ordDragOld = -1;
$data = array();
$nameId = false;
$nameOrd = false;
// Reorder. Get index for 'drag' and 'hover'
foreach ($rows as $key => $row) {
// the dragged element: skip old position.
if ($row[DND_COLUMN_ID] == $dragId) {
$ordDragOld = $row[DND_COLUMN_ORD];
if ($nameId === false) {
$nameId = $this->getFinalColumnName(DND_COLUMN_ID, $row);
$nameOrd = $this->getFinalColumnName(DND_COLUMN_ORD, $row);
}
// The dragged element: skip old position.
if ($row[$nameId] == $dragId) {
$ordDragOld = $row[$nameOrd];
continue;
}
// the dragged element: new position.
if ($row[DND_COLUMN_ID] == $hoverId) {
// The dragged element: new position.
if ($row[$nameId] == $hoverId) {
switch ($setTo) {
case DND_SET_TO_BEFORE:
$data = $this->setNewOrder($tableName, $orderColumn, $dragId, $ordDragOld, $ord, $data);
$ord += $orderInterval;
$data = $this->setNewOrder($tableName, $orderColumn, $row[DND_COLUMN_ID], $row[DND_COLUMN_ORD], $ord, $data);
$data = $this->setNewOrder($tableName, $orderColumn, $row[$nameId], $row[$nameOrd], $ord, $data);
break;
case DND_SET_TO_AFTER:
$data = $this->setNewOrder($tableName, $orderColumn, $row[DND_COLUMN_ID], $row[DND_COLUMN_ORD], $ord, $data);
$data = $this->setNewOrder($tableName, $orderColumn, $row[$nameId], $row[$nameOrd], $ord, $data);
$ord += $orderInterval;
$data = $this->setNewOrder($tableName, $orderColumn, $dragId, $ordDragOld, $ord, $data);
break;
......@@ -152,7 +159,7 @@ class DragAndDrop {
throw new \CodeException(json_encode([ERROR_MESSAGE_TO_USER => 'Unknown "setTo" string', ERROR_MESSAGE_TO_DEVELOPER => "Token found: " . $setTo]), ERROR_UNKNOWN_TOKEN);
}
} else {
$data = $this->setNewOrder($tableName, $orderColumn, $row[DND_COLUMN_ID], $row[DND_COLUMN_ORD], $ord, $data);
$data = $this->setNewOrder($tableName, $orderColumn, $row[$nameId], $row[$nameOrd], $ord, $data);
}
$ord += $orderInterval;
}
......@@ -160,6 +167,25 @@ class DragAndDrop {
return $data;
}
/**
* Check if there is a column called '_' . $name. If yes, return that name, else return $name.
* If none is found, throw an exception.
*
* @param $name
* @param $row
* @return string
* @throws \UserFormException
*/
private function getFinalColumnName($name, $row) {
if (!isset($row[$name]) && !isset($row['_' . $name])) {
throw new \UserFormException(json_encode([ERROR_MESSAGE_TO_USER => "Missing column '$name' or '_$name'", ERROR_MESSAGE_TO_DEVELOPER => "Check your DND SQL statement"]),
ERROR_MISSING_REQUIRED_PARAMETER);
}
return isset($row['_' . $name]) ? '_' . $name : $name;
}
/**
* @param string $tableName
* @param string $orderColumn
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment