API: upload sections list

This commit is contained in:
xiaomlove
2025-02-15 03:15:45 +08:00
parent 0d7cbcde9f
commit c9b2237efd
16 changed files with 262 additions and 10 deletions

View File

@@ -0,0 +1,44 @@
<?php
namespace App\Http\Resources;
use App\Models\SearchBox;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class SearchBoxResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
/** @var SearchBox $resource */
$searchBox = $this->resource;
$out = [
'id' => $this->id,
'name' => $this->displaySectionName,
'categories' => CategoryResource::collection($this->whenLoaded('categories')),
];
$subCategories = [];
$lang = get_langfolder_cookie();
$fields = array_keys(SearchBox::$taxonomies);
if (!empty($searchBox->extra['taxonomy_labels'])) {
$fields = array_column($searchBox->extra['taxonomy_labels'], 'torrent_field');
}
foreach ($fields as $field) {
$relationName = "taxonomy_$field";
if ($searchBox->relationLoaded($relationName)) {
$subCategories[] = [
'field' => $field,
'label' => $item['display_text'][$lang] ?? (nexus_trans("searchbox.sub_category_{$field}_label") ?: ucfirst($field)),
'data' => MediaResource::collection($searchBox->{$relationName}),
];
}
}
$out['sub_categories'] = $this->when($this->showsubcat, $subCategories);
return $out;
}
}