From f72119d93badb6582a6abe556a9d59805d641985 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 26 Feb 2014 20:45:37 -0500 Subject: [PATCH] CRM-13966 - Migrate case autocompletes to select2 --- CRM/Activity/Form/Task/FileOnCase.php | 41 ++---------------- CRM/Case/Form/Activity/LinkCases.php | 10 ++--- CRM/Case/Form/ActivityToCase.php | 5 +-- CRM/Case/Page/AJAX.php | 26 +++++------- .../CRM/Activity/Form/Task/FileOnCase.tpl | 33 +++++++++------ .../CRM/Case/Form/Activity/LinkCases.tpl | 42 ++++++++++++------- templates/CRM/Case/Form/ActivityToCase.tpl | 37 +++++++++------- 7 files changed, 91 insertions(+), 103 deletions(-) diff --git a/CRM/Activity/Form/Task/FileOnCase.php b/CRM/Activity/Form/Task/FileOnCase.php index 8bd80d673c..4ba197bb76 100644 --- a/CRM/Activity/Form/Task/FileOnCase.php +++ b/CRM/Activity/Form/Task/FileOnCase.php @@ -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 diff --git a/CRM/Case/Form/Activity/LinkCases.php b/CRM/Case/Form/Activity/LinkCases.php index d976b33736..1f2cbcdbcd 100644 --- a/CRM/Case/Form/Activity/LinkCases.php +++ b/CRM/Case/Form/Activity/LinkCases.php @@ -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; diff --git a/CRM/Case/Form/ActivityToCase.php b/CRM/Case/Form/ActivityToCase.php index c63128c813..78da81444b 100644 --- a/CRM/Case/Form/ActivityToCase.php +++ b/CRM/Case/Form/ActivityToCase.php @@ -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)); } diff --git a/CRM/Case/Page/AJAX.php b/CRM/Case/Page/AJAX.php index 30ae8a77a5..7e7256cc74 100644 --- a/CRM/Case/Page/AJAX.php +++ b/CRM/Case/Page/AJAX.php @@ -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() { diff --git a/templates/CRM/Activity/Form/Task/FileOnCase.tpl b/templates/CRM/Activity/Form/Task/FileOnCase.tpl index 6088083106..b0e6e6b9a4 100644 --- a/templates/CRM/Activity/Form/Task/FileOnCase.tpl +++ b/templates/CRM/Activity/Form/Task/FileOnCase.tpl @@ -30,10 +30,11 @@
{include file="CRM/common/formButtons.tpl" location="top"}
- - + @@ -44,13 +45,21 @@ {literal} diff --git a/templates/CRM/Case/Form/Activity/LinkCases.tpl b/templates/CRM/Case/Form/Activity/LinkCases.tpl index 7606399bfc..b5dd7c4f75 100644 --- a/templates/CRM/Case/Form/Activity/LinkCases.tpl +++ b/templates/CRM/Case/Form/Activity/LinkCases.tpl @@ -23,25 +23,39 @@ | 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}
-
- - + + + {literal} {/literal} diff --git a/templates/CRM/Case/Form/ActivityToCase.tpl b/templates/CRM/Case/Form/ActivityToCase.tpl index 830d8389c5..44b32404ce 100644 --- a/templates/CRM/Case/Form/ActivityToCase.tpl +++ b/templates/CRM/Case/Form/ActivityToCase.tpl @@ -27,11 +27,9 @@ {if !empty($buildCaseActivityForm)}
{$form.unclosed_cases.label} - *{$form.unclosed_cases.html}
- {ts}Begin typing client name for a list of open cases.{/ts} +
+ {$form.unclosed_case_id.label} + + {$form.unclosed_case_id.html}
- - - + + + @@ -51,16 +49,25 @@ 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} -- 2.25.1
{$form.unclosed_cases.label}{$form.unclosed_cases.html}
- {ts}Begin typing client name for a list of open cases.{/ts} -
{$form.unclosed_case_id.label}{$form.unclosed_case_id.html}
{$form.target_contact_id.label}