migrate custom field management to filament

fix xss
This commit is contained in:
NekoCH
2025-12-28 22:36:52 +08:00
parent f18fa80eac
commit 00ec3d5e8d
11 changed files with 235 additions and 10 deletions

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Filament\Resources\TorrentCustomFields\Pages;
use App\Filament\Resources\TorrentCustomFields\TorrentCustomFieldResource;
use Filament\Actions\CreateAction;
use Filament\Resources\Pages\ListRecords;
class ListTorrentCustomFields extends ListRecords
{
protected static string $resource = TorrentCustomFieldResource::class;
protected function getHeaderActions(): array
{
return [
CreateAction::make(),
];
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Filament\Resources\TorrentCustomFields\Schemas;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Schemas\Schema;
use Nexus\Field\Field;
class TorrentCustomFieldForm
{
public static function configure(Schema $schema): Schema
{
return $schema
->components([
TextInput::make('name')
->label(__('label.field.name'))
->helperText(__('label.field.name_help'))
->alphaDash()
->required(),
TextInput::make('label')
->label(__('label.field.field_label'))
->required(),
Select::make('type')
->options((new Field())->getTypeRadioOptions())
->label(__('label.field.type'))
->required(),
Checkbox::make('required')
->label(__('label.field.required')),
Textarea::make('help')
->label(__('label.field.help'))
->rows(3),
Textarea::make('options')
->label(__('label.field.options'))
->rows(3)
->hiddenJs("\$get('type') !== 'radio' && \$get('type') !== 'checkbox' && \$get('type') !== 'select'")
->helperText(__('label.field.options_help')),
Checkbox::make('is_single_row')
->label(__('label.field.is_single_row')),
TextInput::make('priority')
->label(__('label.priority'))
->numeric(),
Textarea::make('display')
->label(__('label.field.display'))
->rows(3)
->helperText(__('label.search_box.custom_fields_display_help')),
])
->columns(1);
}
}

View File

@@ -0,0 +1,40 @@
<?php
namespace App\Filament\Resources\TorrentCustomFields\Tables;
use Filament\Actions\BulkActionGroup;
use Filament\Actions\DeleteAction;
use Filament\Actions\DeleteBulkAction;
use Filament\Actions\EditAction;
use Filament\Tables\Columns\IconColumn;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
class TorrentCustomFieldsTable
{
public static function configure(Table $table): Table
{
return $table
->columns([
TextColumn::make('id'),
TextColumn::make('name')->label(__('label.field.name')),
TextColumn::make('label')->label(__('label.field.field_label')),
TextColumn::make('type')->label(__('label.field.type')),
IconColumn::make('required')->boolean()->label(__('label.field.required')),
IconColumn::make('is_single_row')->boolean()->label(__('label.field.is_single_row')),
TextColumn::make('priority')->label(__('label.priority')),
])
->filters([
//
])
->recordActions([
EditAction::make(),
DeleteAction::make(),
])
->toolbarActions([
BulkActionGroup::make([
DeleteBulkAction::make(),
]),
]);
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Filament\Resources\TorrentCustomFields;
use App\Filament\Resources\TorrentCustomFields\Pages\ListTorrentCustomFields;
use App\Filament\Resources\TorrentCustomFields\Schemas\TorrentCustomFieldForm;
use App\Filament\Resources\TorrentCustomFields\Tables\TorrentCustomFieldsTable;
use App\Models\TorrentCustomField;
use BackedEnum;
use Filament\Resources\Resource;
use Filament\Schemas\Schema;
use Filament\Support\Icons\Heroicon;
use Filament\Tables\Table;
use UnitEnum;
class TorrentCustomFieldResource extends Resource
{
protected static ?string $model = TorrentCustomField::class;
protected static string|BackedEnum|null $navigationIcon = Heroicon::OutlinedRectangleStack;
protected static string|null|UnitEnum $navigationGroup = 'Section';
protected static ?int $navigationSort = 12;
public static function getNavigationLabel(): string
{
return __('label.field.label');
}
public static function getBreadcrumb(): string
{
return self::getNavigationLabel();
}
public static function form(Schema $schema): Schema
{
return TorrentCustomFieldForm::configure($schema);
}
public static function table(Table $table): Table
{
return TorrentCustomFieldsTable::configure($table);
}
public static function getRelations(): array
{
return [
//
];
}
public static function getPages(): array
{
return [
'index' => ListTorrentCustomFields::route('/'),
];
}
}

View File

@@ -16,6 +16,7 @@ use App\Models\SecondIcon;
use App\Models\Source;
use App\Models\Standard;
use App\Models\Team;
use App\Models\TorrentCustomField;
use App\Models\User;
use App\Policies\CodecPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
@@ -34,6 +35,7 @@ class AuthServiceProvider extends ServiceProvider
Category::class => CodecPolicy::class,
Icon::class => CodecPolicy::class,
SecondIcon::class => CodecPolicy::class,
TorrentCustomField::class => CodecPolicy::class,
Codec::class => CodecPolicy::class,
AudioCodec::class => CodecPolicy::class,

View File

@@ -55,14 +55,13 @@ class Field
public function getTypeHuman($type)
{
global $lang_fields;
$map = [
self::TYPE_TEXT => $lang_fields['field_type_text'],
self::TYPE_TEXTAREA => $lang_fields['field_type_textarea'],
self::TYPE_RADIO => $lang_fields['field_type_radio'],
self::TYPE_CHECKBOX => $lang_fields['field_type_checkbox'],
self::TYPE_SELECT => $lang_fields['field_type_select'],
self::TYPE_IMAGE => $lang_fields['field_type_image'],
self::TYPE_TEXT => nexus_trans('field.type.text'),
self::TYPE_TEXTAREA => nexus_trans('field.type.textarea'),
self::TYPE_RADIO => nexus_trans('field.type.radio'),
self::TYPE_CHECKBOX => nexus_trans('field.type.checkbox'),
self::TYPE_SELECT => nexus_trans('field.type.select'),
self::TYPE_IMAGE => nexus_trans('field.type.image'),
];
return $map[$type] ?? '';
}
@@ -439,7 +438,7 @@ JS;
$customFieldDisplay = $field['display'];
$customFieldDisplay = str_replace("<%{$field['name']}.label%>", $field['label'], $customFieldDisplay);
$customFieldDisplay = str_replace("<%{$field['name']}.value%>", $contentNotFormatted, $customFieldDisplay);
$rowByRowHtml .= tr($field['label'], format_comment($customFieldDisplay, false), 1);
$rowByRowHtml .= tr($field['label'], format_comment($customFieldDisplay), 1);
} else {
$contentFormatted = $this->formatCustomFieldValue($field, true);
$rowByRowHtml .= tr($field['label'], $contentFormatted, 1);
@@ -463,13 +462,13 @@ JS;
switch ($customFieldWithValue['type']) {
case self::TYPE_TEXT:
case self::TYPE_TEXTAREA:
$result .= $doFormatComment ? format_comment($fieldValue, false) : $fieldValue;
$result .= $doFormatComment ? format_comment($fieldValue) : $fieldValue;
break;
case self::TYPE_IMAGE:
if (substr($fieldValue, 0, 4) == 'http') {
$result .= $doFormatComment ? formatImg($fieldValue, true, 700, 0, "attach{$customFieldWithValue['id']}") : $fieldValue;
} else {
$result .= $doFormatComment ? format_comment($fieldValue, false) : $fieldValue;
$result .= $doFormatComment ? format_comment($fieldValue) : $fieldValue;
}
break;
case self::TYPE_RADIO:

View File

@@ -22,6 +22,7 @@ if ($action == 'view') {
begin_main_frame();
echo $field->buildFieldForm();
} elseif ($action == 'submit') {
die("This method is deprecated! This method is no longer available in 1.10, it does not save data correctly, please go to the management system!");
try {
$result = $field->save($_REQUEST);
nexus_redirect('fields.php?action=view');

View File

@@ -0,0 +1,12 @@
<?php
return [
'type' => [
'text' => 'Short text',
'textarea' => 'Long text',
'radio' => 'Horizontal single select',
'checkbox' => 'Horizontal multiple select',
'select' => 'Vertical single select',
'image' => 'Image',
],
];

View File

@@ -456,4 +456,18 @@ Note: In 1.8, the 'searchbox_name' part can be omitted, i.e. the rule is 'pic/ca
'select_section' => 'Selections',
'select_section_help' => "If a selection is not defined, all options from the selection are allowed for the rule. At least one selection should be defined.",
],
'field' => [
'label' => 'Custom field',
'name' => 'Name',
'name_help' => 'Only allow digit, alphabet, underline',
'field_label' => 'Display label',
'type' => 'Type',
'required' => 'Required',
'mod_only' => 'Mod only',
'help' => 'Help text',
'options' => 'Options',
'options_help' => 'Required when type is radio, checkbox, select. One line, one option, format: value|display text',
'is_single_row' => 'Display on a single row',
'display' => 'Custom display',
]
];

View File

@@ -0,0 +1,12 @@
<?php
return [
'type' => [
'text' => '短文本',
'textarea' => '长文本',
'radio' => '横向单选',
'checkbox' => '横向多选',
'select' => '下拉单选',
'image' => '图片',
],
];

View File

@@ -498,4 +498,18 @@ return [
'select_section' => '选择',
'select_section_help' => "如果某个选择未指定,其所有选项都符合此规则。必须至少指定一个选择。",
],
'field' => [
'label' => '自定义字段',
'name' => '名称',
'name_help' => '仅允许数字、字母、下划线',
'field_label' => '显示标签',
'type' => '类型',
'required' => '不能为空',
'mod_only' => '仅管理组可见',
'help' => '辅助说明',
'options' => '选项',
'options_help' => '类型为单选、多选、下拉时必填,一行一个,格式:选项值|选项描述文本',
'is_single_row' => '展示时单独一行',
'display' => '自定义展示',
]
];