Skip to content

Commit

Permalink
fix(form): display and reload entered values
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Sep 18, 2018
1 parent bc418e7 commit 3f4342d
Show file tree
Hide file tree
Showing 25 changed files with 221 additions and 327 deletions.
19 changes: 0 additions & 19 deletions inc/field.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,7 @@ public function show($canEdit = true) {
echo '<div class="form_field">';
$this->displayField($canEdit);
echo '</div>';

echo '</div>';
$value = is_array($this->getAnswer()) ? json_encode($this->getAnswer()) : $this->getAnswer();
// $value = json_encode($this->getAnswer());
}

/**
Expand Down Expand Up @@ -121,22 +118,6 @@ public function getLabel() {
return $this->fields['name'];
}

/**
* Get the value of the answer
* @return mixed
*/
public function getValue() {
return $this->value;
}

/**
* Get the value of the answer
* @return mixed
*/
public function getAnswer() {
return $this->getValue();
}

/**
* Gets the available values for the field
* @return array available values
Expand Down
68 changes: 26 additions & 42 deletions inc/fields.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,37 +120,14 @@ public static function printAllTabFieldsForJS() {
return $tabFieldsForJS;
}

// /**
// *
// * @param array $field fields of a PluginFormcreatorQuestion instance
// * @param mixed|null $data
// * @param boolean $edit
// */
// public static function showField($field, $data = null, $edit = true) {
// // Get field types and file path
// $tab_field_types = self::getTypes();

// if (array_key_exists($field['fieldtype'], $tab_field_types)) {
// $fieldClass = 'PluginFormcreator'.ucfirst($field['fieldtype']).'Field';

// $plugin = new Plugin();
// if ($fieldClass == 'PluginFormcreatorTagField' && !$plugin->isActivated('tag')) {
// return;
// }

// $obj = new $fieldClass($field, $data);
// $obj->show($edit);
// }
// }

