Skip to content

Commit

Permalink
Laravel Settings (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrooksuk authored Feb 1, 2024
1 parent 2222d8f commit 39749c5
Show file tree
Hide file tree
Showing 27 changed files with 462 additions and 111 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"php": "^8.1",
"doctrine/dbal": "^3.6",
"filament/filament": "^3.2",
"filament/spatie-laravel-settings-plugin": "^3.2",
"guzzlehttp/guzzle": "^7.8",
"illuminate/console": "^10.0",
"illuminate/database": "^10.0",
Expand All @@ -30,6 +31,7 @@
"illuminate/support": "^10.0",
"nesbot/carbon": "^2.70",
"spatie/laravel-query-builder": "^5.5",
"spatie/laravel-settings": "^3.2",
"tightenco/ziggy": "^1.6",
"timacdonald/json-api": "^v1.0.0-beta.4",
"twig/twig": "^3.0"
Expand Down
26 changes: 0 additions & 26 deletions database/factories/SettingFactory.php

This file was deleted.

24 changes: 24 additions & 0 deletions database/migrations/2024_01_22_202432_update_settings_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('settings', function (Blueprint $table) {
$table->string('group')->after('id')->default('cachet');
$table->boolean('locked')->default(false)->after('name');
$table->json('payload')->after('locked');

$table->text('value')->nullable()->change();

$table->unique(['group', 'name']);
});
}
};
52 changes: 52 additions & 0 deletions database/migrations/2024_01_22_205002_migrate_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
// If there are no settings, then there's nothing to migrate.
if (DB::table('settings')->count() === 0) {
return;
}

DB::table('settings')
->get()
->each(function ($setting) {
DB::table('settings')->select('id', 'name', 'value')->where('id', $setting->id)->update([
'name' => $this->settingName($setting->name),
'group' => $this->settingGroup($setting->name),
'payload' => json_encode($setting->value),
]);
});
}

private function settingName(string $name): string
{
return match (true) {
Str::startsWith($name, ['app_']) => Str::replaceFirst('app_', '', $name),
Str::startsWith($name, ['style_']) => Str::replaceFirst('style_', '', $name),
Str::startsWith($name, ['analytics_']) => Str::replaceFirst('analytics_', '', $name),
default => $name,
};
}

private function settingGroup(string $name): string
{
return match (true) {
Str::startsWith($name, ['app_', 'show_support', 'display_graphs', 'enable_subscribers', 'dashboard_']) => 'app',
Str::contains($name, ['header', 'footer', 'stylesheet']) => 'customization',
Str::startsWith($name, ['style_']) => 'theme',
Str::contains($name, ['date_format', 'automatic_localization']) => 'localization',
Str::contains($name, ['always_authenticate', 'allowed_domains']) => 'security',
Str::contains($name, ['analytics_']) => 'analytics',
default => 'cachet',
};
}
};
35 changes: 35 additions & 0 deletions database/migrations/2024_01_22_205110_create_default_settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Support\Str;
use Spatie\LaravelSettings\Migrations\SettingsMigration;

return new class extends SettingsMigration
{
/**
* Run the migrations.
*/
public function up(): void
{
// Cachet settings...
rescue(fn () => $this->migrator->add('app.install_id', Str::random(40)));
rescue(fn () => $this->migrator->add('app.name', 'Cachet'));
rescue(fn () => $this->migrator->add('app.domain'));
rescue(fn () => $this->migrator->add('app.about'));
rescue(fn () => $this->migrator->add('app.timezone', 'UTC'));
rescue(fn () => $this->migrator->add('app.locale', 'en'));
rescue(fn () => $this->migrator->add('app.incident_days', 7));
rescue(fn () => $this->migrator->add('app.refresh_rate'));
rescue(fn () => $this->migrator->add('app.display_graphs', true));
rescue(fn () => $this->migrator->add('app.show_support', true));
rescue(fn () => $this->migrator->add('app.show_timezone', false));
rescue(fn () => $this->migrator->add('app.only_disrupted_days', false));

// Customization settings...
rescue(fn () => $this->migrator->add('customization.header'));
rescue(fn () => $this->migrator->add('customization.footer'));
rescue(fn () => $this->migrator->add('customization.stylesheet'));

// Theme settings...
rescue(fn () => $this->migrator->add('theme.app_banner'));
}
};
3 changes: 3 additions & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
"Complete": "Complete",
"Complete Maintenance": "Complete Maintenance",
"Component Group": "Component Group",
"Custom CSS": "Custom CSS",
"Custom Footer HTML": "Custom Footer HTML",
"Custom Header HTML": "Custom Header HTML",
"Fixed": "Fixed",
"Guests": "Guests",
"Identified": "Identified",
Expand Down
6 changes: 6 additions & 0 deletions resources/views/components/about.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<h2 class="text-3xl font-semibold">{{ __('About This Site') }}</h2>
<div class="prose-sm md:prose prose-zinc mt-1 dark:prose-invert prose-a:text-primary-500 prose-a:underline">
{!! $about !!}
</div>
</div>
13 changes: 10 additions & 3 deletions resources/views/components/cachet.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
@props([
'title',
])
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}" class="bg-background-light text-base-light dark:bg-background-dark dark:text-base-dark">
<head>
Expand All @@ -15,6 +12,13 @@
@vite(['resources/css/cachet.css', 'resources/js/cachet.js'], 'vendor/cachethq/cachet/build')
@filamentStyles

