CRM-13966 - Migrate case autocompletes to select2
authorColeman Watts <coleman@civicrm.org>
Thu, 27 Feb 2014 01:45:37 +0000 (20:45 -0500)
committerColeman Watts <coleman@civicrm.org>
Thu, 27 Feb 2014 01:45:37 +0000 (20:45 -0500)
CRM/Activity/Form/Task/FileOnCase.php
CRM/Case/Form/Activity/LinkCases.php
CRM/Case/Form/ActivityToCase.php
CRM/Case/Page/AJAX.php
templates/CRM/Activity/Form/Task/FileOnCase.tpl
templates/CRM/Case/Form/Activity/LinkCases.tpl
templates/CRM/Case/Form/ActivityToCase.tpl

index 8bd80d673c65fc6727af32b2e13dad9bab155482..4ba197bb76ea5c8441e2e1af8931714f80ae0cb7 100644 (file)
@@ -62,25 +62,13 @@ class CRM_Activity_Form_Task_FileOnCase extends CRM_Activity_Form_Task {
    *
    * @return void
    * @access public
-   */ function preProcess() {
-    /*
-         * initialize the task and row fields
-         */
-
+   */
+  function preProcess() {
     parent::preProcess();
     $session = CRM_Core_Session::singleton();
     $this->_userContext = $session->readUserContext();
 
     CRM_Utils_System::setTitle(ts('File on Case'));
-
-    $validationFailed = FALSE;
-
-    // insert validations here
-
-    // then redirect
-    if ($validationFailed) {
-      CRM_Utils_System::redirect($this->_userContext);
-    }
   }
 
   /**
@@ -91,8 +79,7 @@ class CRM_Activity_Form_Task_FileOnCase extends CRM_Activity_Form_Task {
    * @return void
    */
   function buildQuickForm() {
-    $this->addElement('text', 'unclosed_cases', ts('Select Case'));
-    $this->add('hidden', 'unclosed_case_id', '', array('id' => 'unclosed_case_id'));
+    $this->add('text', 'unclosed_case_id', ts('Select Case'), array('class' => 'huge'), TRUE);
     $this->addDefaultButtons(ts('Continue >>'));
   }
 
@@ -103,27 +90,7 @@ class CRM_Activity_Form_Task_FileOnCase extends CRM_Activity_Form_Task {
    *
    * @return void
    */
-  function addRules() {
-    $this->addFormRule(array('CRM_Activity_Form_Task_FileOnCase', 'formRule'));
-  }
-
-  /**
-   * global validation rules for the form
-   *
-   * @param array $fields posted values of the form
-   *
-   * @return array list of errors to be posted back to the form
-   * @static
-   * @access public
-   */
-  static
-  function formRule($fields) {
-    $errors = array();
-    if (empty($fields['unclosed_case_id'])) {
-      $errors['unclosed_case_id'] = ts('Case is a required field.');
-    }
-    return $errors;
-  }
+  function addRules() {}
 
   /**
    * process the form after the input has been submitted and validated
index d976b3373617c56f57365968767310161063f5e3..1f2cbcdbcda838374ad430430bbaffb920100e3f 100644 (file)
@@ -72,8 +72,7 @@ class CRM_Case_Form_Activity_LinkCases {
   }
 
   static function buildQuickForm(&$form) {
-    $form->add('text', 'link_to_case', ts('Link To Case'));
-    $form->add('hidden', 'link_to_case_id', '', array('id' => 'link_to_case_id'));
+    $form->add('text', 'link_to_case_id', ts('Link To Case'), array('class' => 'huge'), TRUE);
   }
 
   /**
@@ -89,17 +88,14 @@ class CRM_Case_Form_Activity_LinkCases {
     $errors = array();
 
     $linkCaseId = CRM_Utils_Array::value('link_to_case_id', $values);
-    if (!$linkCaseId) {
-      $errors['link_to_case'] = ts('Please select a case to link.');
-    }
-    elseif ($linkCaseId == $form->_caseId) {
+    if ($linkCaseId == $form->_caseId) {
       $errors['link_to_case'] = ts('Please select some other case to link.');
     }
 
     // do check for existing related cases.
     $relatedCases = $form->get('relatedCases');
     if (is_array($relatedCases) && array_key_exists($linkCaseId, $relatedCases)) {
-      $errors['link_to_case'] = ts('It looks like selected case is already linked.');
+      $errors['link_to_case'] = ts('Selected case is already linked.');
     }
 
     return empty($errors) ? TRUE : $errors;
index c63128c813115ac80552d86a9243f2f58c396b3a..78da81444b1d748e1cb6031b17023ebe8d26fdb7 100644 (file)
@@ -62,7 +62,7 @@ class CRM_Case_Form_ActivityToCase extends CRM_Core_Form {
    *
    * @access public
    *
-   * @return void
+   * @return array
    */
   function setDefaultValues() {
     $targetContactValues = $defaults = array();
@@ -91,8 +91,7 @@ class CRM_Case_Form_ActivityToCase extends CRM_Core_Form {
     $tokenUrl = CRM_Utils_System::url("civicrm/ajax/checkemail", "noemail=1", FALSE, NULL, FALSE);
     $this->assign('tokenUrl', $tokenUrl);
 
-    $this->add('text', 'unclosed_cases', ts('Select Case'));
-    $this->add('hidden', 'unclosed_case_id', '', array('id' => 'open_case_id'));
+    $this->add('text', 'unclosed_case_id', ts('Select Case'), array('class' => 'huge'), TRUE);
     $this->add('text', 'target_contact_id', ts('With Contact(s)'));
     $this->add('text', 'case_activity_subject', ts('Subject'), array('size' => 50));
   }
index 30ae8a77a5ee252676baea37e2c407029748b14a..7e7256cc744c4502e1fa5c8d11d7bb5c9d870f82 100644 (file)
@@ -41,30 +41,26 @@ class CRM_Case_Page_AJAX {
    * Retrieve unclosed cases.
    */
   static function unclosedCases() {
-    $criteria = explode('-', CRM_Utils_Type::escape(CRM_Utils_Array::value('s', $_GET), 'String'));
-
-    $limit = CRM_Utils_Array::value('limit', $_GET);
-    if ($limit) {
-      $limit = CRM_Utils_Type::escape($limit, 'Integer');
-    }
-
     $params = array(
-      'limit' => $limit,
-      'case_type' => trim(CRM_Utils_Array::value(1, $criteria)),
-      'sort_name' => trim(CRM_Utils_Array::value(0, $criteria)),
+      'limit' => CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'search_autocomplete_count', NULL, 10),
+      'sort_name' => CRM_Utils_Type::escape(CRM_Utils_Array::value('term', $_GET, ''), 'String'),
     );
 
     $excludeCaseIds = array();
-    if ($caseIdStr = CRM_Utils_Array::value('excludeCaseIds', $_GET)) {
-      $excludeIdStr = CRM_Utils_Type::escape($caseIdStr, 'String');
-      $excludeCaseIds = explode(',', $excludeIdStr);
+    if (!empty($_GET['excludeCaseIds'])) {
+      $excludeCaseIds = explode(',', CRM_Utils_Type::escape($_GET['excludeCaseIds'], 'String'));
     }
     $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases($params, $excludeCaseIds);
     $results = array();
     foreach ($unclosedCases as $caseId => $details) {
-      $results["$caseId|" . $details['contact_id'] . '|' . $details['case_type'] . '|' . $details['sort_name']] = $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ')';
+      $results[] = array(
+        'id' => $caseId,
+        'text' => $details['sort_name'] . ' (' . $details['case_type'] . ': ' . $details['case_subject'] . ')',
+        'extra' => $details,
+      );
     }
-    CRM_Core_Page_AJAX::autocompleteResults($results);
+    print json_encode($results);
+    CRM_Utils_System::civiExit();
   }
 
   function processCaseTags() {
index 608808310621b921e42261126cf411934bef9b48..b0e6e6b9a4d33f4f98d32115c82d5c54c6602f23 100644 (file)
 <div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="top"}</div>
    <table class="form-layout-compressed">
       <tr class="crm-file-on-case-form-block-unclosed_cases">
-         <td class="label">{$form.unclosed_cases.label}
-           <span class="crm-marker" title="{ts}This field is required.{/ts}">*</span></td>
-         <td>{$form.unclosed_cases.html}<br />
-           <span class="description">{ts}Begin typing client name for a list of open cases.{/ts}</span>
+         <td class="label">
+           {$form.unclosed_case_id.label}
+         </td>
+         <td>
+           {$form.unclosed_case_id.html}<br />
          </td>
       </tr>
      <tr>
 </div>
 {literal}
 <script type="text/javascript">
-
-var unclosedCaseUrl = {/literal}"{crmURL p='civicrm/case/ajax/unclosed' h=0}"{literal};
-cj( "#unclosed_cases" ).autocomplete( unclosedCaseUrl, { width : 250, selectFirst : false, matchContains:true
-                                    }).result( function(event, data, formatted) {
-                                      cj( "#unclosed_case_id" ).val( data[1] );
-                                    }).bind( 'click', function( ) {
-                                      cj( "#unclosed_case_id" ).val('');
-                                    });
+cj(function($) {
+  var $form = $("#{/literal}{$form.formName}{literal}");
+  $('input[name=unclosed_case_id]', $form).select2({
+    placeholder: {/literal}'{ts escape="js"}- select case -{/ts}'{literal},
+    minimumInputLength: 1,
+    ajax: {
+      url: {/literal}"{crmURL p='civicrm/case/ajax/unclosed' h=0}"{literal},
+      data: function(term) {
+        return {term: term};
+      },
+      results: function(response) {
+        return {results: response};
+      }
+    }
+  });
+});
 {/literal}
 </script>
index 7606399bfc111cbc24d991d7be14c1c674b7980a..b5dd7c4f75d1a01052429edb0d32726d77d29a52 100644 (file)
  | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
  +--------------------------------------------------------------------+
 *}
-{* Template for to create a link between two cases. *}
+{* Template for to create a link between two cases. *}{debug}
    <div class="crm-block crm-form-block crm-case-linkcases-form-block">
-    <tr class="crm-case-linkcases-form-block-link_to_case">
-  <td class="label">{$form.link_to_case.label}</td>
-  <td>{$form.link_to_case.html}</td>
+    <tr class="crm-case-linkcases-form-block-link_to_case_id">
+      <td class="label">{$form.link_to_case_id.label}</td>
+      <td>{$form.link_to_case_id.html}</td>
     </tr>
 
 {literal}
 <script type="text/javascript">
-var unclosedCaseUrl = {/literal}"{crmURL p='civicrm/case/ajax/unclosed' h=0 q='excludeCaseIds='}{$excludeCaseIds}{literal}";
-cj( "#link_to_case").autocomplete( unclosedCaseUrl, { width : 250, selectFirst : false, matchContains:true
-                            }).result( function(event, data, formatted) {
-                   cj( "#link_to_case_id" ).val( data[1] );
-             var subject = {/literal}"Create link between {$client_name} - {$caseTypeLabel} (CaseID: {$caseId})"{literal} + ' AND ' + data[4] + ' - ' + data[3] + ' (CaseID: ' + data[1] + ')';
-             cj( "#subject" ).val( subject );
-                            }).bind( 'click', function( ) {
-                   cj( "#link_to_case_id" ).val('');
-             cj( "#subject" ).val( '' );
-          });
+  cj(function($) {
+    var $form = $("#{/literal}{$form.formName}{literal}");
+    $('input[name=link_to_case_id]', $form).select2({
+      placeholder: {/literal}'{ts escape="js"}- select case -{/ts}'{literal},
+      minimumInputLength: 1,
+      ajax: {
+        url: {/literal}"{crmURL p='civicrm/case/ajax/unclosed' h=0}"{literal},
+        data: function(term) {
+          return {term: term, excludeCaseIds: "{/literal}{$excludeCaseIds}{literal}"};
+        },
+        results: function(response) {
+          return {results: response};
+        }
+      }
+    }).change(function() {
+      if ($(this).val()) {
+        var info = $(this).select2('data').extra;
+        {/literal}{* Mix in variables and placeholders for clientside substitution *}
+        var subject = "{ts escape=js 1="%1" 2="%2" 3="%3" 4=$client.sort_name 5=$caseTypeLabel 6=$caseId}Create link between %1 - %2 (CaseID: %3) and %4 - %5 (CaseID: %6){/ts}";
+        {literal}
+        $('#subject', $form).val(ts(subject, {1: info.sort_name, 2: info.case_type, 3: $(this).val()}));
+      }
+    });
+  });
 </script>
 {/literal}
   </div>
index 830d8389c5bce3ab5031e30748fda198cd83327d..44b32404ce810d5cc1dcfcb98d500808d2776b6d 100644 (file)
 {if !empty($buildCaseActivityForm)}
   <div class="crm-block crm-form-block crm-case-activitytocase-form-block">
     <table class="form-layout">
-      <tr class="crm-case-activitytocase-form-block-unclosed_cases">
-        <td class="label">{$form.unclosed_cases.label}</td>
-        <td>{$form.unclosed_cases.html}<br />
-          <span class="description">{ts}Begin typing client name for a list of open cases.{/ts}</span>
-        </td>
+      <tr class="crm-case-activitytocase-form-block-unclosed_case_id">
+        <td class="label">{$form.unclosed_case_id.label}</td>
+        <td>{$form.unclosed_case_id.html}</td>
       </tr>
       <tr class="crm-case-activitytocase-form-block-target_contact_id">
         <td class="label">{$form.target_contact_id.label}</td>
     var target_contact_id = '';
     var selectedCaseId = '';
     var contactId = '';
-
-    var unclosedCaseUrl = {/literal}"{crmURL p='civicrm/case/ajax/unclosed' h=0 q='excludeCaseIds='}{$currentCaseId}"{literal};
-    cj( "#unclosed_cases" ).autocomplete( unclosedCaseUrl, { width : 250, selectFirst : false, matchContains:true
-    }).result( function(event, data, formatted) {
-      cj( "#unclosed_case_id" ).val( data[1] );
-      contactId = data[2];
-      selectedCaseId = data[1];
-    }).bind( 'click', function( ) {
-      cj( "#unclosed_case_id" ).val('');
-      contactId = selectedCaseId = '';
+    cj(function($) {
+      $('input[name=unclosed_case_id]', '#fileOnCaseDialog').select2({
+        placeholder: {/literal}'{ts escape="js"}- select case -{/ts}'{literal},
+        minimumInputLength: 1,
+        ajax: {
+          url: {/literal}"{crmURL p='civicrm/case/ajax/unclosed' h=0}"{literal},
+          data: function(term) {
+            return {term: term, excludeCaseIds: "{/literal}{$currentCaseId}{literal}"};
+          },
+          results: function(response) {
+            return {results: response};
+          }
+        }
+      }).change(function() {
+        if ($(this).val()) {
+          selectedCaseId = $(this).val();
+          contactId = $(this).select2('data').extra.contact_id;
+        }
+      });
     });
     {/literal}
     {if $targetContactValues}