From e209b543f73379a7ad7e0b6319365358cab3f5e1 Mon Sep 17 00:00:00 2001 From: varshith89 Date: Tue, 10 Apr 2018 05:48:52 +0000 Subject: [PATCH 1/2] CRM-21829: Show Related Cases For Organizations --- CRM/Civicase/APIHelpers/CaseList.php | 3 + CRM/Civicase/Page/ContactCaseTab.php | 4 + ang/civicase/CaseList.js | 2 +- civicase.php | 97 ++++++++++++++++++- .../CRM/Civicase/Page/ContactCaseTab.tpl | 30 +++++- templates/RelatedCasesField.tpl | 11 +++ 6 files changed, 141 insertions(+), 6 deletions(-) create mode 100644 templates/RelatedCasesField.tpl diff --git a/CRM/Civicase/APIHelpers/CaseList.php b/CRM/Civicase/APIHelpers/CaseList.php index c213e4d82..748afded7 100644 --- a/CRM/Civicase/APIHelpers/CaseList.php +++ b/CRM/Civicase/APIHelpers/CaseList.php @@ -84,6 +84,9 @@ public function getCaseList($params) { 'tag_id.name', 'tag_id.color', 'tag_id.description', ); $params['return'] = (isset($params['return']) ? array_merge($defaultAPIReturnedColumns, $params['return']) : $defaultAPIReturnedColumns); + if(isset($params['related_cids'])) { + $params['contact_id'] = $params['related_cids']; + } $cases = civicrm_api3('Case', 'getdetails', $params); foreach ($cases['values'] as &$case) { diff --git a/CRM/Civicase/Page/ContactCaseTab.php b/CRM/Civicase/Page/ContactCaseTab.php index 20956cb03..38515a591 100644 --- a/CRM/Civicase/Page/ContactCaseTab.php +++ b/CRM/Civicase/Page/ContactCaseTab.php @@ -10,6 +10,10 @@ class CRM_Civicase_Page_ContactCaseTab extends CRM_Core_Page { public function run() { $cid = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); $this->assign('cid', $cid); + // For Related cases tab (optional) + $related_cids = CRM_Utils_Request::retrieve('related_cids', 'String'); + if($related_cids) + $this->assign('related_cids', $related_cids); parent::run(); } diff --git a/ang/civicase/CaseList.js b/ang/civicase/CaseList.js index e3e650572..13d65a40d 100644 --- a/ang/civicase/CaseList.js +++ b/ang/civicase/CaseList.js @@ -15,7 +15,7 @@ function loadCaseApiParams(filters, sort, page) { var returnParams = { sequential: 1, - return: ['subject', 'case_type_id', 'status_id', 'is_deleted', 'start_date', 'modified_date', 'contacts', 'activity_summary', 'category_count', 'tag_id.name', 'tag_id.color', 'tag_id.description'], + return: ['subject', 'case_type_id', 'status_id', 'is_deleted', 'start_date', 'modified_date', 'contacts', 'activity_summary', 'category_count', 'tag_id.name', 'tag_id.color', 'tag_id.description', 'related_cids'], options: { sort: sort.field + ' ' + sort.dir, limit: page.size, diff --git a/civicase.php b/civicase.php index 227bd773c..69e87adaa 100644 --- a/civicase.php +++ b/civicase.php @@ -9,7 +9,6 @@ */ function civicase_civicrm_tabset($tabsetName, &$tabs, $context) { $useAng = FALSE; - switch ($tabsetName) { case 'civicrm/contact/view': $caseTabPresent = FALSE; @@ -44,6 +43,22 @@ function civicase_civicrm_tabset($tabsetName, &$tabs, $context) { ); } + if(CRM_Core_Permission::check('basic case information') && + getContactType($context['contact_id']) == 'Organization' && + Civi::settings()->get('civicaseRelatedCasesTab', 0)) { + $caseTabKey = array_search('case', array_column($tabs, 'id')); + $tabs[] = array( + 'id' => 'related_case', + 'url' => CRM_Utils_System::url('civicrm/case/contact-case-tab', array( + 'cid' => $context['contact_id'], + 'related_cids' => implode(',', getOrganizationRelatedCaseContactIds($context['contact_id'])), + )), + 'title' => ts('Related Cases'), + 'weight' => $tabs[$caseTabKey]['weight']+1, + 'count' => getOrganizationRelatedCasesCount($context['contact_id']), + 'class' => 'livePage', + ); + } break; } @@ -195,7 +210,6 @@ function civicase_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) { _civicase_civix_civicrm_alterSettingsFolders($metaDataFolders); } - /** * Implements hook_civicrm_buildForm(). * @@ -340,6 +354,14 @@ function civicase_civicrm_buildForm($formName, &$form) { } } } + if($formName == 'CRM_Admin_Form_Setting_Case') { + $form->addYesNo('_qf_civicaseRelatedCasesTab', ts('Display cases from related individuals on organisation contacts')); + $form->setDefaults(array('_qf_civicaseRelatedCasesTab' => Civi::settings()->get('civicaseRelatedCasesTab', 0))); + $templatePath = realpath(dirname(__FILE__)."/templates"); + CRM_Core_Region::instance('page-body')->add(array( + 'template' => "{$templatePath}/RelatedCasesField.tpl" + )); + } } /** @@ -415,6 +437,12 @@ function civicase_civicrm_postProcess($formName, &$form) { civicrm_api3('Activity', 'delete', array('id' => $urlParams['draft_id'])); } } + if($formName = 'CRM_Admin_Form_Setting_Case' && isset($form->_submitValues['_qf_civicaseRelatedCasesTab'])) { + if($form->_submitValues['_qf_civicaseRelatedCasesTab'] == 1) + Civi::settings()->set('civicaseRelatedCasesTab', 1); + else + Civi::settings()->set('civicaseRelatedCasesTab', 0); + } } /** @@ -556,3 +584,68 @@ function civicase_civicrm_selectWhereClause($entity, &$clauses) { unset($clauses['id']); } } + +/** + * Retrieve contact ids of all contacts(with cases) related to given Organization contact. + * + * @param int $organizationId + * + * @return array + */ +function getOrganizationRelatedCaseContactIds($organizationId, $countOnly=FALSE) { + if(!$organizationId) { + return array(); + } + $sql = 'SELECT'; + if($countOnly) { + $sql .= ' COUNT(cc.case_id) '; + } + else { + $sql .= ' rel.contact_id_a as id '; + } + $sql .= "FROM civicrm_case_contact AS cc + INNER JOIN civicrm_relationship AS rel ON rel.contact_id_a = cc.contact_id + INNER JOIN civicrm_relationship_type AS rtype ON rel.relationship_type_id = rtype.id + WHERE 'Organization' IN (rtype.contact_type_a, rtype.contact_type_b) + AND %1 = CASE + WHEN (rtype.contact_type_a = 'Organization') THEN rel.contact_id_a + ELSE rel.contact_id_b + END"; + $all = CRM_Core_DAO::executeQuery($sql, array( + 1 => array($organizationId, 'Integer'), + )); + if($countOnly) { + return $all->fetchValue(); + } + $all = $all->fetchAll(); + $ids = array(); + foreach($all as $each) { + if(!in_array($each['id'], $ids)) + $ids[] = $each['id']; + } + return $ids; +} + +/** + * Retrieve count of all cases of all contacts that are related to given Organization contact. + * + * @param int $organizationId + * @return int + */ +function getOrganizationRelatedCasesCount($organizationId) { + return getOrganizationRelatedCaseContactIds($organizationId, TRUE); +} + +/** + * Retrieve contact type for given contact id + * + * @param int $contact_id + * @return string + */ +function getContactType($contact_id) { + $sql = "SELECT contact_type FROM civicrm_contact + WHERE id = %1"; + return CRM_Core_DAO::executeQuery($sql, array( + 1 => array($contact_id, 'Integer'), + ))->fetchValue(); +} \ No newline at end of file diff --git a/templates/CRM/Civicase/Page/ContactCaseTab.tpl b/templates/CRM/Civicase/Page/ContactCaseTab.tpl index 46c8ae514..1ab98dbd3 100644 --- a/templates/CRM/Civicase/Page/ContactCaseTab.tpl +++ b/templates/CRM/Civicase/Page/ContactCaseTab.tpl @@ -1,4 +1,8 @@ +{if $related_cids} +
+{else}
+{/if}
{literal} @@ -10,9 +14,18 @@ reloadOnSearch: false, resolve: { hiddenFilters: function() { - return { - "contact_id": [{/literal}{$cid|json}{literal}] + var ret = { + "contact_id": [{/literal}{$cid|json}{literal}] }; + {/literal} + {if $related_cids} + {literal} + var rcids = [{/literal}{$related_cids|json}{literal}]; + ret["related_cids"] = rcids.toString().split(','); + {/literal} + {/if} + {literal} + return ret; } }, controller: 'CivicaseCaseList', @@ -22,7 +35,18 @@ })(angular, CRM.$, CRM._); CRM.$(document).one('crmLoad', function(){ - angular.bootstrap(document.getElementById('civicaseContactTab'), ['civicaseContactTab']); + {/literal} + {if $related_cids} + {literal} + var caseTab = document.getElementById('civicaseRelatedContactTab'); + {/literal} + {else} + {literal} + var caseTab = document.getElementById('civicaseContactTab'); + {/literal} + {/if} + {literal} + angular.bootstrap(caseTab, ['civicaseContactTab']); }); {/literal} \ No newline at end of file diff --git a/templates/RelatedCasesField.tpl b/templates/RelatedCasesField.tpl new file mode 100644 index 000000000..fc8e1f1c8 --- /dev/null +++ b/templates/RelatedCasesField.tpl @@ -0,0 +1,11 @@ + + + + + +
{$form._qf_civicaseRelatedCasesTab.label}{$form._qf_civicaseRelatedCasesTab.html}
+ {ts}With this checkbox enabled cases from individuals who are directly related to the organisation will be displayed on the organisation records on a tab called "Related Cases".{/ts} +
+ \ No newline at end of file From b08c61200559c23914ce9d8aa1843dcc5420d7a0 Mon Sep 17 00:00:00 2001 From: varshith89 Date: Thu, 19 Apr 2018 11:54:13 +0000 Subject: [PATCH 2/2] CRM-21829: Code Improvements --- CRM/Civicase/Page/ContactCaseTab.php | 7 ++- CRM/Civicase/RelatedCases.php | 70 +++++++++++++++++++++++++++ civicase.php | 72 ++-------------------------- templates/RelatedCasesField.tpl | 3 +- 4 files changed, 81 insertions(+), 71 deletions(-) create mode 100644 CRM/Civicase/RelatedCases.php diff --git a/CRM/Civicase/Page/ContactCaseTab.php b/CRM/Civicase/Page/ContactCaseTab.php index 38515a591..db88ac9e5 100644 --- a/CRM/Civicase/Page/ContactCaseTab.php +++ b/CRM/Civicase/Page/ContactCaseTab.php @@ -11,9 +11,12 @@ public function run() { $cid = CRM_Utils_Request::retrieve('cid', 'Positive', CRM_Core_DAO::$_nullObject, TRUE); $this->assign('cid', $cid); // For Related cases tab (optional) - $related_cids = CRM_Utils_Request::retrieve('related_cids', 'String'); - if($related_cids) + $relatedCases = CRM_Utils_Request::retrieve('relatedCases', 'Boolean'); + if($relatedCases) { + $rcases = new CRM_Civicase_RelatedCases(); + $related_cids = implode(',', $rcases->getOrganizationRelatedCaseContactIds($cid)); $this->assign('related_cids', $related_cids); + } parent::run(); } diff --git a/CRM/Civicase/RelatedCases.php b/CRM/Civicase/RelatedCases.php new file mode 100644 index 000000000..00fae2278 --- /dev/null +++ b/CRM/Civicase/RelatedCases.php @@ -0,0 +1,70 @@ + array($organizationId, 'Integer'), + )); + if($countOnly) { + return $all->fetchValue(); + } + $all = $all->fetchAll(); + $ids = array(); + foreach($all as $each) { + if(!in_array($each['id'], $ids)) + $ids[] = $each['id']; + } + return $ids; + } + + /** + * Retrieve count of all cases of all contacts that are related to given Organization contact. + * + * @param int $organizationId + * @return int + */ + public function getOrganizationRelatedCasesCount($organizationId) { + return $this->getOrganizationRelatedCaseContactIds($organizationId, TRUE); + } + + /** + * Retrieve contact type for given contact id + * + * @param int $contact_id + * @return string + */ + public function getContactType($contact_id) { + $sql = "SELECT contact_type FROM civicrm_contact + WHERE id = %1"; + return CRM_Core_DAO::executeQuery($sql, array( + 1 => array($contact_id, 'Integer'), + ))->fetchValue(); + } +} + diff --git a/civicase.php b/civicase.php index 69e87adaa..fcd2269a5 100644 --- a/civicase.php +++ b/civicase.php @@ -43,19 +43,20 @@ function civicase_civicrm_tabset($tabsetName, &$tabs, $context) { ); } + $rcases = new CRM_Civicase_RelatedCases(); if(CRM_Core_Permission::check('basic case information') && - getContactType($context['contact_id']) == 'Organization' && + $rcases->getContactType($context['contact_id']) == 'Organization' && Civi::settings()->get('civicaseRelatedCasesTab', 0)) { $caseTabKey = array_search('case', array_column($tabs, 'id')); $tabs[] = array( 'id' => 'related_case', 'url' => CRM_Utils_System::url('civicrm/case/contact-case-tab', array( 'cid' => $context['contact_id'], - 'related_cids' => implode(',', getOrganizationRelatedCaseContactIds($context['contact_id'])), + 'relatedCases' => TRUE, )), 'title' => ts('Related Cases'), 'weight' => $tabs[$caseTabKey]['weight']+1, - 'count' => getOrganizationRelatedCasesCount($context['contact_id']), + 'count' => $rcases->getOrganizationRelatedCasesCount($context['contact_id']), 'class' => 'livePage', ); } @@ -584,68 +585,3 @@ function civicase_civicrm_selectWhereClause($entity, &$clauses) { unset($clauses['id']); } } - -/** - * Retrieve contact ids of all contacts(with cases) related to given Organization contact. - * - * @param int $organizationId - * - * @return array - */ -function getOrganizationRelatedCaseContactIds($organizationId, $countOnly=FALSE) { - if(!$organizationId) { - return array(); - } - $sql = 'SELECT'; - if($countOnly) { - $sql .= ' COUNT(cc.case_id) '; - } - else { - $sql .= ' rel.contact_id_a as id '; - } - $sql .= "FROM civicrm_case_contact AS cc - INNER JOIN civicrm_relationship AS rel ON rel.contact_id_a = cc.contact_id - INNER JOIN civicrm_relationship_type AS rtype ON rel.relationship_type_id = rtype.id - WHERE 'Organization' IN (rtype.contact_type_a, rtype.contact_type_b) - AND %1 = CASE - WHEN (rtype.contact_type_a = 'Organization') THEN rel.contact_id_a - ELSE rel.contact_id_b - END"; - $all = CRM_Core_DAO::executeQuery($sql, array( - 1 => array($organizationId, 'Integer'), - )); - if($countOnly) { - return $all->fetchValue(); - } - $all = $all->fetchAll(); - $ids = array(); - foreach($all as $each) { - if(!in_array($each['id'], $ids)) - $ids[] = $each['id']; - } - return $ids; -} - -/** - * Retrieve count of all cases of all contacts that are related to given Organization contact. - * - * @param int $organizationId - * @return int - */ -function getOrganizationRelatedCasesCount($organizationId) { - return getOrganizationRelatedCaseContactIds($organizationId, TRUE); -} - -/** - * Retrieve contact type for given contact id - * - * @param int $contact_id - * @return string - */ -function getContactType($contact_id) { - $sql = "SELECT contact_type FROM civicrm_contact - WHERE id = %1"; - return CRM_Core_DAO::executeQuery($sql, array( - 1 => array($contact_id, 'Integer'), - ))->fetchValue(); -} \ No newline at end of file diff --git a/templates/RelatedCasesField.tpl b/templates/RelatedCasesField.tpl index fc8e1f1c8..6f9107fe7 100644 --- a/templates/RelatedCasesField.tpl +++ b/templates/RelatedCasesField.tpl @@ -8,4 +8,5 @@ \ No newline at end of file + +