From 09dda832a48004878c2fa659bb3ecc698462a7d5 Mon Sep 17 00:00:00 2001 From: Marc Michalsky Date: Wed, 6 Sep 2023 16:24:03 +0200 Subject: [PATCH] validate profile when copying --- CRM/Twingle/Form/Profile.php | 30 +++++++++++++++++++++++++----- CRM/Twingle/Profile.php | 8 +++++++- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index 996f22d..5205b52 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -180,12 +180,32 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { if (!$source_id) { $this->profile = CRM_Twingle_Profile::createDefaultProfile(); } else { - $source_profile = CRM_Twingle_Profile::getProfile($source_id); - $this->profile = $source_profile->copy(); + try { + $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')); break; diff --git a/CRM/Twingle/Profile.php b/CRM/Twingle/Profile.php index 335e95c..5d9f53a 100644 --- a/CRM/Twingle/Profile.php +++ b/CRM/Twingle/Profile.php @@ -80,8 +80,14 @@ class CRM_Twingle_Profile { */ public function copy() { $copy = clone $this; + + // Remove unique data $copy->id = NULL; $copy->data['selector'] = NULL; + + // Propose a new name for this profile. + $profile_name = $this->getName() . '_copy'; + $copy->setName($profile_name); 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 NULL; + throw new ProfileException('Profile not found.', ProfileException::ERROR_CODE_PROFILE_NOT_FOUND); } /**