validate profile when copying

This commit is contained in:
Marc Michalsky 2023-09-06 16:24:03 +02:00 committed by Jens Schuppe
parent c7e51b4d3c
commit 09dda832a4
2 changed files with 32 additions and 6 deletions

View file

@ -180,12 +180,32 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form {
if (!$source_id) { if (!$source_id) {
$this->profile = CRM_Twingle_Profile::createDefaultProfile(); $this->profile = CRM_Twingle_Profile::createDefaultProfile();
} else { } else {
$source_profile = CRM_Twingle_Profile::getProfile($source_id); try {
$this->profile = $source_profile->copy(); $source_profile = CRM_Twingle_Profile::getProfile($source_id);
$this->profile = $source_profile->copy();
$this->profile->validate();
} catch (ProfileValidationError $e) {
if ($e->getErrorCode() == ProfileValidationError::ERROR_CODE_PROFILE_VALIDATION_FAILED) {
Civi::log()->error($e->getLogMessage());
CRM_Core_Session::setStatus(E::ts('The profile is invalid and cannot be copied.'), E::ts('Error'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/settings/twingle/profiles', 'reset=1'));
return;
}
} catch (ProfileException $e) {
if ($e->getErrorCode() == ProfileException::ERROR_CODE_PROFILE_NOT_FOUND) {
Civi::log()->error($e->getLogMessage());
CRM_Core_Session::setStatus(E::ts('The profile to be copied could not be found.'), E::ts('Error'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/settings/twingle/profiles', 'reset=1'));
return;
}
}
catch (Civi\Core\Exception\DBQueryException $e) {
Civi::log()->error($e->getMessage());
CRM_Core_Session::setStatus(E::ts('A database error has occurred. See the log for details.'), E::ts('Error'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/admin/settings/twingle/profiles', 'reset=1'));
return;
}
} }
// Propose a new name for this profile.
$profile_name = $this->profile->getName() . '_copy';
$this->profile->setName($profile_name);
CRM_Utils_System::setTitle(E::ts('New Twingle API profile')); CRM_Utils_System::setTitle(E::ts('New Twingle API profile'));
break; break;

View file

@ -80,8 +80,14 @@ class CRM_Twingle_Profile {
*/ */
public function copy() { public function copy() {
$copy = clone $this; $copy = clone $this;
// Remove unique data
$copy->id = NULL; $copy->id = NULL;
$copy->data['selector'] = NULL; $copy->data['selector'] = NULL;
// Propose a new name for this profile.
$profile_name = $this->getName() . '_copy';
$copy->setName($profile_name);
return $copy; return $copy;
} }
@ -484,7 +490,7 @@ class CRM_Twingle_Profile {
return new CRM_Twingle_Profile($profile_data->name, json_decode($profile_data->config, 1), (int) $profile_data->id); return new CRM_Twingle_Profile($profile_data->name, json_decode($profile_data->config, 1), (int) $profile_data->id);
} }
} }
return NULL; throw new ProfileException('Profile not found.', ProfileException::ERROR_CODE_PROFILE_NOT_FOUND);
} }
/** /**