From 228e964633315df93bdd719dc19c2f6aa9ed0022 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Tue, 10 Nov 2020 11:30:58 +0100 Subject: [PATCH] [#29] Use correct profile names and validate before saving profile copies --- CRM/Twingle/Form/Profile.php | 38 ++++++++++++++----------- templates/CRM/Twingle/Page/Profiles.tpl | 2 +- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index be9dd27..e00a339 100644 --- a/CRM/Twingle/Form/Profile.php +++ b/CRM/Twingle/Form/Profile.php @@ -139,7 +139,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { $this->_op = 'create'; } - // Verify that profile with the given name exists. + // Verify that a profile with the given name exists. $profile_name = CRM_Utils_Request::retrieve('name', 'String', $this); if (!$this->profile = CRM_Twingle_Profile::getProfile($profile_name)) { $profile_name = NULL; @@ -174,20 +174,22 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { // This will be a 'create' actually. $this->_op = 'create'; + // Retrieve the source profile name. + $profile_name = CRM_Utils_Request::retrieve('source_name', 'String', $this); // When copying without a valid profile name, copy the default profile. if (!$profile_name) { $profile_name = 'default'; - $this->profile = CRM_Twingle_Profile::getProfile($profile_name); } + $this->profile = CRM_Twingle_Profile::getProfile($profile_name); - // Set a new name for this profile. + // Propose a new name for this profile. $profile_name = $profile_name . '_copy'; $this->profile->setName($profile_name); CRM_Utils_System::setTitle(E::ts('New Twingle API profile')); break; case 'create': // Load factory default profile values. - $this->profile = CRM_twingle_Profile::createDefaultProfile($profile_name); + $this->profile = CRM_Twingle_Profile::createDefaultProfile($profile_name); CRM_Utils_System::setTitle(E::ts('New Twingle API profile')); break; } @@ -436,13 +438,6 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { parent::buildQuickForm(); } - /** - * @inheritdoc - */ - public function addRules() { - $this->addFormRule(array('CRM_Twingle_Form_Profile', 'validateProfileForm')); - } - /** * Validates the profile form. * @@ -453,12 +448,21 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { * TRUE when the form was successfully validated, or an array of error * messages, keyed by form element name. */ - public static function validateProfileForm($values) { - $errors = array(); + public function validate() { + $values = $this->exportValues(); + + // Validate new profile names. + if ( + isset($values['name']) + && ($values['name'] != $this->profile->getName() || $this->_op != 'edit') + && !empty(CRM_Twingle_Profile::getProfile($values['name'])) + ) { + $this->_errors['name'] = E::ts('A profile with this name already exists.'); + } // Restrict profile names to alphanumeric characters and the underscore. if (isset($values['name']) && preg_match("/[^A-Za-z0-9\_]/", $values['name'])) { - $errors['name'] = E::ts('Only alphanumeric characters and the underscore (_) are allowed for profile names.'); + $this->_errors['name'] = E::ts('Only alphanumeric characters and the underscore (_) are allowed for profile names.'); } // Validate custom field mapping. @@ -521,10 +525,10 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { } } catch (Exception $exception) { - $errors['custom_field_mapping'] = $exception->getMessage(); + $this->_errors['custom_field_mapping'] = $exception->getMessage(); } - return empty($errors) ? TRUE : $errors; + return parent::validate(); } /** @@ -551,7 +555,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { */ public function postProcess() { $values = $this->exportValues(); - if (in_array($this->_op, array('create', 'edit'))) { + if (in_array($this->_op, array('create', 'edit', 'copy'))) { if (empty($values['name'])) { $values['name'] = 'default'; } diff --git a/templates/CRM/Twingle/Page/Profiles.tpl b/templates/CRM/Twingle/Page/Profiles.tpl index 30dbfce..f97b9ed 100644 --- a/templates/CRM/Twingle/Page/Profiles.tpl +++ b/templates/CRM/Twingle/Page/Profiles.tpl @@ -39,7 +39,7 @@ {ts domain="de.systopia.twingle"}Edit{/ts} - {ts domain="de.systopia.twingle"}Copy{/ts} + {ts domain="de.systopia.twingle"}Copy{/ts} {if $profile_name == 'default'} {ts domain="de.systopia.twingle"}Reset{/ts} {else}