Skip to content

Commit

Permalink
refactor(formanswer): factorize retrival of fields and values
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <[email protected]>
  • Loading branch information
btry committed Jul 28, 2020
1 parent a0e0873 commit 5801968
Showing 1 changed file with 50 additions and 15 deletions.
65 changes: 50 additions & 15 deletions inc/formanswer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ class PluginFormcreatorFormAnswer extends CommonDBTM
const STATUS_REFUSED = 102;
const STATUS_ACCEPTED = 103;

/** @var $questionFields PluginFormcreatorField[] fields of the form answers */
private $questionFields = [];
/** @var null|PluginFormcreatorField[] fields of the form answers */
private $questionFields = null;

/** @var boolean True if the answers are loaded and are valid */
private $isAnswersValid = false;

public static function getStatuses() {
return [
Expand Down Expand Up @@ -1267,44 +1270,47 @@ public function parseTags($content, PluginFormcreatorTargetInterface $target, $r
* Validates answers of a form
*
* @param array $input fields from the HTML form
* @param bolean $checkValidator True if validator input must be checked
* @return boolean true if answers are valid, false otherwise
*/
protected function validateFormAnswer($input) {
protected function validateFormAnswer($input, $checkValidator = true) {
// Find the form the requester is answering to
$form = new PluginFormcreatorForm();
$form->getFromDB($input['plugin_formcreator_forms_id']);
$this->getQuestionFields($input['plugin_formcreator_forms_id']);

$valid = true;
// Parse form answers
$fieldValidities = [];

$this->questionFields = $form->getFields();
foreach ($this->questionFields as $id => $question) {
foreach (array_keys($this->questionFields) as $id) {
// Test integrity of the value
$fieldValidities[$id] = $this->questionFields[$id]->parseAnswerValues($input);
}
// any invalid field will invalidate the answers
$valid = !in_array(false, $fieldValidities, true);
$this->isAnswersValid = !in_array(false, $fieldValidities, true);

// Mandatory field must be filled
// and fields must contain a value matching the constraints of the field (range for example)
if ($valid) {
if ($this->isAnswersValid) {
foreach ($this->questionFields as $id => $field) {
if (!$this->questionFields[$id]->isPrerequisites()) {
continue;
}
if (PluginFormcreatorFields::isVisible($field->getQuestion(), $this->questionFields) && !$this->questionFields[$id]->isValid()) {
$valid = false;
$this->isAnswersValid = false;
break;
}
}
}

// Check required_validator
if ($form->fields['validation_required'] && empty($input['formcreator_validator'])) {
Session::addMessageAfterRedirect(__('You must select validator!', 'formcreator'), false, ERROR);
$valid = false;
if ($checkValidator) {
// Check required_validator
if ($form->fields['validation_required'] && empty($input['formcreator_validator'])) {
Session::addMessageAfterRedirect(__('You must select validator!', 'formcreator'), false, ERROR);
$this->isAnswersValid = false;
}
}

if (!$valid) {
if (!$this->isAnswersValid) {
// Save answers in session to display it again with the same values
$_SESSION['formcreator']['data'] = Toolbox::stripslashes_deep($input);
return false;
Expand Down Expand Up @@ -1612,4 +1618,33 @@ public static function getMyLastAnswersAsValidator($limit = 5) {

return $DB->request($request);
}

/**
* get all fields from a form
*
* @param integer $formId ID of the form where come the fileds to load
* @return PluginFormcreatorField[]
*/
private function getQuestionFields($formId) {
if ($this->questionFields !== null) {
return $this->questionFields;
}

$form = new PluginFormcreatorForm();
if ($form->isNewID($formId)) {
return [];
}
if (!$form->getFromDB($formId)) {
return [];
}

$this->questionFields = $form->getFields();
$this->isAnswersValid = false;

return $this->questionFields;
}

public function getIsAnswersValid() {
return $this->isAnswersValid;
}
}

0 comments on commit 5801968

Please sign in to comment.