Fixes and cleanup for file-on-case
authorColeman Watts <coleman@civicrm.org>
Sat, 6 Sep 2014 01:00:47 +0000 (21:00 -0400)
committerColeman Watts <coleman@civicrm.org>
Sat, 6 Sep 2014 01:00:47 +0000 (21:00 -0400)
CRM/Activity/Page/AJAX.php
CRM/Activity/Selector/Activity.php
CRM/Case/BAO/Case.php
templates/CRM/Case/Form/ActivityToCase.tpl
tests/phpunit/WebTest/Case/ActivityToCaseTest.php

index 2c6f56b9c0745c1015fad4f7d0bfad023bf37d25..842d447a5624c3c85edc0e5b6d956de399a809f5 100644 (file)
@@ -266,20 +266,22 @@ class CRM_Activity_Page_AJAX {
       // edit links
       $row['actions'] = '';
       if ($hasAccessToAllCases) {
-        switch($row['source']){
+        $contactType = empty($row['relation_type']) ? '' : (string) CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_RelationshipType', $row['relation_type'], 'contact_type_b');
+        $contactType = $contactType == 'Contact' ? '' : $contactType;
+        switch($row['source']) {
         case 'caseRel':
           $row['actions'] =
-            '<a href="#editCaseRoleDialog" title="'. ts('Reassign %1', array(1 => $typeLabel)) .'" class="crm-hover-button case-miniform" data-rel_type="'. $row['relation_type'] .'" data-rel_id="'. $row['rel_id'] .'"data-key="'. CRM_Core_Key::get('civicrm/ajax/relation') .'">'.
+            '<a href="#editCaseRoleDialog" title="'. ts('Reassign %1', array(1 => $typeLabel)) .'" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="'. $row['relation_type'] .'" data-rel_id="'. $row['rel_id'] .'"data-key="'. CRM_Core_Key::get('civicrm/ajax/relation') .'">'.
               '<span class="icon edit-icon"></span>'.
             '</a>'.
-            '<a href="#deleteCaseRoleDialog" title="'. ts('Remove %1', array(1 => $typeLabel)) .'" class="crm-hover-button case-miniform" data-rel_type="'.$row['relation_type'].'" data-key="'. CRM_Core_Key::get('civicrm/ajax/delcaserole') .'">'.
+            '<a href="#deleteCaseRoleDialog" title="'. ts('Remove %1', array(1 => $typeLabel)) .'" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="'.$row['relation_type'].'" data-key="'. CRM_Core_Key::get('civicrm/ajax/delcaserole') .'">'.
               '<span class="icon delete-icon"></span>'.
             '</a>';
           break;
 
         case 'caseRoles':
           $row['actions'] =
-            '<a href="#editCaseRoleDialog" title="'. ts('Assign %1', array(1 => $typeLabel)) .'" class="crm-hover-button case-miniform" data-rel_type="'. $row['relation_type'] .'" data-key="'. CRM_Core_Key::get('civicrm/ajax/relation') .'">'.
+            '<a href="#editCaseRoleDialog" title="'. ts('Assign %1', array(1 => $typeLabel)) .'" class="crm-hover-button case-miniform" data-contact_type="' . $contactType . '" data-rel_type="'. $row['relation_type'] .'" data-key="'. CRM_Core_Key::get('civicrm/ajax/relation') .'">'.
               '<span class="icon edit-icon"></span>'.
             '</a>';
           break;
index 2b5c370ca46eaeb68affd9399d6ad10637e69099..3fbc6cd2af1f02acdc5f4a07f9b62c11b994a64b 100644 (file)
@@ -246,7 +246,7 @@ class CRM_Activity_Selector_Activity extends CRM_Core_Selector_Base implements C
     ) {
       $actionLinks += array(CRM_Core_Action::ADD => array('name' => ts('File On Case'),
         'url' => '#',
-        'extra' => 'onclick="javascript:fileOnCase( \'file\', \'%%id%%\' ); return false;"',
+        'extra' => 'onclick="javascript:fileOnCase( \'file\', \'%%id%%\', null, this ); return false;"',
         'title' => ts('File On Case'),
       ));
     }
index 35069f3e75c8d0c9d3ff2c8bea158ecb34b21d96..39823568ebe26d6af7fca940f8e40d62e7e05bed 100644 (file)
@@ -1307,10 +1307,10 @@ SELECT case_status.label AS case_status, status_id, civicrm_case_type.title AS c
 
       //check for operations.
       if (self::checkPermission($dao->id, 'Move To Case', $dao->activity_type_id)) {
-        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'move\',' . $dao->id . ', ' . $caseID . ' ); return false;">' . ts('Move To Case') . '</a> ';
+        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'move\',' . $dao->id . ', ' . $caseID . ', this ); return false;">' . ts('Move To Case') . '</a> ';
       }
       if (self::checkPermission($dao->id, 'Copy To Case', $dao->activity_type_id)) {
-        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'copy\',' . $dao->id . ',' . $caseID . ' ); return false;">' . ts('Copy To Case') . '</a> ';
+        $url .= ' <a ' . $css . ' href="#" onClick="Javascript:fileOnCase( \'copy\',' . $dao->id . ',' . $caseID . ', this ); return false;">' . ts('Copy To Case') . '</a> ';
       }
       // if there are file attachments we will return how many and, if only one, add a link to it
       if (!empty($dao->attachment_ids)) {
index b7723b75e30629b4f49a13d58593674581034054..afa678a17891cc642a3ab08b5d652722e68fc5af 100644 (file)
@@ -46,7 +46,8 @@
 {literal}
   <script type="text/javascript">
     CRM.$(function($) {
-      $('input[name=file_on_case_unclosed_case_id]', '#fileOnCaseDialog').crmSelect2({
+      var $form = $('form.{/literal}{$form.formClass}{literal}');
+      $('input[name=file_on_case_unclosed_case_id]', $form).crmSelect2({
         placeholder: {/literal}'{ts escape="js"}- select case -{/ts}'{literal},
         minimumInputLength: 1,
         formatResult: CRM.utils.formatSelect2Result,
         }
       });
     });
-
-    cj( "#fileOnCaseDialog" ).hide( );
-
   </script>
 {/literal}
 {* main form end *}
 
 {else}
 {* Markup and js to go on the main page for loading the above form in a popup *}
-<div id="fileOnCaseDialog"></div>
 {literal}
 <script type="text/javascript">
-  function fileOnCase( action, activityID, currentCaseId ) {
+(function($) {
+  window.fileOnCase = function(action, activityID, currentCaseId, a) {
     if ( action == "move" ) {
       var dialogTitle = "{/literal}{ts escape='js'}Move to Case{/ts}{literal}";
     } else if ( action == "copy" ) {
       var dialogTitle = "{/literal}{ts escape='js'}File On Case{/ts}{literal}";
     }
 
-    var dataUrl = {/literal}"{crmURL p='civicrm/case/addToCase' q='reset=1&snippet=4' h=0}"{literal};
+    var dataUrl = {/literal}"{crmURL p='civicrm/case/addToCase' q='reset=1' h=0}"{literal};
     dataUrl += '&activityId=' + activityID + '&caseId=' + currentCaseId + '&cid=' + {/literal}"{$contactID}"{literal};
 
-    cj.ajax({
-      url     : dataUrl,
-      success : function ( content ) {
-        cj("#fileOnCaseDialog").show( ).html( content).trigger('crmLoad').dialog({
-          title: dialogTitle,
-          modal: true,
-          width: 600,
-          height: 'auto',
-          close: function( event, ui ) {
-            cj('input[name=file_on_case_unclosed_case_id]', '#fileOnCaseDialog').select2('destroy');
-            cj(this).hide().dialog("destroy");
-          },
-          buttons: {
-            "{/literal}{ts escape='js'}Save{/ts}{literal}": function() {
-              var selectedCaseId = cj('input[name=file_on_case_unclosed_case_id]', '#fileOnCaseDialog').val();
-              var contactId = cj('input[name=file_on_case_unclosed_case_id]', '#fileOnCaseDialog').select2('data').extra.contact_id;
-              var subject         = cj("#file_on_case_activity_subject").val( );
-              var targetContactId = cj("#file_on_case_target_contact_id").val( );
-
-              if ( !cj("#file_on_case_unclosed_case_id").val( )  ) {
-                cj("#file_on_case_unclosed_case_id").crmError('{/literal}{ts escape="js"}Please select a case from the list{/ts}{literal}.');
-                return false;
-              }
-
-              cj(this).dialog("close");
+    function save() {
+      var $context = $('div.crm-confirm-dialog'),
+        selectedCaseId = $('input[name=file_on_case_unclosed_case_id]', $context).val(),
+        caseTitle = $('input[name=file_on_case_unclosed_case_id]', $context).select2('data').label,
+        contactId = $('input[name=file_on_case_unclosed_case_id]', $context).select2('data').extra.contact_id,
+        subject = $("#file_on_case_activity_subject").val(),
+        targetContactId = $("#file_on_case_target_contact_id").val();
 
-              var postUrl = {/literal}"{crmURL p='civicrm/ajax/activity/convert' h=0 }"{literal};
-              cj.post( postUrl, { activityID: activityID, caseID: selectedCaseId, contactID: contactId, newSubject: subject, targetContactIds: targetContactId, mode: action, key: {/literal}"{crmKey name='civicrm/ajax/activity/convert'}"{literal} },
-                function( values ) {
-                  if ( values.error_msg ) {
-                    cj().crmError(values.error_msg, "{/literal}{ts escape='js'}Unable to file on case{/ts}{literal}.");
-                    return false;
-                  } else {
-                    var destUrl = {/literal}"{crmURL p='civicrm/contact/view/case' q='reset=1&action=view&id=' h=0 }"{literal};
-                    var context = '';
-                    {/literal}{if !empty($fulltext)}{literal}
-                    context = '&context={/literal}{$fulltext}{literal}';
-                    {/literal}{/if}{literal}
-                    var caseUrl = destUrl + selectedCaseId + '&cid=' + contactId + context;
-                    var redirectToCase = false;
-                    var reloadWindow = false;
-                    if ( action == 'move' ) redirectToCase = true;
-                    if ( action == 'file' ) {
-                      var curPath = document.location.href;
-                      if ( curPath.indexOf( 'civicrm/contact/view' ) != -1 ) {
-                        //hide current activity row.
-                        cj( "#crm-activity_" + activityID ).hide( );
-                        var visibleRowCount = 0;
-                        cj('[id^="'+ 'crm-activity' +'"]:visible').each(function() {
-                          visibleRowCount++;
-                        } );
-                        if ( visibleRowCount < 1 ) {
-                          reloadWindow = true;
-                        }
-                      }
-                      if ( ( curPath.indexOf( 'civicrm/contact/view/activity' ) != -1 ) ||
-                        ( curPath.indexOf( 'civicrm/activity' ) != -1 ) ) {
-                        redirectToCase = true;
-                      }
-                    }
+      if (!$("#file_on_case_unclosed_case_id").val()) {
+        $("#file_on_case_unclosed_case_id").crmError('{/literal}{ts escape="js"}Please select a case from the list{/ts}{literal}.');
+        return false;
+      }
 
-                    if ( redirectToCase ) {
-                      window.location.href = caseUrl;
-                    } else if ( reloadWindow ) {
-                      window.location.reload( );
-                    } else {
-                      var activitySubject = cj("#file_on_case_activity_subject").val( );
-                      var statusMsg = activitySubject + '" has been filed to selected case: <a href="' + caseUrl + '">' + cj("#unclosed_cases").val( ) + '</a>.';
-                      CRM.alert(statusMsg, '{/literal}{ts escape="js"}Activity Filed{/ts}{literal}', 'success');
+      var postUrl = {/literal}"{crmURL p='civicrm/ajax/activity/convert' h=0 }"{literal};
+      $.post( postUrl, { activityID: activityID, caseID: selectedCaseId, contactID: contactId, newSubject: subject, targetContactIds: targetContactId, mode: action, key: {/literal}"{crmKey name='civicrm/ajax/activity/convert'}"{literal} },
+        function( values ) {
+          if ( values.error_msg ) {
+            $().crmError(values.error_msg, "{/literal}{ts escape='js'}Unable to file on case.{/ts}{literal}");
+          } else {
+            var destUrl = {/literal}"{crmURL p='civicrm/contact/view/case' q='reset=1&action=view&id=' h=0 }"{literal};
+            var context = '';
+            {/literal}{if !empty($fulltext)}{literal}
+            context = '&context={/literal}{$fulltext}{literal}';
+            {/literal}{/if}{literal}
+            var caseUrl = destUrl + selectedCaseId + '&cid=' + contactId + context;
 
-                    }
-                  }
-                }
-              );
-            },
-            "{/literal}{ts escape='js'}Cancel{/ts}{literal}": function() {
-              cj(this).dialog("close");
-            }
+            var statusMsg = {/literal}'{ts escape='js' 1='%1'}Activity has been filed to %1 case.{/ts}'{literal};
+            CRM.alert(ts(statusMsg, {1: '<a href="' + caseUrl + '">' + caseTitle + '</a>'}), '{/literal}{ts escape="js"}Saved{/ts}{literal}', 'success');
+            CRM.refreshParent(a);
           }
-
-        });
+        }
+      );
+    }
+    
+    CRM.confirm({
+      title: dialogTitle,
+      width: '600',
+      resizable: true,
+      options: {yes: "{/literal}{ts escape='js'}Save{/ts}{literal}", no: "{/literal}{ts escape='js'}Cancel{/ts}{literal}"},
+      open: function() {
+        CRM.loadPage(dataUrl, {target: this});
       }
-    });
+    }).on('crmConfirm:yes', save);
+    
   }
+})(CRM.$);
 </script>
 {/literal}
 {/if}
index b88c3c1b8e77960ef3f6f0af95841c8474caece8..d9bda6f2c930bfa9782a089d1384d3df51a7c09d 100644 (file)
@@ -285,7 +285,6 @@ class WebTest_Case_ActivityToCaseTest extends CiviSeleniumTestCase {
     // click through to the Activity view screen
     $this->waitForElementPresent("xpath=//div[@id='contact-activity-selector-activity_wrapper']//table/tbody/tr[1]/td[8]");
     $this->click("xpath=//div[@id='contact-activity-selector-activity_wrapper']//table/tbody/tr[2]/td[8]/span[2]/ul/li/a[text()='File On Case']");
-    $this->waitForElementPresent("css=div#fileOnCaseDialog");
     $this->waitForElementPresent('file_on_case_activity_subject');
 
     // file activity on case