Merge pull request #5252 from JKingsnorth/CRM-10551
[civicrm-core.git] / CRM / Contact / Form / Edit / Address.php
index f67e165fb5b5e92bed274a55af089bea37e8932a..b6cdd6b7f938157bda5aac3d89b17d4df3d3a15e 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2014                                |
+ | Copyright CiviCRM LLC (c) 2004-2015                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
  | GNU Affero General Public License or the licensing of CiviCRM,     |
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
-*/
+ */
 
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2014
+ * @copyright CiviCRM LLC (c) 2004-2015
  * $Id$
  *
  */
 class CRM_Contact_Form_Edit_Address {
 
   /**
-   * Build form for address input fields
+   * Build form for address input fields.
    *
    * @param CRM_Core_Form $form
-   * @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 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
    *
-   * @static
    */
   public static function buildQuickForm(&$form, $addressBlockCount = NULL, $sharing = TRUE, $inlineEdit = FALSE) {
     // passing this via the session is AWFUL. we need to fix this
@@ -70,101 +72,77 @@ class CRM_Contact_Form_Edit_Address {
     }
 
     //make location type required for inline edit
-    $form->addSelect("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') + $js, $inlineEdit);
+    $form->addField("address[$blockId][location_type_id]", array('entity' => 'address', 'class' => 'eight') + $js, $inlineEdit);
 
     if (!$inlineEdit) {
       $js = array('id' => 'Address_' . $blockId . '_IsPrimary', 'onClick' => 'singleSelect( this.id );');
     }
 
-    $form->addElement(
-      'checkbox',
-      "address[$blockId][is_primary]",
-      ts('Primary location for this contact'),
-      ts('Primary location for this contact'),
-      $js
-    );
+    $form->addField(
+      "address[$blockId][is_primary]", array(
+        'entity' => 'address',
+        'label' => ts('Primary location for this contact'),
+        'text' => ts('Primary location for this contact')) + $js);
 
     if (!$inlineEdit) {
       $js = array('id' => 'Address_' . $blockId . '_IsBilling', 'onClick' => 'singleSelect( this.id );');
     }
 
-    $form->addElement(
-      'checkbox',
-      "address[$blockId][is_billing]",
-      ts('Billing location for this contact'),
-      ts('Billing location for this contact'),
-      $js
-    );
+    $form->addField(
+      "address[$blockId][is_billing]", array(
+        'entity' => 'address',
+        'label' => ts('Primary location for this contact'),
+        'text' => ts('Primary location for this contact')) + $js);
 
     // hidden element to store master address id
-    $form->addElement('hidden', "address[$blockId][master_id]");
-
+    $form->addField("address[$blockId][master_id]", array('entity' => 'address', 'type' => 'hidden'));
     $addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
       'address_options', TRUE, NULL, TRUE
     );
     $attributes = CRM_Core_DAO::getAttribute('CRM_Core_DAO_Address');
 
     $elements = array(
-      'address_name' => array(ts('Address Name'), $attributes['address_name'], NULL),
-      'street_address' => array(ts('Street Address'), $attributes['street_address'], NULL),
-      'supplemental_address_1' => array(ts('Supplemental Address 1'), $attributes['supplemental_address_1'], NULL),
-      'supplemental_address_2' => array(ts('Supplemental Address 2'), $attributes['supplemental_address_2'], NULL),
-      '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),
-      '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),
-      'street_name' => array(ts('Street Name'), $attributes['street_name'], NULL),
-      'street_unit' => array(ts('Apt/Unit/Suite'), $attributes['street_unit'], NULL),
+      'address_name',
+      'street_address',
+      'supplemental_address_1',
+      'supplemental_address_2',
+      'city',
+      'postal_code',
+      'postal_code_suffix',
+      'country_id',
+      'state_province_id',
+      'county_id',
+      'geo_code_1',
+      'geo_code_2',
+      'street_number',
+      'street_name',
+      'street_unit',
     );
 
-    foreach ($elements as $name => $v) {
-      list($title, $attributes, $select) = $v;
-
+    foreach ($elements as $name) {
+      //Remove id from name, to allow comparison against enabled addressOtions.
       $nameWithoutID = strpos($name, '_id') !== FALSE ? substr($name, 0, -3) : $name;
+      // Skip fields which are not enabled in the address options.
       if (empty($addressOptions[$nameWithoutID])) {
         $continue = TRUE;
+        //Don't skip street parsed fields when parsing is enabled.
         if (in_array($nameWithoutID, array(
-          'street_number', 'street_name', 'street_unit')) && !empty($addressOptions['street_address_parsing'])) {
+            'street_number',
+            'street_name',
+            'street_unit',
+          )) && !empty($addressOptions['street_address_parsing'])
+        ) {
           $continue = FALSE;
         }
         if ($continue) {
           continue;
         }
       }
-
-      //build normal select if country is not present in address block
-      if ($name == 'state_province_id' && !$addressOptions['country']) {
-        $select = 'stateProvince';
+      if ($name == 'address_name') {
+        $name = 'name';
       }
 
-      if (!$select) {
-        if ($name == 'state_province_id' || $name == 'county_id') {
-          $form->addChainSelect("address[$blockId][$name]");
-        }
-        else {
-          if ($name == 'address_name') {
-            $name = 'name';
-          }
-
-          $form->addElement('text',
-            "address[$blockId][$name]",
-            $title,
-            $attributes
-          );
-        }
-      }
-      else {
-        $form->addElement('select',
-          "address[$blockId][$name]",
-          $title,
-          array('' => ts('- select -')) + CRM_Core_PseudoConstant::$select()
-        );
-      }
+      $form->addField("address[$blockId][$name]", array('entity' => 'address'));
     }
 
     $entityId = NULL;
@@ -207,7 +185,7 @@ class CRM_Contact_Form_Edit_Address {
       // since we change element name for address custom data, we need to format the setdefault values
       $addressDefaults = array();
       foreach ($defaults as $key => $val) {
-        if ( empty( $val ) ) {
+        if (empty($val)) {
           continue;
         }
 
@@ -230,10 +208,9 @@ class CRM_Contact_Form_Edit_Address {
         CRM_Contact_Form_Edit_Address::storeRequiredCustomDataInfo($form, $groupTree);
       }
 
-      $template     = CRM_Core_Smarty::singleton();
+      $template = CRM_Core_Smarty::singleton();
       $tplGroupTree = $template->get_template_vars('address_groupTree');
-      $tplGroupTree = empty($tplGroupTree) ? array(
-        ) : $tplGroupTree;
+      $tplGroupTree = empty($tplGroupTree) ? array() : $tplGroupTree;
 
       $form->assign('address_groupTree', $tplGroupTree + array($blockId => $groupTree));
       // unset the temp smarty var that got created
@@ -246,7 +223,11 @@ class CRM_Contact_Form_Edit_Address {
       $form->addElement('checkbox', "address[$blockId][use_shared_address]", NULL, ts('Use another contact\'s address'));
 
       // Override the default profile links to add address form
-      $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array('new_individual', 'new_organization', 'new_household'), 'shared_address');
+      $profileLinks = CRM_Core_BAO_UFGroup::getCreateLinks(array(
+          'new_individual',
+          'new_organization',
+          'new_household',
+        ), 'shared_address');
       $form->addEntityRef("address[$blockId][master_contact_id]", ts('Share With'), array('create' => $profileLinks));
     }
   }
@@ -258,9 +239,9 @@ class CRM_Contact_Form_Edit_Address {
    * @param $files
    * @param $self
    *
-   * @return array|bool if no errors
+   * @return array|bool
+   *   if no errors
    *
-   * @static
    */
   public static function formRule($fields, $files, $self) {
     $errors = array();
@@ -277,7 +258,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
@@ -291,7 +272,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
@@ -310,14 +291,15 @@ class CRM_Contact_Form_Edit_Address {
   }
 
   /**
-   * Set default values for address block
+   * Set default values for address block.
    *
-   * @param array  $defaults  defaults associated array
-   * @param CRM_Core_Form $form     form object
+   * @param array $defaults
+   *   Defaults associated array.
+   * @param CRM_Core_Form $form
+   *   Form object.
    *
-   * @static
    */
-  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'])
@@ -336,7 +318,10 @@ class CRM_Contact_Form_Edit_Address {
           $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)),
+            '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;
@@ -360,9 +345,15 @@ class CRM_Contact_Form_Edit_Address {
         foreach ($defaults['address'] as $cnt => & $address) {
           $streetAddress = NULL;
           foreach (array(
-            'street_number', 'street_number_suffix', 'street_name', 'street_unit') as $fld) {
+                     'street_number',
+                     'street_number_suffix',
+                     'street_name',
+                     'street_unit',
+                   ) as $fld) {
             if (in_array($fld, array(
-              'street_name', 'street_unit'))) {
+              'street_name',
+              'street_unit',
+            ))) {
               $streetAddress .= ' ';
             }
             $streetAddress .= CRM_Utils_Array::value($fld, $address);
@@ -438,4 +429,5 @@ class CRM_Contact_Form_Edit_Address {
       $form->_addressRequireOmission = rtrim($requireOmission, ',');
     }
   }
+
 }