diff --git a/AnnotationParser.php b/AnnotationParser.php index 7d57874..69e0d63 100644 --- a/AnnotationParser.php +++ b/AnnotationParser.php @@ -108,6 +108,34 @@ public static function getListenersFromAnot($field_annotation, $field, $annotati } } break; + case "@REPLACE": + $selectedFields = explode(",", $sourceField); + $badfield = false; + if (count($selectedFields) != 3) { + $badfield = true; + $warnings [] = 'REPLACE for '.$sourceField.' is badly formatted.'; + } else { + $field0 = trim($selectedFields[0]); // From + $field1 = trim($selectedFields[1]); // What to replace + $field2 = trim($selectedFields[2]); // replace with. + if (!in_array($field0, $fieldNames)) { + $warnings [] = 'For field: ' . $destinationField . ' ' . $field0 . ' is not a field.'; + $badfield = true; + } + if (!in_array($field1, $fieldNames)) { + $warnings [] = 'For field: ' . $destinationField . ' ' . $field1 . ' is not a field.'; + $badfield = true; + } + if (!in_array($field2, $fieldNames)) { + $warnings [] = 'For field: ' . $destinationField . ' ' . $field2 . ' is not a field.'; + $badfield = true; + } + + } + if (!$badfield){ + @$listeners[$field0] .= '$("input[name=\'' . $destinationField . '\']").val($("input[name=\'' . $field0 . '\']").val().split($("input[name=\'' . $field1 . '\']").val()).join( $("input[name=\'' . $field2. '\']").val())).change();'; + } + break; default: break; } diff --git a/StringUtils.php b/StringUtils.php index 8dd7966..5d1e63d 100644 --- a/StringUtils.php +++ b/StringUtils.php @@ -9,7 +9,7 @@ class StringUtils extends \ExternalModules\AbstractExternalModule { - const annotation = ["@TOLOWER", "@TOUPPER", "@SUBSTR", "@LTRIM", "@RTRIM", "@TRIM", "@STRLEN", "@INDEXOF", "@CONCAT", "@RIGHT", "@LEFT"]; + const annotation = ["@TOLOWER", "@TOUPPER", "@SUBSTR", "@LTRIM", "@RTRIM", "@TRIM", "@STRLEN", "@INDEXOF", "@CONCAT", "@RIGHT", "@LEFT", "@REPLACE"]; public function redcap_data_entry_form($project_id, $record, $instrument, $event_id, $group_id, $repeat_instance) diff --git a/StringUtilsTest.php b/StringUtilsTest.php index 6d3b03e..e4066d6 100644 --- a/StringUtilsTest.php +++ b/StringUtilsTest.php @@ -68,4 +68,8 @@ assert(count($listeners) == 3); assert(array_key_exists("img_1", $listeners)); assert(array_key_exists("url", $listeners)); -assert(array_key_exists("img_2", $listeners)); \ No newline at end of file +assert(array_key_exists("img_2", $listeners)); + +list($warnings, $listeners) = $stringUtils->getListenersFromAnot("@REPLACE=sourceField, searchvaluefield, newvaluefield", ["field_name" => "resultField"], "@REPLACE", [], [], ["resultField", "sourceField", "searchvaluefield", "newvaluefield"]); +assert(count($warnings) == 0); +assert(count($listeners) == 1); \ No newline at end of file diff --git a/howto.html b/howto.html index 7d0bd1c..a03b8f3 100644 --- a/howto.html +++ b/howto.html @@ -125,6 +125,14 @@

How to use the StringUti Concatenates other text fields into one field. The fields must be valid field names. The list can contain any amount of fields. + + + @REPLACE=sourceDataField, SearchTextField, ReplaceByTextField + + + Searches in the sourceDataField for a specified value defined in SearchTextField and returns a new string where all the specified values in ReplaceByTextField are replaced. All parameters are field names.The fields must be valid field names. + +