Skip to content

Commit 73af77d

Browse files
committed
feat(form): condition on submit button
Signed-off-by: Thierry Bugier <[email protected]>
1 parent f3bb34a commit 73af77d

13 files changed

+209
-35
lines changed

ajax/condition.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,22 @@
6666
$section->getFromDB($sectionId);
6767
$form->getFromDBBySection($section);
6868
break;
69-
case PluginFormcreatorSection::class:
69+
case PluginFormcreatorSection::class:
7070
if (!isset($_REQUEST['plugin_formcreator_forms_id'])) {
7171
http_response_code(400);
7272
exit;
7373
}
7474
$formId = (int) $_REQUEST['plugin_formcreator_forms_id'];
7575
$form->getFromDB($formId);
7676
break;
77+
case PluginFormcreatorForm::class:
78+
if (!isset($_REQUEST['id'])) {
79+
http_response_code(400);
80+
exit;
81+
}
82+
$formId = (int) $_REQUEST['id'];
83+
$form->getFromDB($formId);
84+
break;
7785
}
7886

7987
if ($form->isNewItem()) {

front/form.form.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
// Edit an existing form
5151
Session::checkRight('entity', UPDATE);
5252
$form->update($_POST);
53+
$form->updateConditions($_POST);
5354
Html::back();
5455

5556
} else if (isset($_POST['delete'])) {

front/section.form.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,12 @@
4343
// Add a new Section
4444
Session::checkRight('entity', UPDATE);
4545
$section->add($_POST);
46-
$section->updateConditions($_POST);
4746
Html::back();
4847

4948
} else if (isset($_POST['update'])) {
5049
// Edit an existing section
5150
Session::checkRight("entity", UPDATE);
5251
$section->update($_POST);
53-
$section->updateConditions($_POST);
5452
Html::back();
5553

5654
} else if (isset($_POST['delete_section'])) {

inc/condition.class.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -217,18 +217,11 @@ public function getConditionsFromItem(PluginFormcreatorConditionnableInterface $
217217
*/
218218
public function showConditionsForItem($form, PluginFormcreatorConditionnableInterface $item) {
219219
$rand = mt_rand();
220-
echo '<tr>';
221-
echo '<th colspan="4">';
222-
echo '<label for="dropdown_show_rule'.$rand.'" id="label_show_type">';
223-
echo __('Show field', 'formcreator');
224-
echo '</label>';
225-
echo '</th>';
226-
echo '</tr>';
227220

228-
echo '<tr">';
221+
echo '<tr>';
229222
echo '<td colspan="4">';
230223
Dropdown::showFromArray(
231-
'show_rule',
224+
'show_rule',
232225
$this->getEnumShowRule(),
233226
[
234227
'value' => $item->fields['show_rule'],
@@ -239,15 +232,15 @@ public function showConditionsForItem($form, PluginFormcreatorConditionnableInte
239232
echo '</td>';
240233
echo '</tr>';
241234

242-
// Get conditionsexisting conditions for the item
235+
// Get existing conditions for the item
243236
$conditions = $this->getConditionsFromItem($item);
244237
reset($conditions);
245238
$condition = array_shift($conditions);
246239
if ($condition !== null) {
247-
echo $condition->getConditionHtml($form, PluginFormcreatorQuestion::class, 0, true);
240+
echo $condition->getConditionHtml($form, $condition->fields['itemtype'], $condition->fields['items_id'], true);
248241
}
249242
foreach ($conditions as $condition) {
250-
echo $condition->getConditionHtml($form, PluginFormcreatorQuestion::class, 0);
243+
echo $condition->getConditionHtml($form, $condition->fields['itemtype'], $condition->fields['items_id']);
251244
}
252245
}
253246

@@ -317,7 +310,6 @@ public function getConditionHtml($form, $itemtype, $itemId = 0, $isFirst = false
317310
$html.= '<div class="div_show_condition_field">';
318311
$html.= Dropdown::showFromArray('plugin_formcreator_questions_id[]', $questions_tab, [
319312
'display' => false,
320-
'used' => [$itemId => ''],
321313
'value' => $show_field,
322314
'rand' => $rand,
323315
]);

inc/fields.class.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ public static function isVisible(PluginFormcreatorConditionnableInterface $item,
138138
if ($item instanceof CommonDBChild) {
139139
if (is_subclass_of($item::$itemtype, PluginFormcreatorConditionnableInterface::class)) {
140140
if ($parent = $item->getItem(true, false)) {
141+
if ($parent->getType() == PluginFormcreatorForm::class) {
142+
// the condition for form is only for its submit button. A form is always visible
143+
return true;
144+
}
141145
// Use visibility of the parent item
142146
$evalItem[$itemtype][$itemId] = self::isVisible($parent, $fields);
143147
return $evalItem[$itemtype][$itemId];
@@ -331,6 +335,9 @@ public static function updateVisibility($input) {
331335
$fields[$id]->parseAnswerValues($input, true);
332336
}
333337

338+
// Get the visibility for the submit button of the form
339+
$submitShow = PluginFormcreatorFields::isVisible($form, $fields);
340+
334341
// Get the visibility result of questions
335342
$questionToShow = [];
336343
foreach ($fields as $id => $field) {
@@ -341,12 +348,13 @@ public static function updateVisibility($input) {
341348
$sectionToShow = [];
342349
$sections = (new PluginFormcreatorSection)->getSectionsFromForm($form->getID());
343350
foreach($sections as $section) {
344-
$sectionToShow[$section->getID()] = PluginFormcreatorFields::isVisible($section, $fields);;
351+
$sectionToShow[$section->getID()] = PluginFormcreatorFields::isVisible($section, $fields);
345352
}
346353

347354
return [
348355
PluginFormcreatorQuestion::class => $questionToShow,
349356
PluginFormcreatorSection::class => $sectionToShow,
357+
PluginFormcreatorForm::class => $submitShow,
350358
];
351359
}
352360

inc/form.class.php

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737

3838
class PluginFormcreatorForm extends CommonDBTM implements
3939
PluginFormcreatorExportableInterface,
40-
PluginFormcreatorDuplicatableInterface
40+
PluginFormcreatorDuplicatableInterface,
41+
PluginFormcreatorConditionnableInterface
4142
{
4243
static $rightname = 'entity';
4344

@@ -1718,6 +1719,14 @@ function export($remove_uuid = false) {
17181719
$form['_sections'][] = $form_section->export($remove_uuid);
17191720
}
17201721

1722+
// get submit conditions
1723+
$form['_conditions'] = [];
1724+
$condition = new PluginFormcreatorCondition();
1725+
$all_conditions = $condition->getConditionsFromItem($this);
1726+
foreach ($all_conditions as $condition) {
1727+
$form['_conditions'][] = $condition->export($remove_uuid);
1728+
}
1729+
17211730
// get validators
17221731
$form['_validators'] = [];
17231732
$all_validators = $DB->request([
@@ -2042,6 +2051,13 @@ public static function import(PluginFormcreatorLinker $linker, $input = [], $con
20422051
]);
20432052
}
20442053

2054+
// Import submit conditions
2055+
if (isset($input['_conditions'])) {
2056+
foreach ($input['_conditions'] as $condition) {
2057+
PluginFormcreatorCondition::import($linker, $condition, $itemId);
2058+
}
2059+
}
2060+
20452061
// import form's targets
20462062
if (isset($input['_targets'])) {
20472063
foreach ((new self())->getTargetTypes() as $targetType) {
@@ -2582,4 +2598,59 @@ public function post_getFromDB() {
25822598
$this->fields += \PluginFormcreatorSection::getFullData($this->fields['id']);
25832599
}
25842600
}
2601+
2602+
public function updateConditions($input) {
2603+
if (!isset($input['plugin_formcreator_questions_id']) || !isset($input['show_condition'])
2604+
|| !isset($input['show_value']) || !isset($input['show_logic'])) {
2605+
return false;
2606+
}
2607+
2608+
if (!is_array($input['plugin_formcreator_questions_id']) || !is_array($input['show_condition'])
2609+
|| !is_array($input['show_value']) || !is_array($input['show_logic'])) {
2610+
return false;
2611+
}
2612+
2613+
// All arrays of condition exists
2614+
if ($input['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) {
2615+
return false;
2616+
}
2617+
2618+
if (!(count($input['plugin_formcreator_questions_id']) == count($input['show_condition'])
2619+
&& count($input['show_value']) == count($input['show_logic'])
2620+
&& count($input['plugin_formcreator_questions_id']) == count($input['show_value']))) {
2621+
return false;
2622+
}
2623+
2624+
// Delete all existing conditions for the question
2625+
$condition = new PluginFormcreatorCondition();
2626+
$condition->deleteByCriteria([
2627+
'itemtype' => static::class,
2628+
'items_id' => $input['id'],
2629+
]);
2630+
2631+
// Arrays all have the same count and have at least one item
2632+
$order = 0;
2633+
while (count($input['plugin_formcreator_questions_id']) > 0) {
2634+
$order++;
2635+
$value = array_shift($input['show_value']);
2636+
$questionID = (int) array_shift($input['plugin_formcreator_questions_id']);
2637+
$showCondition = html_entity_decode(array_shift($input['show_condition']));
2638+
$showLogic = array_shift($input['show_logic']);
2639+
$condition = new PluginFormcreatorCondition();
2640+
$condition->add([
2641+
'itemtype' => static::class,
2642+
'items_id' => $input['id'],
2643+
'plugin_formcreator_questions_id' => $questionID,
2644+
'show_condition' => $showCondition,
2645+
'show_value' => $value,
2646+
'show_logic' => $showLogic,
2647+
'order' => $order,
2648+
]);
2649+
if ($condition->isNewItem()) {
2650+
return false;
2651+
}
2652+
}
2653+
2654+
return true;
2655+
}
25852656
}

inc/question.class.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,24 @@ public static function showForForm(CommonDBTM $item, $withtemplate = '') {
270270
echo '</tr>';
271271

272272
echo '</table>';
273+
274+
echo '<form name="plugin_formcreator_form" method="post" action="'.PluginFormcreatorForm::getFormURL().'">';
275+
echo '<table class="tab_cadre_fixe">';
276+
277+
echo '<tr>';
278+
echo '<th colspan="4">';
279+
echo __('Show submit button', 'formcreator');
280+
echo '</th>';
281+
echo '</tr>';
282+
$condition = new PluginFormcreatorCondition();
283+
$condition->showConditionsForItem($item, $item);
284+
285+
echo '</table>';
286+
287+
$item->showFormButtons([
288+
'candel' => false
289+
]);
290+
Html::closeForm();
273291
}
274292

275293
/**
@@ -585,11 +603,6 @@ public function setRequired($isRequired) {
585603
]);
586604
}
587605

588-
/**
589-
* Updates the conditions of the question
590-
* @param array $input
591-
* @return boolean true if success, false otherwise
592-
*/
593606
public function updateConditions($input) {
594607
if (!isset($input['plugin_formcreator_questions_id']) || !isset($input['show_condition'])
595608
|| !isset($input['show_value']) || !isset($input['show_logic'])) {
@@ -891,6 +904,11 @@ public function showForm($ID, $options = []) {
891904
echo '</td>';
892905
echo '</tr>';
893906

907+
echo '<tr>';
908+
echo '<th colspan="4">';
909+
echo __('Show field', 'formcreator');
910+
echo '</th>';
911+
echo '</tr>';
894912
$condition = new PluginFormcreatorCondition();
895913
$condition->showConditionsForItem($form, $this);
896914

inc/section.class.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
die("Sorry. You can't access this file directly");
3636
}
3737

38-
class PluginFormcreatorSection extends CommonDBChild implements
38+
class PluginFormcreatorSection extends CommonDBChild implements
3939
PluginFormcreatorExportableInterface,
4040
PluginFormcreatorDuplicatableInterface,
4141
PluginFormcreatorConditionnableInterface
@@ -350,7 +350,7 @@ public function showForm($ID, $options = []) {
350350
}
351351
echo '<form name="plugin_formcreator_form" method="post" action="'.static::getFormURL().'">';
352352
echo '<table class="tab_cadre_fixe">';
353-
353+
354354
echo '<tr>';
355355
echo '<th colspan="4">';
356356
echo $title;
@@ -364,6 +364,11 @@ public function showForm($ID, $options = []) {
364364
echo '</td>';
365365
echo '</tr>';
366366

367+
echo '<tr>';
368+
echo '<th colspan="4">';
369+
echo __('Show section', 'formcreator');
370+
echo '</th>';
371+
echo '</tr>';
367372
$form = new PluginFormcreatorForm();
368373
$form->getFromDBBySection($this);
369374
$condition = new PluginFormcreatorCondition();
@@ -433,12 +438,15 @@ public function post_getFromDB() {
433438
}
434439
}
435440

436-
/**
437-
* Updates the conditions of the question
438-
* @param array $input
439-
* @return boolean true if success, false otherwise
440-
*/
441-
public function updateConditions($input) {
441+
public function post_addItem() {
442+
$this->updateConditions($this->input);
443+
}
444+
445+
public function post_updateItem($history = 1) {
446+
$this->updateConditions($this->input);
447+
}
448+
449+
public function updateConditions($input) {
442450
if (!isset($input['plugin_formcreator_questions_id']) || !isset($input['show_condition'])
443451
|| !isset($input['show_value']) || !isset($input['show_logic'])) {
444452
return false;
@@ -478,7 +486,7 @@ public function updateConditions($input) {
478486
$condition = new PluginFormcreatorCondition();
479487
$condition->add([
480488
'itemtype' => static::class,
481-
'items_id' => $input['id'],
489+
'items_id' => $this->getID(),
482490
'plugin_formcreator_questions_id' => $questionID,
483491
'show_condition' => $showCondition,
484492
'show_value' => $value,

install/mysql/plugin_formcreator_empty.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ CREATE TABLE IF NOT EXISTS `glpi_plugin_formcreator_forms` (
5353
`validation_required` tinyint(1) NOT NULL DEFAULT '0',
5454
`usage_count` int(11) NOT NULL DEFAULT '0',
5555
`is_default` tinyint(1) NOT NULL DEFAULT '0',
56+
`show_rule` INT(11) NOT NULL DEFAULT '1' COMMENT 'Conditions setting to show the submit button',
5657
`uuid` varchar(255) DEFAULT NULL,
5758
PRIMARY KEY (`id`),
5859
INDEX `entities_id` (`entities_id`),

0 commit comments

Comments
 (0)