@if($refresh_rate)
<meta http-equiv="refresh" content="{{ $refresh_rate }}">
@endif

<!-- Custom Cachet Header -->
{!! $cachet_header !!}

<style type="text/css">
/* Cachet custom styles */
:root {
Expand All @@ -27,5 +31,8 @@
</head>
<body class="flex min-h-screen flex-col items-stretch antialiased">
{{ $slot }}

<!-- Custom Cachet Footer -->
{!! $cachet_footer !!}
</body>
</html>
2 changes: 2 additions & 0 deletions resources/views/components/footer.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@if ($showSupport)
<div class="flex items-center justify-center gap-2 border-t px-8 py-6 text-center text-sm tracking-tight text-zinc-500 dark:border-zinc-700">
Powered by
<a href="https://cachethq.io" title="The open-source status page." rel="noopener" class="inline-flex items-center font-semibold transition hover:opacity-80">
Expand All @@ -6,3 +7,4 @@
<span class="ml-2">{{ $cachetVersion }}</span>
</a>
</div>
@endif
4 changes: 4 additions & 0 deletions resources/views/components/header.blade.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
<div class="flex items-center justify-between border-b border-zinc-200 px-8 py-6 dark:border-zinc-700">
<div>
<a href="{{ route('cachet.status-page') }}" class="transition hover:opacity-80">
@if($appBanner)
<img src="{{ asset($appBanner) }}" alt="{{ $siteName }}" class="h-8 w-auto" />
@else
<x-cachet::logo class="hidden h-8 w-auto sm:block" />
<x-cachet::logomark class="h-8 w-auto sm:hidden" />
@endif
</a>
</div>

Expand Down
12 changes: 6 additions & 6 deletions resources/views/components/incident.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@
<x-cachet::incident-badge type="fixed" />
</div>
</div>
<p class="mt-5 text-sm">We're investigating an issue with our monkeys not performing as they should be. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras porttitor sapien ac est rutrum varius. Morbi quis sem auctor, dapibus massa id, sagittis arcu.</p>
<p class="mt-5 text-sm md:text-base">We're investigating an issue with our monkeys not performing as they should be. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras porttitor sapien ac est rutrum varius. Morbi quis sem auctor, dapibus massa id, sagittis arcu.</p>
</div>
<div class="relative">
<div class="absolute inset-y-0 -left-9">
<div class="ml-3.5 h-full border-l-2 border-dashed dark:border-zinc-700"></div>
<div class="absolute inset-x-0 top-0 h-24 w-full bg-gradient-to-t from-transparent to-zinc-50 dark:from-transparent dark:to-gray-900"></div>
<div class="absolute inset-x-0 bottom-0 h-24 w-full bg-gradient-to-b from-transparent to-zinc-50 dark:from-transparent dark:to-gray-900"></div>
<div class="absolute inset-x-0 top-0 h-24 w-full bg-gradient-to-t from-transparent to-zinc-50 dark:from-transparent dark:to-zinc-900"></div>
<div class="absolute inset-x-0 bottom-0 h-24 w-full bg-gradient-to-b from-transparent to-zinc-50 dark:from-transparent dark:to-zinc-900"></div>
</div>
<div class="flex flex-col divide-y px-4 dark:divide-zinc-700">
<div class="relative py-4">
<div class="absolute -left-[calc(28px+10px+13px)] top-4 flex h-7 w-7 items-center justify-center rounded-full bg-orange-200">
<div class="absolute -left-[calc(28px+10px+13px)] top-4 flex h-7 w-7 items-center justify-center rounded-full bg-orange-200 dark:text-zinc-900">
@svg('cachet-unknown', 'h-5 w-5')
</div>
<h3 class="text-lg font-semibold">Incident Update Title</h3>
<span class="text-xs text-zinc-500">3 Days Ago — Friday 1st December 2023, 14:00</span>

<div class="prose-sm mt-2.5">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ad amet autem blanditiis, consectetur cupiditate delectus dolore dolorem dolores maxime nesciunt non quis tempora, unde ut velit voluptates voluptatibus voluptatum.</div>
<div class="prose-sm md:prose mt-2.5">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ad amet autem blanditiis, consectetur cupiditate delectus dolore dolorem dolores maxime nesciunt non quis tempora, unde ut velit voluptates voluptatibus voluptatum.</div>
</div>
<div class="relative py-4">
<div class="absolute -left-[calc(28px+10px+13px)] top-4 flex h-7 w-7 items-center justify-center rounded-full bg-red-200 dark:text-zinc-900">
Expand All @@ -37,7 +37,7 @@
<h3 class="text-lg font-semibold">Incident Update Title</h3>
<span class="text-xs text-zinc-500">3 Days Ago — Friday 1st December 2023, 14:00</span>

<div class="prose-sm mt-2.5">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ad amet autem blanditiis, consectetur cupiditate delectus dolore dolorem dolores maxime nesciunt non quis tempora, unde ut velit voluptates voluptatibus voluptatum.</div>
<div class="prose-sm md:prose mt-2.5">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Accusamus ad amet autem blanditiis, consectetur cupiditate delectus dolore dolorem dolores maxime nesciunt non quis tempora, unde ut velit voluptates voluptatibus voluptatum.</div>
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/status-bar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="flex sm:items-center gap-3">
{{-- <CheckCircleIcon class="w-5 h-5 text-green-400" aria-hidden="true" />--}}
<div class="flex flex-1 justify-between items-center">
<p class="text-sm text-custom-800">{{ $label }}</p>
<p class="text-sm md:text-base text-custom-800">{{ $label }}</p>
{{-- <p class="mt-2 sm:mt-3 text-xs sm:text-sm md:ml-6 md:mt-0 text-green-800">--}}
{{-- Updated <time datetime="2008-02-14 20:00" title="2008-02-14 20:00" class="underline decoration-dotted underline-offset-2 cursor-help">10 minutes</time> ago--}}
{{-- </p>--}}
Expand Down
22 changes: 6 additions & 16 deletions resources/views/status-page/index.blade.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
<x-cachet::cachet>
<x-cachet::header/>
<x-cachet::header />

<div class="mx-auto max-w-5xl px-4 py-10 sm:px-6 lg:px-8">
<div>
<h2 class="text-3xl font-semibold">About This Site</h2>
<div class="prose prose-zinc mt-1 dark:prose-invert prose-a:text-primary-500 prose-a:underline">
{{-- format-ignore-start --}}
<p>
This is the demo instance of <a href="https://cachethq.io/" target="_blank">Cachet</a>. The
open-source status page system.
</p>
{{-- format-ignore-end --}}
</div>
</div>
<x-cachet::about />

<div class="mt-6 space-y-10">
<x-cachet::status-bar/>
<x-cachet::status-bar />

@foreach($componentGroups as $componentGroup)
<x-cachet::component-group :component-group="$componentGroup"/>
<x-cachet::component-group :component-group="$componentGroup"/>
@endforeach

@if($schedules->isNotEmpty())
<x-cachet::schedules :schedules="$schedules" />
@endif

<x-cachet::incidents/>
<x-cachet::incidents />
</div>
</div>

<x-cachet::footer/>
<x-cachet::footer />
</x-cachet::cachet>
72 changes: 72 additions & 0 deletions src/Filament/Pages/ManageCachet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Cachet\Filament\Pages;

use Cachet\Settings\AppSettings;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Pages\SettingsPage;
use Illuminate\Support\Str;

class ManageCachet extends SettingsPage
{
protected static string $settings = AppSettings::class;

protected static ?string $navigationGroup = 'Settings';

public function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make()->columns(2)->schema([
Forms\Components\TextInput::make('name')
->label(__('Site Name'))
->maxLength(255),
Forms\Components\MarkdownEditor::make('about')
->label(__('About This Site'))
->columnSpanFull(),

Forms\Components\Select::make('timezone')
->label(__('Timezone'))
->options(fn () => collect(timezone_identifiers_list())
->mapToGroups(
fn($timezone) => [
Str::of($timezone)
->before("/")
->toString() => [$timezone => $timezone]
]
)
->map(fn($group) => $group->collapse()))
->searchable()
->suffixIcon('heroicon-o-globe-alt'),

Forms\Components\TextInput::make('incident_days')
->numeric()
->label(__('Incident Days'))
->minValue(1)
->maxValue(365)
->step(1),

Forms\Components\TextInput::make('refresh_rate')
->numeric()
->label(__('Automatically Refresh Page'))
->minValue(0)
->nullable()
->step(1)
->suffix(__('seconds')),

Forms\Components\Grid::make(2)
->schema([
Forms\Components\Toggle::make('show_support')
->label(__('Support Cachet')),
Forms\Components\Toggle::make('display_graphs')
->label(__('Display Graphs')),
]),
Forms\Components\Toggle::make('show_timezone')
->label(__('Show Timezone')),
Forms\Components\Toggle::make('only_disrupted_days')
->label(__('Only Show Disrupted Days')),
]),
]);
}
}
Loading

0 comments on commit 39749c5

Please sign in to comment.