Skip to content

Commit c9b2237

Browse files
committed
API: upload sections list
1 parent 0d7cbcd commit c9b2237

File tree

16 files changed

+262
-10
lines changed

16 files changed

+262
-10
lines changed

app/Auth/Permission.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Auth;
4+
5+
use App\Enums\PermissionEnum;
6+
7+
class Permission
8+
{
9+
public static function canUploadToSpecialSection(): bool
10+
{
11+
return user_can(PermissionEnum::UPLOAD_TO_SPECIAL_SECTION->value);
12+
}
13+
}

app/Console/Commands/Test.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Console\Commands;
44

5+
use App\Enums\PermissionEnum;
56
use App\Events\TorrentUpdated;
67
use App\Filament\Resources\System\AgentAllowResource;
78
use App\Http\Resources\TagResource;
@@ -102,11 +103,10 @@ public function __construct()
102103
*/
103104
public function handle()
104105
{
105-
$today = Carbon::today();
106-
$yesterday = Carbon::yesterday();
107-
$tomorrow = Carbon::tomorrow();
108-
$diff = $tomorrow->diffInDays();
109-
dd($today, $tomorrow, $diff);
106+
$with = ["ss" => function($query) {$query->orWhere("mode", 0);}];
107+
$r = SearchBox::query()->with($with)->find(4);
108+
// $r = SearchBox::query()->find(4)->ss()->orWhere("mode", 0)->get();
109+
dd($r);
110110
}
111111

112112
}

app/Enums/PermissionEnum.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace App\Enums;
4+
5+
enum PermissionEnum: string {
6+
case UPLOAD_TO_SPECIAL_SECTION = 'uploadspecial';
7+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Resources\SearchBoxResource;
6+
use App\Repositories\SearchBoxRepository;
7+
use Illuminate\Http\Request;
8+
9+
class SearchBoxController extends Controller
10+
{
11+
private $repository;
12+
13+
public function __construct(SearchBoxRepository $repository)
14+
{
15+
$this->repository = $repository;
16+
}
17+
18+
}

app/Http/Controllers/ToolController.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Models\PluginStore;
66
use App\Repositories\ToolRepository;
7+
use App\Repositories\UploadRepository;
78
use Illuminate\Http\Request;
89
use Illuminate\Support\Facades\Auth;
910
use Symfony\Component\Process\Process;
@@ -30,7 +31,9 @@ public function notifications(): array
3031

3132
public function test(Request $request)
3233
{
33-
34+
$rep = new UploadRepository();
35+
$result = $rep->listSections();
36+
return $result;
3437
}
3538

3639
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace App\Http\Controllers;
4+
5+
use App\Http\Resources\SearchBoxResource;
6+
use App\Repositories\SearchBoxRepository;
7+
use App\Repositories\UploadRepository;
8+
use Illuminate\Http\Request;
9+
10+
class UploadController extends Controller
11+
{
12+
private $repository;
13+
14+
private $searchBoxRepository;
15+
16+
public function __construct(UploadRepository $repository, SearchBoxRepository $searchBoxRepository)
17+
{
18+
$this->repository = $repository;
19+
$this->searchBoxRepository = $searchBoxRepository;
20+
}
21+
22+
public function sections(Request $request)
23+
{
24+
$sections = $this->searchBoxRepository->listSections();
25+
$resource = SearchBoxResource::collection($sections);
26+
return $this->success($resource);
27+
}
28+
29+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace App\Http\Resources;
4+
5+
use App\Models\SearchBox;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Http\Resources\Json\JsonResource;
8+
9+
class SearchBoxResource extends JsonResource
10+
{
11+
/**
12+
* Transform the resource into an array.
13+
*
14+
* @return array<string, mixed>
15+
*/
16+
public function toArray(Request $request): array
17+
{
18+
/** @var SearchBox $resource */
19+
$searchBox = $this->resource;
20+
$out = [
21+
'id' => $this->id,
22+
'name' => $this->displaySectionName,
23+
'categories' => CategoryResource::collection($this->whenLoaded('categories')),
24+
];
25+
$subCategories = [];
26+
$lang = get_langfolder_cookie();
27+
$fields = array_keys(SearchBox::$taxonomies);
28+
if (!empty($searchBox->extra['taxonomy_labels'])) {
29+
$fields = array_column($searchBox->extra['taxonomy_labels'], 'torrent_field');
30+
}
31+
foreach ($fields as $field) {
32+
$relationName = "taxonomy_$field";
33+
if ($searchBox->relationLoaded($relationName)) {
34+
$subCategories[] = [
35+
'field' => $field,
36+
'label' => $item['display_text'][$lang] ?? (nexus_trans("searchbox.sub_category_{$field}_label") ?: ucfirst($field)),
37+
'data' => MediaResource::collection($searchBox->{$relationName}),
38+
];
39+
}
40+
}
41+
$out['sub_categories'] = $this->when($this->showsubcat, $subCategories);
42+
return $out;
43+
}
44+
}

app/Models/SearchBox.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models;
44

5+
use App\Http\Middleware\Locale;
56
use Illuminate\Database\Eloquent\Casts\Attribute;
67
use Illuminate\Database\Query\Builder;
78
use Illuminate\Support\Str;
@@ -182,6 +183,25 @@ public function setCustomFieldsAttribute($value)
182183
}
183184
}
184185

