Skip to content

Commit

Permalink
fix(textfield): wrong method signature
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry committed Aug 22, 2019
1 parent 87d7bc5 commit 9683807
Show file tree
Hide file tree
Showing 2 changed files with 234 additions and 1 deletion.
2 changes: 1 addition & 1 deletion inc/fields/textfield.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ public static function canRequire() {
return true;
}

public function parseAnswerValues($input) {
public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!isset($input[$key])) {
return false;
Expand Down
233 changes: 233 additions & 0 deletions tests/fixture/PluginFormcreatorDependentField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
<?php
/**
* ---------------------------------------------------------------------
* Formcreator is a plugin which allows creation of custom forms of
* easy access.
* ---------------------------------------------------------------------
* LICENSE
*
* This file is part of Formcreator.
*
* Formcreator is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Formcreator is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Formcreator. If not, see <http://www.gnu.org/licenses/>.
* ---------------------------------------------------------------------
* @copyright Copyright © 2011 - 2019 Teclib'
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+
* @link https://github.com/pluginsGLPI/formcreator/
* @link https://pluginsglpi.github.io/formcreator/
* @link http://plugins.glpi-project.org/#/plugin/formcreator
* ---------------------------------------------------------------------
*/

use GlpiPlugin\Formcreator\Exception\ImportFailureException;

class PluginFormcreatorDependentField extends PluginFormcreatorField
{
public function isPrerequisites() {
return true;
}

public static function getName() {
return _n('User ID', 'User IDs', 1, 'formcreator');
}

public static function canRequire() {
return true;
}

public function getEmptyParameters() {
return [
'firstname' => new PluginFormcreatorQuestionDependency(
$this,
[
'fieldName' => 'firstname',
'label' => __('First name field', 'formcreator'),
'fieldType' => ['text'],
]),
'lastname' => new PluginFormcreatorQuestionDependency(
$this,
[
'fieldName' => 'lastname',
'label' => __('Last name field', 'formcreator'),
'fieldType' => ['text'],
]),
];
}

public function prepareQuestionInputForSave($input) {
$success = true;
$fieldType = $this->getFieldTypeName();
if ($input['_parameters'][$fieldType]['firstname']['plugin_formcreator_questions_id_2'] === '0') {
Session::addMessageAfterRedirect(__('No text field selected for firstname', 'formcreator'), false, ERROR);
$success = false;
}
if ($input['_parameters'][$fieldType]['lastname']['plugin_formcreator_questions_id_2'] === '0') {
Session::addMessageAfterRedirect(__('No text field selected for lastname', 'formcreator'), false, ERROR);
$success = false;
}
if (!$success) {
return false;
}

return $input;
}

public function serializeValue() {
if ($this->value === null || $this->value === '') {
return '';
}

return strval((int) $this->value);
}

public function deserializeValue($value) {
$this->value = ($value !== null && $value !== '')
? $value
: '';
}

public function show($canEdit = true) {
parent::show($canEdit);
$questionId = $this->fields['id'];
$domId = "input[name=\"formcreator_field_$questionId\"]";
$parameters = $this->getEmptyParameters();
foreach ($parameters as $fieldName => $parameter) {
$parameter->getFromDBByCrit([
'plugin_formcreator_questions_id' => $this->fields['id'],
'fieldname' => $fieldName,
]);
}
$firstnameQuestionId = $parameters['firstname']->getField('plugin_formcreator_questions_id_2');
$lastnameQuestionId = $parameters['lastname']->getField('plugin_formcreator_questions_id_2');
echo Html::scriptBlock("$(function() {
plugin_formcreator_field_$questionId()
$('input[name=\"formcreator_field_$firstnameQuestionId\"]').on('input', plugin_formcreator_field_$questionId)
$('input[name=\"formcreator_field_$lastnameQuestionId\"]').on('input', plugin_formcreator_field_$questionId)
})
function plugin_formcreator_field_$questionId() {
var firstname = $('input[name=\"formcreator_field_$firstnameQuestionId\"]').val().toUpperCase()
var lastname = $('input[name=\"formcreator_field_$lastnameQuestionId\"]').val().toUpperCase()
if (firstname.length < 2 || lastname.length < 2) {
$('$domId').val('')
return
}
$('$domId').val(lastname.substring(0, 2) + firstname.substring(0, 2))
}");
}

public function displayField($canEdit = true) {
$id = $this->fields['id'];
$rand = mt_rand();
$fieldName = 'formcreator_field_' . $id;
$domId = $fieldName . '_' . $rand;
if ($canEdit) {
echo '<input type="text" class="form-control" readonly="readonly"
name="' . $fieldName . '"
id="' . $domId . '"
value="' . $this->value . '" />';
} else {
echo $this->value;
}
}

public function getValueForDesign() {
if ($this->value === null) {
return '';
}

return $this->value;
}

public function getValueForTargetText($richText) {
return Toolbox::addslashes_deep($this->value);
}

public function getDocumentsForTarget() {
return [];
}

public function isValid() {
if ($this->isRequired() && $this->value === '') {
Session::addMessageAfterRedirect(
__('A required field is empty:', 'formcreator') . ' ' . $this->getLabel(),
false,
ERROR);
return false;
}
if (!$this->isValidValue($this->value)) {
Session::addMessageAfterRedirect(
__('A field does not match the expected format:', 'formcreator') . ' ' . $this->getLabel(),
false,
ERROR);
return false;
}

// All is OK
return true;
}

/**
* Checks the value of the field is in the expected form
* @param string $value the value of the field
*/
private function isValidValue($value) {
// TODO: use all fields of the form to check the scheme of the string
$parameters = $this->getEmptyParameters();
foreach ($parameters as $fieldname => $parameter) {
$parameter->getFromDBByCrit([
'plugin_formcreator_questions_id' => $this->fields['id'],
'fieldname' => $fieldname,
]);
}
$firstnameQuestionId = $parameters['firstname']->getField('plugin_formcreator_questions_id_2');
$lastnameQuestionId = $parameters['lastname']->getField('plugin_formcreator_questions_id_2');

$firstname = strtoupper($this->fields['answer']["formcreator_field_$firstnameQuestionId"]);
$lastname = strtoupper($this->fields['answer']["formcreator_field_$lastnameQuestionId"]);
if (strlen($firstname) < 2 || strlen($lastname) < 2) {
return false;
}
$expected = substr($lastname, 0, 2) . substr($firstname, 0, 2);
return ($value === $expected);
}

public function parseAnswerValues($input, $nonDestructive = false) {
$key = 'formcreator_field_' . $this->fields['id'];
if (!is_string($input[$key])) {
return false;
}

$this->value = $input[$key];
return true;
}

public function equals($value) {
return ($this->value) === ($value);
}

public function notEquals($value) {
return !$this->equals($value);
}

public function greaterThan($value) {
return ($this->value) > ($value);
}

public function lessThan($value) {
return !$this->greaterThan($value) && !$this->equals($value);
}

public function isAnonymousFormCompatible() {
return true;
}
}

0 comments on commit 9683807

Please sign in to comment.