Skip to content

Commit

Permalink
feat(formaccesstype): massive action
Browse files Browse the repository at this point in the history
  • Loading branch information
btry committed May 12, 2023
1 parent 11c86b7 commit 53a4285
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 23 deletions.
11 changes: 6 additions & 5 deletions hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,12 @@ function plugin_formcreator_MassiveActions($itemtype) {
switch ($itemtype) {
case PluginFormcreatorForm::class:
return [
'PluginFormcreatorForm' . MassiveAction::CLASS_ACTION_SEPARATOR . 'Duplicate' => _x('button', 'Duplicate'),
'PluginFormcreatorForm' . MassiveAction::CLASS_ACTION_SEPARATOR . 'Transfert' => __('Transfer'),
'PluginFormcreatorForm' . MassiveAction::CLASS_ACTION_SEPARATOR . 'Export' => _sx('button', 'Export'),
PluginFormcreatorForm::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'Duplicate' => _x('button', 'Duplicate'),
PluginFormcreatorForm::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'Transfert' => __('Transfer'),
PluginFormcreatorForm::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'Export' => _sx('button', 'Export'),
PluginFormcreatorForm::class . MassiveAction::CLASS_ACTION_SEPARATOR . 'AccessRights' => _sx('button', 'Access rights'),
];
}
}
return [];
}

Expand Down Expand Up @@ -803,4 +804,4 @@ function plugin_formcreator_transfer(array $options) {
'tickets_id' => $options['newID'],
]);
}
}
}
64 changes: 56 additions & 8 deletions inc/form.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public function rawSearchOptions() {
'0' => 'equals',
'1' => 'notequals'
],
'massiveaction' => true
'massiveaction' => false
];

$tab[] = [
Expand Down Expand Up @@ -1195,16 +1195,16 @@ public function prepareInputForUpdate($input) {
return false;
}

if (!$this->checkConditionSettings($input)) {
$input['show_rule'] = PluginFormcreatorCondition::SHOW_RULE_ALWAYS;
}
// if (!$this->checkConditionSettings($input)) {
// $input['show_rule'] = PluginFormcreatorCondition::SHOW_RULE_ALWAYS;
// }

if (!$this->checkValidators($input)) {
$input['validation_required'] = self::VALIDATION_NONE;
}
// if (!$this->checkValidators($input)) {
// $input['validation_required'] = self::VALIDATION_NONE;
// }
}

return $input;
// return $input;
}

// Control fields values :
Expand Down Expand Up @@ -1239,6 +1239,13 @@ public function prepareInputForUpdate($input) {
}
}

if (isset($input['restrictions'])) {
$input['users'] = AbstractRightsDropdown::getPostedIds($input['restrictions'], User::class);
$input['groups'] = AbstractRightsDropdown::getPostedIds($input['restrictions'], Group::class);
$input['profiles'] = AbstractRightsDropdown::getPostedIds($input['restrictions'], Profile::class);
unset($input['restrictions']);
}

return $input;
}

Expand Down Expand Up @@ -1458,6 +1465,35 @@ public static function showMassiveActionsSubForm(MassiveAction $ma) {
]);
echo '<br /><br />' . Html::submit(_x('button', 'Post'), ['name' => 'massiveaction']);
return true;

