From 19120d5a1972858cd01924b2f1d05f44bbdced69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ya=C4=9F=C4=B1zhan=20Burak=20Yakar?= Date: Thu, 5 Feb 2026 07:16:33 +0300 Subject: [PATCH] Introducing widgets (#2853) * Add "widgets" to the list of core modules * Patch 44 for widget support * +Widgets module * Basic widget discovery * +Render logic * +render_widget_slot Twig function * +get_list * +Basic settings page to list active widgets * +Add an `enabled` column to toggle the widget * +disconnectUnavailable * Catch exceptions while rendering templates and display an error block * Types, checks and stuff * +Add widgets/batch_connect to the crons Also removed the 11+ year old "finish fixing, time to sleep" docblock * Demonstration: Replace hardcoded branding text with a widget * Changed the directory structure * +Add an error block for the admin panel * +Added the `widgets` table * Actually this is pretty unnecessary. * "render_widget_slot" -> "render_widgets" * Updated the dates * Also inject admin/client API if accessible * +Add an options column * Implemented optional "add_br" toggle * Log the exception * Redid the widgets module * Added a few new slots * If even the error template fails, there are bigger problems. Just display the error instead of catching it. * Created and implemented WidgetProviderInterface --------- Co-authored-by: Adam Daley --- src/library/Box/TwigExtensions.php | 40 ++++ .../Interfaces/WidgetProviderInterface.php | 33 +++ src/library/FOSSBilling/Module.php | 3 +- src/modules/Branding/Service.php | 15 +- .../widgets/mod_branding_footer.html.twig | 6 + src/modules/Cron/Service.php | 5 +- src/modules/Widgets/Api/Admin.php | 37 +++ src/modules/Widgets/Controller/Admin.php | 53 +++++ src/modules/Widgets/Service.php | 218 ++++++++++++++++++ .../html_admin/mod_widgets_index.html.twig | 156 +++++++++++++ .../widgets/mod_widgets_error.html.twig | 27 +++ .../widgets/mod_widgets_error.html.twig | 13 ++ src/modules/Widgets/icon.svg | 1 + src/modules/Widgets/manifest.json | 8 + .../html/layout_default.html.twig | 4 + .../huraga/html/layout_default.html.twig | 12 +- 16 files changed, 617 insertions(+), 14 deletions(-) create mode 100644 src/library/FOSSBilling/Interfaces/WidgetProviderInterface.php create mode 100644 src/modules/Branding/html_client/widgets/mod_branding_footer.html.twig create mode 100644 src/modules/Widgets/Api/Admin.php create mode 100644 src/modules/Widgets/Controller/Admin.php create mode 100644 src/modules/Widgets/Service.php create mode 100644 src/modules/Widgets/html_admin/mod_widgets_index.html.twig create mode 100644 src/modules/Widgets/html_admin/widgets/mod_widgets_error.html.twig create mode 100644 src/modules/Widgets/html_client/widgets/mod_widgets_error.html.twig create mode 100644 src/modules/Widgets/icon.svg create mode 100644 src/modules/Widgets/manifest.json diff --git a/src/library/Box/TwigExtensions.php b/src/library/Box/TwigExtensions.php index eb8994ed7..ec3db31f9 100644 --- a/src/library/Box/TwigExtensions.php +++ b/src/library/Box/TwigExtensions.php @@ -133,6 +133,7 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte public function getFunctions() { return [ + new TwigFunction('render_widgets', $this->twig_render_widgets(...), ['needs_environment' => true, 'is_safe' => ['html']]), new TwigFunction('svg_sprite', $this->twig_svg_sprite(...), ['needs_environment' => true, 'is_safe' => ['html']]), // FOSSBilling API functions @@ -152,6 +153,45 @@ class Box_TwigExtensions extends AbstractExtension implements InjectionAwareInte return 'bb'; } + /** + * Part of the Widgets module. Renders the widgets of a specified slot. + * + * @param \Twig\Environment $env the Twig environment (injected automatically) + * @param string $slot name of the slot + * @param array $context optional slot context, such as order or client details + * + * @return string slot content + */ + public function twig_render_widgets(\Twig\Environment $env, string $slot, array $context = []): string + { + $widgets = $this->di['mod_service']('Widgets')->getSlotWidgets($slot); + + if (empty($widgets)) { + return ''; + } + + $output = ''; + + foreach ($widgets as $widget) { + try { + $templateName = 'widgets/' . $widget['template'] . '.html.twig'; + $output .= $env->render($templateName, $context); + } catch (\Throwable $e) { + // Render error widget on failure + $output .= $env->render('widgets/mod_widgets_error.html.twig', array_merge($context, [ + 'widget' => [ + 'slot' => $slot, + 'mod_name' => $widget['module'], + 'template' => $widget['template'], + ], + 'error' => \FOSSBilling\Environment::isDevelopment() ? $e->getMessage() : null, + ])); + } + } + + return $output; + } + public function twig_ipcountryname_filter($value) { if (empty($value)) { diff --git a/src/library/FOSSBilling/Interfaces/WidgetProviderInterface.php b/src/library/FOSSBilling/Interfaces/WidgetProviderInterface.php new file mode 100644 index 000000000..5b6874f07 --- /dev/null +++ b/src/library/FOSSBilling/Interfaces/WidgetProviderInterface.php @@ -0,0 +1,33 @@ + + */ + public function getWidgets(): array; +} diff --git a/src/library/FOSSBilling/Module.php b/src/library/FOSSBilling/Module.php index 237ac7514..073b4e36e 100644 --- a/src/library/FOSSBilling/Module.php +++ b/src/library/FOSSBilling/Module.php @@ -32,7 +32,8 @@ class Module implements InjectionAwareInterface 'page', 'product', 'profile', 'security', 'servicecustom', 'servicedomain', 'servicedownloadable', 'servicehosting', 'servicelicense', 'staff', 'stats', - 'support', 'system', 'theme', 'orderbutton', 'formbuilder']; + 'support', 'system', 'theme', 'orderbutton', 'formbuilder', + 'widgets']; public function setDi(?\Pimple\Container $di): void { diff --git a/src/modules/Branding/Service.php b/src/modules/Branding/Service.php index c2df3241f..70720ef44 100644 --- a/src/modules/Branding/Service.php +++ b/src/modules/Branding/Service.php @@ -11,7 +11,10 @@ namespace Box\Mod\Branding; -class Service implements \FOSSBilling\InjectionAwareInterface +use \FOSSBilling\InjectionAwareInterface; +use \FOSSBilling\Interfaces\WidgetProviderInterface; + +class Service implements InjectionAwareInterface, WidgetProviderInterface { protected ?\Pimple\Container $di = null; @@ -32,6 +35,16 @@ class Service implements \FOSSBilling\InjectionAwareInterface ]; } + public function getWidgets(): array + { + return [ + [ + 'slot' => 'client.theme.footer.end', + 'template' => 'mod_branding_footer', + ], + ]; + } + public function uninstall(): bool { return true; diff --git a/src/modules/Branding/html_client/widgets/mod_branding_footer.html.twig b/src/modules/Branding/html_client/widgets/mod_branding_footer.html.twig new file mode 100644 index 000000000..e0763bf48 --- /dev/null +++ b/src/modules/Branding/html_client/widgets/mod_branding_footer.html.twig @@ -0,0 +1,6 @@ +
+ {{ 'Powered by'|trans }}  + {{ 'FOSSBilling'|trans }} +
\ No newline at end of file diff --git a/src/modules/Cron/Service.php b/src/modules/Cron/Service.php index 3dde2d93f..e9302e97e 100644 --- a/src/modules/Cron/Service.php +++ b/src/modules/Cron/Service.php @@ -39,9 +39,6 @@ class Service ]; } - /** - * @todo finish fixing, time to sleep (note: idk what exactly this is referring to. It predates FOSSBilling and is from BoxBilling well before we touched this code) - */ public function runCrons(): bool { $api = $this->di['api_system']; @@ -80,7 +77,7 @@ class Service /** * @param string $method */ - protected function _exec($api, $method, $params = null) + protected function _exec($api, $method, $params = null): void { try { $api->{$method}($params); diff --git a/src/modules/Widgets/Api/Admin.php b/src/modules/Widgets/Api/Admin.php new file mode 100644 index 000000000..793e22d71 --- /dev/null +++ b/src/modules/Widgets/Api/Admin.php @@ -0,0 +1,37 @@ +getService()->getWidgetList(); + } + + /** + * Force rebuild of the widget registry cache. + * Useful for debugging or after manual changes. + */ + public function rebuild(): bool + { + $service = $this->getService(); + $service->invalidateCache(); + $service->getRegistry(); // Trigger rebuild + + return true; + } +} diff --git a/src/modules/Widgets/Controller/Admin.php b/src/modules/Widgets/Controller/Admin.php new file mode 100644 index 000000000..b38312546 --- /dev/null +++ b/src/modules/Widgets/Controller/Admin.php @@ -0,0 +1,53 @@ +di = $di; + } + + public function getDi(): ?\Pimple\Container + { + return $this->di; + } + + public function fetchNavigation(): array + { + return [ + 'subpages' => [ + [ + 'location' => 'extensions', + 'label' => __trans('Widgets'), + 'index' => 500, + 'uri' => $this->di['url']->adminLink('widgets'), + 'class' => '', + ], + ], + ]; + } + + public function register(\Box_App &$app): void + { + $app->get('/widgets', 'get_index', [], static::class); + } + + public function get_index(\Box_App $app): string + { + $this->di['is_admin_logged']; + + return $app->render('mod_widgets_index'); + } +} diff --git a/src/modules/Widgets/Service.php b/src/modules/Widgets/Service.php new file mode 100644 index 000000000..e03085b49 --- /dev/null +++ b/src/modules/Widgets/Service.php @@ -0,0 +1,218 @@ +di = $di; + } + + public function getDi(): ?\Pimple\Container + { + return $this->di; + } + + /** + * Get the widget registry from cache, rebuild if necessary. + * + * @return array> + */ + public function getRegistry(): array + { + return $this->di['cache']->get(self::CACHE_KEY, function (ItemInterface $item) { + $item->expiresAfter(null); // No need to expire. We will manually invalidate on module activation/deactivation + + return $this->buildRegistry(); + }); + } + + /** + * Build the widget registry by scanning all active modules. + * + * @return array> + */ + public function buildRegistry(): array + { + $registry = []; + $modules = $this->di['mod_service']('Extension')->getCoreAndActiveModules(); + + foreach ($modules as $modName) { + try { + $widgets = $this->getModuleWidgets($modName); + + foreach ($widgets as $widget) { + if (!isset($widget['slot']) || !isset($widget['template'])) { + continue; // Skip invalid widget definitions + } + + $slot = $widget['slot']; + $priority = $widget['priority'] ?? self::DEFAULT_PRIORITY; + + if (!is_int($priority) || $priority <= 0) { + $priority = self::DEFAULT_PRIORITY; + } + + $registry[$slot][] = [ + 'module' => $modName, + 'template' => $widget['template'], + 'priority' => $priority, + ]; + } + } catch (\Throwable $e) { + // Log the error but continue with other modules + $this->di['logger']->err("Error loading widgets from module '{$modName}': " . $e->getMessage()); + } + } + + // Sort each slot's widgets by priority (ascending) + foreach ($registry as $slot => &$widgets) { + usort($widgets, fn ($a, $b) => $a['priority'] <=> $b['priority']); + } + + return $registry; + } + + /** + * Invalidate the widget cache, forcing a rebuild on next access. + */ + public function invalidateCache(): void + { + $this->di['cache']->delete(self::CACHE_KEY); + } + + /** + * Get widgets for a specific slot. + * + * Slot naming convention: {area}.{namespace}.{location}[.{position}] + * - area: 'admin' or 'client' + * - namespace: 'theme' for layout slots, or module name for module-specific slots + * - location: where the slot appears (e.g., 'footer', 'sidebar', 'details') + * - position: optional refinement (e.g., 'start', 'end', 'before', 'after') + * + * Examples: + * - 'client.theme.footer.end' - End of footer in client theme layout + * - 'admin.theme.sidebar.top' - Top of sidebar in admin theme layout + * - 'client.invoice.details.after' - After invoice details in Invoice module + * + * @param string $slot name of the slot (e.g., 'client.theme.footer.end') + * + * @return array widgets for the slot + */ + public function getSlotWidgets(string $slot): array + { + $registry = $this->getRegistry(); + + return $registry[$slot] ?? []; + } + + /** + * Get a module's widget definitions. + * + * @param string $modName the module name + * + * @return array list of widget definitions + */ + public function getModuleWidgets(string $modName): array + { + $mod = $this->di['mod']($modName); + + if ($mod->hasService()) { + $service = $mod->getService(); + + if ($this->serviceProvidesWidgets($service)) { + return $service->getWidgets(); + } + } + + return []; + } + + /** + * Check if a service class provides widgets. + * + * @param object $service the service class instance + * + * @return bool true if the service implements WidgetProviderInterface + */ + private function serviceProvidesWidgets(object $service): bool + { + return $service instanceof WidgetProviderInterface; + } + + /** + * Get all widgets grouped by slot (for admin listing). + * + * @return array list of slots with their widgets + */ + public function getWidgetList(): array + { + $registry = $this->getRegistry(); + $result = []; + + foreach ($registry as $slot => $widgets) { + $result[] = [ + 'slot' => $slot, + 'widgets' => $widgets, + 'count' => count($widgets), + ]; + } + + // Sort by slot name for consistent display + usort($result, fn ($a, $b) => strcmp($a['slot'], $b['slot'])); + + return $result; + } + + /** + * Event handler: Invalidate cache when a module is activated. + */ + public static function onAfterAdminActivateExtension(\Box_Event $event): void + { + $params = $event->getParameters(); + + if (isset($params['id'])) { + $di = $event->getDi(); + $ext = $di['db']->load('extension', $params['id']); + + if (is_object($ext) && $ext->type === 'mod') { + $di['mod_service']('Widgets')->invalidateCache(); + } + } + + $event->setReturnValue(true); + } + + /** + * Event handler: Invalidate cache when a module is deactivated. + */ + public static function onAfterAdminDeactivateExtension(\Box_Event $event): void + { + $params = $event->getParameters(); + + if (($params['type'] ?? null) === 'mod') { + $di = $event->getDi(); + $di['mod_service']('Widgets')->invalidateCache(); + } + + $event->setReturnValue(true); + } +} diff --git a/src/modules/Widgets/html_admin/mod_widgets_index.html.twig b/src/modules/Widgets/html_admin/mod_widgets_index.html.twig new file mode 100644 index 000000000..34ea70e28 --- /dev/null +++ b/src/modules/Widgets/html_admin/mod_widgets_index.html.twig @@ -0,0 +1,156 @@ +{% extends request.ajax ? 'layout_blank.html.twig' : 'layout_default.html.twig' %} + +{% import 'macro_functions.html.twig' as mf %} + +{% block meta_title %}{{ 'Widgets'|trans }}{% endblock %} + +{% set active_menu = 'extensions' %} + +{% block breadcrumbs %} + +{% endblock %} + +{% set slots = admin.widgets_list() %} + +{% block content %} +
+
+
+

{{ 'Widget Registry'|trans }}

+

{{ 'Modules can register widgets to dynamically display content in theme slots'|trans }}

+
+ +
+ + {% if slots is empty %} +
+
+
+ + + +
+

{{ 'No widgets registered'|trans }}

+

+ {{ 'Widgets will appear here when modules that provide them are activated.'|trans }}
+ {{ 'Modules register widgets by implementing a getWidgets() method in their Service class.'|trans|raw }} +

+ +
+
+ {% else %} +
+
+
+ {{ 'Showing'|trans }} + {{ slots|length }} + {{ slots|length == 1 ? 'slot'|trans : 'slots'|trans }} + {{ 'with'|trans }} + {% set total_widgets = 0 %}{% for slot in slots %}{% set total_widgets = total_widgets + slot.count %}{% endfor %}{{ total_widgets }} + {{ total_widgets == 1 ? 'widget'|trans : 'widgets'|trans }} +
+
+
+ +
+ + + + + + + + + + {% for slot in slots %} + + + + + + {% endfor %} + +
{{ 'Slot'|trans }}{{ 'Widgets'|trans }}
+
+ + + + + +
+ {{ slot.slot }} +
+ {% set slot_parts = slot.slot|split('.') %} + {% if slot_parts[0] == 'admin' %} + {{ 'Admin'|trans }} + {% elseif slot_parts[0] == 'client' %} + {{ 'Client'|trans }} + {% endif %} + {% if slot_parts[1] == 'theme' %} + {{ 'Theme layout slot'|trans }} + {% else %} + {{ 'Module slot'|trans }} + {% endif %} +
+
+
+
+
+ {% for widget in slot.widgets %} +
+ {{ widget.priority }} + {{ widget.module|capitalize }} + → + widgets/{{ widget.template }}.html.twig +
+ {% endfor %} +
+
+ {{ slot.count }} +
+
+ + + {% endif %} +
+{% endblock %} \ No newline at end of file diff --git a/src/modules/Widgets/html_admin/widgets/mod_widgets_error.html.twig b/src/modules/Widgets/html_admin/widgets/mod_widgets_error.html.twig new file mode 100644 index 000000000..25601828d --- /dev/null +++ b/src/modules/Widgets/html_admin/widgets/mod_widgets_error.html.twig @@ -0,0 +1,27 @@ + diff --git a/src/modules/Widgets/html_client/widgets/mod_widgets_error.html.twig b/src/modules/Widgets/html_client/widgets/mod_widgets_error.html.twig new file mode 100644 index 000000000..4fc4490c0 --- /dev/null +++ b/src/modules/Widgets/html_client/widgets/mod_widgets_error.html.twig @@ -0,0 +1,13 @@ + diff --git a/src/modules/Widgets/icon.svg b/src/modules/Widgets/icon.svg new file mode 100644 index 000000000..0b40834c8 --- /dev/null +++ b/src/modules/Widgets/icon.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/modules/Widgets/manifest.json b/src/modules/Widgets/manifest.json new file mode 100644 index 000000000..605561107 --- /dev/null +++ b/src/modules/Widgets/manifest.json @@ -0,0 +1,8 @@ +{ + "id": "widgets", + "type": "mod", + "name": "Widgets", + "description": "Widget system for FOSSBilling. Allows modules to register widgets that can be rendered in designated slots.", + "icon_url": "icon.svg", + "version": "1.0.0" +} diff --git a/src/themes/admin_default/html/layout_default.html.twig b/src/themes/admin_default/html/layout_default.html.twig index 7de0d67d9..60cf49d5f 100644 --- a/src/themes/admin_default/html/layout_default.html.twig +++ b/src/themes/admin_default/html/layout_default.html.twig @@ -209,16 +209,20 @@ {% endblock %} + {{ render_widgets('admin.theme.content.before') }} {% block top_content %}{% endblock %} {% block content %}{% endblock %} + {{ render_widgets('admin.theme.content.after') }}
+ {{ render_widgets('admin.theme.footer.start') }}

© {{ now|format_date(pattern='yyyy') }} FOSSBilling contributors. Licensed under Apache 2.0. Running version {{ FOSSBillingVersion }}.

+ {{ render_widgets('admin.theme.footer.end') }}
diff --git a/src/themes/huraga/html/layout_default.html.twig b/src/themes/huraga/html/layout_default.html.twig index 6940ea4e7..b4a843a11 100644 --- a/src/themes/huraga/html/layout_default.html.twig +++ b/src/themes/huraga/html/layout_default.html.twig @@ -167,7 +167,9 @@ {% endblock %} {% endif %} + {{ render_widgets('client.theme.content.before') }} {% block content %}{% endblock %} + {{ render_widgets('client.theme.content.after') }} {{ include('partial_message.html.twig') }} @@ -180,6 +182,7 @@ {% if settings.footer_enabled %} {% endif %}