From abd06efc426540ed16cb091093f90e13df2579f1 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Wed, 13 Jan 2016 21:22:23 -0500 Subject: [PATCH] CRM-17645 - Convert case autocompletes to use EntityRef --- CRM/Activity/Form/Task/FileOnCase.php | 2 +- CRM/Case/BAO/Case.php | 14 ++++--- CRM/Case/BAO/CaseContact.php | 4 +- CRM/Case/Form/Activity/LinkCases.php | 26 +++++++++---- CRM/Case/Form/ActivityToCase.php | 33 ++++++++++++----- CRM/Case/Form/CaseView.php | 14 +++++-- CRM/Case/Page/AJAX.php | 27 -------------- CRM/Case/xml/Menu/Case.xml | 4 -- api/v3/Case.php | 18 ++++++--- .../CRM/Activity/Form/Task/FileOnCase.tpl | 24 ------------ .../CRM/Case/Form/Activity/LinkCases.tpl | 22 ++--------- templates/CRM/Case/Form/ActivityToCase.tpl | 37 +++---------------- tests/phpunit/CRM/Case/BAO/CaseTest.php | 8 ---- 13 files changed, 83 insertions(+), 150 deletions(-) diff --git a/CRM/Activity/Form/Task/FileOnCase.php b/CRM/Activity/Form/Task/FileOnCase.php index 8667562b24..0154767630 100644 --- a/CRM/Activity/Form/Task/FileOnCase.php +++ b/CRM/Activity/Form/Task/FileOnCase.php @@ -68,7 +68,7 @@ class CRM_Activity_Form_Task_FileOnCase extends CRM_Activity_Form_Task { * Build the form object. */ public function buildQuickForm() { - $this->add('text', 'unclosed_case_id', ts('Select Case'), array('class' => 'huge'), TRUE); + $this->addEntityRef('unclosed_case_id', ts('Select Case'), array('entity' => 'Case'), TRUE); $this->addDefaultButtons(ts('Save')); } diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index 97fd1764ee..a4521dbcde 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -2790,15 +2790,19 @@ WHERE id IN (' . implode(',', $copiedActivityIds) . ')'; ); if (in_array($operation, $caseActOperations)) { - static $unclosedCases; - if (!is_array($unclosedCases)) { - $unclosedCases = self::getUnclosedCases(); + static $caseCount; + if (!isset($caseCount)) { + $caseCount = civicrm_api3('Case', 'getcount', array( + 'status_id' => array('!=' => 'Closed'), + 'is_deleted' => 0, + 'end_date' => array('IS NULL' => 1), + )); } if ($operation == 'File On Case') { - $allow = (empty($unclosedCases)) ? FALSE : TRUE; + $allow = !empty($caseCount); } else { - $allow = (count($unclosedCases) > 1) ? TRUE : FALSE; + $allow = ($caseCount > 1); } } diff --git a/CRM/Case/BAO/CaseContact.php b/CRM/Case/BAO/CaseContact.php index 4b4bd06c58..3329017ff6 100644 --- a/CRM/Case/BAO/CaseContact.php +++ b/CRM/Case/BAO/CaseContact.php @@ -91,10 +91,10 @@ class CRM_Case_BAO_CaseContact extends CRM_Case_DAO_CaseContact { $caseSubclauses = array(); $caseBao = new CRM_Case_BAO_Case(); foreach ($caseBao->apiWhereClause() as $field => $fieldClauses) { - if ($field == 'id') { + if ($field == 'id' && $fieldClauses) { $clauses['case_id'] = array_merge($clauses['case_id'], (array) $fieldClauses); } - else { + elseif ($fieldClauses) { $caseSubclauses[] = "$field " . implode(" AND $field ", (array) $fieldClauses); } } diff --git a/CRM/Case/Form/Activity/LinkCases.php b/CRM/Case/Form/Activity/LinkCases.php index 25880a8f15..805f3e64ae 100644 --- a/CRM/Case/Form/Activity/LinkCases.php +++ b/CRM/Case/Form/Activity/LinkCases.php @@ -32,7 +32,7 @@ */ /** - * This class generates form components for OpenCase Activity. + * This class generates form components for LinkCase Activity. */ class CRM_Case_Form_Activity_LinkCases { /** @@ -41,7 +41,7 @@ class CRM_Case_Form_Activity_LinkCases { * @throws Exception */ public static function preProcess(&$form) { - if (!isset($form->_caseId)) { + if (empty($form->_caseId)) { CRM_Core_Error::fatal(ts('Case Id not found.')); } if (count($form->_caseId) != 1) { @@ -51,6 +51,7 @@ class CRM_Case_Form_Activity_LinkCases { $caseId = CRM_Utils_Array::first($form->_caseId); $form->assign('clientID', $form->_currentlyViewedContactId); + $form->assign('sortName', CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $form->_currentlyViewedContactId, 'sort_name')); $form->assign('caseTypeLabel', CRM_Case_BAO_Case::getCaseType($caseId)); // get the related cases for given case. @@ -59,11 +60,6 @@ class CRM_Case_Form_Activity_LinkCases { $relatedCases = CRM_Case_BAO_Case::getRelatedCases($caseId, $form->_currentlyViewedContactId); $form->set('relatedCases', empty($relatedCases) ? FALSE : $relatedCases); } - $excludeCaseIds = array($caseId); - if (is_array($relatedCases) && !empty($relatedCases)) { - $excludeCaseIds = array_merge($excludeCaseIds, array_keys($relatedCases)); - } - $form->assign('excludeCaseIds', implode(',', $excludeCaseIds)); } /** @@ -81,7 +77,21 @@ class CRM_Case_Form_Activity_LinkCases { * @param CRM_Core_Form $form */ public static function buildQuickForm(&$form) { - $form->add('text', 'link_to_case_id', ts('Link To Case'), array('class' => 'huge'), TRUE); + $excludeCaseIds = (array) $form->_caseId; + $relatedCases = $form->get('relatedCases'); + if (is_array($relatedCases) && !empty($relatedCases)) { + $excludeCaseIds = array_merge($excludeCaseIds, array_keys($relatedCases)); + } + $form->addEntityRef('link_to_case_id', ts('Link To Case'), array( + 'entity' => 'Case', + 'api' => array( + 'extra' => array('case_id.case_type_id.title', 'contact_id.sort_name'), + 'params' => array( + 'case_id' => array('NOT IN' => $excludeCaseIds), + 'case_id.is_deleted' => 0, + ), + ), + ), TRUE); } /** diff --git a/CRM/Case/Form/ActivityToCase.php b/CRM/Case/Form/ActivityToCase.php index 5ce068eff3..3a981f898a 100644 --- a/CRM/Case/Form/ActivityToCase.php +++ b/CRM/Case/Form/ActivityToCase.php @@ -68,18 +68,20 @@ class CRM_Case_Form_ActivityToCase extends CRM_Core_Form { // If this contact has an open case, supply it as a default $cid = CRM_Utils_Request::retrieve('cid', 'Integer'); if ($cid) { - $cases = CRM_Case_BAO_Case::getUnclosedCases(array('contact_id' => $cid), $this->_currentCaseId); - foreach ($cases as $id => $details) { - $defaults['file_on_case_unclosed_case_id'] = $id; - $value = array( - 'label' => $details['sort_name'] . ' - ' . $details['case_type'], - 'extra' => array('contact_id' => $cid), - ); - $this->updateElementAttr('file_on_case_unclosed_case_id', array('data-value' => json_encode($value))); + $cases = civicrm_api3('CaseContact', 'get', array( + 'contact_id' => $cid, + 'case_id' => array('!=' => $this->_currentCaseId), + 'case_id.status_id' => array('!=' => "Closed"), + 'case_id.is_deleted' => 0, + 'case_id.end_date' => array('IS NULL' => 1), + 'options' => array('limit' => 1), + 'return' => 'case_id', + )); + foreach ($cases['values'] as $record) { + $defaults['file_on_case_unclosed_case_id'] = $record['case_id']; break; } } - return $defaults; } @@ -87,7 +89,18 @@ class CRM_Case_Form_ActivityToCase extends CRM_Core_Form { * Build the form object. */ public function buildQuickForm() { - $this->add('text', 'file_on_case_unclosed_case_id', ts('Select Case'), array('class' => 'huge'), TRUE); + $this->addEntityRef('file_on_case_unclosed_case_id', ts('Select Case'), array( + 'entity' => 'Case', + 'api' => array( + 'extra' => array('contact_id'), + 'params' => array( + 'case_id' => array('!=' => $this->_currentCaseId), + 'case_id.is_deleted' => 0, + 'case_id.status_id' => array('!=' => "Closed"), + 'case_id.end_date' => array('IS NULL' => 1), + ), + ), + ), TRUE); $this->addEntityRef('file_on_case_target_contact_id', ts('With Contact(s)'), array('multiple' => TRUE)); $this->add('text', 'file_on_case_activity_subject', ts('Subject'), array('size' => 50)); } diff --git a/CRM/Case/Form/CaseView.php b/CRM/Case/Form/CaseView.php index d9990326cd..62a38cf5db 100644 --- a/CRM/Case/Form/CaseView.php +++ b/CRM/Case/Form/CaseView.php @@ -240,10 +240,16 @@ class CRM_Case_Form_CaseView extends CRM_Core_Form { unset($aTypes[$openActTypeId]); } - //check for link cases. - $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases(NULL, array($this->_caseID)); - if (empty($unclosedCases) && ($linkActTypeId = array_search('Link Cases', $allActTypes))) { - unset($aTypes[$linkActTypeId]); + // Only show "link cases" activity if other cases exist. + $linkActTypeId = array_search('Link Cases', $allActTypes); + if ($linkActTypeId) { + $count = civicrm_api3('Case', 'getcount', array( + 'id' => array('!=' => $this->_caseID), + 'is_deleted' => 0, + )); + if (!$count) { + unset($aTypes[$linkActTypeId]); + } } if (!$xmlProcessor->getNaturalActivityTypeSort()) { diff --git a/CRM/Case/Page/AJAX.php b/CRM/Case/Page/AJAX.php index 874c2aa135..2e3a999ea2 100644 --- a/CRM/Case/Page/AJAX.php +++ b/CRM/Case/Page/AJAX.php @@ -37,33 +37,6 @@ */ class CRM_Case_Page_AJAX { - /** - * Retrieve unclosed cases. - */ - public static function unclosedCases() { - $params = array( - 'limit' => Civi::settings()->get('search_autocomplete_count'), - 'sort_name' => CRM_Utils_Type::escape(CRM_Utils_Array::value('term', $_GET, ''), 'String'), - ); - - $excludeCaseIds = array(); - if (!empty($_GET['excludeCaseIds'])) { - $excludeCaseIds = explode(',', CRM_Utils_Type::escape($_GET['excludeCaseIds'], 'String')); - } - $unclosedCases = CRM_Case_BAO_Case::getUnclosedCases($params, $excludeCaseIds, TRUE, TRUE); - $results = array(); - foreach ($unclosedCases as $caseId => $details) { - $results[] = array( - 'id' => $caseId, - 'label' => $details['sort_name'] . ' - ' . $details['case_type'] . ($details['end_date'] ? ' (' . ts('closed') . ')' : ''), - 'label_class' => $details['end_date'] ? 'strikethrough' : '', - 'description' => array("#$caseId: " . $details['case_subject'] . ' (' . $details['case_status'] . ')'), - 'extra' => $details, - ); - } - CRM_Utils_JSON::output($results); - } - /** * @throws \CRM_Core_Exception */ diff --git a/CRM/Case/xml/Menu/Case.xml b/CRM/Case/xml/Menu/Case.xml index 25ce7cf1c9..2d270503cc 100644 --- a/CRM/Case/xml/Menu/Case.xml +++ b/CRM/Case/xml/Menu/Case.xml @@ -110,10 +110,6 @@ CRM_Case_XMLProcessor_Report::printCaseReport access all cases and activities - - civicrm/case/ajax/unclosed - CRM_Case_Page_AJAX::unclosedCases - civicrm/case/ajax/addclient CRM_Case_Page_AJAX::addClient diff --git a/api/v3/Case.php b/api/v3/Case.php index 722c97c37e..076f001a73 100644 --- a/api/v3/Case.php +++ b/api/v3/Case.php @@ -250,13 +250,19 @@ function civicrm_api3_case_get($params) { $foundcases = _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Case', $sql); - // For historic reasons we return these by default only when fetching a case by id - if (!empty($params['id']) && empty($options['return'])) { - $options['return'] = array('contacts' => 1, 'activities' => 1, 'contact_id' => 1); - } + if (empty($options['is_count'])) { + // For historic reasons we return these by default only when fetching a case by id + if (!empty($params['id']) && empty($options['return'])) { + $options['return'] = array( + 'contacts' => 1, + 'activities' => 1, + 'contact_id' => 1, + ); + } - foreach ($foundcases['values'] as &$case) { - _civicrm_api3_case_read($case, $options); + foreach ($foundcases['values'] as &$case) { + _civicrm_api3_case_read($case, $options); + } } return $foundcases; diff --git a/templates/CRM/Activity/Form/Task/FileOnCase.tpl b/templates/CRM/Activity/Form/Task/FileOnCase.tpl index 6c5f5a094e..addca92712 100644 --- a/templates/CRM/Activity/Form/Task/FileOnCase.tpl +++ b/templates/CRM/Activity/Form/Task/FileOnCase.tpl @@ -43,27 +43,3 @@
{include file="CRM/common/formButtons.tpl" location="bottom"}
-{literal} - diff --git a/templates/CRM/Case/Form/Activity/LinkCases.tpl b/templates/CRM/Case/Form/Activity/LinkCases.tpl index 2db24f92f2..5b4b4fe5c4 100644 --- a/templates/CRM/Case/Form/Activity/LinkCases.tpl +++ b/templates/CRM/Case/Form/Activity/LinkCases.tpl @@ -34,29 +34,13 @@ -{/literal} {* main form end *} {else} @@ -90,6 +63,11 @@ dataUrl += '&activityId=' + activityID + '&caseId=' + currentCaseId + '&cid=' + {/literal}"{$contactID}"{literal}; function save() { + 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; + } + 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, @@ -97,11 +75,6 @@ subject = $("#file_on_case_activity_subject").val(), targetContactId = $("#file_on_case_target_contact_id").val(); - 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; - } - 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 ) { diff --git a/tests/phpunit/CRM/Case/BAO/CaseTest.php b/tests/phpunit/CRM/Case/BAO/CaseTest.php index 3a2addb01f..1912ea7cc7 100644 --- a/tests/phpunit/CRM/Case/BAO/CaseTest.php +++ b/tests/phpunit/CRM/Case/BAO/CaseTest.php @@ -70,14 +70,6 @@ class CRM_Case_BAO_CaseTest extends CiviUnitTestCase { $this->assertEquals(1, $cases['rows']['Housing Support']['Ongoing']['count']); } - public function testGetUnclosedCases() { - $params = array( - 'case_type' => 'ousing Suppor', - ); - $cases = CRM_Case_BAO_Case::getUnclosedCases($params); - $this->assertEquals('Housing Support', $cases[1]['case_type']); - } - public function testGetContactCases() { $cases = CRM_Case_BAO_Case::getContactCases(3); $this->assertEquals('Housing Support', $cases[1]['case_type']); -- 2.25.1