From 9a13600cfec015749dc5c8e86efda82f2b91e891 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 15 Oct 2018 16:44:47 -0400 Subject: [PATCH] Simple function extraction --- CRM/Contact/Page/View/Summary.php | 133 ++++++++++++++++-------------- CRM/Core/Component.php | 6 +- 2 files changed, 72 insertions(+), 67 deletions(-) diff --git a/CRM/Contact/Page/View/Summary.php b/CRM/Contact/Page/View/Summary.php index d41da69d8f..c00fbe6574 100644 --- a/CRM/Contact/Page/View/Summary.php +++ b/CRM/Contact/Page/View/Summary.php @@ -264,23 +264,57 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { $lastModified = CRM_Core_BAO_Log::lastModified($this->_contactId, 'civicrm_contact'); $this->assign_by_ref('lastModified', $lastModified); - $allTabs = array(); - $weight = 10; - $this->_viewOptions = CRM_Core_BAO_Setting::valueOptions( CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_view_options', TRUE ); - // show the tabs only if user has generic access to CiviCRM - $accessCiviCRM = CRM_Core_Permission::check('access CiviCRM'); - $changeLog = $this->_viewOptions['log']; $this->assign_by_ref('changeLog', $changeLog); - $components = CRM_Core_Component::getEnabledComponents(); - foreach ($components as $name => $component) { + $this->assign('allTabs', $this->getTabs()); + + // hook for contact summary + // ignored but needed to prevent warnings + $contentPlacement = CRM_Utils_Hook::SUMMARY_BELOW; + CRM_Utils_Hook::summary($this->_contactId, $content, $contentPlacement); + if ($content) { + $this->assign_by_ref('hookContent', $content); + $this->assign('hookContentPlacement', $contentPlacement); + } + } + + /** + * @return string + */ + public function getTemplateFileName() { + if ($this->_contactId) { + $contactSubtypes = $this->get('contactSubtype') ? explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->get('contactSubtype')) : array(); + + // there could be multiple subtypes. We check templates for each of the subtype, and return the first one found. + foreach ($contactSubtypes as $csType) { + if ($csType) { + $templateFile = "CRM/Contact/Page/View/SubType/{$csType}.tpl"; + $template = CRM_Core_Page::getTemplate(); + if ($template->template_exists($templateFile)) { + return $templateFile; + } + } + } + } + return parent::getTemplateFileName(); + } + + /** + * @return array + * @throws \CRM_Core_Exception + */ + public function getTabs() { + $allTabs = []; + $weight = 10; + + foreach (CRM_Core_Component::getEnabledComponents() as $name => $component) { if (!empty($this->_viewOptions[$name]) && CRM_Core_Permission::access($component->name) ) { @@ -302,14 +336,14 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { if (CRM_Utils_Request::retrieve('isTest', 'Positive', $this)) { $q .= "&isTest=1"; } - $allTabs[] = array( + $allTabs[] = [ 'id' => $i, 'url' => CRM_Utils_System::url("civicrm/contact/view/$u", $q), 'title' => $elem['title'], 'weight' => $elem['weight'], 'count' => CRM_Contact_BAO_Contact::getCountComponent($u, $this->_contactId), 'class' => 'livePage', - ); + ]; // make sure to get maximum weight, rest of tabs go after // FIXME: not very elegant again if ($weight < $elem['weight']) { @@ -318,34 +352,36 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { } } - $rest = array( - 'activity' => array( + $rest = [ + 'activity' => [ 'title' => ts('Activities'), 'class' => 'livePage', - ), - 'rel' => array( + ], + 'rel' => [ 'title' => ts('Relationships'), 'class' => 'livePage', - ), - 'group' => array( + ], + 'group' => [ 'title' => ts('Groups'), 'class' => 'ajaxForm', - ), - 'note' => array( + ], + 'note' => [ 'title' => ts('Notes'), 'class' => 'livePage', - ), - 'tag' => array( + ], + 'tag' => [ 'title' => ts('Tags'), - ), - 'log' => array( + ], + 'log' => [ 'title' => ts('Change Log'), - ), - ); + ], + ]; + // show the tabs only if user has generic access to CiviCRM + $accessCiviCRM = CRM_Core_Permission::check('access CiviCRM'); foreach ($rest as $k => $v) { if ($accessCiviCRM && !empty($this->_viewOptions[$k])) { - $allTabs[] = $v + array( + $allTabs[] = $v + [ 'id' => $k, 'url' => CRM_Utils_System::url( "civicrm/contact/view/$k", @@ -353,7 +389,7 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { ), 'weight' => $weight, 'count' => CRM_Contact_BAO_Contact::getCountComponent($k, $this->_contactId), - ); + ]; $weight += 10; } } @@ -368,7 +404,7 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { foreach ($activeGroups as $group) { $id = "custom_{$group['id']}"; - $allTabs[] = array( + $allTabs[] = [ 'id' => $id, 'url' => CRM_Utils_System::url($group['path'], $group['query'] . "&selectedChild=$id"), 'title' => $group['title'], @@ -376,56 +412,25 @@ class CRM_Contact_Page_View_Summary extends CRM_Contact_Page_View { 'count' => CRM_Contact_BAO_Contact::getCountComponent($id, $this->_contactId, $group['table_name']), 'hideCount' => !$group['is_multiple'], 'class' => 'livePage', - ); + ]; $weight += 10; } - $context = array('contact_id' => $this->_contactId); + $context = ['contact_id' => $this->_contactId]; // see if any other modules want to add any tabs CRM_Utils_Hook::tabs($allTabs, $this->_contactId); CRM_Utils_Hook::tabset('civicrm/contact/view', $allTabs, $context); - $allTabs[] = array( + $allTabs[] = [ 'id' => 'summary', 'url' => '#contact-summary', 'title' => ts('Summary'), 'weight' => 0, - ); + ]; // now sort the tabs based on weight - usort($allTabs, array('CRM_Utils_Sort', 'cmpFunc')); - - $this->assign('allTabs', $allTabs); - - // hook for contact summary - // ignored but needed to prevent warnings - $contentPlacement = CRM_Utils_Hook::SUMMARY_BELOW; - CRM_Utils_Hook::summary($this->_contactId, $content, $contentPlacement); - if ($content) { - $this->assign_by_ref('hookContent', $content); - $this->assign('hookContentPlacement', $contentPlacement); - } - } - - /** - * @return string - */ - public function getTemplateFileName() { - if ($this->_contactId) { - $contactSubtypes = $this->get('contactSubtype') ? explode(CRM_Core_DAO::VALUE_SEPARATOR, $this->get('contactSubtype')) : array(); - - // there could be multiple subtypes. We check templates for each of the subtype, and return the first one found. - foreach ($contactSubtypes as $csType) { - if ($csType) { - $templateFile = "CRM/Contact/Page/View/SubType/{$csType}.tpl"; - $template = CRM_Core_Page::getTemplate(); - if ($template->template_exists($templateFile)) { - return $templateFile; - } - } - } - } - return parent::getTemplateFileName(); + usort($allTabs, ['CRM_Utils_Sort', 'cmpFunc']); + return $allTabs; } } diff --git a/CRM/Core/Component.php b/CRM/Core/Component.php index 8316aafd86..4a379eaf9f 100644 --- a/CRM/Core/Component.php +++ b/CRM/Core/Component.php @@ -47,7 +47,7 @@ class CRM_Core_Component { /** * @param bool $force * - * @return array|null + * @return CRM_Core_Component_Info[] */ private static function &_info($force = FALSE) { if (!isset(Civi::$statics[__CLASS__]['info'])|| $force) { @@ -84,7 +84,7 @@ class CRM_Core_Component { /** * @param bool $force * - * @return array + * @return CRM_Core_Component_Info[] * @throws Exception */ public static function &getComponents($force = FALSE) { @@ -132,7 +132,7 @@ class CRM_Core_Component { /** * @param bool $force * - * @return array|null + * @return CRM_Core_Component_Info[] */ static public function &getEnabledComponents($force = FALSE) { return self::_info($force); -- 2.25.1