diff --git a/CRM/Twingle/Form/Profile.php b/CRM/Twingle/Form/Profile.php index a38dc7e..ebc75c1 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; @@ -171,23 +171,22 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { CRM_Utils_System::setTitle(E::ts('Edit Twingle API profile %1', array(1 => $this->profile->getName()))); break; case 'copy': - // 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 = clone 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 +427,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 +437,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 +514,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(); } /** @@ -524,7 +525,7 @@ class CRM_Twingle_Form_Profile extends CRM_Core_Form { */ public function setDefaultValues() { $defaults = parent::setDefaultValues(); - if (in_array($this->_op, array('create', 'edit'))) { + if (in_array($this->_op, array('create', 'edit', 'copy'))) { $defaults['name'] = $this->profile->getName(); $profile_data = $this->profile->getData(); foreach ($profile_data as $element_name => $value) { @@ -543,7 +544,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/api/v3/TwingleDonation/Submit.php b/api/v3/TwingleDonation/Submit.php index 75220bb..44c415f 100644 --- a/api/v3/TwingleDonation/Submit.php +++ b/api/v3/TwingleDonation/Submit.php @@ -400,27 +400,6 @@ function civicrm_api3_twingle_donation_Submit($params) { $contact_data += $custom_fields['Individual']; } - if (!$contact_id = CRM_Twingle_Submission::getContact( - 'Individual', - $contact_data, - $profile, - $params - )) { - throw new CiviCRM_API3_Exception( - E::ts('Individual contact could not be found or created.'), - 'api_error' - ); - } - - // Save user_extrafield as contact note. - if (!empty($params['user_extrafield'])) { - civicrm_api3('Note', 'create', array( - 'entity_table' => 'civicrm_contact', - 'entity_id' => $contact_id, - 'note' => $params['user_extrafield'], - )); - } - // Organisation lookup. if (!empty($params['organization_name'])) { $organisation_data = array( @@ -449,6 +428,31 @@ function civicrm_api3_twingle_donation_Submit($params) { ); } } + elseif (!empty($submitted_address)) { + $contact_data += $submitted_address; + } + + if (!$contact_id = CRM_Twingle_Submission::getContact( + 'Individual', + $contact_data, + $profile, + $params + )) { + throw new CiviCRM_API3_Exception( + E::ts('Individual contact could not be found or created.'), + 'api_error' + ); + } + + // Save user_extrafield as contact note. + if (!empty($params['user_extrafield'])) { + civicrm_api3('Note', 'create', array( + 'entity_table' => 'civicrm_contact', + 'entity_id' => $contact_id, + 'note' => $params['user_extrafield'], + )); + } + // Share organisation address with individual contact, using configured // location type for organisation address. $address_shared = ( @@ -460,19 +464,6 @@ function civicrm_api3_twingle_donation_Submit($params) { ) ); - // Address is not shared, use submitted address with - // configured location type. - if (!$address_shared && !empty($submitted_address)) { - // Do not use `Address.create` API action, let XCM decide - // whether to create an address. - CRM_Twingle_Submission::getContact( - 'Individual', - array('id' => $contact_id) + $submitted_address, - $profile, - $params - ); - } - // Create employer relationship between organization and individual. if (isset($organisation_id)) { CRM_Twingle_Submission::updateEmployerRelation($contact_id, $organisation_id); diff --git a/templates/CRM/Twingle/Form/Profile.tpl b/templates/CRM/Twingle/Form/Profile.tpl index 708cb5a..dff57ba 100644 --- a/templates/CRM/Twingle/Form/Profile.tpl +++ b/templates/CRM/Twingle/Form/Profile.tpl @@ -14,7 +14,7 @@