let CRM_Twingle_Profile class handle its validation

This commit is contained in:
Marc Michalsky 2023-08-07 16:46:57 +02:00 committed by Jens Schuppe
parent aa2c938abe
commit 96c072eb8e
2 changed files with 163 additions and 96 deletions

View file

@ -553,89 +553,23 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
* TRUE when the form was successfully validated.
*/
public function validate() {
$values = $this->exportValues();
// Validate new profile names.
if (
isset($values['name'])
&& (!isset($this->profile) || $values['name'] != $this->profile->getName() || $this->_op != 'edit')
&& NULL !== CRM_Twingle_Profile::getProfile($values['name'])
) {
$this->_errors['name'] = E::ts('A profile with this name already exists.');
}
if (in_array($this->_op, ['create', 'edit', 'copy'])) {
// Create profile with new values
$profile_values = $this->exportValues();
$profile = new CRM_Twingle_Profile(
$profile_values['name'],
$profile_values,
$this->profile_id
);
// Restrict profile names to alphanumeric characters and the underscore.
if (isset($values['name']) && 1 === preg_match('/[^A-Za-z0-9\_]/', $values['name'])) {
$this->_errors['name'] =
E::ts('Only alphanumeric characters and the underscore (_) are allowed for profile names.');
}
// Validate custom field mapping.
try {
if (isset($values['custom_field_mapping'])) {
$custom_field_mapping = preg_split('/\r\n|\r|\n/', $values['custom_field_mapping'], -1, PREG_SPLIT_NO_EMPTY);
if (!is_array($custom_field_mapping)) {
throw new BaseException(
E::ts('Could not parse custom field mapping.')
);
}
foreach ($custom_field_mapping as $custom_field_map) {
$custom_field_map = explode('=', $custom_field_map);
if (count($custom_field_map) !== 2) {
throw new BaseException(
E::ts('Could not parse custom field mapping.')
);
}
[$twingle_field_name, $custom_field_name] = $custom_field_map;
$custom_field_id = substr($custom_field_name, strlen('custom_'));
// Check for custom field existence
try {
$custom_field = civicrm_api3('CustomField', 'getsingle', [
'id' => $custom_field_id,
]);
}
catch (CRM_Core_Exception $exception) {
throw new BaseException(
E::ts(
'Custom field custom_%1 does not exist.',
[1 => $custom_field_id]
),
NULL,
$exception
);
}
// Only allow custom fields on relevant entities.
try {
$custom_group = civicrm_api3('CustomGroup', 'getsingle', [
'id' => $custom_field['custom_group_id'],
'extends' => [
'IN' => [
'Contact',
'Individual',
'Organization',
'Contribution',
'ContributionRecur',
],
],
]);
}
catch (CRM_Core_Exception $exception) {
throw new BaseException(
E::ts(
'Custom field custom_%1 is not in a CustomGroup that extends one of the supported CiviCRM entities.',
[1 => $custom_field['id']]
),
NULL,
$exception
);
}
}
// Validate profile data
try {
$profile->validate();
}
catch (ProfileValidationError $e) {
$this->setElementError($e->getAffectedFieldName(), $e->getMessage());
}
}
catch (BaseException $exception) {
$this->_errors['custom_field_mapping'] = $exception->getMessage();
}
return parent::validate();