Skip to content

Commit

Permalink
feat(question,section): drag drop accross sections
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Mar 13, 2020
1 parent 26d2fdb commit 24d6cc8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 21 deletions.
4 changes: 3 additions & 1 deletion ajax/question_move.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
http_response_code(403);
echo __('You don\'t have right for this action', 'formcreator');
exit;
}
}
$questions[$id] = $question;
}

Expand All @@ -59,6 +59,8 @@
$question->fields['row'] = (int) $_REQUEST['move'][$id]['y'];
$question->fields['col'] = (int) $_REQUEST['move'][$id]['x'];
$question->fields['width'] = (int) $_REQUEST['move'][$id]['width'];
if (isset($_REQUEST['move'][$id]['plugin_formcreator_sections_id']))
$question->fields['plugin_formcreator_sections_id'] = (int) $_REQUEST['move'][$id]['plugin_formcreator_sections_id'];
$success = $question->change($question->fields);
if (!$success) {
$error = true;
Expand Down
1 change: 1 addition & 0 deletions css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ input.submit_button:focus {
text-align: left;
padding-left: 0;
position: relative;
min-height: 32px;
}

#plugin_formcreator_form.plugin_formcreator_form_design .grid-stack-item {
Expand Down
51 changes: 31 additions & 20 deletions inc/question.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -425,25 +425,25 @@ public function prepareInputForUpdate($input) {
$oldId = $this->fields[$sectionFk];
$newId = $input[$sectionFk];
$row = $this->fields['row'];
// Reorder other questions from the old section
$DB->update(
self::getTable(),
new QueryExpression("`row` = `row` - 1"),
[
'row' => ['>', $row],
$sectionFk => $oldId,
]
);
// Reorder other questions from the old section (handled by code client side)
// $DB->update(
// self::getTable(),
// new QueryExpression("`row` = `row` - 1"),
// [
// 'row' => ['>', $row],
// $sectionFk => $oldId,
// ]
// );

// Get the order for the new question
$maxRow = PluginFormcreatorCommon::getMax($this, [
$sectionFk => $newId
], 'row');
if ($maxRow === null) {
$input['row'] = 1;
} else {
$input['row'] = $maxRow + 1;
}
// $maxRow = PluginFormcreatorCommon::getMax($this, [
// $sectionFk => $newId
// ], 'row');
// if ($maxRow === null) {
// $input['row'] = 1;
// } else {
// $input['row'] = $maxRow + 1;
// }
}
}

Expand All @@ -461,6 +461,7 @@ public function change($input) {
$width = $this->fields['width'];
$height = 1;

$sectionFk = PluginFormcreatorSection::getForeignKeyField();
if (isset($input['x'])) {
if ($input['x'] < 0) {
return false;
Expand All @@ -475,7 +476,6 @@ public function change($input) {
if ($input['y'] < 0) {
return false;
}
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
$maxRow = 1 + PluginFormcreatorCommon::getMax(
$this, [
$sectionFk => $this->fields[$sectionFk]
Expand Down Expand Up @@ -508,14 +508,25 @@ public function change($input) {
$height = $input['height'];
}

$success = $this->update([
if (isset($input[$sectionFk])) {
$section = new PluginFormcreatorSection();
if (!$section->getFromDB($input[$sectionFk])) {
return false;
}
}

$input2 = [
'id' => $this->getID(),
'_skip_checks' => true,
'col' => $x,
'row' => $y,
'width' => $width,
'height' => $height,
]);
];
if (isset($input[$sectionFk])) {
$input2[$sectionFk] = $input[$sectionFk];
}
$success = $this->update($input2);

return $success;
}
Expand Down
25 changes: 25 additions & 0 deletions js/scripts.js.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,30 @@ function buildTiles(list) {
// Remove empty rows
plugin_formcreator.moveUpItems(group);
});
group.on('dropped', function (event, previousWidget, newWidget) {
var changes = {};
var section = $(newWidget.el).closest('[data-itemtype="PluginFormcreatorSection"]');
var itemId = $(newWidget.el).attr('data-id');
changes[itemId] = {
plugin_formcreator_sections_id: section.attr('data-id'),
width: newWidget.width,
height: newWidget.height,
x: newWidget.x,
y: newWidget.y
};
$.ajax({
'url': rootDoc + '/plugins/formcreator/ajax/question_move.php',
type: 'POST',
data: {
move: changes,
}
}).fail(function() {
plugin_formcreator.cancelChangeItems(event, items);
plugin_formcreator.dirty = false;
}).done(function(response) {
plugin_formcreator.dirty = false;
});
});
};

this.initGridStack = function (sectionId) {
Expand All @@ -474,6 +498,7 @@ function buildTiles(list) {
cellHeight: '32px',
verticalMargin: '5px',
float: false,
acceptWidgets: true,
resizeable: {
handles: 'e, w'
}
Expand Down

0 comments on commit 24d6cc8

Please sign in to comment.