mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-08-05 15:42:14 +00:00
Takes over #690 by csoscd. The companion-view idea is theirs; this reworks it onto the shared page setup and fills in the gaps that stopped it landing. A `{template}_header` or `{template}_footer` view next to a template is rendered alongside it and repeated by Chromium on every page. The suffix resolves through the pdf_templates:: namespace too, so custom templates get it with no extra wiring. Two things had to change for that to be useful. Companion views are now hidden from the template picker. getFormattedTemplates() lists every .blade.php it finds, so an invoice1_footer would otherwise appear as a separately selectable template with no preview image -- the feature would have introduced that the moment anyone used it. And it does something out of the box. #690 shipped no companion views, so both of its margin settings were visible no-ops until someone hand-wrote a Blade file. Instead there is a pdf_page_numbers setting, off by default, that supplies a footer when a template has none. A template's own companion still wins, so turning page numbers on cannot overwrite a designed footer. The setting sits under Gotenberg because only Chromium can repeat a footer; dompdf has no equivalent. Its value is still carried by the dompdf form so saving from there cannot clear the choice -- the field is absent from that payload, and the controller only writes it when present. Margins come from the page setup rather than #690's separate header_margin and footer_margin. Chromium draws header and footer inside the page margin, so the existing margins are the space they occupy; two more settings for the same distance would have been a second way to say the same thing. Verified against a live gotenberg:8 on a two-page document: off produces no footer, on produces "1/2" and "2/2" on the respective pages, and a companion footer replaces both. #690's own test is not carried over. It asserted nothing: a bare View::shouldReceive('exists') is an allowance rather than an expectation, andReturn(false) never entered the companion branch, and the call sat inside try { } catch (Throwable) { }, so it passed with the feature deleted. Claude-Session: https://claude.ai/code/session_01QmECndmNZwzN65Zz9P87dF
116 lines
3.9 KiB
PHP
116 lines
3.9 KiB
PHP
<?php
|
|
|
|
namespace App\Providers;
|
|
|
|
use App\Models\FileDisk;
|
|
use App\Models\Setting;
|
|
use App\Services\Mail\MailConfigurationService;
|
|
use App\Services\Storage\FileDiskService;
|
|
use App\Support\Setup\InstallUtils;
|
|
use Illuminate\Support\Facades\Config;
|
|
use Illuminate\Support\ServiceProvider;
|
|
|
|
class AppConfigProvider extends ServiceProvider
|
|
{
|
|
/**
|
|
* Bootstrap services.
|
|
*/
|
|
public function boot(): void
|
|
{
|
|
// Check if database is available
|
|
if (! InstallUtils::isDbCreated()) {
|
|
return;
|
|
}
|
|
|
|
$this->configureMailFromDatabase();
|
|
$this->configurePDFFromDatabase();
|
|
$this->configureFileSystemFromDatabase();
|
|
}
|
|
|
|
/**
|
|
* Configure mail settings from database
|
|
*/
|
|
protected function configureMailFromDatabase(): void
|
|
{
|
|
try {
|
|
app(MailConfigurationService::class)->applyGlobalConfig();
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
// This prevents the application from breaking during setup
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure PDF settings from database
|
|
*/
|
|
protected function configurePDFFromDatabase(): void
|
|
{
|
|
try {
|
|
$pageSettings = [
|
|
'pdf_paper_width' => 'pdf.page.paper_width',
|
|
'pdf_paper_height' => 'pdf.page.paper_height',
|
|
'pdf_orientation' => 'pdf.page.orientation',
|
|
'pdf_margin_top' => 'pdf.page.margin_top',
|
|
'pdf_margin_right' => 'pdf.page.margin_right',
|
|
'pdf_margin_bottom' => 'pdf.page.margin_bottom',
|
|
'pdf_margin_left' => 'pdf.page.margin_left',
|
|
];
|
|
|
|
$pdfSettings = Setting::getSettings(array_merge(
|
|
['pdf_driver', 'gotenberg_host', 'pdf_page_numbers'],
|
|
array_keys($pageSettings),
|
|
));
|
|
|
|
if (! empty($pdfSettings['pdf_driver'])) {
|
|
Config::set('pdf.driver', $pdfSettings['pdf_driver']);
|
|
|
|
if ($pdfSettings['pdf_driver'] === 'gotenberg' && ! empty($pdfSettings['gotenberg_host'])) {
|
|
Config::set('pdf.connections.gotenberg.host', $pdfSettings['gotenberg_host']);
|
|
}
|
|
}
|
|
|
|
// Page geometry is applied regardless of driver. Note the isset guard
|
|
// rather than !empty: a saved margin of "0mm" is a deliberate choice,
|
|
// and !empty() would discard it and silently fall back to the default.
|
|
foreach ($pageSettings as $setting => $configKey) {
|
|
if (isset($pdfSettings[$setting]) && trim((string) $pdfSettings[$setting]) !== '') {
|
|
Config::set($configKey, $pdfSettings[$setting]);
|
|
}
|
|
}
|
|
|
|
// Stored as a string, so cast rather than passing '0' through as a
|
|
// truthy value.
|
|
if (isset($pdfSettings['pdf_page_numbers'])) {
|
|
Config::set(
|
|
'pdf.page.page_numbers',
|
|
filter_var($pdfSettings['pdf_page_numbers'], FILTER_VALIDATE_BOOLEAN)
|
|
);
|
|
}
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
// This prevents the application from breaking during setup
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Configure file system settings from database
|
|
*/
|
|
protected function configureFileSystemFromDatabase(): void
|
|
{
|
|
try {
|
|
$fileDisk = FileDisk::whereSetAsDefault(true)->first();
|
|
|
|
if (! $fileDisk) {
|
|
return;
|
|
}
|
|
|
|
$diskName = app(FileDiskService::class)->registerDisk($fileDisk);
|
|
|
|
// Point Spatie Media Library at the resolved disk
|
|
config(['media-library.disk_name' => $diskName]);
|
|
} catch (\Exception $e) {
|
|
// Silently fail if database is not available (during installation, migrations, etc.)
|
|
}
|
|
}
|
|
}
|