CRM-15109 - Fix deleting custom records and update UX to 4.5 conventions
authorColeman Watts <coleman@civicrm.org>
Thu, 14 Aug 2014 14:21:33 +0000 (15:21 +0100)
committerColeman Watts <coleman@civicrm.org>
Thu, 14 Aug 2014 14:21:33 +0000 (15:21 +0100)
CRM/Contact/Page/View/CustomData.php
templates/CRM/Contact/Form/Contact.tpl
templates/CRM/Contact/Form/Edit/CustomData.tpl
templates/CRM/Custom/Form/CustomData.tpl
templates/CRM/Custom/Page/CustomDataView.tpl

index 2406084a6aac24137afc31b5c1eaaf072b930a7e..07a89e82779eca4606b8624af3e85bc83511d808 100644 (file)
@@ -128,9 +128,12 @@ class CRM_Contact_Page_View_CustomData extends CRM_Core_Page {
         'style'
       );
 
-      if ($displayStyle === 'Tab with table' && $this->_multiRecordDisplay != 'single') {
+      if ($this->_multiRecordDisplay != 'single') {
         $id = "custom_{$this->_groupId}";
         $this->ajaxResponse['tabCount'] = CRM_Contact_BAO_Contact::getCountComponent($id, $this->_contactId, $groupTree[$this->_groupId]['table_name']);
+      }
+
+      if ($displayStyle === 'Tab with table' && $this->_multiRecordDisplay != 'single') {
         $ctype = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
           $this->_contactId,
           'contact_type'
index 8923334e0188167c78d4691f382238f5b6cfbd17..732830e129a1aedba2f028c6e68ae0bd8937f282 100644 (file)
 
   <script type="text/javascript" >
   CRM.$(function($) {
+    var $form = $("#{/literal}{$form.formName}{literal}");
     var action = "{/literal}{$action}{literal}";
 
     $('.crm-accordion-body').each( function() {
       return true;
     });
     {/literal}{/if}{literal}
+
+    // Handle delete of multi-record custom data
+    $form.on('click', '.crm-custom-value-del', function(e) {
+      e.preventDefault();
+      var $el = $(this),
+        msg = '{/literal}{ts escape="js"}The record will be deleted immediately. This action cannot be undone.{/ts}{literal}';
+      CRM.confirm({title: $el.attr('title'), message: msg})
+        .on('crmConfirm:yes', function() {
+          var url = CRM.url('civicrm/ajax/customvalue');
+          var request = $.post(url, $el.data('post'));
+          CRM.status({success: '{/literal}{ts escape="js"}Record Deleted{/ts}{literal}'}, request);
+          var addClass = '.add-more-link-' + $el.data('post').groupID;
+          $el.closest('div.crm-custom-accordion').remove();
+          $('div' + addClass).last().show();
+        });
+    });
   });
 
 </script>
index d0ea6b03dce584881db894342edfd5d8a518702f..ba1c641c18c47cd2885cfee9f8789d68d9f5523f 100644 (file)
  +--------------------------------------------------------------------+
 *}
 
-<script type="text/javascript">var showTab = Array();</script>
 {foreach from=$groupTree item=cd_edit key=group_id}
   {if $cd_edit.is_multiple eq 1}
     {assign var=tableID value=$cd_edit.table_id}
     {assign var=divName value=$group_id|cat:"_$tableID"}
     <div></div>
-    <div id="{$cd_edit.name|cat:'_$divName'}"
+    <div
      class="crm-accordion-wrapper crm-custom-accordion {if $cd_edit.collapse_display and !$skipTitle}collapsed{/if}">
   {else}
     <div id="{$cd_edit.name}"
       {if $cd_edit.is_multiple eq 1}
         {if $cd_edit.table_id}
           <table class="no-border">
-            <tr id="statusmessg_{$group_id|cat:"_$tableID"}" class="hiddenElement">
-              <td><span class="success-status"></span></td>
-            </tr>
             <tr>
-              <div class="crm-submit-buttons">
-                <a href="#"
-                   onclick="showDelete( {$tableID}, '{$cd_edit.name}_{$group_id|cat:"_$tableID"}', {$group_id}, {$contactId} ); return false;"
-                   class="button delete-button" title="{ts 1=$cv_edit.title}Delete this %1 record{/ts}">
-                  <span><div class="icon delete-icon"></div>{ts}Delete{/ts}</span>
-                </a>
-              </div>
+              <a href="#" class="crm-hover-button crm-custom-value-del" title="{ts 1=$cd_edit.title}Delete %1{/ts}"
+               data-post='{ldelim}"valueID": "{$tableID}", "groupID": "{$group_id}", "contactId": "{$contactId}", "key": "{crmKey name='civicrm/ajax/customvalue'}"{rdelim}'>
+                <span class="icon delete-icon"></span> {ts}Delete{/ts}
+              </a>
               <!-- crm-submit-buttons -->
             </tr>
           </table>
   {/foreach}
 
   {include file="CRM/common/customData.tpl"}
-  <script type="text/javascript">
-    {literal}
-
-    function hideStatus(valueID, groupID) {
-      cj('#statusmessg_' + groupID + '_' + valueID).hide();
-    }
-
-    function showDelete(valueID, elementID, groupID, contactID) {
-      var confirmMsg = '{/literal}{ts escape='js'}Are you sure you want to delete this record?{/ts}{literal} &nbsp; <a href="#" onclick="deleteCustomValue( ' + valueID + ',\'' + elementID + '\',' + groupID + ',' + contactID + ' ); return false;" style="text-decoration: underline;">{/literal}{ts escape='js'}Yes{/ts}{literal}</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="hideStatus( ' + valueID + ', ' + groupID + ' ); return false;" style="text-decoration: underline;">{/literal}{ts escape='js'}No{/ts}{literal}</a>';
-      cj('tr#statusmessg_' + groupID + '_' + valueID).show().children().find('span').html(confirmMsg);
-    }
-
-    function deleteCustomValue(valueID, elementID, groupID, contactID) {
-      var postUrl = {/literal}"{crmURL p='civicrm/ajax/customvalue' h=0 }"{literal},
-      request = cj.ajax({
-        type: "POST",
-        data: "valueID=" + valueID + "&groupID=" + groupID + "&contactId=" + contactID + "&key={/literal}{crmKey name='civicrm/ajax/customvalue'}{literal}",
-        url: postUrl,
-        success: function (html) {
-          cj('#' + elementID).hide();
-          hideStatus(valueID, groupID);
-          var element = cj('.ui-tabs-nav #tab_custom_' + groupID + ' a');
-          cj(element).html(cj(element).attr('title') + ' (' + html + ') ');
-        }
-      });
-      CRM.status({success: '{/literal}{ts escape="js"}Record Deleted{/ts}{literal}'}, request);
-    }
-
-    {/literal}
-  </script>
 
   {include file="CRM/Form/attachmentjs.tpl"}
index 0960fae327c32844ee06f925400530ad3bd94fc3..1a4677d58f43ad91de6c7b1438d9f487a69a5f44 100644 (file)
     <div class="messages help">{$cd_edit.help_post}</div>
   {/if}
   {if $cd_edit.is_multiple and ( ( $cd_edit.max_multiple eq '' )  or ( $cd_edit.max_multiple > 0 and $cd_edit.max_multiple > $cgCount ) ) }
-    <div id="add-more-link-{$cgCount}" class="add-more-link-{$group_id}-{$cgCount}"><a href="#" onclick="CRM.buildCustomData('{$cd_edit.extends}',{if $cd_edit.subtype}'{$cd_edit.subtype}'{else}'{$cd_edit.extends_entity_column_id}'{/if}, '', {$cgCount}, {$group_id}, true ); return false;">{ts 1=$cd_edit.title}Add another %1 record{/ts}</a></div>
+    <div id="add-more-link-{$cgCount}" class="add-more-link-{$group_id} add-more-link-{$group_id}-{$cgCount}">
+      <a href="#" class="crm-hover-button" onclick="CRM.buildCustomData('{$cd_edit.extends}',{if $cd_edit.subtype}'{$cd_edit.subtype}'{else}'{$cd_edit.extends_entity_column_id}'{/if}, '', {$cgCount}, {$group_id}, true ); return false;">
+        <span class="icon ui-icon-circle-plus"></span>
+        {ts 1=$cd_edit.title}Another %1 record{/ts}
+      </a>
+    </div>
   {/if}
 {else}
   {foreach from=$groupTree item=cd_edit key=group_id name=custom_sets}
@@ -61,7 +66,7 @@
      <div class="custom-group custom-group-{$cd_edit.name} crm-accordion-wrapper {if $cd_edit.collapse_display and !$skipTitle}collapsed{/if}">
       {if !$skipTitle}
       <div class="crm-accordion-header">
-        {$cd_edit.title}
+        {$cd_edit.title} {$cgCount}
        </div><!-- /.crm-accordion-header -->
       {/if}
       <div class="crm-accordion-body">
           <em>{ts 1=$cd_edit.title}Click "Edit Contact" to add more %1 records{/ts}</em>
         </div>
       {else}
-        <div id="add-more-link-{$cgCount}"><a href="#" onclick="CRM.buildCustomData('{$cd_edit.extends}',{if $cd_edit.subtype}'{$cd_edit.subtype}'{else}'{$cd_edit.extends_entity_column_id}'{/if}, '', {$cgCount}, {$group_id}, true ); return false;">{ts 1=$cd_edit.title}Add another %1 record{/ts}</a></div>       
+        <div id="add-more-link-{$cgCount}">
+          <a href="#" class="crm-hover-button" onclick="CRM.buildCustomData('{$cd_edit.extends}',{if $cd_edit.subtype}'{$cd_edit.subtype}'{else}'{$cd_edit.extends_entity_column_id}'{/if}, '', {$cgCount}, {$group_id}, true ); return false;">
+            <span class="icon ui-icon-circle-plus"></span>
+            {ts 1=$cd_edit.title}Another %1 record{/ts}
+          </a>
+        </div>
       {/if}
     {/if}
     {/if}
index 28474618b50ed88637cc4d13e4b020c9260f4e62..66783eaf8feac9b5961d1abef8f9525b645298e5 100644 (file)
@@ -25,6 +25,7 @@
 *}
 {* Custom Data view mode*}
 {assign var="showEdit" value=1}
