From: monishdeb Date: Wed, 6 Nov 2013 13:04:33 +0000 (+0530) Subject: State/Province required-ness fix if Country doesn't contain it on CiviProfile X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f5b34faf096e69639992aae3dddd409935ea7f78;p=civicrm-core.git State/Province required-ness fix if Country doesn't contain it on CiviProfile --- diff --git a/CRM/Contribute/Form/Contribution/OnBehalfOf.php b/CRM/Contribute/Form/Contribution/OnBehalfOf.php index 9218596500..2c62bcaa23 100644 --- a/CRM/Contribute/Form/Contribution/OnBehalfOf.php +++ b/CRM/Contribute/Form/Contribution/OnBehalfOf.php @@ -73,7 +73,7 @@ class CRM_Contribute_Form_Contribution_OnBehalfOf { if (!empty($form->_membershipContactID) && $contactID != $form->_membershipContactID) { // renewal case - membership being renewed may or may not be for organization if (!empty($form->_employers) && array_key_exists($form->_membershipContactID, $form->_employers)) { - // if _membershipContactID belongs to employers list, we can say: + // if _membershipContactID belongs to employers list, we can say: $form->_relatedOrganizationFound = TRUE; } } else if (!empty($form->_employers)) { @@ -152,6 +152,7 @@ class CRM_Contribute_Form_Contribution_OnBehalfOf { } $stateCountryMap = array(); + $location_type_id = null; foreach ($profileFields as $name => $field) { if (in_array($field['field_type'], $fieldTypes)) { list($prefixName, $index) = CRM_Utils_System::explode('-', $name, 2); @@ -162,6 +163,14 @@ class CRM_Contribute_Form_Contribution_OnBehalfOf { } $stateCountryMap[$index][$prefixName] = 'onbehalf[' . $name . ']'; + + if (count($form->_submitValues)) { + $location_type_id = $field['location_type_id']; + if(!empty($form->_submitValues['onbehalf']["country-{$location_type_id}"]) && + $prefixName == "state_province") { + $field['is_required'] = CRM_Core_Payment_Form::checkRequiredStateProvince($form, "country-{$location_type_id}", TRUE); + } + } } elseif (in_array($prefixName, array( 'organization_name', 'email')) && diff --git a/CRM/Contribute/Form/ContributionBase.php b/CRM/Contribute/Form/ContributionBase.php index e92bdf45c1..23a3b24b8e 100644 --- a/CRM/Contribute/Form/ContributionBase.php +++ b/CRM/Contribute/Form/ContributionBase.php @@ -689,6 +689,7 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { CRM_Core_BAO_Address::checkContactSharedAddressFields($fields, $contactID); $addCaptcha = FALSE; + $location_type_id = NULL; foreach ($fields as $key => $field) { if ($viewOnly && isset($field['data_type']) && @@ -704,6 +705,25 @@ class CRM_Contribute_Form_ContributionBase extends CRM_Core_Form { $stateCountryMap[$index] = array(); } $stateCountryMap[$index][$prefixName] = $key; + + if ($prefixName == "state_province") { + if ($onBehalf) { + //CRM-11881: Bypass required-ness check for state/province on Contribution Confirm page + //as already done during Contribution registration via onBehalf's quickForm + $field['is_required'] = FALSE; + } + else { + if (count($this->_submitValues)) { + $location_type_id = $field['location_type_id']; + if (array_key_exists("country-{$location_type_id}", $fields) && + array_key_exists("state_province-{$location_type_id}", $fields) && + !empty($this->_submitValues["country-{$location_type_id}"])) { + $field['is_required'] = + CRM_Core_Payment_Form::checkRequiredStateProvince($this, "country-{$location_type_id}"); + } + } + } + } } if ($onBehalf) { diff --git a/CRM/Core/Payment/Form.php b/CRM/Core/Payment/Form.php index 34a8cf849c..a80ed93fa7 100644 --- a/CRM/Core/Payment/Form.php +++ b/CRM/Core/Payment/Form.php @@ -111,7 +111,7 @@ class CRM_Core_Payment_Form { 'attributes' => array( '' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvince(), - 'is_required' => self::checkRequiredStateProvince($form), + 'is_required' => self::checkRequiredStateProvince($form, "billing_country_id-{$bltID}"), ); $form->_paymentFields["billing_postal_code-{$bltID}"] = array( @@ -436,13 +436,28 @@ class CRM_Core_Payment_Form { /** * function to return state/province is_required = true/false * + * @param obj $form: Form object + * @param string $name: Country index name on $_submitValues array + * @param bool $onBehalf: Is 'On Behalf Of' profile? + * + * @return bool + * TRUE/FALSE for is_required if country consist/not consist of state/province respectively + * @static */ - static function checkRequiredStateProvince($form) { + static function checkRequiredStateProvince($form, $name, $onBehalf = FALSE) { // If selected country has possible values for state/province mark the // state/province field as required. $config = CRM_Core_Config::singleton(); $stateProvince = new CRM_Core_DAO_StateProvince(); - $stateProvince->country_id = CRM_Utils_Array::value("billing_country_id-{$form->_bltID}", $form->_submitValues); + + if ($onBehalf) { + $stateProvince->country_id = CRM_Utils_Array::value($name, $form->_submitValues['onbehalf']); + } + else { + $stateProvince->country_id = CRM_Utils_Array::value($name, $form->_submitValues); + } + + $limitCountryId = $stateProvince->country_id; if ($stateProvince->count() > 0) { // check that the state/province data is not excluded by a @@ -454,7 +469,6 @@ class CRM_Core_Payment_Form { $limitIds = array_merge($limitIds, array_keys($countryIsoCodes, $code)); } - $limitCountryId = CRM_Utils_Array::value("billing_country_id-{$form->_bltID}", $form->_submitValues); if ($limitCountryId && in_array($limitCountryId, $limitIds)) { return TRUE; } diff --git a/CRM/Profile/Form.php b/CRM/Profile/Form.php index 27d9d21e56..c55b089dd8 100644 --- a/CRM/Profile/Form.php +++ b/CRM/Profile/Form.php @@ -680,6 +680,21 @@ class CRM_Profile_Form extends CRM_Core_Form { return FALSE; } + if (count($this->_submitValues)) { + $location_type_id = null; + foreach ($this->_fields as $field) { + if (!empty($field['location_type_id'])) { + $location_type_id = $field['location_type_id']; + } + if (array_key_exists("country-{$location_type_id}", $this->_fields) && + array_key_exists("state_province-{$location_type_id}", $this->_fields) && + !empty($this->_submitValues["country-{$location_type_id}"])) { + $this->_fields["state_province-{$location_type_id}"]['is_required'] = + CRM_Core_Payment_Form::checkRequiredStateProvince($this, "country-{$location_type_id}"); + } + } + } + $this->assign('id', $this->_id); $this->assign('mode', $this->_mode); $this->assign('action', $this->_action);