CRM-15287 - More consistent behavior when editing/adding shared address
authorColeman Watts <coleman@civicrm.org>
Sat, 13 Sep 2014 00:19:11 +0000 (20:19 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 13 Sep 2014 00:19:11 +0000 (20:19 -0400)
CRM/Contact/BAO/Contact/Utils.php
CRM/Contact/Form/Edit/Address.php
CRM/Core/BAO/Address.php
templates/CRM/Contact/Form/ShareAddress.tpl

index 5da54baa545bdc7487613065a0ff8f66a1489d3a..94b2dc79df7236775f609f2a9e956abb15220f01 100644 (file)
@@ -850,6 +850,7 @@ Group By  componentId";
         $contactNames[$dao->id] = array(
           'name' => "<a href='{$contactViewUrl}'>{$dao->display_name}</a>",
           'is_deleted' => $dao->is_deleted,
+          'contact_id' => $dao->cid,
         );
       }
     }
index 936869c1c946570d78ceadc9ffe59e40ef949ebe..676c4282e5822c78355ed3708747c8e00bd2f894 100644 (file)
@@ -339,10 +339,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;
index 5903c00ef5ccc1af9722920749435a85edacae8c..1c1ebf7604a262bbc38919256089969221212e46 100644 (file)
@@ -463,7 +463,7 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
    * @access public
    * @static
    */
-  static function &getValues(&$entityBlock, $microformat = FALSE, $fieldName = 'contact_id') {
+  static function &getValues($entityBlock, $microformat = FALSE, $fieldName = 'contact_id') {
     if (empty($entityBlock)) {
       return NULL;
     }
index 95a301b2493e03b9e8c1a935d4e17dce90ace2ef..3e39050c4c11ecb73e0c59f42a507c5d438b7bb4 100644 (file)
 {* template for handling share address functionality*}
 <tr>
   <td>
-    {$form.address.$blockId.use_shared_address.html}{$form.address.$blockId.use_shared_address.label} {help id="id-sharedAddress" file="CRM/Contact/Form/Contact.hlp"}<br />
-    {if !empty($sharedAddresses.$blockId.shared_address_display)}
-      <span class="shared-address-display" id="shared-address-display-name-{$blockId}">
-        {$sharedAddresses.$blockId.shared_address_display.name}
-      </span>
-
-      <span class="shared-address-display" id="shared-address-display-{$blockId}" onclick="cj(this).hide( );cj('#shared-address-display-name-{$blockId}').hide( );cj('#shared-address-display-cancel-{$blockId}').show( );cj('#shared-address-{$blockId}').show( );">
-              {$sharedAddresses.$blockId.shared_address_display.address} <a href='#' onclick='return false;'>( {ts}Change current shared address{/ts} )</a>
-      </span>
-
-      <span id="shared-address-display-cancel-{$blockId}" class="hiddenElement" onclick="cj(this).hide( );cj('#shared-address-display-name-{$blockId}').show( );cj('#shared-address-display-{$blockId}').show( );cj('#shared-address-{$blockId}').hide( );">
-              <a href='#' onclick='return false;'>( {ts}Cancel{/ts} )</a>
-      </span>
-    {/if}
-
-    <div id="shared-address-{$blockId}" class="form-layout-compressed hiddenElement">
+    {$form.address.$blockId.use_shared_address.html}{$form.address.$blockId.use_shared_address.label} {help id="id-sharedAddress" file="CRM/Contact/Form/Contact.hlp"}
+    <div id="shared-address-{$blockId}" class="form-layout-compressed">
       {$form.address.$blockId.master_contact_id.label}
       {$form.address.$blockId.master_contact_id.html}
-      <div class="shared-address-list"></div>
+      <div class="shared-address-list">
+        {if !empty($sharedAddresses.$blockId.shared_address_display)}
+          {foreach item='sa' from=$sharedAddresses.$blockId.shared_address_display.options}
+            {assign var="sa_name" value="selected_shared_address-`$blockId`"}
+            {assign var="sa_id" value="`$sa_name`-`$sa.id`"}
+            <input type="radio" name="{$sa_name}" id="{$sa_id}" value="{$sa.id}" {if $sa.id eq $sharedAddresses.$blockId.shared_address_display.master_id}checked="checked"{/if}>
+            <label for="{$sa_id}">{$sa.display_text}</label><br/>
+          {/foreach}
+        {/if}
+      </div>
     </div>
   </td>
 </tr>
 {literal}
 <script type="text/javascript">
   CRM.$(function($) {
-    var blockNo = {/literal}{$blockId}{literal};
+    var blockNo = {/literal}{$blockId}{literal},
+      $contentArea = $('#shared-address-' + blockNo + ' .shared-address-list'),
+      $masterElement = $('input[name="address[' + blockNo + '][master_id]"]');
 
     function showHideSharedAddress() {
       // based on checkbox, show or hide
       var share = $(this).prop('checked');
-      $('#shared-address-' + blockNo + ', #shared-address-display-' + blockNo + ', #shared-address-display-name-' + blockNo).toggle(!!share);
-      $('table#address_table_' + blockNo +', #shared-address-display-cancel-' + blockNo + ', .crm-address-custom-set-block-' + blockNo).toggle(!share);
+      $('#shared-address-' + blockNo).toggle(!!share);
+      $('table#address_table_' + blockNo +', .crm-address-custom-set-block-' + blockNo).toggle(!share);
     }
 
     // "Use another contact's address" checkbox
     $('#address\\[' + blockNo + '\\]\\[use_shared_address\\]').each(showHideSharedAddress).click(showHideSharedAddress);
 
+    // When an address is selected
+    $contentArea.off().on('click', 'input', function() {
+      $masterElement.val($(this).val());
+    });
+
     // When shared contact is selected/unselected
     $('input[name="address[' + blockNo +'][master_contact_id]"]').change(function() {
       var $el = $(this),
-        sharedContactId = $el.val(),
-        $contentArea = $('#shared-address-' + blockNo + ' .shared-address-list').html('').off(),
-        $masterElement = $('input[name="address[' + blockNo + '][master_id]"]').val('');
+        sharedContactId = $el.val();
+
+      $contentArea.html('');
+      $masterElement.val('');
 
       if (!sharedContactId || isNaN(sharedContactId)) {
         return;
               addressHTML = {/literal}"{ts escape='js'}Selected contact does not have an address. Please edit that contact to add an address, or select a different contact.{/ts}"{literal};
             }
 
-            $contentArea.html(addressHTML).on('click', 'input', function() {
-              $masterElement.val($(this).val());
-            });
+            $contentArea.html(addressHTML);
           }
         },'json');
     });