CRM-15172 - Refactor forms to use addChainSelect method
[civicrm-core.git] / CRM / Core / BAO / UFGroup.php
index 478501d45242a61871a81626f371f97d63a248c6..e26683a53bffdedf75704a8e294bd7dadf551f65 100644 (file)
@@ -1736,7 +1736,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
    * @params int     $contactID  contact id
    * @params string  $usedFor    for building up prefixed fieldname for special cases (e.g. onBehalf, Honor)
    *
-   * @param $form
+   * @param CRM_Core_Form $form
    * @param $field
    * @param $mode
    * @param null $contactId
@@ -1794,6 +1794,11 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
       $name = $fieldName;
     }
 
+    // CRM-15172 - keep track of all the fields we've processed
+    // This is hackish but we can't always rely on $form->_fields depending on how this is called
+    static $fieldsProcessed = array();
+    $fieldsProcessed[] = $name;
+
     if ($fieldName == 'image_URL' && $mode == CRM_Profile_Form::MODE_EDIT) {
       $deleteExtra = ts('Are you sure you want to delete contact image.');
       $deleteURL = array(
@@ -1824,10 +1829,14 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
     );
 
     if (substr($fieldName, 0, 14) === 'state_province') {
-      $form->add('select', $name, $title,
-        array(
-          '' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvince(), $required
-      );
+      $controlField = str_replace('state_province', 'country', $name);
+      if (isset($form->_fields[$controlField]) || in_array($controlField, $fieldsProcessed)) {
+        $form->addChainSelect($name, array('label' => $title, 'required' => $required));
+      }
+      else {
+        $form->add('select', $name, $title,
+          array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvince(), $required);
+      }
       $config = CRM_Core_Config::singleton();
       if (!in_array($mode, array(
         CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH)) &&
@@ -1838,10 +1847,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
       }
     }
     elseif (substr($fieldName, 0, 7) === 'country') {
-      $form->add('select', $name, $title,
-        array(
-          '' => ts('- select -')) + CRM_Core_PseudoConstant::country(), $required
-      );
+      $form->add('select', $name, $title, array('' => ts('- select -')) + CRM_Core_PseudoConstant::country(), $required);
       $config = CRM_Core_Config::singleton();
       if (!in_array($mode, array(
         CRM_Profile_Form::MODE_EDIT, CRM_Profile_Form::MODE_SEARCH)) &&
@@ -1853,9 +1859,7 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
     }
     elseif (substr($fieldName, 0, 6) === 'county') {
       if ($addressOptions['county']) {
-        $form->add('select', $name, $title,
-          array('' => ts('Choose state first')), $required
-        );
+        $form->addChainSelect($name, array('label' => $title, 'required' => $required));
       }
     }
     elseif (substr($fieldName, 0, 9) === 'image_URL') {
@@ -2233,14 +2237,6 @@ AND    ( entity_id IS NULL OR entity_id <= 0 )
       'non_deductible_amount', 'total_amount', 'fee_amount', 'net_amount'))) {
       $form->addRule($name, ts('Please enter a valid amount.'), 'money');
     }
-    $stateCountryMap = array();
-    if (!empty($form->_stateCountryMap['state_province']) && !empty($form->_stateCountryMap['country'])) {
-      foreach ($form->_stateCountryMap['state_province'] as $key => $value) {
-        $stateCountryMap[$key]['state_province'] = $value;
-        $stateCountryMap[$key]['country'] = $form->_stateCountryMap['country'][$key];
-      }
-      CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
-    }
     if ($rule) {
       if (!($rule == 'email' && $mode == CRM_Profile_Form::MODE_SEARCH)) {
         $form->addRule($name, ts('Please enter a valid %1', array(1 => $title)), $rule);