From 484920f47d4a7f71296031277c1575dc1be0dfbd Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Tue, 10 Nov 2020 11:41:20 +0100 Subject: [PATCH 1/4] [#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 a38dc7e..d1dd193 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; } @@ -428,13 +430,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. * @@ -445,12 +440,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. @@ -513,10 +517,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(); } /** @@ -543,7 +547,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} From 76a7a28483e011a92ea17db80fdd5abf0d85ecde Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Tue, 10 Nov 2020 11:41:49 +0100 Subject: [PATCH 2/4] Version 1.2-beta4 --- info.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/info.xml b/info.xml index f4918e7..d950c38 100644 --- a/info.xml +++ b/info.xml @@ -14,9 +14,9 @@ https://github.com/systopia/de.systopia.twingle/issues http://www.gnu.org/licenses/agpl-3.0.html - - 1.2-dev - dev + 2020-11-10 + 1.2-beta4 + beta 5.0 5.19 From 521784d7432277422dca6a5f96b1b2dcc9006c38 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Tue, 10 Nov 2020 11:42:05 +0100 Subject: [PATCH 3/4] Back to dev --- info.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/info.xml b/info.xml index d950c38..f4918e7 100644 --- a/info.xml +++ b/info.xml @@ -14,9 +14,9 @@ https://github.com/systopia/de.systopia.twingle/issues http://www.gnu.org/licenses/agpl-3.0.html - 2020-11-10 - 1.2-beta4 - beta + + 1.2-dev + dev 5.0 5.19 From 6786af0c92a280663e5257a6ed66a372e87c10b7 Mon Sep 17 00:00:00 2001 From: Jens Schuppe Date: Tue, 10 Nov 2020 12:31:09 +0100 Subject: [PATCH 4/4] Version 1.2 --- info.xml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/info.xml b/info.xml index f4918e7..f59bf0a 100644 --- a/info.xml +++ b/info.xml @@ -14,9 +14,9 @@ https://github.com/systopia/de.systopia.twingle/issues http://www.gnu.org/licenses/agpl-3.0.html - - 1.2-dev - dev + 2020-11-10 + 1.2 + stable 5.0 5.19