+{assign var="rowCount" value=1}
 {foreach from=$viewCustomData item=customValues key=customGroupId}
   {foreach from=$customValues item=cd_edit key=cvID}
 {if $multiRecordDisplay neq 'single'}
           <div class="crm-accordion-wrapper {if $cd_edit.collapse_display eq 0 or $skipTitle} {else}collapsed{/if}">
             {if !$skipTitle}
               <div class="crm-accordion-header">
-                {$cd_edit.title}
+                {$cd_edit.title} {$rowCount}
               </div>
             {/if}
             <div class="crm-accordion-body">
               {if $groupId and $cvID and $editCustomData}
                 <div class="crm-submit-buttons">
-                  <a href="#"
-                     onclick="showDelete( {$cvID}, '{$cd_edit.name}_{$index}', {$customGroupId}, {$contactId} ); return false;"
-                     class="button delete-button" title="{ts 1=$cd_edit.title}Delete this %1 record{/ts}">
-                    <span><div class="icon delete-icon"></div>{ts}Delete{/ts}</span>
+                  <a href="#" class="crm-hover-button crm-custom-value-del"
+                     data-post='{ldelim}"valueID": "{$cvID}", "groupID": "{$customGroupId}", "contactId": "{$contactId}", "key": "{crmKey name='civicrm/ajax/customvalue'}"{rdelim}'
+                     title="{ts 1=$cd_edit.title|cat:" `$rowCount`"}Delete %1{/ts}">
+                    <span class="icon delete-icon"></span> {ts}Delete{/ts}
                   </a>
                 </div>
               {/if}
                   </tr>
                 </table>
               {/foreach}
