Skip to content

Commit

Permalink
feat(form): single click to toggle default form flag
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed Mar 30, 2022
1 parent a608390 commit 0130897
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 12 deletions.
34 changes: 29 additions & 5 deletions ajax/form_toggle.php → ajax/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,38 @@
*/

include ('../../../inc/includes.php');
Session::checkRight('entity', UPDATE);
if (!Session::haveRight('entity', UPDATE)) {
http_response_code(403);
die();
}

$form = PluginFormcreatorCommon::getForm();

$success = $form->update([
'id' => $_POST['id'],
'toggle' => 'toggle',
]);
if (!isset($_REQUEST['id']) || !isset($_REQUEST['action'])) {
http_response_code(400);
die();
}

$success = false;
switch ($_REQUEST['action']) {
case 'toggle_active':
$success = $form->update([
'id' => $_POST['id'],
'toggle' => 'active',
]);
break;

case 'toggle_default':
$success = $form->update([
'id' => $_POST['id'],
'toggle' => 'default',
]);
break;

default:
http_response_code(400);
die();
}

if (!$success) {
http_response_code(500);
Expand Down
60 changes: 56 additions & 4 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,20 @@ public function rawSearchOptions() {
'massiveaction' => true
];

$tab[] = [
'id' => '35',
'table' => $this::getTable(),
'field' => 'is_default',
'datatype' => 'specific',
'searchtype' => [
'0' => 'equals',
'1' => 'notequals'
],
'name' => __('Default form', 'formcreator'),
'searchtype' => ['equals'],
'massiveaction' => true
];

return $tab;
}

Expand Down Expand Up @@ -321,6 +335,17 @@ public static function getSpecificValueToSelect($field, $name = '', $values = ''
]);
break;

case 'is_default' :
return Dropdown::showFromArray($name, [
'0' => __('Not default form'),
'1' => __('Default form'),
], [
'value' => $values[$field],
'display_emptychoice' => false,
'display' => false
]);
break;

case 'access_rights' :
return Dropdown::showFromArray(
$name,
Expand Down Expand Up @@ -382,6 +407,25 @@ public static function getSpecificValueToDisplay($field, $values, array $options
return $output;
break;

case 'is_default':
if ($values[$field] == 0) {
$class = "plugin-formcreator-inactive";
$title = __('Not default form', 'formcreator');
} else {
$class = "plugin-formcreator-active";
$title = __('Default form', 'formcreator');
}
if (isset($options['raw_data']['id'])) {
$output = '<i class="fa fa-circle '
. $class
. '" aria-hidden="true" title="' . $title . '"></i>';
$output = '<div style="text-align: center" onclick="plugin_formcreator.toggleDefaultForm(' . $options['raw_data']['id']. ')">' . $output . '</div>';
} else {
$output = $title;
}
return $output;
break;

case 'access_rights':
switch ($values[$field]) {
case self::ACCESS_PUBLIC :
Expand Down Expand Up @@ -1054,10 +1098,18 @@ public function post_updateItem($history = 1) {
public function prepareInputForUpdate($input) {
if (isset($input['toggle'])) {
// Enable / disable form
return [
'id' => $input['id'],
'is_active' => $this->fields['is_active'] == '0' ? '1' : '0',
];
if ($input['toggle'] == 'active') {
return [
'id' => $input['id'],
'is_active' => $this->fields['is_active'] == '0' ? '1' : '0',
];
}
if ($input['toggle'] == 'default') {
return [
'id' => $input['id'],
'is_default' => $this->fields['is_default'] == '0' ? '1' : '0',
];
}
}

if (isset($input['usage_count'])) {
Expand Down
19 changes: 16 additions & 3 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1140,12 +1140,25 @@ var plugin_formcreator = new function() {
}, 200);
});

this.toggleForm = function (id) {
this.toggleForm = function(id) {
$.ajax({
url: formcreatorRootDoc + '/ajax/form_toggle.php',
url: formcreatorRootDoc + '/ajax/form.php',
type: 'POST',
data: {
toggle: 'toggle',
action: 'toggle_active',
id: id
}
}).success(function () {
location.reload();
});
}

this.toggleDefaultForm = function(id) {
$.ajax({
url: formcreatorRootDoc + '/ajax/form.php',
type: 'POST',
data: {
action: 'toggle_default',
id: id
}
}).success(function () {
Expand Down

0 comments on commit 0130897

Please sign in to comment.