Skip to content

Commit

Permalink
Get & validate custom field using standard API
Browse files Browse the repository at this point in the history
Use mci_get_custom_field_id_from_objectref() to retrieve the custom
field's id.

Throw exceptions if
- the returned Id == 0 (invalid field)
- the field is not linked to the current project

Fixes #26541
  • Loading branch information
dregad committed Feb 17, 2020
1 parent 021304c commit b01b677
Showing 1 changed file with 17 additions and 27 deletions.
44 changes: 17 additions & 27 deletions api/soap/mc_project_api.php
Expand Up @@ -637,16 +637,6 @@ function mci_project_custom_fields_validate( $p_project_id, &$p_custom_fields )
$t_custom_field_defs[$t_custom_field_id] = $t_def;
}

$fn_normalize_name = function( $p_name, $p_custom_field_defs ) {
foreach( $p_custom_field_defs as $t_custom_field_def ) {
if( strcasecmp( $t_custom_field_def['name'], $p_name ) == 0 ) {
return $t_custom_field_def['name'];
}
}

return $p_name;
};

$t_custom_field_values = array();
if( isset( $p_custom_fields ) ) {
if( !is_array( $p_custom_fields ) ) {
Expand Down Expand Up @@ -674,25 +664,25 @@ function mci_project_custom_fields_validate( $p_project_id, &$p_custom_fields )
);
}

$t_custom_field['field'] = ApiObjectFactory::objectToArray( $t_custom_field['field'] );

if( isset( $t_custom_field['field']['id'] ) ) {
$t_def = $t_custom_field_defs[(int)$t_custom_field['field']['id']];
$t_custom_field_id = mci_get_custom_field_id_from_objectref( (object)$t_custom_field['field'] );
if( $t_custom_field_id == 0 ) {
throw new ClientException(
'Invalid Custom Field '
# Output JSON stripped of quotes to help caller identify offending field
. str_replace( '"', '', json_encode( $t_custom_field['field'] ) ),
ERROR_CUSTOM_FIELD_NOT_FOUND
);
} else {
# Make sure the custom field is linked to the current project
if( !isset( $t_custom_field_defs[$t_custom_field_id] ) ) {
throw new ClientException(
"Custom Field Id '$t_custom_field_id' not found in Project '$p_project_id'.",
ERROR_CUSTOM_FIELD_NOT_FOUND
);
}
$t_def = $t_custom_field_defs[$t_custom_field_id];
$t_custom_field_values[$t_def['name']] = $t_custom_field['value'];
continue;
}

if( isset( $t_custom_field['field']['name'] ) ) {
$t_name = $fn_normalize_name( $t_custom_field['field']['name'], $t_custom_field_defs );
$t_custom_field_values[$t_name] = $t_custom_field['value'];
continue;
}

throw new ClientException(
'Custom field with no specified id or name.',
ERROR_EMPTY_FIELD,
"custom_field['field']['id']"
);
}
}

Expand Down

0 comments on commit b01b677

Please sign in to comment.