State/Province required-ness fix if Country doesn't contain it on CiviProfile
authormonishdeb <monish.deb@webaccess.co.in>
Wed, 6 Nov 2013 13:04:33 +0000 (18:34 +0530)
committermonishdeb <monish.deb@webaccess.co.in>
Wed, 6 Nov 2013 13:04:33 +0000 (18:34 +0530)
CRM/Contribute/Form/Contribution/OnBehalfOf.php
CRM/Contribute/Form/ContributionBase.php
CRM/Core/Payment/Form.php
CRM/Profile/Form.php

index 9218596500e6da168034d864c8a18881569ab834..2c62bcaa23d4e1393050c2cc8990fc1d423756d6 100644 (file)
@@ -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')) &&
index e92bdf45c1cd341035654df73d3788d05a488ab5..23a3b24b8efd794566f09f8914bc88dbffb053aa 100644 (file)
@@ -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) {
index 34a8cf849cc42470d7e9361635171afe8132261f..a80ed93fa7e91e121d5ae1cca3d374ba1d75142f 100644 (file)
@@ -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;
       }
index 27d9d21e56d2046e2344699e017b33361d8c0154..c55b089dd8471f93b734fee1e6810e10a5fd568e 100644 (file)
@@ -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);