186+
public function getDisplaySectionNameAttribute()
187+
{
188+
$locale = Locale::getDefault();
189+
if (!empty($this->section_name[$locale])) {
190+
return $this->section_name[$locale];
191+
}
192+
$defaultLang = get_setting("main.defaultlang");
193+
if (!empty($this->section_name[$defaultLang])) {
194+
return $this->section_name[$defaultLang];
195+
}
196+
if ($this->isSectionBrowse()) {
197+
return nexus_trans("searchbox.sections.browse");
198+
}
199+
if ($this->isSectionSpecial()) {
200+
return nexus_trans("searchbox.sections.special");
201+
}
202+
return $this->name;
203+
}
204+
185205
public static function listSearchModes(): array
186206
{
187207
$result = [];
@@ -206,6 +226,16 @@ public static function getSpecialMode()
206226
return Setting::get('main.specialcat');
207227
}
208228

229+
public function isSectionBrowse(): bool
230+
{
231+
return $this->id == self::getBrowseMode();
232+
}
233+
234+
public function isSectionSpecial(): bool
235+
{
236+
return $this->id == self::getSpecialMode();
237+
}
238+
209239

210240
public function categories(): \Illuminate\Database\Eloquent\Relations\HasMany
211241
{
@@ -247,6 +277,17 @@ public function taxonomy_processing(): \Illuminate\Database\Eloquent\Relations\H
247277
return $this->hasMany(Processing::class, 'mode');
248278
}
249279

280+
public function loadSubCategories(): void
281+
{
282+
foreach (self::$taxonomies as $name => $info) {
283+
$relationName = "taxonomy_" . $name;
284+
$show = "show" . $name;
285+
if ($this->{$show}) {
286+
$this->setRelation($relationName, $this->{$relationName}()->orWhere('mode', 0)->get());
287+
}
288+
}
289+
}
290+
250291
public static function getDefaultSearchMode()
251292
{
252293
$meiliConf = get_setting("meilisearch");

app/Models/Setting.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,9 @@ public static function normalizeValue(Setting $setting)
100100
return $value;
101101
}
102102

103+
public static function getDefaultLang()
104+
{
105+
return self::get("main.defaultlang");
106+
}
107+
103108
}

app/Repositories/SearchBoxRepository.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Repositories;
44

5+
use App\Auth\Permission;
56
use App\Exceptions\InsufficientPermissionException;
67
use App\Http\Middleware\Locale;
78
use App\Models\Category;
@@ -242,5 +243,20 @@ public function deleteCategory($id)
242243
return Category::query()->whereIn('id', $idArr)->delete();
243244
}
244245

246+
public function listSections()
247+
{
248+
$modeIds = [SearchBox::getBrowseMode()];
249+
if (SearchBox::isSpecialEnabled() && Permission::canUploadToSpecialSection()) {
250+
$modeIds[] = SearchBox::getSpecialMode();
251+
}
252+
$searchBoxList = SearchBox::query()->with("categories")->find($modeIds);
253+
foreach ($searchBoxList as $searchBox) {
254+
if ($searchBox->showsubcat) {
255+
$searchBox->loadSubCategories();
256+
}
257+
}
258+
return $searchBoxList;
259+
}
260+
245261

246262
}

0 commit comments

Comments
 (0)