From 521b232a706cb95b6ef14ab16802de2a4a13c3b1 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 19 Nov 2020 20:44:03 -0800 Subject: [PATCH] afform - Interpret the 'is_dashlet' property and auto-(de)register a `Dashboard` element --- .../core/Civi/Api4/Utils/AfformSaveTrait.php | 10 ++++++ ext/afform/core/afform.php | 34 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php index d35689ce8a..2577ed1323 100644 --- a/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php +++ b/ext/afform/core/Civi/Api4/Utils/AfformSaveTrait.php @@ -64,6 +64,16 @@ trait AfformSaveTrait { $isChanged = function($field) use ($item, $orig) { return ($item[$field] ?? NULL) !== ($orig[$field] ?? NULL); }; + + if ($isChanged('is_dashlet')) { + // FIXME: more targetted reconciliation + \CRM_Core_ManagedEntities::singleton()->reconcile(); + } + elseif ($orig['is_dashlet'] && $isChanged('title')) { + // FIXME: more targetted reconciliation + \CRM_Core_ManagedEntities::singleton()->reconcile(); + } + // Right now, permission-checks are completely on-demand. if ($isChanged('server_route') /* || $isChanged('permission') */) { \CRM_Core_Menu::store(); diff --git a/ext/afform/core/afform.php b/ext/afform/core/afform.php index e9bceafed7..b73043d1fb 100644 --- a/ext/afform/core/afform.php +++ b/ext/afform/core/afform.php @@ -127,6 +127,40 @@ function afform_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { */ function afform_civicrm_managed(&$entities) { _afform_civix_civicrm_managed($entities); + + /** @var \CRM_Afform_AfformScanner $scanner */ + if (\Civi::container()->has('afform_scanner')) { + $scanner = \Civi::service('afform_scanner'); + } + else { + // This might happen at oddballs points - e.g. while you're in the middle of re-enabling the ext. + // This AfformScanner instance only lives during this method call, and it feeds off the regular cache. + $scanner = new CRM_Afform_AfformScanner(); + } + + foreach ($scanner->getMetas() as $afform) { + if (empty($afform['is_dashlet']) || empty($afform['name'])) { + continue; + } + $entities[] = [ + 'module' => E::LONG_NAME, + 'name' => 'afform_dashlet_' . $afform['name'], + 'entity' => 'Dashboard', + 'update' => 'always', + // ideal cleanup policy might be to (a) deactivate if used and (b) remove if unused + 'cleanup' => 'always', + 'params' => [ + 'version' => 3, + // Q: Should we loop through all domains? + 'domain_id' => CRM_Core_BAO_Domain::getDomain()->id, + 'is_active' => TRUE, + 'name' => $afform['name'], + 'label' => $afform['title'] ?? ts('(Untitled)'), + 'directive' => _afform_angular_module_name($afform['name'], 'dash'), + 'permission' => "@afform:" . $afform['name'], + ], + ]; + } } /** -- 2.25.1