/**
* Check if a field should be shown or not
* Check if a question should be shown or not
*
* @param integer $id ID of the current question
* @param array $values Array of current fields values (id => value)
* @return boolean Should be shown or not
* @param integer $id ID of the question tested for visibility
* @param array $fields Array of fields instances (question id => instance)
* @return boolean If true the question should be visible
*/
public static function isVisible($id, $values) {
public static function isVisible($id, $fields) {
/**
* Keep track of questions being evaluated to detect infinite loops
*/
Expand Down Expand Up @@ -183,10 +160,10 @@ public static function isVisible($id, $values) {

foreach ($questionConditions as $question_condition) {
$conditions[] = [
'logic' => $question_condition->getField('show_logic'),
'field' => 'formcreator_field_' . $question_condition->getField('show_field'),
'operator' => $question_condition->getField('show_condition'),
'value' => $question_condition->getField('show_value')
'logic' => $question_condition->fields['show_logic'],
'field' => $question_condition->fields['show_field'],
'operator' => $question_condition->fields['show_condition'],
'value' => $question_condition->fields['show_value']
];
}

Expand All @@ -204,15 +181,11 @@ public static function isVisible($id, $values) {
// To ensure the low precedence return part is used at the end of the whole evaluation
$nextLogic = 'OR';
}
if (!isset($values[$condition['field']])) {
$values[$condition['field']] = '';
}

// TODO: find the best behavior if the question does not exists
$conditionQuestion = new PluginFormcreatorQuestion();
$conditionQuestion->getFromDB($condition['field']);
$conditionField = PluginFormcreatorFields::getFieldInstance($conditionQuestion->fields['fieldtype'], $conditionQuestion);
$conditionField->prepareAnswerValueForSave($values[$condition['field']]);
$conditionField = $fields[$condition['field']];
switch ($condition['operator']) {
case '!=' :
try {
Expand Down Expand Up @@ -319,15 +292,26 @@ public static function isVisible($id, $values) {
/**
* compute visibility of all fields of a form
*
* @param array $values values of all fields of the form
* id => mixed value of a field
* @param array $input values of all fields of the form
*
* @rturn array
*/
public static function updateVisibility($currentValues) {
public static function updateVisibility($input) {
$fields = [];
// Prepare form fields for validation
$question = new PluginFormcreatorQuestion();

$formId = $input['formcreator_form'];
$found_questions = $question->getQuestionsFromForm($formId);
foreach ($found_questions as $id => $question) {
$key = 'formcreator_field_' . $id;
$fields[$id] = PluginFormcreatorFields::getFieldInstance($question->fields['fieldtype'], $question);
$fields[$id]->parseAnswerValues($input);
}

$questionToShow = [];
foreach ($currentValues as $id => $value) {
$questionToShow[$id] = PluginFormcreatorFields::isVisible($id, $currentValues);
foreach ($input as $id => $value) {
$questionToShow[$id] = PluginFormcreatorFields::isVisible($id, $fields);
}

return $questionToShow;
Expand Down
6 changes: 1 addition & 5 deletions inc/fields/actorfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ public function displayField($canEdit = true) {
global $CFG_GLPI;

$readonly = $canEdit ? 'false' : 'true';
if (isset($this->value)) {
$value = $this->sanitizeValue($this->value);
} else {
$value = $this->sanitizeValue($this->fields['default_values']);
}
$value = $this->sanitizeValue($this->value);
$initialValue = [];
foreach ($value as $id => $item) {
$initialValue[] = [
Expand Down
29 changes: 13 additions & 16 deletions inc/fields/checkboxesfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ public function displayField($canEdit = true) {
$rand = mt_rand();
$fieldName = 'formcreator_field_' . $id;
$domId = $fieldName . '_' . $rand;
// echo '<input type="hidden" class="form-control"
// name="' . $fieldName . '" value="" />' . PHP_EOL;

$values = [];
$values = $this->getAvailableValues();
Expand All @@ -50,16 +48,15 @@ public function displayField($canEdit = true) {
foreach ($values as $value) {
if ((trim($value) != '')) {
$i++;
$current_value = null;
$current_value = $this->getValue();
$current_value = $this->value;
echo "<div class='checkbox'>";
echo Html::getCheckbox([
'title' => $value,
'id' => $domId.'_'.$i,
'name' => $fieldName . '[]',
'value' => $value,
'zero_on_empty' => false,
'checked' => (!empty($current_value) && in_array($value, $current_value))
'checked' => in_array($value, $this->value)
]);
echo '<label for="' . $domId . '_' . $i . '">';
echo '&nbsp;' . $value;
Expand All @@ -74,22 +71,22 @@ public function displayField($canEdit = true) {
});");

} else {
$answer = null;
$answer = $this->getAnswer();
if (!empty($answer)) {
if (is_array($answer)) {
echo implode("<br />", $answer);
} else if (is_array(json_decode($answer))) {
echo implode("<br />", json_decode($answer));
} else {
echo $this->getAnswer();
}
if (count($this->value)) {
echo implode('<br />', $this->value);
} else {
echo '';
}
}
}

/**
* Gets the available values for the field
* @return array available values
*/
public function getAvailableValues() {
return explode("\r\n", $this->fields['values']);
}

public function serializeValue() {
if ($this->value === null || $this->value === '') {
return '';
Expand Down Expand Up @@ -260,7 +257,7 @@ public function getEmptyParameters() {
}

public function equals($value) {
if (!is_array( $this->value)) {
if (!is_array($this->value)) {
// No checkbox enabled
return ($value === '');
}
Expand Down
19 changes: 3 additions & 16 deletions inc/fields/datefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,18 @@ public function displayField($canEdit = true) {
$required = ($canEdit && $this->fields['required']) ? ' required' : '';

Html::showDateField($fieldName, [
'value' => $this->getValue(),
'value' => (strtotime($this->value) != '') ? $this->value : '',
'rand' => $rand,
]);
echo Html::scriptBlock("$(function() {
pluginFormcreatorInitializeDate('$fieldName', '$rand');
});");

} else {
echo $this->getAnswer();
echo $this->value();
}
}

public function getValue() {
if (isset($this->value)) {
$date = $this->value;
} else {
$date = $this->fields['default_values'];
}
return (strtotime($date) != '') ? $date : null;
}

public function getAnswer() {
return Html::convDate($this->getValue());
}

public function serializeValue() {
return '';
}
Expand Down Expand Up @@ -118,7 +105,7 @@ public static function getJSFields() {
}

public function equals($value) {
if (empty($this->value)) {
if ($this->value === '') {
$answer = '0000-00-00 00:00';
} else {
$answer = $this->value;
Expand Down
19 changes: 3 additions & 16 deletions inc/fields/datetimefield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ public function displayField($canEdit = true) {
$required = ($canEdit && $this->fields['required']) ? ' required' : '';

Html::showDateTimeField($fieldName, [
'value' => $this->getValue(),
'value' => strtotime($this->value) != '' ? $this->value : '',
'rand' => $rand,
]);
echo Html::scriptBlock("$(function() {
pluginFormcreatorInitializeDate('$fieldName', '$rand');
});");

} else {
echo $this->getAnswer();
echo $this->value();
}
}

Expand All @@ -66,19 +66,6 @@ public function getValueForDesign() {
return '';
}

public function getValue() {
if (isset($this->value)) {
$date = $this->value;
} else {
$date = $this->fields['default_values'];
}
return (strtotime($date) != '') ? $date : null;
}

public function getAnswer() {
return Html::convDateTime($this->getValue());
}

public function isValid() {
// If the field is required it can't be empty
if ($this->isRequired() && (strtotime($this->value) == '')) {
Expand Down Expand Up @@ -118,7 +105,7 @@ public static function getJSFields() {
}

public function equals($value) {
if (empty($this->value)) {
if ($this->value === '') {
$answer = '0000-00-00 00:00';
} else {
$answer = $this->value;
Expand Down
21 changes: 2 additions & 19 deletions inc/fields/dropdownfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function displayField($canEdit = true) {
}

$dparams = ['name' => $fieldName,
'value' => $this->getValue(),
'value' => $this->value,
'comments' => false,
'rand' => $rand];

Expand Down Expand Up @@ -111,7 +111,7 @@ public function displayField($canEdit = true) {
pluginFormcreatorInitializeDropdown('$fieldName', '$rand');
});");
} else {
echo $this->getAnswer();
echo $this->value();
}
}

Expand All @@ -137,23 +137,6 @@ public function getValueForDesign() {
return $this->value;
}

public function getValue() {
if (isset($this->value)) {
return $this->value;
}
if (!empty($this->fields['default_values'])) {
return $this->fields['default_values'];
}
return 0;
}

public function getAnswer() {
$value = $this->getValue();
$DbUtil = new DbUtils();
$decodedValues = json_decode($this->fields['values'], JSON_OBJECT_AS_ARRAY);
return Dropdown::getDropdownName($DbUtil->getTableForItemType($decodedValues['itemtype']), $value);
}

public function prepareQuestionInputForTarget($input) {
$DbUtil = new DbUtils();
$decodedValues = json_decode($this->fields['values'], JSON_OBJECT_AS_ARRAY);
Expand Down
Loading

0 comments on commit 3f4342d

Please sign in to comment.