From a8f56d7109e2ae12ed502df0c89e5a89893ac3ca Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Tue, 9 Aug 2016 18:42:56 -0400 Subject: [PATCH] CRM-17663 - Configurable cache time per dashlet & auto-refresh --- CRM/Admin/Form/Setting/Miscellaneous.php | 2 -- CRM/Core/BAO/Dashboard.php | 4 +++ CRM/Core/Config/MagicMerge.php | 1 - CRM/Report/BAO/ReportInstance.php | 5 +++- CRM/Report/Form/Instance.php | 15 +++++----- CRM/Upgrade/Incremental/php/FourSeven.php | 7 +++++ api/v3/examples/Setting/GetFields.php | 21 ------------- js/jquery/jquery.dashboard.js | 30 ++++++++++++++----- settings/Core.setting.php | 19 ------------ .../CRM/Admin/Form/Setting/Miscellaneous.tpl | 7 ----- templates/CRM/Report/Form/Tabs/Instance.tpl | 27 ++++++++++------- templates/CRM/Report/Form/Tabs/Settings.hlp | 7 +++++ templates/CRM/common/dashboard.tpl | 8 ----- xml/schema/Core/Dashboard.xml | 9 ++++++ xml/templates/civicrm_navigation.tpl | 14 ++++----- 15 files changed, 85 insertions(+), 91 deletions(-) diff --git a/CRM/Admin/Form/Setting/Miscellaneous.php b/CRM/Admin/Form/Setting/Miscellaneous.php index 68f68d15a2..762d8b3468 100644 --- a/CRM/Admin/Form/Setting/Miscellaneous.php +++ b/CRM/Admin/Form/Setting/Miscellaneous.php @@ -39,7 +39,6 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { protected $_settings = array( 'max_attachments' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, - 'dashboardCacheTimeout' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'empoweredBy' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'logging' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'maxFileSize' => CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, @@ -89,7 +88,6 @@ class CRM_Admin_Form_Setting_Miscellaneous extends CRM_Admin_Form_Setting { parent::buildQuickForm(); $this->addRule('checksum_timeout', ts('Value should be a positive number'), 'positiveInteger'); - $this->addRule('dashboardCacheTimeout', ts('Value should be a positive number'), 'positiveInteger'); } /** diff --git a/CRM/Core/BAO/Dashboard.php b/CRM/Core/BAO/Dashboard.php index a763c18564..f0f4c03f8d 100644 --- a/CRM/Core/BAO/Dashboard.php +++ b/CRM/Core/BAO/Dashboard.php @@ -118,6 +118,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { 'dashboard_id.label', 'dashboard_id.url', 'dashboard_id.fullscreen_url', + 'dashboard_id.cache_minutes', 'dashboard_id.permission', 'dashboard_id.permission_operator', ), @@ -132,6 +133,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { 'name' => $item['dashboard_id.name'], 'label' => $item['dashboard_id.label'], 'url' => $item['dashboard_id.url'], + 'cache_minutes' => $item['dashboard_id.cache_minutes'], 'fullscreen_url' => $item['dashboard_id.fullscreen_url'], ); } @@ -159,6 +161,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { 'name' => $item['name'], 'title' => $item['label'], 'url' => self::parseUrl($item['url']), + 'cacheMinutes' => $item['cache_minutes'], 'fullscreenUrl' => self::parseUrl($item['fullscreen_url']), ); } @@ -212,6 +215,7 @@ class CRM_Core_BAO_Dashboard extends CRM_Core_DAO_Dashboard { 'column_no' => $values['column_no'], 'name' => $dashlet['name'], 'label' => $dashlet['label'], + 'cache_minutes' => $dashlet['cache_minutes'], 'url' => $dashlet['url'], 'fullscreen_url' => $dashlet['fullscreen_url'], ); diff --git a/CRM/Core/Config/MagicMerge.php b/CRM/Core/Config/MagicMerge.php index b1b84ab868..37c15ef555 100644 --- a/CRM/Core/Config/MagicMerge.php +++ b/CRM/Core/Config/MagicMerge.php @@ -118,7 +118,6 @@ class CRM_Core_Config_MagicMerge { 'backtrace' => array('setting'), 'contact_default_language' => array('setting'), 'countryLimit' => array('setting'), - 'dashboardCacheTimeout' => array('setting'), 'dateInputFormat' => array('setting'), 'dateformatDatetime' => array('setting'), 'dateformatFull' => array('setting'), diff --git a/CRM/Report/BAO/ReportInstance.php b/CRM/Report/BAO/ReportInstance.php index 16680501f0..fcb3a7ff9b 100644 --- a/CRM/Report/BAO/ReportInstance.php +++ b/CRM/Report/BAO/ReportInstance.php @@ -215,9 +215,12 @@ class CRM_Report_BAO_ReportInstance extends CRM_Report_DAO_ReportInstance { $section = 1; $chart = "&charts=" . $params['charts']; } - if (!empty($params['row_count'])) { + if (!empty($params['row_count']) && CRM_Utils_Rule::positiveInteger($params['row_count'])) { $limitResult = '&rowCount=' . $params['row_count']; } + if (!empty($params['cache_minutes']) && CRM_Utils_Rule::positiveInteger($params['cache_minutes'])) { + $dashletParams['cache_minutes'] = $params['cache_minutes']; + } $dashletParams['name'] = "report/{$instance->id}"; $dashletParams['url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}{$chart}&context=dashlet" . $limitResult; $dashletParams['fullscreen_url'] = "civicrm/report/instance/{$instance->id}?reset=1§ion={$section}{$chart}&context=dashletFullscreen" . $limitResult; diff --git a/CRM/Report/Form/Instance.php b/CRM/Report/Form/Instance.php index b57391871b..22cf52d7c3 100644 --- a/CRM/Report/Form/Instance.php +++ b/CRM/Report/Form/Instance.php @@ -82,13 +82,10 @@ class CRM_Report_Form_Instance { $attributes['email_subject'] ); - $form->add('text', + $form->add('number', 'row_count', ts('Limit Dashboard Results'), - array( - 'maxlength' => 64, - 'size' => 5, - ) + array('class' => 'four', 'min' => 1) ); $form->add('textarea', @@ -112,8 +109,8 @@ class CRM_Report_Form_Instance { 'criteria' => ts('Show Criteria'), )); - $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?'), NULL, - array('onclick' => "return showHideByValue('addToDashboard','','limit_result','table-row','radio',false);")); + $form->addElement('checkbox', 'addToDashboard', ts('Available for Dashboard?')); + $form->add('number', 'cache_minutes', ts('Cache dashlet for'), array('class' => 'four', 'min' => 1)); $form->addElement('checkbox', 'add_to_my_reports', ts('Add to My Reports?'), NULL); $form->addElement('checkbox', 'is_reserved', ts('Reserved Report?')); @@ -263,6 +260,10 @@ class CRM_Report_Form_Instance { $defaults['view_mode'] = 'criteria'; } + if (empty($defaults['cache_minutes'])) { + $defaults['cache_minutes'] = '60'; + } + if ($instanceID) { // this is already retrieved via Form.php $defaults['description'] = CRM_Utils_Array::value('description', $defaults); diff --git a/CRM/Upgrade/Incremental/php/FourSeven.php b/CRM/Upgrade/Incremental/php/FourSeven.php index 0e523bf5f1..9988e9c6d6 100644 --- a/CRM/Upgrade/Incremental/php/FourSeven.php +++ b/CRM/Upgrade/Incremental/php/FourSeven.php @@ -760,6 +760,13 @@ FROM `civicrm_dashboard_contact` JOIN `civicrm_contact` WHERE civicrm_dashboard_ CRM_Core_BAO_SchemaHandler::dropColumn('civicrm_dashboard', 'weight'); CRM_Core_DAO::executeQuery('UPDATE civicrm_dashboard SET url = REPLACE(url, "&snippet=5", ""), fullscreen_url = REPLACE(fullscreen_url, "&snippet=5", "")'); + + if (!CRM_Core_BAO_SchemaHandler::checkIfFieldExists('civicrm_dashboard', 'cache_minutes')) { + CRM_Core_DAO::executeQuery('ALTER TABLE civicrm_dashboard ADD COLUMN cache_minutes int unsigned NOT NULL DEFAULT 60 COMMENT "Number of minutes to cache dashlet content in browser localStorage."'); + } + + CRM_Core_DAO::executeQuery('UPDATE civicrm_dashboard SET cache_minutes = 1440 WHERE name = "blog"'); + CRM_Core_DAO::executeQuery('UPDATE civicrm_dashboard SET cache_minutes = 7200 WHERE name IN ("activity","getting-started")'); return TRUE; } diff --git a/api/v3/examples/Setting/GetFields.php b/api/v3/examples/Setting/GetFields.php index 4d3549586d..df623eb6ce 100644 --- a/api/v3/examples/Setting/GetFields.php +++ b/api/v3/examples/Setting/GetFields.php @@ -620,27 +620,6 @@ function setting_getfields_expectedresult() { 'description' => '', 'help_text' => '', ), - 'dashboardCacheTimeout' => array( - 'group_name' => 'CiviCRM Preferences', - 'group' => 'core', - 'name' => 'dashboardCacheTimeout', - 'prefetch' => 1, - 'config_only' => 1, - 'type' => 'Integer', - 'quick_form_type' => 'Element', - 'html_attributes' => array( - 'size' => 3, - 'maxlength' => 5, - ), - 'html_type' => 'Text', - 'default' => '', - 'add' => '4.3', - 'title' => 'Dashboard cache timeout', - 'is_domain' => 1, - 'is_contact' => 0, - 'description' => '', - 'help_text' => '', - ), 'checksumTimeout' => array( 'group_name' => 'CiviCRM Preferences', 'group' => 'core', diff --git a/js/jquery/jquery.dashboard.js b/js/jquery/jquery.dashboard.js index 3561922369..394635d361 100644 --- a/js/jquery/jquery.dashboard.js +++ b/js/jquery/jquery.dashboard.js @@ -130,12 +130,13 @@ $("#empty-message").show( ); } + // Cache dashlet info in localStorage function saveLocalCache() { localCache = {}; $.each(dashboard.widgets, function(id, widget) { localCache[id] = { content: widget.content, - expires: widget.expires, + lastLoaded: widget.lastLoaded, minimized: widget.minimized }; }); @@ -181,6 +182,17 @@ dashboard.saveColumns(); dashboard.ready = true; invokeCallback(opts.callbacks.ready, dashboard); + + // Auto-refresh widgets when content is stale + window.setInterval(function() { + if (!document.hasFocus || document.hasFocus()) { + $.each(dashboard.widgets, function (i, widget) { + if (!widget.cacheIsFresh()) { + widget.reloadContent(); + } + }); + } + }, 5000); } // Callback for when any list has changed (and the user has finished resorting). @@ -361,7 +373,7 @@ // If minimized, we'll reload later if (widget.minimized) { widget.contentLoaded = false; - widget.expires = 0; + widget.lastLoaded = 0; } else { CRM.loadPage(widget.url, {target: widget.contentElement}); } @@ -382,6 +394,10 @@ ); }; + widget.cacheIsFresh = function() { + return (((widget.cacheMinutes * 60000 + widget.lastLoaded) > $.now()) && widget.content); + }; + /** * Public properties of widget. */ @@ -419,7 +435,7 @@ */ function loadContent() { - var loadFromCache = (widget.expires > $.now() && widget.content); + var loadFromCache = widget.cacheIsFresh(); if (loadFromCache) { widget.contentElement.html(widget.content).trigger('crmLoad', widget); } @@ -427,7 +443,7 @@ if ($(event.target).is(widget.contentElement)) { widget.content = data.content; // Cache for one day - widget.expires = $.now() + 86400000; + widget.lastLoaded = $.now(); saveLocalCache(); invokeCallback(opts.widgetCallbacks.get, widget); } @@ -544,7 +560,7 @@ // Public static properties of dashboard. Default settings. $.fn.dashboard.defaults = { columns: 2, - emptyPlaceholderInner: ts('There are no dashlets in this column of your dashboard.'), + emptyPlaceholderInner: '', throbberMarkup: '', animationSpeed: 200, callbacks: {}, @@ -556,9 +572,9 @@ defaults: { minimized: false, content: null, - expires: 0, + lastLoaded: 0, settings: false - // url, fullscreenUrl, title, name + // id, url, fullscreenUrl, title, name, cacheMinutes } }; })(jQuery); diff --git a/settings/Core.setting.php b/settings/Core.setting.php index f8fa1904bf..6430d873b8 100644 --- a/settings/Core.setting.php +++ b/settings/Core.setting.php @@ -497,25 +497,6 @@ return array( 'description' => NULL, 'help_text' => NULL, ), - 'dashboardCacheTimeout' => array( - 'group_name' => 'CiviCRM Preferences', - 'group' => 'core', - 'name' => 'dashboardCacheTimeout', - 'type' => 'Integer', - 'quick_form_type' => 'Element', - 'html_attributes' => array( - 'size' => 3, - 'maxlength' => 5, - ), - 'html_type' => 'Text', - 'default' => NULL, - 'add' => '4.3', - 'title' => 'Dashboard cache timeout', - 'is_domain' => 1, - 'is_contact' => 0, - 'description' => NULL, - 'help_text' => NULL, - ), 'checksum_timeout' => array( 'group_name' => 'CiviCRM Preferences', 'group' => 'core', diff --git a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl index 5d5c187049..32125f8966 100644 --- a/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl +++ b/templates/CRM/Admin/Form/Setting/Miscellaneous.tpl @@ -25,13 +25,6 @@ *}
{include file="CRM/common/formButtons.tpl" location="top"}
- - - - - -
{$form.dashboardCacheTimeout.label}{$form.dashboardCacheTimeout.html}
- {ts}The number of minutes to cache dashlet content on dashboard.{/ts}
diff --git a/templates/CRM/Report/Form/Tabs/Instance.tpl b/templates/CRM/Report/Form/Tabs/Instance.tpl index ca7db8c37e..b269d618b0 100644 --- a/templates/CRM/Report/Form/Tabs/Instance.tpl +++ b/templates/CRM/Report/Form/Tabs/Instance.tpl @@ -106,9 +106,13 @@ {ts}Users with appropriate permissions can add this report to their dashboard.{/ts} - + - + + + + +
{$form.row_count.label} {help id="id-dash_limit" file="CRM/Report/Form/Tabs/Settings.hlp"}{$form.row_count.html}{$form.row_count.html} {ts}rows{/ts}
{$form.cache_minutes.label} {help id="cache_minutes" file="CRM/Report/Form/Tabs/Settings.hlp"}{$form.cache_minutes.html} {ts}minutes{/ts}
@@ -121,15 +125,16 @@ field_type ="radio" invert = 0 } -{include file="CRM/common/showHideByFieldValue.tpl" - trigger_field_id ="addToDashboard" - trigger_value ="" - target_element_id ="limit_result" - target_element_type ="table-row" - field_type ="radio" - invert = 0 -} - +{literal} + +{/literal} {if $is_navigation}