migrate searchbox to mode related

This commit is contained in:
xiaomlove
2022-09-13 19:21:05 +08:00
parent cee206df4c
commit f2c4a7d30f
12 changed files with 168 additions and 30 deletions
@@ -10,6 +10,8 @@ class CreateCategoryIcon extends CreateRecord
{
protected static string $resource = CategoryIconResource::class;
protected static string $view = 'filament.resources.system.category-icon-resource.pages.create-record';
protected function mutateFormDataBeforeCreate(array $data): array
{
return array_filter($data);
@@ -19,4 +21,11 @@ class CreateCategoryIcon extends CreateRecord
{
return static::$resource::getUrl('index');
}
protected function getViewData(): array
{
return [
'desc' => nexus_trans('label.icon.desc')
];
}
}
@@ -10,6 +10,8 @@ class EditCategoryIcon extends EditRecord
{
protected static string $resource = CategoryIconResource::class;
protected static string $view = 'filament.resources.system.category-icon-resource.pages.edit-record';
protected function getActions(): array
{
return [
@@ -26,4 +28,11 @@ class EditCategoryIcon extends EditRecord
{
return static::$resource::getUrl('index');
}
protected function getViewData(): array
{
return [
'desc' => nexus_trans('label.icon.desc')
];
}
}
@@ -17,4 +17,11 @@ class ListCategoryIcons extends PageList
Actions\CreateAction::make(),
];
}
protected function getViewData(): array
{
return [
'desc' => nexus_trans('label.icon.desc')
];
}
}
@@ -79,10 +79,27 @@ class SectionResource extends Resource
,
Forms\Components\Toggle::make('showsubcat')->label(__('label.search_box.showsubcat')),
Forms\Components\Section::make(__('label.search_box.showsubcat'))->schema([
Forms\Components\Repeater::make('extra.' . SearchBox::EXTRA_TAXONOMY_LABELS)->schema([
Forms\Components\Select::make('torrent_field')->options(SearchBox::getSubCatOptions())->label(__('label.search_box.torrent_field')),
Forms\Components\TextInput::make('display_text')->label(__('label.search_box.taxonomy_display_text')),
])->label(__('label.search_box.taxonomies'))->columns(2),
Forms\Components\Repeater::make('extra.' . SearchBox::EXTRA_TAXONOMY_LABELS)
->schema([
Forms\Components\Select::make('torrent_field')->options(SearchBox::getSubCatOptions())->label(__('label.search_box.torrent_field')),
Forms\Components\TextInput::make('display_text')->label(__('label.search_box.taxonomy_display_text')),
])
->label(__('label.search_box.taxonomies'))->columns(2)
->rules([
function () {
return function (string $attribute, $value, \Closure $fail) {
$fields = [];
foreach ($value as $item) {
if (!in_array($item['torrent_field'], $fields)) {
$fields[] = $item['torrent_field'];
} else {
$fail(__('label.search_box.torrent_field_duplicate', ['field' => $item['torrent_field']]));
}
}
};
}
])
,
]),
])->columns(3);
}
@@ -13,16 +13,7 @@ class CreateSection extends CreateRecord
protected function mutateFormDataBeforeCreate(array $data): array
{
foreach (SearchBox::$subCatFields as $field) {
$data["show{$field}"] = 0;
foreach ($data['extra'][SearchBox::EXTRA_TAXONOMY_LABELS] ?? [] as $item) {
if ($field == $item['torrent_field']) {
$data["show{$field}"] = 1;
$data["extra->" . SearchBox::EXTRA_TAXONOMY_LABELS][] = $item;
}
}
}
return $data;
return SearchBox::formatTaxonomyExtra($data);
}
}
@@ -20,16 +20,7 @@ class EditSection extends EditRecord
protected function mutateFormDataBeforeSave(array $data): array
{
foreach (SearchBox::$subCatFields as $field) {
$data["show{$field}"] = 0;
foreach ($data['extra'][SearchBox::EXTRA_TAXONOMY_LABELS] ?? [] as $item) {
if ($field == $item['torrent_field']) {
$data["show{$field}"] = 1;
$data["extra->" . SearchBox::EXTRA_TAXONOMY_LABELS][] = $item;
}
}
}
return $data;
return SearchBox::formatTaxonomyExtra($data);
}
}
+22 -5
View File
@@ -27,8 +27,14 @@ class SearchBox extends NexusModel
const EXTRA_DISPLAY_COVER_ON_TORRENT_LIST = 'display_cover_on_torrent_list';
const EXTRA_DISPLAY_SEED_BOX_ICON_ON_TORRENT_LIST = 'display_seed_box_icon_on_torrent_list';
public static array $subCatFields = [
'source', 'medium', 'codec', 'audiocodec', 'team', 'standard', 'processing'
public static array $subCategories = [
'source' => 'surces',
'medium' => 'media',
'codec' => 'codecs',
'audiocodec' => 'audiocodecs',
'team' => 'teams',
'standard' => 'standards',
'processing' => 'processings'
];
public static array $extras = [
@@ -45,9 +51,18 @@ class SearchBox extends NexusModel
return $result;
}
public static function getTaxonomyDisplayText($field)
public static function formatTaxonomyExtra(array $data): array
{
foreach (self::$subCategories as $field => $table) {
$data["show{$field}"] = 0;
foreach ($data['extra'][self::EXTRA_TAXONOMY_LABELS] ?? [] as $item) {
if ($field == $item['torrent_field']) {
$data["show{$field}"] = 1;
$data["extra->" . self::EXTRA_TAXONOMY_LABELS][] = $item;
}
}
}
return $data;
}
protected function customFields(): Attribute
@@ -60,7 +75,7 @@ class SearchBox extends NexusModel
public static function getSubCatOptions(): array
{
return array_combine(self::$subCatFields, self::$subCatFields);
return array_combine(array_keys(self::$subCategories), array_keys(self::$subCategories));
}
public function categories(): \Illuminate\Database\Eloquent\Relations\HasMany
@@ -115,4 +130,6 @@ class SearchBox extends NexusModel
}
+41
View File
@@ -6,7 +6,9 @@ use App\Models\Icon;
use App\Models\NexusModel;
use App\Models\SearchBox;
use App\Models\SearchBoxField;
use App\Models\Setting;
use Illuminate\Support\Arr;
use Nexus\Database\NexusDB;
class SearchBoxRepository extends BaseRepository
{
@@ -100,5 +102,44 @@ class SearchBoxRepository extends BaseRepository
return Icon::query()->find(array_keys($iconIdArr));
}
public static function migrateToModeRelated()
{
$secondIconTable = 'secondicons';
$normalId = Setting::get('main.browsecat');
$specialId = Setting::get('main.specialcat');
$searchBoxList = SearchBox::query()->get();
$tables = array_values(SearchBox::$subCategories);
foreach ($searchBoxList as $searchBox) {
if ($searchBox->id == $normalId) {
//all sub categories add `mode` field
foreach ($tables as $table) {
NexusDB::table($table)->update(['mode' => $normalId]);
do_log("update table $table mode = $normalId");
}
//also second icons
NexusDB::table($secondIconTable)->update(['mode' => $normalId]);
do_log("update table $secondIconTable mode = $normalId");
} elseif ($searchBox->id == $specialId && $specialId != $normalId) {
//copy from normal section
foreach ($tables as $table) {
$sql = sprintf(
"insert into `%s` (name, sort_index, mode) select name, sort_index, '%s' from `%s`",
$table, $specialId, $table
);
NexusDB::statement($sql);
do_log("copy table $table, $sql");
}
$fields = array_keys(SearchBox::$subCategories);
$fields = array_merge($fields, ['name', 'class_name', 'image']);
$fieldStr = implode(', ', $fields);
$sql = sprintf(
"insert into `%s` (%s, mode) select %s, '%s' from `%s`",
$secondIconTable, $fieldStr, $fieldStr, $specialId, $secondIconTable
);
NexusDB::statement($sql);
do_log("copy table $secondIconTable, $sql");
}
}
}
}
@@ -7,7 +7,7 @@ use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
private static array $tables = [
'sources', 'media', 'standards', 'codecs', 'audiocodecs', 'teams', 'processings'
'sources', 'media', 'standards', 'codecs', 'audiocodecs', 'teams', 'processings', 'secondicons'
];
/**
* Run the migrations.
+14
View File
@@ -247,6 +247,7 @@ return [
'custom_fields_display' => '自定义字段展示',
'custom_fields_display_help' => '使用特殊的标签代表字段的名称和值,如某字段其 Name 为 artist,则它的名称为:<%artist.label%>,它的值为:<%artist.value%>',
'category' => '主分类',
'torrent_field_duplicate' => '种子表字段::field 不能重复使用!',
'taxonomy' => [
'name' => '名称',
'sort_index' => '排序',
@@ -272,5 +273,18 @@ return [
'designer_help' => '此图标集的设计者。',
'comment' => '说明',
'comment_help' => '此图标集的说明。',
'desc' => "你必须将图标文件放入服务器正确的目录才能使得以下设定起作用。将普通的分类图标放入'pic/category/分类模式名字/图标文件夹[语言缩写/]',将第二图标放入'pic/category/分类模式名字/图标文件夹[语言缩写/]additional/'.不明白?参考下面的例子:
分类模式名字='nhd'
图标文件夹='scenetorrents/'
多语言='是'
第二图标='否'
你应该将一个英语的电影类型的图标(如'movies.png')文件放入'pic/category/nhd/scenetorrents/en/'。
分类模式名字='chd'
图标文件夹='nanosofts/'
多语言='否'
第二图标='是'
你应该将一个电影类型的图标(如'movies.png')文件放入'pic/category/chd/nanosofts/',将一个第二图标(如'bdh264.png')放入'pic/category/chd/nanosofts/additional/'。",
],
];
@@ -0,0 +1,17 @@
<x-filament::page
:class="\Illuminate\Support\Arr::toCssClasses([
'filament-resources-create-record-page',
'filament-resources-' . str_replace('/', '-', $this->getResource()::getSlug()),
])"
>
<x-filament::form wire:submit.prevent="create">
<div style="margin-bottom: 40px;white-space: pre-wrap">{!! $desc !!}</div>
<hr/>
{{ $this->form }}
<x-filament::form.actions
:actions="$this->getCachedFormActions()"
:full-width="$this->hasFullWidthFormActions()"
/>
</x-filament::form>
</x-filament::page>
@@ -0,0 +1,25 @@
<x-filament::page
:widget-data="['record' => $record]"
:class="\Illuminate\Support\Arr::toCssClasses([
'filament-resources-create-record-page',
'filament-resources-' . str_replace('/', '-', $this->getResource()::getSlug()),
'filament-resources-record-' . $record->getKey(),
])"
>
<x-filament::form wire:submit.prevent="save">
<div style="margin-bottom: 40px;white-space: pre-wrap">{!! $desc !!}</div>
<hr/>
{{ $this->form }}
<x-filament::form.actions
:actions="$this->getCachedFormActions()"
:full-width="$this->hasFullWidthFormActions()"
/>
</x-filament::form>
@if (count($relationManagers = $this->getRelationManagers()))
<x-filament::hr />
<x-filament::resources.relation-managers :active-manager="$activeRelationManager" :managers="$relationManagers" :owner-record="$record" :page-class="static::class" />
@endif
</x-filament::page>