Skip to content

Commit 24d6cc8

Browse files
committed
feat(question,section): drag drop accross sections
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent 26d2fdb commit 24d6cc8

File tree

4 files changed

+60
-21
lines changed

4 files changed

+60
-21
lines changed

ajax/question_move.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
http_response_code(403);
5050
echo __('You don\'t have right for this action', 'formcreator');
5151
exit;
52-
}
52+
}
5353
$questions[$id] = $question;
5454
}
5555

@@ -59,6 +59,8 @@
5959
$question->fields['row'] = (int) $_REQUEST['move'][$id]['y'];
6060
$question->fields['col'] = (int) $_REQUEST['move'][$id]['x'];
6161
$question->fields['width'] = (int) $_REQUEST['move'][$id]['width'];
62+
if (isset($_REQUEST['move'][$id]['plugin_formcreator_sections_id']))
63+
$question->fields['plugin_formcreator_sections_id'] = (int) $_REQUEST['move'][$id]['plugin_formcreator_sections_id'];
6264
$success = $question->change($question->fields);
6365
if (!$success) {
6466
$error = true;

css/styles.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ input.submit_button:focus {
173173
text-align: left;
174174
padding-left: 0;
175175
position: relative;
176+
min-height: 32px;
176177
}
177178

178179
#plugin_formcreator_form.plugin_formcreator_form_design .grid-stack-item {

inc/question.class.php

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -425,25 +425,25 @@ public function prepareInputForUpdate($input) {
425425
$oldId = $this->fields[$sectionFk];
426426
$newId = $input[$sectionFk];
427427
$row = $this->fields['row'];
428-
// Reorder other questions from the old section
429-
$DB->update(
430-
self::getTable(),
431-
new QueryExpression("`row` = `row` - 1"),
432-
[
433-
'row' => ['>', $row],
434-
$sectionFk => $oldId,
435-
]
436-
);
428+
// Reorder other questions from the old section (handled by code client side)
429+
// $DB->update(
430+
// self::getTable(),
431+
// new QueryExpression("`row` = `row` - 1"),
432+
// [
433+
// 'row' => ['>', $row],
434+
// $sectionFk => $oldId,
435+
// ]
436+
// );
437437

438438
// Get the order for the new question
439-
$maxRow = PluginFormcreatorCommon::getMax($this, [
440-
$sectionFk => $newId
441-
], 'row');
442-
if ($maxRow === null) {
443-
$input['row'] = 1;
444-
} else {
445-
$input['row'] = $maxRow + 1;
446-
}
439+
// $maxRow = PluginFormcreatorCommon::getMax($this, [
440+
// $sectionFk => $newId
441+
// ], 'row');
442+
// if ($maxRow === null) {
443+
// $input['row'] = 1;
444+
// } else {
445+
// $input['row'] = $maxRow + 1;
446+
// }
447447
}
448448
}
449449

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

464+
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
464465
if (isset($input['x'])) {
465466
if ($input['x'] < 0) {
466467
return false;
@@ -475,7 +476,6 @@ public function change($input) {
475476
if ($input['y'] < 0) {
476477
return false;
477478
}
478-
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
479479
$maxRow = 1 + PluginFormcreatorCommon::getMax(
480480
$this, [
481481
$sectionFk => $this->fields[$sectionFk]
@@ -508,14 +508,25 @@ public function change($input) {
508508
$height = $input['height'];
509509
}
510510

511-
$success = $this->update([
511+
if (isset($input[$sectionFk])) {
512+
$section = new PluginFormcreatorSection();
513+
if (!$section->getFromDB($input[$sectionFk])) {
514+
return false;
515+
}
516+
}
517+
518+
$input2 = [
512519
'id' => $this->getID(),
513520
'_skip_checks' => true,
514521
'col' => $x,
515522
'row' => $y,
516523
'width' => $width,
517524
'height' => $height,
518-
]);
525+
];
526+
if (isset($input[$sectionFk])) {
527+
$input2[$sectionFk] = $input[$sectionFk];
528+
}
529+
$success = $this->update($input2);
519530

520531
return $success;
521532
}

js/scripts.js.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,30 @@ function buildTiles(list) {
463463
// Remove empty rows
464464
plugin_formcreator.moveUpItems(group);
465465
});
466+
group.on('dropped', function (event, previousWidget, newWidget) {
467+
var changes = {};
468+
var section = $(newWidget.el).closest('[data-itemtype="PluginFormcreatorSection"]');
469+
var itemId = $(newWidget.el).attr('data-id');
470+
changes[itemId] = {
471+
plugin_formcreator_sections_id: section.attr('data-id'),
472+
width: newWidget.width,
473+
height: newWidget.height,
474+
x: newWidget.x,
475+
y: newWidget.y
476+
};
477+
$.ajax({
478+
'url': rootDoc + '/plugins/formcreator/ajax/question_move.php',
479+
type: 'POST',
480+
data: {
481+
move: changes,
482+
}
483+
}).fail(function() {
484+
plugin_formcreator.cancelChangeItems(event, items);
485+
plugin_formcreator.dirty = false;
486+
}).done(function(response) {
487+
plugin_formcreator.dirty = false;
488+
});
489+
});
466490
};
467491

468492
this.initGridStack = function (sectionId) {
@@ -474,6 +498,7 @@ function buildTiles(list) {
474498
cellHeight: '32px',
475499
verticalMargin: '5px',
476500
float: false,
501+
acceptWidgets: true,
477502
resizeable: {
478503
handles: 'e, w'
479504
}

0 commit comments

Comments
 (0)