Files
InvoiceShelf/app/Http/Controllers/Company/Modules/CompanyModulesController.php
Darko Gjorgjijoski 345bfde306 feat(modules): translated display names and inline settings modal
CompanyModulesController attaches a translated display_name to each module before returning the list. ModuleSettingsController gains a translateSchema() helper that resolves section titles and field labels against the host app's i18n store before sending the schema to the frontend, so module authors can keep their 'my_module::settings.field' keys and users still see localized strings.

Per-module settings now open in an inline ModuleSettingsModal rather than routing to a standalone page. The modal reuses BaseSchemaForm for rendering, so the whole interaction takes place in-context next to the module card the user clicked — no navigation, no loss of place.

CompanyModuleCard displays the translated display_name instead of the raw slug and emits open-settings with the module payload; the parent view hands that to the modal store.
2026-04-10 21:30:00 +02:00

77 lines
2.6 KiB
PHP

<?php
namespace App\Http\Controllers\Company\Modules;
use App\Http\Controllers\Controller;
use App\Models\Module;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;
use InvoiceShelf\Modules\Registry as ModuleRegistry;
/**
* Read-only company-context Active Modules index.
*
* Lists every module the super admin has activated on this instance
* (Module::enabled = true) and reports whether each one has registered a
* settings schema. The frontend uses this to render the company-context
* "Modules" landing page with a Settings button per active module.
*
* Activation is instance-global; per-company customization happens through
* settings (per CompanySetting under the module.{slug}.* prefix).
*
* Slug convention: nwidart stores the module's PascalCase class name in
* `modules.name` (e.g. "HelloWorld"), but URLs and registry keys use the
* kebab-case form ("hello-world") for readability. We normalize via
* Str::kebab() so module authors can call Registry::registerMenu('hello-world')
* naturally without thinking about the storage format.
*/
class CompanyModulesController extends Controller
{
public function index(): JsonResponse
{
$this->authorize('manage modules');
$modules = Module::query()
->where('enabled', true)
->get()
->map(function (Module $module) {
$slug = Str::kebab($module->name);
$menu = ModuleRegistry::menuFor($slug);
$translatedMenuTitle = $this->translateMenuTitle($menu['title'] ?? null);
$displayName = $translatedMenuTitle ?? Str::headline($module->name);
return [
'slug' => $slug,
'name' => $module->name,
'display_name' => $displayName,
'version' => $module->version,
'has_settings' => ModuleRegistry::settingsFor($slug) !== null,
'menu' => $menu === null
? null
: [
...$menu,
'title' => $translatedMenuTitle ?? $menu['title'],
],
];
})
->values();
return response()->json(['data' => $modules]);
}
private function translateMenuTitle(?string $title): ?string
{
if ($title === null) {
return null;
}
$translatedTitle = __($title);
if (! is_string($translatedTitle) || $translatedTitle === $title) {
return null;
}
return $translatedTitle;
}
}