Skip to content

Commit

Permalink
Added REPLACE.
Browse files Browse the repository at this point in the history
  • Loading branch information
denshade committed Apr 10, 2020
1 parent a8865a7 commit 7634614
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
28 changes: 28 additions & 0 deletions AnnotationParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion StringUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 5 additions & 1 deletion StringUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
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);
8 changes: 8 additions & 0 deletions howto.html
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ <h1><span class="glyphicon glyphicon-info-sign"></span> How to use the StringUti
Concatenates other <b>text</b> fields into one field. The fields must be valid field names. The list can contain any amount of fields.
</td>
</tr>
<tr>
<td>
@REPLACE=sourceDataField, SearchTextField, ReplaceByTextField
</td>
<td>
Searches in the <b>sourceDataField</b> for a specified value defined in <b>SearchTextField</b> and returns a new string where <strong>all</strong> the specified values in <b>ReplaceByTextField</b> are replaced. All parameters are field names.The fields must be valid field names.
</td>
</tr>
</tbody>
</table>
</div>
Expand Down

0 comments on commit 7634614

Please sign in to comment.