Fixes the configuration file
This commit is contained in:
74
app/Configs/Content.php
Normal file
74
app/Configs/Content.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Configs;
|
||||
|
||||
use Illuminate\Support\Arr;
|
||||
use Laravel\Nova\Fields\Markdown;
|
||||
|
||||
/**
|
||||
* Config Field class for the "content" config group.
|
||||
* Defines fields, defaults, and Nova fields for content settings.
|
||||
*/
|
||||
final class Content
|
||||
{
|
||||
public string $title = 'Content';
|
||||
|
||||
public string $description = 'Content settings';
|
||||
|
||||
public string $key = 'content';
|
||||
|
||||
/**
|
||||
* Returns the field definitions with their type and default value for this config group.
|
||||
*/
|
||||
public function getFieldKeys(): array
|
||||
{
|
||||
return [
|
||||
'disclaimer' => [
|
||||
'type' => 'markdown',
|
||||
'default' => '',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default value for the given field key, or null if the key does not exist.
|
||||
*/
|
||||
public function getDefault(string $key): mixed
|
||||
{
|
||||
$fields = $this->getFieldKeys();
|
||||
|
||||
if (! Arr::has($fields, $key)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Arr::get($fields, "{$key}.default");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns Nova field instances for this config group, each wired with
|
||||
* resolveUsing (read from json_value with default fallback) and
|
||||
* fillUsing (write back into json_value) callbacks.
|
||||
*/
|
||||
public function getNovaFields(): array
|
||||
{
|
||||
return [
|
||||
Markdown::make('Disclaimer', 'disclaimer')
|
||||
->resolveUsing(function (mixed $value, mixed $resource): string {
|
||||
$jsonValue = Arr::get((array) $resource->json_value, 'disclaimer');
|
||||
|
||||
if ($jsonValue === null || $jsonValue === '') {
|
||||
return (string) $this->getDefault('disclaimer');
|
||||
}
|
||||
|
||||
return (string) $jsonValue;
|
||||
})
|
||||
->fillUsing(function (mixed $request, mixed $model, string $attribute, string $requestAttribute): void {
|
||||
$current = Arr::wrap((array) $model->json_value);
|
||||
Arr::set($current, $attribute, $request->{$requestAttribute});
|
||||
$model->json_value = $current;
|
||||
}),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -103,15 +103,17 @@ private function calculateAndUpdateScore(Screening $screening, array $answers):
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the total score from the answers.
|
||||
* Calculate the total score from the answers — yes = 1, unknown = 0.5, no = 0.
|
||||
*/
|
||||
private function calculateScore(array $answers): int
|
||||
private function calculateScore(array $answers): float
|
||||
{
|
||||
$score = 0;
|
||||
|
||||
foreach ($answers as $value) {
|
||||
if ($value === 'yes') {
|
||||
$score++;
|
||||
} elseif ($value === 'unknown') {
|
||||
$score += 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public function rules(): array
|
||||
{
|
||||
return [
|
||||
'answers' => ['required', 'array', 'size:10'],
|
||||
'answers.*' => ['required', 'string', 'in:yes,no'],
|
||||
'answers.*' => ['required', 'string', 'in:yes,unknown,no'],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ public function messages(): array
|
||||
'answers.size' => 'All 10 screening questions must be answered.',
|
||||
'answers.*.required' => 'Each screening question must have an answer.',
|
||||
'answers.*.string' => 'Each answer must be a valid text value.',
|
||||
'answers.*.in' => 'Each answer must be either "yes" or "no".',
|
||||
'answers.*.in' => 'Each answer must be "yes", "I don\'t know", or "no".',
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
42
app/Models/Config.php
Normal file
42
app/Models/Config.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
final class Config extends Model
|
||||
{
|
||||
/** The primary key column for config entries is a string key, not an auto-incrementing integer. */
|
||||
protected $primaryKey = 'key';
|
||||
|
||||
/** Disable auto-incrementing since the primary key is a string. */
|
||||
public $incrementing = false;
|
||||
|
||||
/** The primary key type is a string. */
|
||||
protected $keyType = 'string';
|
||||
|
||||
/** Allow mass assignment on all columns. */
|
||||
protected $guarded = [];
|
||||
|
||||
/**
|
||||
* Returns the attribute cast definitions for this model,
|
||||
* ensuring json_value is always hydrated as a PHP array.
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'json_value' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Scope to filter config records by their string key identifier.
|
||||
*/
|
||||
public function scopeForKey(Builder $query, string $key): Builder
|
||||
{
|
||||
return $query->where('key', $key);
|
||||
}
|
||||
}
|
||||
139
app/Nova/ConfigResource.php
Normal file
139
app/Nova/ConfigResource.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Nova;
|
||||
|
||||
use Laravel\Nova\Fields\DateTime;
|
||||
use Laravel\Nova\Fields\Text;
|
||||
use Laravel\Nova\Http\Requests\NovaRequest;
|
||||
|
||||
final class ConfigResource extends Resource
|
||||
{
|
||||
/**
|
||||
* The model the resource corresponds to.
|
||||
*
|
||||
* @var class-string<\App\Models\Config>
|
||||
*/
|
||||
public static string $model = \App\Models\Config::class;
|
||||
|
||||
/**
|
||||
* The columns that should be searched.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $search = ['key'];
|
||||
|
||||
/**
|
||||
* The logical group associated with the resource.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public static $group = 'Other';
|
||||
|
||||
/**
|
||||
* Get the displayable label of the resource.
|
||||
*/
|
||||
public static function label(): string
|
||||
{
|
||||
return 'Settings';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the displayable singular label of the resource.
|
||||
*/
|
||||
public static function singularLabel(): string
|
||||
{
|
||||
return 'Setting';
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the display title from the config class title or falls back to the raw key.
|
||||
*/
|
||||
public function title(): string
|
||||
{
|
||||
return $this->getConfigClass()?->title ?? $this->resource->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the fields displayed by the resource.
|
||||
*
|
||||
* @return array<int, \Laravel\Nova\Fields\Field|\Laravel\Nova\Panel|\Laravel\Nova\ResourceTool|\Illuminate\Http\Resources\MergeValue>
|
||||
*/
|
||||
public function fields(NovaRequest $request): array
|
||||
{
|
||||
return array_merge(
|
||||
[
|
||||
Text::make('Name', 'key')
|
||||
->resolveUsing(fn ($value) => $this->getConfigClass()?->title ?? $value)
|
||||
->exceptOnForms(),
|
||||
|
||||
Text::make('Description', 'key')
|
||||
->resolveUsing(fn () => $this->getConfigClass()?->description ?? '')
|
||||
->exceptOnForms(),
|
||||
],
|
||||
array_map(
|
||||
fn ($field) => $field->hideFromIndex(),
|
||||
$this->getConfigClass()?->getNovaFields() ?? []
|
||||
),
|
||||
[
|
||||
DateTime::make('Created At')->exceptOnForms()->hideFromIndex()->sortable()->filterable(),
|
||||
DateTime::make('Updated At')->exceptOnForms()->hideFromIndex()->sortable()->filterable(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the cards available for the request.
|
||||
*
|
||||
* @return array<int, \Laravel\Nova\Card>
|
||||
*/
|
||||
public function cards(NovaRequest $request): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the filters available for the resource.
|
||||
*
|
||||
* @return array<int, \Laravel\Nova\Filters\Filter>
|
||||
*/
|
||||
public function filters(NovaRequest $request): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lenses available for the resource.
|
||||
*
|
||||
* @return array<int, \Laravel\Nova\Lenses\Lens>
|
||||
*/
|
||||
public function lenses(NovaRequest $request): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the actions available for the resource.
|
||||
*
|
||||
* @return array<int, \Laravel\Nova\Actions\Action>
|
||||
*/
|
||||
public function actions(NovaRequest $request): array
|
||||
{
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves the config class instance for this resource's key via the Config service.
|
||||
*/
|
||||
private function getConfigClass(): ?object
|
||||
{
|
||||
$key = $this->resource?->key;
|
||||
|
||||
if ($key === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return app(\App\Services\Config::class)->getConfigClassForGroup($key);
|
||||
}
|
||||
}
|
||||
71
app/Policies/ConfigPolicy.php
Normal file
71
app/Policies/ConfigPolicy.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Policies;
|
||||
|
||||
use App\Models\Config;
|
||||
use App\Models\User;
|
||||
|
||||
final class ConfigPolicy
|
||||
{
|
||||
/**
|
||||
* Determine whether the user can view any config records.
|
||||
* Config records are auto-created by the Config Service, so all users may view the list.
|
||||
*/
|
||||
public function viewAny(User $user): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can view a specific config record.
|
||||
*/
|
||||
public function view(User $user, Config $config): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can update the config record.
|
||||
* All users with Nova access may edit config values.
|
||||
*/
|
||||
public function update(User $user, Config $config): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can create config records.
|
||||
* Config records are auto-created by the Config Service — manual creation is not permitted.
|
||||
*/
|
||||
public function create(User $user): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can delete the config record.
|
||||
* Config records are managed by the Config Service and must not be manually deleted.
|
||||
*/
|
||||
public function delete(User $user, Config $config): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can restore the config record.
|
||||
*/
|
||||
public function restore(User $user, Config $config): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine whether the user can permanently delete the config record.
|
||||
*/
|
||||
public function forceDelete(User $user, Config $config): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,7 @@ final class AppServiceProvider extends ServiceProvider
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
//
|
||||
$this->app->singleton(\App\Services\Config::class);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
use App\Models\User;
|
||||
use App\Nova\CategoryResource;
|
||||
use App\Nova\ConfigResource;
|
||||
use App\Nova\Dashboards\Main;
|
||||
use App\Nova\LogResource;
|
||||
use App\Nova\QuestionGroupResource;
|
||||
@@ -46,6 +47,10 @@ public function boot(): void
|
||||
MenuSection::make('Users', [
|
||||
MenuItem::resource(\App\Nova\User::class),
|
||||
])->icon('users')->collapsible(),
|
||||
|
||||
MenuSection::make('Settings', [
|
||||
MenuItem::resource(ConfigResource::class),
|
||||
])->icon('cog')->collapsible(),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
126
app/Services/Config.php
Normal file
126
app/Services/Config.php
Normal file
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Config as ConfigModel;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
final class Config
|
||||
{
|
||||
/** Typed public property for the content group's disclaimer field. */
|
||||
public string $contentDisclaimer = '';
|
||||
|
||||
/** Indexed config class instances keyed by their group key string. */
|
||||
private array $configClasses = [];
|
||||
|
||||
/**
|
||||
* Auto-discovers Config Field classes from app/Configs/, loads or creates
|
||||
* their database rows, and populates typed public properties with cast values.
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->discoverConfigClasses();
|
||||
$this->hydrateProperties();
|
||||
}
|
||||
|
||||
/**
|
||||
* Scans app/Configs/ for PHP files, instantiates each class,
|
||||
* and indexes them by their key property.
|
||||
*/
|
||||
private function discoverConfigClasses(): void
|
||||
{
|
||||
$files = glob(app_path('Configs/*.php')) ?: [];
|
||||
|
||||
foreach ($files as $file) {
|
||||
$className = 'App\\Configs\\'.pathinfo($file, PATHINFO_FILENAME);
|
||||
$instance = new $className;
|
||||
$this->configClasses[$instance->key] = $instance;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterates over all discovered config classes, ensures a DB row exists,
|
||||
* resolves typed field values, and assigns them to public properties.
|
||||
*/
|
||||
private function hydrateProperties(): void
|
||||
{
|
||||
try {
|
||||
$dbRecords = ConfigModel::all()->keyBy('key');
|
||||
|
||||
foreach ($this->configClasses as $key => $instance) {
|
||||
$record = $this->ensureDbRecord($key, $dbRecords);
|
||||
$jsonValue = Arr::wrap((array) ($record->json_value ?? []));
|
||||
$this->assignFieldProperties($key, $instance, $jsonValue);
|
||||
}
|
||||
} catch (QueryException) {
|
||||
// Silently skip if configs table does not yet exist during migrations.
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures a database record exists for the given config key,
|
||||
* creating one with an empty json_value if it does not.
|
||||
*/
|
||||
private function ensureDbRecord(string $key, Collection $dbRecords): ConfigModel
|
||||
{
|
||||
if ($dbRecords->has($key)) {
|
||||
return $dbRecords->get($key);
|
||||
}
|
||||
|
||||
return ConfigModel::firstOrCreate(
|
||||
['key' => $key],
|
||||
['json_value' => ''],
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves each field value from json_value (with default fallback),
|
||||
* applies type casting, and assigns to the matching typed public property.
|
||||
*/
|
||||
private function assignFieldProperties(string $key, object $instance, array $jsonValue): void
|
||||
{
|
||||
foreach ($instance->getFieldKeys() as $fieldKey => $definition) {
|
||||
$type = Arr::get($definition, 'type', 'string');
|
||||
$rawValue = Arr::get($jsonValue, $fieldKey);
|
||||
|
||||
if ($rawValue === null || $rawValue === '') {
|
||||
$rawValue = $instance->getDefault($fieldKey);
|
||||
}
|
||||
|
||||
$castedValue = $this->castValue($rawValue, $type);
|
||||
$propertyName = Str::camel($key).Str::studly($fieldKey);
|
||||
|
||||
if (property_exists($this, $propertyName)) {
|
||||
$this->{$propertyName} = $castedValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Casts a raw value to the PHP type defined by the config field type string.
|
||||
*/
|
||||
private function castValue(mixed $value, string $type): mixed
|
||||
{
|
||||
return match ($type) {
|
||||
'string', 'markdown' => (string) $value,
|
||||
'int', 'integer' => (int) $value,
|
||||
'bool', 'boolean' => (bool) $value,
|
||||
'float' => (float) $value,
|
||||
default => $value,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Config Field class instance for a given group key,
|
||||
* or null when no class has been discovered for that key.
|
||||
*/
|
||||
public function getConfigClassForGroup(string $group): ?object
|
||||
{
|
||||
return Arr::get($this->configClasses, $group);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user