From b08c61200559c23914ce9d8aa1843dcc5420d7a0 Mon Sep 17 00:00:00 2001 From: varshith89 Date: Thu, 19 Apr 2018 11:54:13 +0000 Subject: [PATCH] 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 + +