+              {assign var="rowCount" value=$rowCount+1}
             </div>
             <!-- end of body -->
             <div class="clear"></div>
 {if $groupId}
   <script type="text/javascript">
     {literal}
-    function hideStatus(valueID, groupID) {
-      cj('#statusmessg_' + groupID + '_' + valueID).hide();
-    }
-    function showDelete(valueID, elementID, groupID, contactID) {
-      var confirmMsg = '{/literal}{ts escape='js'}Are you sure you want to delete this record?{/ts}{literal} &nbsp; <a href="#" onclick="deleteCustomValue( ' + valueID + ',\'' + elementID + '\',' + groupID + ',' + contactID + ' ); return false;" style="text-decoration: underline;">{/literal}{ts escape='js'}Yes{/ts}{literal}</a>&nbsp;&nbsp;&nbsp;<a href="#" onclick="hideStatus( ' + valueID + ', ' + groupID + ' ); return false;" style="text-decoration: underline;">{/literal}{ts escape='js'}No{/ts}{literal}</a>';
-      cj('tr#statusmessg_' + groupID + '_' + valueID).show().children().find('span').html(confirmMsg);
-    }
-    function deleteCustomValue(valueID, elementID, groupID, contactID) {
-      var postUrl = {/literal}"{crmURL p='civicrm/ajax/customvalue' h=0 }"{literal},
-      request = cj.ajax({
-        type: "POST",
-        data: "valueID=" + valueID + "&groupID=" + groupID + "&contactId=" + contactID + "&key={/literal}{crmKey name='civicrm/ajax/customvalue'}{literal}",
-        url: postUrl,
-        success: function (html) {
-          cj('#' + elementID).hide();
-          hideStatus(valueID, groupID);
-          var element = cj('.ui-tabs-nav #tab_custom_' + groupID + ' a');
-          cj(element).html(cj(element).attr('title') + ' (' + html + ') ');
-        }
+    CRM.$(function($) {
+      // Handle delete of multi-record custom data
+      $('#crm-container')
+        .off('.customValueDel')
+        .on('click.customValueDel', '.crm-custom-value-del', function(e) {
+          e.preventDefault();
+          var $el = $(this),
+            msg = '{/literal}{ts escape="js"}The record will be deleted immediately. This action cannot be undone.{/ts}{literal}';
+          CRM.confirm({title: $el.attr('title'), message: msg})
+            .on('crmConfirm:yes', function() {
+              var url = CRM.url('civicrm/ajax/customvalue');
+              var request = $.post(url, $el.data('post'))
+                .done(CRM.refreshParent($el));
+              CRM.status({success: '{/literal}{ts escape="js"}Record Deleted{/ts}{literal}'}, request);
+            });
+        });
       });
-      CRM.status({success: '{/literal}{ts escape="js"}Record Deleted{/ts}{literal}'}, request);
-    }
     {/literal}
   </script>
 {/if}