INFRA-132 - Put space after flow-control (if/switch/for/foreach/while)
[civicrm-core.git] / CRM / Contact / Form / Edit / Address.php
index b32044a09ff872e79b10597374a088db938acd10..cdfaeef9c731bda5eb2032917f7ca26af49a6390 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
 class CRM_Contact_Form_Edit_Address {
 
   /**
-   * build form for address input fields
+   * Build form for address input fields
    *
-   * @param object  $form - CRM_Core_Form (or subclass)
-   * @param int     $addressBlockCount - the index of the address array (if multiple addresses on a page)
-   * @param boolean $sharing - false, if we want to skip the address sharing features
-   * @param boolean $inlineEdit true when edit used in inline edit
+   * @param CRM_Core_Form $form
+   * @param int $addressBlockCount
+   *   The index of the address array (if multiple addresses on a page).
+   * @param bool $sharing
+   *   False, if we want to skip the address sharing features.
+   * @param bool $inlineEdit
+   *   True when edit used in inline edit.
    *
    * @return void
    *
-   * @access public
    * @static
    */
-  static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
+  public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
     // passing this via the session is AWFUL. we need to fix this
     if (!$addressBlockCount) {
       $blockId = ($form->get('Address_Block_Count')) ? $form->get('Address_Block_Count') : 1;
@@ -113,9 +115,9 @@ class CRM_Contact_Form_Edit_Address {
       'city' => array(ts('City'), $attributes['city'], NULL),
       'postal_code' => array(ts('Zip / Postal Code'), array_merge($attributes['postal_code'], array('class' => 'crm_postal_code')), NULL),
       'postal_code_suffix' => array(ts('Postal Code Suffix'), array('size' => 4, 'maxlength' => 12, 'class' => 'crm_postal_code_suffix'), NULL),
+      'country_id' => array(ts('Country'), $attributes['country_id'], 'country'),
+      'state_province_id' => array(ts('State/Province'), $attributes['state_province_id'], NULL),
       'county_id' => array(ts('County'), $attributes['county_id'], NULL),
-      'state_province_id' => array(ts('State / Province'), $attributes['state_province_id'], NULL),
-      'country_id' => array(ts('Country'), $attributes['country_id'], NULL),
       'geo_code_1' => array(ts('Latitude'), array('size' => 9, 'maxlength' => 11), NULL),
       'geo_code_2' => array(ts('Longitude'), array('size' => 9, 'maxlength' => 11), NULL),
       'street_number' => array(ts('Street Number'), $attributes['street_number'], NULL),
@@ -123,7 +125,6 @@ class CRM_Contact_Form_Edit_Address {
       'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL),
     );
 
-    $stateCountryMap = array();
     foreach ($elements as $name => $v) {
       list($title, $attributes, $select) = $v;
 
@@ -139,47 +140,14 @@ class CRM_Contact_Form_Edit_Address {
         }
       }
 
-      if (!$attributes) {
-        $attributes = $attributes[$name];
-      }
-
       //build normal select if country is not present in address block
       if ($name == 'state_province_id' && !$addressOptions['country']) {
         $select = 'stateProvince';
       }
 
       if (!$select) {
-        if ($name == 'country_id' || $name == 'state_province_id' || $name == 'county_id') {
-          if ($name == 'country_id') {
-            $stateCountryMap[$blockId]['country'] = "address_{$blockId}_{$name}";
-            $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::country();
-          }
-          elseif ($name == 'state_province_id') {
-            $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
-            if ($countryDefault) {
-              $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::stateProvinceForCountry($countryDefault);
-            }
-            else {
-              $selectOptions = array('' => ts('- select a country -'));
-            }
-          }
-          elseif ($name == 'county_id') {
-            $stateCountryMap[$blockId]['county'] = "address_{$blockId}_{$name}";
-            if ($form->getSubmitValue("address[{$blockId}][state_province_id]")) {
-              $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::countyForState($form->getSubmitValue("address[{$blockId}][state_province_id]"));
-            }
-            elseif ($form->getSubmitValue("address[{$blockId}][county_id]")) {
-              $selectOptions = array('' => ts('- select -')) + CRM_Core_PseudoConstant::county();
-            }
-            else {
-              $selectOptions = array('' => ts('- select a state -'));
-            }
-          }
-          $form->addElement('select',
-            "address[$blockId][$name]",
-            $title,
-            $selectOptions
-          );
+        if ($name == 'state_province_id' || $name == 'county_id') {
+          $form->addChainSelect("address[$blockId][$name]");
         }
         else {
           if ($name == 'address_name') {
@@ -194,9 +162,6 @@ class CRM_Contact_Form_Edit_Address {
         }
       }
       else {
-        if ($name == 'state_province_id') {
-          $stateCountryMap[$blockId]['state_province'] = "address_{$blockId}_{$name}";
-        }
         $form->addElement('select',
           "address[$blockId][$name]",
           $title,
@@ -205,8 +170,6 @@ class CRM_Contact_Form_Edit_Address {
       }
     }
 
-    CRM_Core_BAO_Address::addStateCountryMap($stateCountryMap);
-
     $entityId = NULL;
     if (!empty($form->_values['address']) && !empty($form->_values['address'][$blockId])) {
       $entityId = $form->_values['address'][$blockId]['id'];
@@ -292,22 +255,17 @@ class CRM_Contact_Form_Edit_Address {
   }
 
   /**
-   * check for correct state / country mapping.
+   * Check for correct state / country mapping.
    *
    * @param $fields
    * @param $files
    * @param $self
    *
-   * @internal param \reference $array $fields - submitted form values.
-   * @internal param \reference $array $errors - if any errors found add to this array. please.
-   *
-   * @return true if no errors
-   *         array of errors if any present.
+   * @return array|bool if no errors
    *
-   * @access public
    * @static
    */
-  static function formRule($fields, $files, $self) {
+  public static function formRule($fields, $files, $self) {
     $errors = array();
 
     $customDataRequiredFields = array();
@@ -315,7 +273,6 @@ class CRM_Contact_Form_Edit_Address {
       $customDataRequiredFields = explode(',', $self->_addressRequireOmission);
     }
 
-    // check for state/county match if not report error to user.
     if (!empty($fields['address']) && is_array($fields['address'])) {
       foreach ($fields['address'] as $instance => $addressValues) {
 
@@ -323,7 +280,7 @@ class CRM_Contact_Form_Edit_Address {
           // DETACH 'required' form rule error to
           // custom data only if address data not exists upon submission
           if (!empty($customDataRequiredFields)) {
-            foreach($customDataRequiredFields as $customElementName) {
+            foreach ($customDataRequiredFields as $customElementName) {
               $elementName = "address[$instance][$customElementName]";
               if ($self->getElementError($elementName)) {
                 // set element error to none
@@ -337,7 +294,7 @@ class CRM_Contact_Form_Edit_Address {
         // DETACH 'required' form rule error to
         // custom data only if address data not exists upon submission
         if (!empty($customDataRequiredFields) && !CRM_Core_BAO_Address::dataExists($addressValues)) {
-          foreach($customDataRequiredFields as $customElementName) {
+          foreach ($customDataRequiredFields as $customElementName) {
             $elementName = "address[$instance][$customElementName]";
             if ($self->getElementError($elementName)) {
               // set element error to none
@@ -346,48 +303,6 @@ class CRM_Contact_Form_Edit_Address {
           }
         }
 
-        $countryId = CRM_Utils_Array::value('country_id', $addressValues);
-
-        $stateProvinceId = CRM_Utils_Array::value('state_province_id', $addressValues);
-
-        //do check for mismatch countries
-        if ($stateProvinceId && $countryId) {
-          $stateProvinceDAO = new CRM_Core_DAO_StateProvince();
-          $stateProvinceDAO->id = $stateProvinceId;
-          $stateProvinceDAO->find(TRUE);
-          if ($stateProvinceDAO->country_id != $countryId) {
-            // countries mismatch hence display error
-            $stateProvinces = CRM_Core_PseudoConstant::stateProvince();
-            $countries = CRM_Core_PseudoConstant::country();
-            $errors["address[$instance][state_province_id]"] = ts('State/Province %1 is not part of %2. It belongs to %3.',
-              array(
-                1 => $stateProvinces[$stateProvinceId],
-                2 => $countries[$countryId],
-                3 => $countries[$stateProvinceDAO->country_id]
-              )
-            );
-          }
-        }
-
-        $countyId = CRM_Utils_Array::value('county_id', $addressValues);
-
-        //state county validation
-        if ($stateProvinceId && $countyId) {
-          $countyDAO = new CRM_Core_DAO_County();
-          $countyDAO->id = $countyId;
-          $countyDAO->find(TRUE);
-          if ($countyDAO->state_province_id != $stateProvinceId) {
-            $counties = CRM_Core_PseudoConstant::county();
-            $errors["address[$instance][county_id]"] = ts('County %1 is not part of %2. It belongs to %3.',
-              array(
-                1 => $counties[$countyId],
-                2 => $stateProvinces[$stateProvinceId],
-                3 => $stateProvinces[$countyDAO->state_province_id]
-              )
-            );
-          }
-        }
-
         if (!empty($addressValues['use_shared_address']) && empty($addressValues['master_id'])) {
           $errors["address[$instance][use_shared_address]"] = ts('Please select valid shared contact or a contact with valid address.');
         }
@@ -398,98 +313,16 @@ class CRM_Contact_Form_Edit_Address {
   }
 
   /**
-   * @param $form
-   * @param $countryElementName
-   * @param $stateElementName
-   * @param $countyElementName
-   * @param $countryDefaultValue
-   * @param null $stateDefaultValue
-   */
-  static function fixStateSelect(&$form,
-    $countryElementName,
-    $stateElementName,
-    $countyElementName,
-    $countryDefaultValue,
-    $stateDefaultValue = NULL
-  ) {
-    $countryID = $stateID = NULL;
-    if (isset($form->_elementIndex[$countryElementName])) {
-      //get the country id to load states -
-      //first check for submitted value,
-      //then check for user passed value.
-      //finally check for element default val.
-      $submittedVal = $form->getSubmitValue($countryElementName);
-      if ($submittedVal) {
-        $countryID = $submittedVal;
-      }
-      elseif ($countryDefaultValue) {
-        $countryID = $countryDefaultValue;
-      }
-      else {
-        $countryID = CRM_Utils_Array::value(0, $form->getElementValue($countryElementName));
-      }
-    }
-    $stateTitle = ts('State/Province');
-    if (isset($form->_fields[$stateElementName]['title'])) {
-      $stateTitle = $form->_fields[$stateElementName]['title'];
-    }
-
-    if (isset($form->_elementIndex[$stateElementName])) {
-      $submittedValState = $form->getSubmitValue($stateElementName);
-      if ($submittedValState) {
-        $stateID = $submittedValState;
-      }
-      elseif ($stateDefaultValue) {
-        $stateID = $stateDefaultValue;
-      }
-      else {
-        $stateID = CRM_Utils_Array::value(0, $form->getElementValue($stateElementName));
-      }
-    }
-
-    if (isset($form->_elementIndex[$stateElementName])) {
-      if ($countryID) {
-        $stateProvinces = CRM_Core_PseudoConstant::stateProvinceForCountry($countryID);
-      }
-      else {
-        $stateProvinces = CRM_Core_PseudoConstant::stateProvince();
-      }
-
-      $stateSelect = & $form->addElement('select', $stateElementName, $stateTitle,
-        array('' => ts('- select -')) + $stateProvinces);
-    }
-
-    if (isset($form->_elementIndex[$stateElementName]) && isset($form->_elementIndex[$countyElementName])) {
-      if ($stateID) {
-        $counties = CRM_Core_PseudoConstant::countyForState($stateID);
-      }
-      else {
-        $counties = CRM_Core_PseudoConstant::county();
-      }
-
-      $form->addElement('select', $countyElementName, ts('County'), array('' => ts('- select -')) + $counties);
-    }
-
-    // CRM-7296 freeze the select for state if address is shared with household
-    // CRM-9070 freeze the select for state if it is view only
-    if (isset($form->_fields) && !empty($form->_fields[$stateElementName]) &&
-      (!empty($form->_fields[$stateElementName]['is_shared']) || !empty($form->_fields[$stateElementName]['is_view'])) &&
-      !empty($stateSelect)
-    ) {
-      $stateSelect->freeze();
-    }
-  }
-
-  /**
-   * function to set default values for address block
+   * Set default values for address block
    *
-   * @param array  $defaults  defaults associated array
-   * @param object $form     form object
+   * @param array $defaults
+   *   Defaults associated array.
+   * @param CRM_Core_Form $form
+   *   Form object.
    *
    * @static
-   * @access public
    */
-  static function setDefaultValues( &$defaults, &$form ) {
+  public static function setDefaultValues( &$defaults, &$form ) {
     $addressValues = array();
     if (isset($defaults['address']) && is_array($defaults['address']) &&
       !CRM_Utils_System::isNull($defaults['address'])
@@ -504,10 +337,14 @@ class CRM_Contact_Form_Edit_Address {
 
       foreach ($defaults['address'] as $key => $addressValue) {
         if (!empty($addressValue['master_id']) && !$shareAddressContactNames[$addressValue['master_id']]['is_deleted']) {
+          $master_cid = $shareAddressContactNames[$addressValue['master_id']]['contact_id'];
           $sharedAddresses[$key]['shared_address_display'] = array(
             'address' => $addressValue['display'],
             'name' => $shareAddressContactNames[$addressValue['master_id']]['name'],
+            'options' => CRM_Core_BAO_Address::getValues(array('entity_id' => $master_cid, 'contact_id' => $master_cid)),
+            'master_id' => $addressValue['master_id'],
           );
+          $defaults['address'][$key]['master_contact_id'] = $master_cid;
         }
         else {
           $defaults['address'][$key]['use_shared_address'] = 0;
@@ -580,10 +417,10 @@ class CRM_Contact_Form_Edit_Address {
 
 
   /**
-   * @param $form
+   * @param CRM_Core_Form $form
    * @param $groupTree
    */
-  static function storeRequiredCustomDataInfo(&$form, $groupTree) {
+  public static function storeRequiredCustomDataInfo(&$form, $groupTree) {
     if (CRM_Utils_System::getClassName($form) == 'CRM_Contact_Form_Contact') {
       $requireOmission = NULL;
       foreach ($groupTree as $csId => $csVal) {