Skip to content

Commit bfeabe5

Browse files
committed
feat(form): conditions for submot button
also refactor conditions on all itemtypes Signed-off-by: Thierry Bugier <tbugier@teclib.com>
1 parent f0a0ecd commit bfeabe5

14 files changed

+211
-245
lines changed

ajax/condition.php

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -32,72 +32,6 @@
3232
include ('../../../inc/includes.php');
3333
Session::checkRight('entity', UPDATE);
3434

35-
if (!isset($_REQUEST['items_id'])) {
36-
http_response_code(400);
37-
exit;
38-
}
39-
$itemId = (int) $_REQUEST['items_id'];
40-
41-
if (!isset($_REQUEST['itemtype'])) {
42-
http_response_code(400);
43-
exit;
44-
}
45-
$itemtype = $_REQUEST['itemtype'];
46-
47-
if (!class_exists($itemtype)) {
48-
http_response_code(400);
49-
exit;
50-
}
51-
52-
if (!is_subclass_of($itemtype, CommonDBTM::class)) {
53-
http_response_code(400);
54-
exit;
55-
}
56-
$item = new $itemtype();
57-
$parentType = $item::$itemtype;
58-
$parent = new $parentType();
59-
$parentFk = $parent::getForeignKeyField();
60-
if (!$item->getFromDB($itemId)) {
61-
// check for existence of container object
62-
if (!isset($_REQUEST[$parentFk])) {
63-
http_response_code(400);
64-
exit;
65-
}
66-
$parentId = (int) $_REQUEST[$parentFk];
67-
if (!$parent->getFromDB($parentId)) {
68-
http_response_code(400);
69-
exit;
70-
}
71-
// Set the relation of the empty item with the container
72-
$item->getEmpty();
73-
$item->fields[$parentFk] = $parentId;
74-
} else {
75-
$parentId = $item->fields[$parentFk];
76-
if (!$parent->getFromDB($parentId)) {
77-
http_response_code(400);
78-
exit;
79-
}
80-
}
81-
$form = new PluginFormcreatorForm();
82-
switch ($itemtype) {
83-
case PluginFormcreatorQuestion::class:
84-
85-
$form->getFromDBBySection($parent);
86-
break;
87-
case PluginFormcreatorSection::class:
88-
case PluginFormcreatorTargetTicket::class:
89-
case PluginFormcreatorTargetChange::class:
90-
$form->getFromDB($parentId);
91-
break;
92-
default:
93-
http_response_code(400);
94-
exit;
95-
}
96-
if ($form->isNewItem()) {
97-
http_response_code(400);
98-
exit;
99-
}
100-
10135
// get an empty condition HTML table row
10236
$condition = new PluginFormcreatorCondition();
103-
echo $condition->getConditionHtml($item);
37+
echo $condition->getConditionHtml($_POST);

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 {

inc/condition.class.php

Lines changed: 59 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,10 @@ public function export($remove_uuid = false) {
190190
/**
191191
* get conditions applied to an item
192192
*
193-
* @param PluginFormcreatorConditionnableInterface $item
193+
* @param CommonDBTM $item
194194
* @return array array of PluginFotrmcreatorCondition
195195
*/
196-
public function getConditionsFromItem(PluginFormcreatorConditionnableInterface $item) {
196+
public function getConditionsFromItem(CommonDBTM $item) {
197197
global $DB;
198198

199199
if ($item->isNewItem()) {
@@ -222,69 +222,65 @@ public function getConditionsFromItem(PluginFormcreatorConditionnableInterface $
222222
/**
223223
* Display HTML for conditions applied on an item
224224
*
225-
* @param PluginFormcreatorForm $form form of the item
226-
* @param PluginFormcreatorConditionnableInterface $item item where conditions applies to
225+
* @param CommonDBTM $item item where conditions applies to
227226
* @return void
228227
*/
229-
public function showConditionsForItem(PluginFormcreatorConditionnableInterface $item) {
228+
public function showConditionsForItem(CommonDBTM $item) {
230229
$rand = mt_rand();
231230

232-
echo '<tr">';
231+
echo '<tr>';
233232
echo '<td colspan="4">';
234233
Dropdown::showFromArray(
235-
'show_rule',
234+
'show_rule',
236235
$this->getEnumShowRule(),
237236
[
238237
'value' => $item->fields['show_rule'],
239-
'on_change' => 'plugin_formcreator_toggleCondition(this);',
238+
'on_change' => 'plugin_formcreator_toggleCondition(this, "' . $item->getType() . '");',
240239
'rand' => $rand,
241240
]
242241
);
243242
echo '</td>';
244243
echo '</tr>';
245244

246-
// Get conditionsexisting conditions for the item
245+
// Get existing conditions for the item
247246
$conditions = $this->getConditionsFromItem($item);
248-
reset($conditions);
249-
$condition = array_shift($conditions);
250-
if ($condition !== null) {
251-
echo $condition->getConditionHtml($item, true);
252-
}
253247
foreach ($conditions as $condition) {
254-
echo $condition->getConditionHtml($item);
248+
echo $condition->getConditionHtml($item->fields);
255249
}
256250
}
257251

258252
/**
259253
* return HTML to show a condition line for a question
260254
*
261-
* @param PluginFormcreatorConditionnableInterface $item ID of the question (or 0 for a new question)
262-
* @param boolean $isFirst true if this is the first condition in all conditions applied to a question
255+
* @param array $input
263256
*
264257
* @return string HTML to insert in a rendered web page
265258
*/
266-
public function getConditionHtml(PluginFormcreatorConditionnableInterface $item, $isFirst = false) {
267-
$itemtype = $item->getType();
268-
$itemId = $item->getID();
259+
public function getConditionHtml($input) {
269260
if ($this->isNewItem()) {
270-
$questionId = '';
271-
$show_condition = static::SHOW_CONDITION_EQ;
272-
$show_value = '';
273-
$show_logic = '';
261+
$this->getEmpty();
262+
$itemtype = $input['itemtype'];
263+
$itemId = $input['items_id'];
264+
$questionId = '';
265+
$show_condition = self::SHOW_CONDITION_EQ;
266+
$show_value = '';
267+
$show_logic = '';
274268
} else {
275-
$questionId = $this->fields['plugin_formcreator_questions_id'];
276-
$show_condition = $this->fields['show_condition'];
277-
$show_value = $this->fields['show_value'];
278-
$show_logic = $this->fields['show_logic'];
269+
$itemtype = $this->fields['itemtype'];
270+
$itemId = $this->fields['items_id'];
271+
$questionId = $this->fields['plugin_formcreator_questions_id'];
272+
$show_condition = $this->fields['show_condition'];
273+
$show_value = $this->fields['show_value'];
274+
$show_logic = $this->fields['show_logic'];
279275
}
280-
$rand = mt_rand();
276+
$item = new $itemtype();
281277

282278
// Get list of question in the form of the item
283-
$itemtype = $item->getType();
284279
if (!is_subclass_of($item, PluginFormcreatorConditionnableInterface::class)) {
285280
throw new Exception("$itemtype is not a " . PluginFormcreatorConditionnableInterface::class);
286281
}
287282

283+
$rand = mt_rand();
288284
$html = '';
289285
$html .= '<tr'
290286
. ' data-itemtype="' . self::class . '"'
@@ -294,10 +290,9 @@ public function getConditionHtml(PluginFormcreatorConditionnableInterface $item,
294290
$html .= '<div class="div_show_condition">';
295291

296292
// Boolean operator
297-
$showLogic = $isFirst ? 'style="display: none"' : '';
298-
$html.= '<div class="div_show_condition_logic"' . $showLogic . '>';
299-
$html.= Dropdown::showFromArray('show_logic[]',
300-
static::getEnumShowLogic(),
293+
$html.= '<div class="div_show_condition_logic">';
294+
$html.= Dropdown::showFromArray('_conditions[show_logic][]',
295+
self::getEnumShowLogic(),
301296
[
302297
'display' => false,
303298
'value' => $show_logic,
@@ -310,30 +305,45 @@ public function getConditionHtml(PluginFormcreatorConditionnableInterface $item,
310305
$form = new PluginFormcreatorForm();
311306
$questionListExclusion = [];
312307
switch ($itemtype) {
313-
case PluginFormcreatorQuestion::class:
314-
$questionListExclusion = [PluginFormcreatorQuestion::getTable() . '.id' => ['<>', $itemId]];
315-
$form->getFromDBByQuestion($item);
308+
case PluginFormcreatorForm::class:
309+
$form->getFromDB($itemId);
316310
break;
311+
317312
case PluginFormcreatorSection::class:
318313
$sectionFk = PluginFormcreatorSection::getForeignKeyField();
319314
$questionListExclusion = [PluginFormcreatorQuestion::getTable() . '.' . $sectionFk => ['<>', $itemId]];
320-
$form->getFromDBBySection($item);
321-
break;
322315
case PluginFormcreatorTargetTicket::class:
323316
case PluginFormcreatorTargetChange::class:
324-
$form->getFromDBByTarget($item);
317+
$form->getFromDB($input['plugin_formcreator_forms_id']);
318+
break;
319+
320+
case PluginFormcreatorQuestion::class:
321+
if ($item->isNewID($itemId)) {
322+
$parentItemtype = $item::$itemtype;;
323+
$section = new $parentItemtype();
324+
$section->getFromDB($input[$parentItemtype::getForeignKeyField()]);
325+
$form->getFromDBBySection($section);
326+
} else {
327+
$item->getFromDB($itemId);
328+
$form->getFromDBByQuestion($item);
329+
$questionListExclusion = [PluginFormcreatorQuestion::getTable() . '.id' => ['<>', $itemId]];
330+
}
325331
break;
332+
326333
default:
327334
throw new RuntimeException("Unsupported conditionnable");
335+
328336
}
329-
$questionsInForm = (new PluginFormcreatorQuestion)->getQuestionsFromFormBySection($form->getID(), $questionListExclusion);
337+
$questionsInForm = (new PluginFormcreatorQuestion())->getQuestionsFromFormBySection($form->getID(), $questionListExclusion);
330338
$html.= '<div class="div_show_condition_field">';
331-
$html.= Dropdown::showFromArray('plugin_formcreator_questions_id[]', $questionsInForm, [
332-
'display' => false,
333-
'used' => [$itemId => ''],
334-
'value' => $questionId,
335-
'rand' => $rand,
336-
]);
339+
$html.= Dropdown::showFromArray(
340+
'_conditions[plugin_formcreator_questions_id][]',
341+
$questionsInForm, [
342+
'display' => false,
343+
'value' => $questionId,
344+
'rand' => $rand,
345+
]
346+
);
337347
$html.= '</div>';
338348

339349
// Equality / inequality operator
@@ -346,7 +356,7 @@ function ($item) {
346356
);
347357

348358
$html.= Dropdown::showFromArray(
349-
'show_condition[]',
359+
'_conditions[show_condition][]',
350360
$showConditions, [
351361
'display' => false,
352362
'value' => $show_condition,
@@ -357,7 +367,7 @@ function ($item) {
357367

358368
// Value of comparison
359369
$html.= '<div class="div_show_condition_value">';
360-
$html.= Html::input('show_value[]', [
370+
$html.= Html::input('_conditions[show_value][]', [
361371
'class' => 'small_text',
362372
'size' => '8',
363373
'value' => $show_value,

inc/conditionnable.class.php

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@
3131

3232
trait PluginFormcreatorConditionnable
3333
{
34-
/**
35-
* Updates the conditions of the question
36-
* @param array $input
37-
* @return boolean true if success, false otherwise
38-
*/
39-
public function updateConditions($input) {
34+
public function updateConditions(CommonDBTM $item, $input) {
35+
$showRule = $input['show_rule'];
36+
if ($showRule == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) {
37+
return false;
38+
}
39+
40+
$input = $input['_conditions'];
41+
42+
// All arrays of condition exists
4043
if (!isset($input['plugin_formcreator_questions_id']) || !isset($input['show_condition'])
4144
|| !isset($input['show_value']) || !isset($input['show_logic'])) {
4245
return false;
@@ -47,18 +50,13 @@ public function updateConditions($input) {
4750
return false;
4851
}
4952

50-
// All arrays of condition exists
51-
if ($input['show_rule'] == PluginFormcreatorCondition::SHOW_RULE_ALWAYS) {
52-
return false;
53-
}
54-
5553
if (!(count($input['plugin_formcreator_questions_id']) == count($input['show_condition'])
5654
&& count($input['show_value']) == count($input['show_logic'])
5755
&& count($input['plugin_formcreator_questions_id']) == count($input['show_value']))) {
5856
return false;
5957
}
6058

61-
$itemtype = static::class;
59+
$itemtype = $item->getType();
6260
$itemId = $this->getID();
6361

6462
// Delete all existing conditions for the question
@@ -77,7 +75,7 @@ public function updateConditions($input) {
7775
$questionID = (int) array_shift($input[$questionFk]);
7876
$showCondition = html_entity_decode(array_shift($input['show_condition']));
7977
$showLogic = array_shift($input['show_logic']);
80-
$condition = new PluginFormcreatorCondition();
78+
$condition = new PluginFormcreatorCondition();
8179
$condition->add([
8280
'itemtype' => $itemtype,
8381
'items_id' => $itemId,

inc/conditionnableinterface.class.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@
3131

3232
interface PluginFormcreatorConditionnableInterface
3333
{
34-
/**
35-
* Updates the conditions of the question
36-
* @param array $input
37-
* @return boolean true if success, false otherwise
38-
*/
39-
public function updateConditions($input);
34+
/**
35+
* Updates the conditions of the question
36+
* @param CommonDBTM $item the item on which applies the conditions
37+
* @param array $input
38+
* @return boolean true if success, false otherwise
39+
*/
40+
public function updateConditions(CommonDBTM $item, $input);
4041
}

inc/fields.class.php

Lines changed: 7 additions & 0 deletions
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];
@@ -330,6 +334,8 @@ public static function updateVisibility($input) {
330334
foreach ($fields as $id => $field) {
331335
$fields[$id]->parseAnswerValues($input, true);
332336
}
337+
// Get the visibility for the submit button of the form
338+
$submitShow = PluginFormcreatorFields::isVisible($form, $fields);
333339

334340
// Get the visibility result of questions
335341
$questionToShow = [];
@@ -347,6 +353,7 @@ public static function updateVisibility($input) {
347353
return [
348354
PluginFormcreatorQuestion::class => $questionToShow,
349355
PluginFormcreatorSection::class => $sectionToShow,
356+
PluginFormcreatorForm::class => $submitShow,
350357
];
351358
}
352359

inc/fields/requesttypefield.class.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,4 +195,14 @@ public function isAnonymousFormCompatible() {
195195
public function getHtmlIcon() {
196196
return '<i class="fa fa-exclamation" aria-hidden="true"></i>';
197197
}
198+
199+
public function isVisibleField()
200+
{
201+
return true;
202+
}
203+
204+
public function isEditableField()
205+
{
206+
return true;
207+
}
198208
}

0 commit comments

Comments
 (0)