Merge branch 'issue/29'

[#29] Copying profile overwrites existing or default profile
This commit is contained in:
Jens Schuppe 2020-11-10 12:25:26 +01:00
commit 677f9b8380
2 changed files with 22 additions and 18 deletions

View file

@ -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';
}

View file

@ -39,7 +39,7 @@
</td>
<td>
<a href="{crmURL p="civicrm/admin/settings/twingle/profile" q="op=edit&name=$profile_name"}" title="{ts domain="de.systopia.twingle" 1=$profile.name}Edit profile %1{/ts}" class="action-item crm-hover-button">{ts domain="de.systopia.twingle"}Edit{/ts}</a>
<a href="{crmURL p="civicrm/admin/settings/twingle/profile" q="op=copy&name=$profile_name"}" title="{ts domain="de.systopia.twingle" 1=$profile.name}Copy profile %1{/ts}" class="action-item crm-hover-button">{ts domain="de.systopia.twingle"}Copy{/ts}</a>
<a href="{crmURL p="civicrm/admin/settings/twingle/profile" q="op=copy&source_name=$profile_name"}" title="{ts domain="de.systopia.twingle" 1=$profile.name}Copy profile %1{/ts}" class="action-item crm-hover-button">{ts domain="de.systopia.twingle"}Copy{/ts}</a>
{if $profile_name == 'default'}
<a href="{crmURL p="civicrm/admin/settings/twingle/profile" q="op=delete&name=$profile_name"}" title="{ts domain="de.systopia.twingle" 1=$profile.name}Reset profile %1{/ts}" class="action-item crm-hover-button">{ts domain="de.systopia.twingle"}Reset{/ts}</a>
{else}