Skip to content

Commit 0130897

Browse files
committed
feat(form): single click to toggle default form flag
1 parent a608390 commit 0130897

File tree

3 files changed

+101
-12
lines changed

3 files changed

+101
-12
lines changed

ajax/form_toggle.php renamed to ajax/form.php

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,38 @@
3030
*/
3131

3232
include ('../../../inc/includes.php');
33-
Session::checkRight('entity', UPDATE);
33+
if (!Session::haveRight('entity', UPDATE)) {
34+
http_response_code(403);
35+
die();
36+
}
3437

3538
$form = PluginFormcreatorCommon::getForm();
3639

37-
$success = $form->update([
38-
'id' => $_POST['id'],
39-
'toggle' => 'toggle',
40-
]);
40+
if (!isset($_REQUEST['id']) || !isset($_REQUEST['action'])) {
41+
http_response_code(400);
42+
die();
43+
}
44+
45+
$success = false;
46+
switch ($_REQUEST['action']) {
47+
case 'toggle_active':
48+
$success = $form->update([
49+
'id' => $_POST['id'],
50+
'toggle' => 'active',
51+
]);
52+
break;
53+
54+
case 'toggle_default':
55+
$success = $form->update([
56+
'id' => $_POST['id'],
57+
'toggle' => 'default',
58+
]);
59+
break;
60+
61+
default:
62+
http_response_code(400);
63+
die();
64+
}
4165

4266
if (!$success) {
4367
http_response_code(500);

inc/form.class.php

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,20 @@ public function rawSearchOptions() {
288288
'massiveaction' => true
289289
];
290290

291+
$tab[] = [
292+
'id' => '35',
293+
'table' => $this::getTable(),
294+
'field' => 'is_default',
295+
'datatype' => 'specific',
296+
'searchtype' => [
297+
'0' => 'equals',
298+
'1' => 'notequals'
299+
],
300+
'name' => __('Default form', 'formcreator'),
301+
'searchtype' => ['equals'],
302+
'massiveaction' => true
303+
];
304+
291305
return $tab;
292306
}
293307

@@ -321,6 +335,17 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
321335
]);
322336
break;
323337

338+
case 'is_default' :
339+
return Dropdown::showFromArray($name, [
340+
'0' => __('Not default form'),
341+
'1' => __('Default form'),
342+
], [
343+
'value' => $values[$field],
344+
'display_emptychoice' => false,
345+
'display' => false
346+
]);
347+
break;
348+
324349
case 'access_rights' :
325350
return Dropdown::showFromArray(
326351
$name,
@@ -382,6 +407,25 @@ public static function getSpecificValueToDisplay($field, $values, array $options
382407
return $output;
383408
break;
384409

410+
case 'is_default':
411+
if ($values[$field] == 0) {
412+
$class = "plugin-formcreator-inactive";
413+
$title = __('Not default form', 'formcreator');
414+
} else {
415+
$class = "plugin-formcreator-active";
416+
$title = __('Default form', 'formcreator');
417+
}
418+
if (isset($options['raw_data']['id'])) {
419+
$output = '<i class="fa fa-circle '
420+
. $class
421+
. '" aria-hidden="true" title="' . $title . '"></i>';
422+
$output = '<div style="text-align: center" onclick="plugin_formcreator.toggleDefaultForm(' . $options['raw_data']['id']. ')">' . $output . '</div>';
423+
} else {
424+
$output = $title;
425+
}
426+
return $output;
427+
break;
428+
385429
case 'access_rights':
386430
switch ($values[$field]) {
387431
case self::ACCESS_PUBLIC :
@@ -1054,10 +1098,18 @@ public function post_updateItem($history = 1) {
10541098
public function prepareInputForUpdate($input) {
10551099
if (isset($input['toggle'])) {
10561100
// Enable / disable form
1057-
return [
1058-
'id' => $input['id'],
1059-
'is_active' => $this->fields['is_active'] == '0' ? '1' : '0',
1060-
];
1101+
if ($input['toggle'] == 'active') {
1102+
return [
1103+
'id' => $input['id'],
1104+
'is_active' => $this->fields['is_active'] == '0' ? '1' : '0',
1105+
];
1106+
}
1107+
if ($input['toggle'] == 'default') {
1108+
return [
1109+
'id' => $input['id'],
1110+
'is_default' => $this->fields['is_default'] == '0' ? '1' : '0',
1111+
];
1112+
}
10611113
}
10621114

10631115
if (isset($input['usage_count'])) {

js/scripts.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,12 +1140,25 @@ var plugin_formcreator = new function() {
11401140
}, 200);
11411141
});
11421142

1143-
this.toggleForm = function (id) {
1143+
this.toggleForm = function(id) {
11441144
$.ajax({
1145-
url: formcreatorRootDoc + '/ajax/form_toggle.php',
1145+
url: formcreatorRootDoc + '/ajax/form.php',
11461146
type: 'POST',
11471147
data: {
1148-
toggle: 'toggle',
1148+
action: 'toggle_active',
1149+
id: id
1150+
}
1151+
}).success(function () {
1152+
location.reload();
1153+
});
1154+
}
1155+
1156+
this.toggleDefaultForm = function(id) {
1157+
$.ajax({
1158+
url: formcreatorRootDoc + '/ajax/form.php',
1159+
type: 'POST',
1160+
data: {
1161+
action: 'toggle_default',
11491162
id: id
11501163
}
11511164
}).success(function () {

0 commit comments

Comments
 (0)