|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace OpenCoreEMR\ModuleConfig; |
| 6 | + |
| 7 | +use OpenEMR\Events\Globals\GlobalsInitializedEvent; |
| 8 | +use OpenEMR\Services\Globals\GlobalSetting; |
| 9 | +use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
| 10 | +use Symfony\Component\HttpFoundation\ParameterBag; |
| 11 | + |
| 12 | +/** |
| 13 | + * Register a module's globals section: enable/disable toggle and settings page link. |
| 14 | + * |
| 15 | + * Eliminates the boilerplate every OCE module copy-pastes in its Bootstrap class. |
| 16 | + * |
| 17 | + * @author Michael A. Smith <michael@opencoreemr.com> |
| 18 | + * @copyright Copyright (c) 2026 OpenCoreEMR Inc. <https://www.opencoreemr.com> |
| 19 | + */ |
| 20 | +class GlobalsRegistrar |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly ParameterBag $globalsBag, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Subscribe to GlobalsInitializedEvent and register the module's globals section. |
| 29 | + */ |
| 30 | + public function register( |
| 31 | + EventDispatcherInterface $eventDispatcher, |
| 32 | + GlobalsSectionDescriptor $descriptor, |
| 33 | + ): void { |
| 34 | + $globalsBag = $this->globalsBag; |
| 35 | + |
| 36 | + $eventDispatcher->addListener( |
| 37 | + GlobalsInitializedEvent::EVENT_HANDLE, |
| 38 | + static function (GlobalsInitializedEvent $event) use ($descriptor, $globalsBag): void { |
| 39 | + $service = $event->getGlobalsService(); |
| 40 | + |
| 41 | + $service->createSection($descriptor->sectionName); |
| 42 | + |
| 43 | + // Enable/disable toggle |
| 44 | + $enableSetting = new GlobalSetting( |
| 45 | + sprintf(xlt('Enable %s'), $descriptor->sectionName), |
| 46 | + GlobalSetting::DATA_TYPE_BOOL, |
| 47 | + '0', |
| 48 | + sprintf(xlt('Enable or disable the %s module'), $descriptor->sectionName), |
| 49 | + ); |
| 50 | + $service->appendToSection( |
| 51 | + $descriptor->sectionName, |
| 52 | + $descriptor->enableKey, |
| 53 | + $enableSetting, |
| 54 | + ); |
| 55 | + |
| 56 | + // Settings page link |
| 57 | + $webroot = $globalsBag->getString('webroot'); |
| 58 | + $settingsPath = $webroot |
| 59 | + . '/interface/modules/custom_modules/' . $descriptor->moduleDirName |
| 60 | + . '/public/settings.php'; |
| 61 | + |
| 62 | + $linkSetting = new GlobalSetting( |
| 63 | + xlt('Module Settings'), |
| 64 | + GlobalSetting::DATA_TYPE_HTML_DISPLAY_SECTION, |
| 65 | + '', |
| 66 | + xlt('Link to the module settings page'), |
| 67 | + ); |
| 68 | + $linkSetting->addFieldOption( |
| 69 | + GlobalSetting::DATA_TYPE_OPTION_RENDER_CALLBACK, |
| 70 | + static function () use ($settingsPath, $descriptor): string { |
| 71 | + $url = attr($settingsPath); |
| 72 | + $label = xlt('Open Module Settings'); |
| 73 | + $description = xlt($descriptor->settingsDescription); |
| 74 | + return <<<HTML |
| 75 | + <p>{$description}</p> |
| 76 | + <a href="{$url}" class="btn btn-secondary btn-sm" |
| 77 | + onclick="top.restoreSession()">{$label}</a> |
| 78 | + HTML; |
| 79 | + }, |
| 80 | + ); |
| 81 | + $service->appendToSection( |
| 82 | + $descriptor->sectionName, |
| 83 | + $descriptor->enableKey . '_settings_link', |
| 84 | + $linkSetting, |
| 85 | + ); |
| 86 | + }, |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments