Merge pull request #2760 from pradpnayak/CRM-14191
[civicrm-core.git] / CRM / Core / BAO / Address.php
index fba41e28d3a9f92937c19072fe2e076d7ab4d8a3..40785fcb7883d6ecaa4c9dda2b549b02e8a63a18 100644 (file)
@@ -1,9 +1,7 @@
 <?php
-// $Id$
-
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
@@ -364,12 +362,15 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
       CRM_Utils_Address_USPS::checkAddress($params);
 
       // do street parsing again if enabled, since street address might have changed
-      $parseStreetAddress = CRM_Utils_Array::value('street_address_parsing',
-        CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-          'address_options'
-        ),
-        FALSE
-      );
+      $parseStreetAddress =
+        CRM_Utils_Array::value(
+          'street_address_parsing',
+          CRM_Core_BAO_Setting::valueOptions(
+            CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
+            'address_options'
+          ),
+          FALSE
+        );
 
       if ($parseStreetAddress && !empty($params['street_address'])) {
         foreach (array(
@@ -663,22 +664,31 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
     $config->stateCountryMap = array_merge($config->stateCountryMap, $stateCountryMap);
   }
 
-  static function fixAllStateSelects(&$form, &$defaults) {
+  static function fixAllStateSelects(&$form, $defaults, $batchFieldNames = false) {
     $config = CRM_Core_Config::singleton();
-
-    if (!empty($config->stateCountryMap)) {
-      foreach ($config->stateCountryMap as $index => $match) {
-        if (
-          array_key_exists('state_province', $match) &&
-          array_key_exists('country', $match)
+    $map = null;
+    if (is_array($batchFieldNames)) {
+      $map = $batchFieldNames;
+    }
+    elseif (!empty($config->stateCountryMap)) {
+      $map = $config->stateCountryMap;
+    }
+    if (!empty($map)) {
+      foreach ($map as $index => $match) {
+        if (array_key_exists('state_province', $match)
+          || array_key_exists('country', $match)
+          || array_key_exists('county', $match)
         ) {
+          $countryElementName = CRM_Utils_Array::value('country', $match);
+          $stateProvinceElementName = CRM_Utils_Array::value('state_province', $match);
+          $countyElementName = CRM_Utils_Array::value('county', $match);
           CRM_Contact_Form_Edit_Address::fixStateSelect(
             $form,
-            $match['country'],
-            $match['state_province'],
-            CRM_Utils_Array::value('county', $match),
-            CRM_Utils_Array::value($match['country'], $defaults),
-            CRM_Utils_Array::value($match['state_province'], $defaults)
+            $countryElementName,
+            $stateProvinceElementName,
+            $countyElementName,
+            CRM_Utils_Array::value($countryElementName, $defaults),
+            CRM_Utils_Array::value($stateProvinceElementName, $defaults)
           );
         }
         else {
@@ -867,9 +877,11 @@ ORDER BY civicrm_address.is_primary DESC, civicrm_address.location_type_id DESC,
   static function validateAddressOptions($fields) {
     static $addressOptions = NULL;
     if (!$addressOptions) {
-      $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
-        'address_options', TRUE, NULL, TRUE
-      );
+      $addressOptions =
+        CRM_Core_BAO_Setting::valueOptions(
+          CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
+          'address_options'
+        );
     }
 
     if (is_array($fields) && !empty($fields)) {
@@ -1135,6 +1147,55 @@ SELECT is_primary,
    * Call common delete function
    */
   static function del($id) {
-    CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Address', $id);
+    return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('Address', $id);
+  }
+
+  /**
+   * Get options for a given address field.
+   * @see CRM_Core_DAO::buildOptions
+   *
+   * TODO: Should we always assume chainselect? What fn should be responsible for controlling that flow?
+   * TODO: In context of chainselect, what to return if e.g. a country has no states?
+   *
+   * @param String $fieldName
+   * @param String $context: @see CRM_Core_DAO::buildOptionsContext
+   * @param Array  $props: whatever is known about this dao object
+   */
+  public static function buildOptions($fieldName, $context = NULL, $props = array()) {
+    $params = array();
+    // Special logic for fields whose options depend on context or properties
+    switch ($fieldName) {
+      // Filter state_province list based on chosen country or site defaults
+      case 'state_province_id':
+        if (empty($props['country_id'])) {
+          $config = CRM_Core_Config::singleton();
+          if (!empty($config->provinceLimit)) {
+            $props['country_id'] = $config->provinceLimit;
+          }
+          else {
+            $props['country_id'] = $config->defaultContactCountry;
+          }
+        }
+        if (!empty($props['country_id'])) {
+          $params['condition'] = 'country_id IN (' . implode(',', (array) $props['country_id']) . ')';
+        }
+        break;
+      // Filter country list based on site defaults
+      case 'country_id':
+        if ($context != 'get' && $context != 'validate') {
+          $config = CRM_Core_Config::singleton();
+          if (!empty($config->countryLimit) && is_array($config->countryLimit)) {
+            $params['condition'] = 'id IN (' . implode(',', $config->countryLimit) . ')';
+          }
+        }
+        break;
+      // Filter county list based on chosen state
+      case 'county_id':
+        if (!empty($props['state_province_id'])) {
+          $params['condition'] = 'state_province_id IN (' . implode(',', (array) $props['state_province_id']) . ')';
+        }
+        break;
+    }
+    return CRM_Core_PseudoConstant::get(__CLASS__, $fieldName, $params, $context);
   }
-}
+}
\ No newline at end of file