case 'AccessRights':
echo '<p></p>';
Dropdown::showFromArray(
'access_rights',
PluginFormcreatorForm::getEnumAccessType(),
[
'value' => PluginFormcreatorForm::ACCESS_PRIVATE,
'on_change' => 'plugin_formcreator.showMassiveRestrictions(this)',
]
);
echo '<p></p>';
echo '<div id="plugin_formcreator_restrictions_head" style="display: none">';
echo PluginFormcreatorFormAccessType::getTypeName(2);
echo '</div>';
echo '<div id="plugin_formcreator_restrictions" style="display: none">';
echo PluginFormcreatorRestrictedFormDropdown::show('restrictions', [
'users_id' => [],
'groups_id' => [],
'profiles_id' => [],
]);
echo '</div>';
echo '<div id="plugin_formcreator_captcha" style="display: none">';
echo __('Enable captcha', 'formcreator') . '&nbsp;';
Dropdown::showYesNo('is_captcha_enabled');
echo '</div>';
echo '<br /><br />' . Html::submit(_x('button', 'Post'), ['name' => 'massiveaction']);
return true;

}
return parent::showMassiveActionsSubForm($ma);
}
Expand Down Expand Up @@ -1511,6 +1547,18 @@ public static function processMassiveActionsForOneItemtype(MassiveAction $ma, Co
Html::redirect(FORMCREATOR_ROOTDOC."/front/export.php?".Toolbox::append_params($listOfId));
header("Content-disposition:attachment filename=\"test\"");
return;

case 'AccessRights':
foreach ($ids as $id) {
if ($item->getFromDB($id) && $item->update($ma->POST + ['id' => $id])) {
Session::addMessageAfterRedirect(sprintf(__('Form updated: %s', 'formcreator'), $item->getName()));
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_OK);
} else {
// Example of ko count
$ma->itemDone($item->getType(), $id, MassiveAction::ACTION_KO);
}
}
return;
}
parent::processMassiveActionsForOneItemtype($ma, $item, $ids);
}
Expand Down
27 changes: 17 additions & 10 deletions inc/formaccesstype.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
'access_rights',
PluginFormcreatorForm::getEnumAccessType(),
[
'value' => (isset($item->fields['access_rights'])) ? $item->fields['access_rights'] : '1',
'value' => $item->fields['access_rights'] ?? PluginFormcreatorForm::ACCESS_PRIVATE,
'on_change' => 'plugin_formcreator.showMassiveRestrictions(this)',
]
);
echo '</td>';
Expand All @@ -94,26 +95,32 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
echo '</tr>';

// Captcha
if ($item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_PUBLIC) {
echo '<tr>';
$is_visible = $item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_PUBLIC;
// if ($item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_PUBLIC) {
echo '<tr id="plugin_formcreator_captcha" style="display: ' . ($is_visible ? 'block' : 'none') . '">';
echo '<td>' . __('Enable captcha', 'formcreator') . '</td>';
echo '<td>';
Dropdown::showYesNo('is_captcha_enabled', $item->fields['is_captcha_enabled']);
echo '</td>';
echo '</tr>';
}
// }

if ($item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
echo '<tr><th colspan="2">'.self::getTypeName(2).'</th></tr>';
echo '<tr>';
$is_visible = $item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_RESTRICTED;
// if ($item->fields["access_rights"] == PluginFormcreatorForm::ACCESS_RESTRICTED) {
echo '<tr id="plugin_formcreator_restrictions_head" style="display: ' . ($is_visible ? 'block' : 'none') . '">';
echo '<th colspan="2">' . self::getTypeName(2) . '</th>';
echo '</tr>';
echo '<tr id="plugin_formcreator_restrictions" style="display: ' . ($is_visible ? 'block' : 'none') . '">';
echo '<td><label>' . __('Restricted to') . '</label></td>';
echo '<td class="restricted-form">' . PluginFormcreatorRestrictedFormDropdown::show('restrictions', [
echo '<td class="restricted-form">';
echo PluginFormcreatorRestrictedFormDropdown::show('restrictions', [
'users_id' => $item->fields['users'] ?? [],
'groups_id' => $item->fields['groups'] ?? [],
'profiles_id' => $item->fields['profiles'] ?? [],
]) . '</td>';
]); // Does not supports display parameter to return the output instead of printing it
echo '</td>';
echo '</tr>';
}
// }

$formFk = PluginFormcreatorForm::getForeignKeyField();
echo '<tr>';
Expand Down
12 changes: 12 additions & 0 deletions js/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -1416,6 +1416,18 @@ var plugin_formcreator = new function() {
return item.name != '_glpi_csrf_token';
});
}

this.showMassiveRestrictions = function (item) {
document.querySelector('#plugin_formcreator_restrictions_head').style.display = 'none';
document.querySelector('#plugin_formcreator_restrictions').style.display = 'none';
document.querySelector('#plugin_formcreator_captcha').style.display = 'none';
if (item.value == 2 /* PluginFormcreatorForm::ACCESS_RESTRICTED */) {
document.querySelector('#plugin_formcreator_restrictions').style.display = 'block';
document.querySelector('#plugin_formcreator_restrictions_head').style.display = 'block';
} else if (item.value == 0 /* PluginFormcreatorForm::ACCESS_PUBLIC */) {
document.querySelector('#plugin_formcreator_captcha').style.display = 'block';
}
}
}

// === TARGETS ===
Expand Down

0 comments on commit 53a4285

Please sign in to comment.