* 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'));
}
);
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);
}
}
$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);
}
}
*/
/**
- * This class generates form components for OpenCase Activity.
+ * This class generates form components for LinkCase Activity.
*/
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) {
$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.
$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));
}
/**
* @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);
}
/**
// 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;
}
* 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));
}
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()) {
*/
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
*/
<page_callback>CRM_Case_XMLProcessor_Report::printCaseReport</page_callback>
<access_arguments>access all cases and activities</access_arguments>
</item>
- <item>
- <path>civicrm/case/ajax/unclosed</path>
- <page_callback>CRM_Case_Page_AJAX::unclosedCases</page_callback>
- </item>
<item>
<path>civicrm/case/ajax/addclient</path>
<page_callback>CRM_Case_Page_AJAX::addClient</page_callback>
$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;
</table>
<div class="crm-submit-buttons">{include file="CRM/common/formButtons.tpl" location="bottom"}</div>
</div>
-{literal}
-<script type="text/javascript">
-CRM.$(function($) {
- var $form = $("form.{/literal}{$form.formClass}{literal}");
- $('input[name=unclosed_case_id]', $form).crmSelect2({
- placeholder: {/literal}'{ts escape="js"}- select case -{/ts}'{literal},
- minimumInputLength: 1,
- formatResult: CRM.utils.formatSelect2Result,
- formatSelection: function(row) {
- return row.label;
- },
- 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>
<script type="text/javascript">
CRM.$(function($) {
var $form = $("form.{/literal}{$form.formClass}{literal}");
- $('input[name=link_to_case_id]', $form).crmSelect2({
- placeholder: {/literal}'{ts escape="js"}- select case -{/ts}'{literal},
- minimumInputLength: 1,
- formatResult: CRM.utils.formatSelect2Result,
- formatSelection: function(row) {
- return row.label;
- },
- 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() {
+ $('input[name=link_to_case_id]', $form).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}";
+ var subject = "{ts escape=js 1="%1" 2="%2" 3="%3" 4=$sortName 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()}));
+ $('#subject', $form).val(ts(subject, {1: info['contact_id.sort_name'], 2: info['case_id.case_type_id.title'], 3: $(this).val()}));
}
});
});
</tr>
</table>
</div>
-{literal}
- <script type="text/javascript">
- CRM.$(function($) {
- 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,
- formatSelection: function(row) {
- return row.label;
- },
- initSelection: function($el, callback) {
- callback($el.data('value'));
- },
- 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};
- }
- }
- });
- });
- </script>
-{/literal}
{* main form end *}
{else}
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,
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 ) {
$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']);