Files
law-api/app/Http/Controllers/api/VolumController.php
2026-08-18 14:35:18 +03:30

539 lines
21 KiB
PHP

<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\CheckBookFilterRequest;
use App\Http\Requests\CheckBookRequest;
use App\Http\Requests\VolumRequest;
use App\Models\Art;
use App\Models\Book;
use App\Models\Branch;
use App\Models\Category;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Gate;
use App\Models\Law;
use App\Models\Part;
use App\Models\RecentArt;
use App\Models\Section;
use App\Models\Volum;
use App\Traits\BaseApiResponse;
class VolumController extends Controller
{
use BaseApiResponse;
public function index(VolumRequest $request)
{
$validatedData = $request->validated();
$perPage = $validatedData['per_page'] ?? 15;
$page = $validatedData['page'] ?? 1;
$volumes = Volum::where('law_id', $validatedData['law_id'])->paginate($perPage, ['*'], 'page', $page);
$volumes->getCollection()->transform(function ($volume) {
$volume['has_book'] = Book::where('volum_id', $volume->id)->exists();
$volume['is_locked'] = auth()->user()->isSubscriber() !== false ? false : Law::where('id', $volume['law_id'])->first()?->is_locked;
unset($volume['law_id']);
return $volume;
});
return $this->success($volumes->items(), 'Success');
}
public function check(CheckBookRequest $request)
{
$bookId = $request->book_id;
$volumeId = $request->volume_id;
$law_Id = $request->law_id;
$section_id = $request->section_id;
$division_id = $request->division_id;
$part_id = $request->part_id;
$branch_id = $request->branch_id;
$chapter_id = $request->chapter_id;
$gate_id = $request->gate_id;
$perPage = $request->per_page ?? 10;
if ($bookId) {
$book = Book::with(['divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])->find($bookId);
if (! $book) return $this->success([], 'Book not found');
$data = $this->checkRelations($book, ['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
return $this->success($data ?? [], 'Success'); // Force return to prevent fall-through
}
if ($division_id) {
$division = Division::with(['sections', 'chapters', 'parts', 'gate', 'branch', 'art'])->find($division_id);
if (! $division) return $this->success([], 'Division not found');
$data = $this->checkRelations($division, ['sections', 'chapters', 'parts', 'gate', 'branch', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($section_id) {
$section = Section::with(['chapters', 'parts', 'gate', 'branch', 'art'])->find($section_id);
if (! $section) return $this->success([], 'Section not found');
$data = $this->checkRelations($section, ['chapters', 'parts', 'gate', 'branch', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($volumeId) {
$volume = Volum::with(['book', 'divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])->find($volumeId);
if (! $volume) return $this->success([], 'Volume not found');
$data = $this->checkRelations($volume, ['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($chapter_id) {
$chapter = Chapter::with(['parts', 'gate', 'branch', 'art'])->find($chapter_id);
if (! $chapter) return $this->success([], 'Chapter not found');
$data = $this->checkRelations($chapter, ['parts', 'gate', 'branch', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($part_id) {
$part = Part::with(['gate', 'branch', 'art'])->find($part_id);
if (! $part) return $this->success([], 'Part not found');
$data = $this->checkRelations($part, ['gate', 'branch', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($gate_id) {
$gate = Gate::with(['branch', 'art'])->find($gate_id);
if (! $gate) return $this->success([], 'Gate not found');
$data = $this->checkRelations($gate, ['branch', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($branch_id) {
$branch = Branch::with(['art'])->find($branch_id);
if (! $branch) return $this->success([], 'Branch not found');
$data = $this->checkRelations($branch, ['art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($law_Id) {
$law = Law::find($law_Id);
if (! $law) return $this->success([], 'Law not found');
$arts = Volum::query()->where('law_id', $law->id)->get();
RecentArt::query()->updateOrCreate(
['user_id' => auth()->user()->id, 'law_id' => $law_Id],
['user_id' => auth()->user()->id, 'law_id' => $law_Id]
);
if (count($arts) > 0) {
$data = $arts->map(function ($item) use ($law) {
return [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => 'volume',
'is_locked' => auth()->user()->isSubscriber() !== false ? false : $law->is_locked,
];
})->toArray();
return $this->success($data, 'Success');
}
$arts = Art::query()
->where('law_id', $law->id)
->whereNull('section_id')->whereNull('gate_id')->whereNull('part_id')
->whereNull('chapter_id')->whereNull('book_id')->whereNull('volum_id')
->get();
$data = $arts->map(function ($item) use ($law) {
return [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => 'art',
'is_locked' => auth()->user()->isSubscriber() !== false ? false : $law->is_locked,
];
})->toArray();
return $this->success($data, 'Success');
}
return $this->success([], 'No law or book or volume specified');
}
public function check_filter(CheckBookFilterRequest $request)
{
$filters = [
'category_id' => [
'model' => Law::class,
'foreign_key' => 'category_id',
'type' => (new Law)->getTable(),
'check_mode' => Volum::class,
'check_key' => 'law_id',
],
'law_id' => [
'model' => Volum::class,
'foreign_key' => 'law_id',
'type' => (new Volum)->getTable(),
'check_mode' => Book::class,
'check_key' => 'volum_id',
],
'volume_id' => [
'model' => Book::class,
'foreign_key' => 'volum_id',
'type' => (new Book)->getTable(),
'check_mode' => Division::class,
'check_key' => 'book_id',
],
'book_id' => [
'model' => Division::class,
'foreign_key' => 'book_id',
'type' => (new Division)->getTable(),
'check_mode' => Section::class,
'check_key' => 'division_id',
],
'division_id' => [
'model' => Section::class,
'foreign_key' => 'division_id',
'type' => 'sections',
'check_mode' => Chapter::class,
'check_key' => 'section_id',
],
'section_id' => [
'model' => Chapter::class,
'foreign_key' => 'section_id',
'type' => (new Chapter)->getTable(),
'check_mode' => Part::class,
'check_key' => 'chapter_id',
],
'chapter_id' => [
'model' => Part::class,
'foreign_key' => 'chapter_id',
'type' => (new Part)->getTable(),
'check_mode' => Gate::class,
'check_key' => 'part_id',
],
'part_id' => [
'model' => Gate::class,
'foreign_key' => 'part_id',
'type' => (new Gate)->getTable(),
'check_mode' => Branch::class,
'check_key' => 'gate_id',
],
'gate_id' => [
'model' => Branch::class,
'foreign_key' => 'gate_id',
'type' => (new Branch)->getTable(),
'check_mode' => Art::class,
'check_key' => 'branch_id',
],
'branch_id' => [
'model' => Art::class,
'foreign_key' => 'branch_id',
'type' => (new Art)->getTable(),
'check_mode' => Art::class,
'check_key' => 'branch_id',
],
];
foreach ($filters as $filterKey => $filterInfo) {
$filterValue = $request->get($filterKey);
if ($filterValue) {
$model = $filterInfo['model'];
$foreignKey = $filterInfo['foreign_key'];
$type = $filterInfo['type'];
$check_model = $filterInfo['check_mode'];
$check_key = $filterInfo['check_key'];
$items = $model::where($foreignKey, $filterValue)->paginate(10);
if ($filterKey === 'category_id') {
$items = $model::whereIn('category_id', Category::where('type', $this->convertValueTo($filterValue))->get()->pluck('id'))->paginate(10);
}
if ($foreignKey == 'branch_id') {
return $this->success([], '');
}
$data = $items->map(function ($item) use ($type, $check_model, $check_key) {
return [
'id' => $item?->id,
'title' => $item?->title,
'number' => $item?->number,
'type' => $type,
'is_end' => $check_model::where($check_key, $item?->id)->count() !== 0 ? false : true,
];
});
return $this->success($data, 'Success');
}
}
return $this->success([], 'No law or book or volume specified');
}
public function check_filter_with_art(CheckBookFilterRequest $request)
{
$lawId = $request->law_id;
$bookId = $request->book_id;
$volumeId = $request->volume_id;
$sectionId = $request->section_id;
$divisionId = $request->division_id;
$partId = $request->part_id;
$branchId = $request->branch_id;
$chapterId = $request->chapter_id;
$gateId = $request->gate_id;
$categoryId = $request->category_id;
$perPage = $request->per_page ?? 10;
if ($categoryId) {
$categories = Category::with(['laws'])->where('type', $this->convertValueTo($categoryId))->get()->pluck('id');
$items = Law::query()->whereIn('category_id', $categories)->get();
$data = [];
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => 'laws',
'is_locked' => auth()->user()->isSubscriber() !== false ? false : $item->is_locked,
'image' => $item?->image,
'law' => $item?->title,
'count_art' => $item->arts->count(),
'count_volums' => $item->volums->count(),
'price' => $item->price,
];
}
return $this->success($data, 'Success');
}
if ($branchId) {
$branch = Branch::with(['gates', 'chapters', 'art'])->find($branchId);
if (! $branch) return $this->success([], 'Branch not found');
$data = $this->checkRelations($branch, ['gates', 'chapters', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($gateId) {
$gate = Gate::with(['art'])->find($gateId);
if (! $gate) return $this->success([], 'Gate not found');
$data = $this->checkRelations($gate, ['art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($partId) {
$part = Part::with(['gates', 'art'])->find($partId);
if (! $part) return $this->success([], 'Part not found');
$data = $this->checkRelations($part, ['gates', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($chapterId) {
$chapter = Chapter::with(['gate', 'parts', 'art'])->find($chapterId);
if (! $chapter) return $this->success([], 'Chapter not found');
$data = $this->checkRelations($chapter, ['gate', 'parts', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($sectionId) {
$section = Section::with(['chapters', 'parts', 'gates', 'branchs', 'art'])->find($sectionId);
if (! $section) return $this->success([], 'Section not found');
$data = $this->checkRelations($section, ['chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($divisionId) {
$division = Division::with(['chapters', 'parts', 'gates', 'branchs', 'art'])->find($divisionId);
if (! $division) return $this->success([], 'Division not found');
$data = $this->checkRelations($division, ['chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($bookId) {
$book = Book::with(['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'])->find($bookId);
if (! $book) return $this->success([], 'Book not found');
$data = $this->checkRelations($book, ['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($volumeId) {
$volume = Volum::with(['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'])->find($volumeId);
if (! $volume) return $this->success([], 'Volume not found');
$data = $this->checkRelations($volume, ['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($lawId) {
$law = Law::with(['volums', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'arts'])->find($lawId);
if (! $law) return $this->success([], 'Law not found');
$data = $this->checkRelations($law, ['volums', 'book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'arts'], $perPage);
return $this->success($data ?? [], 'Success');
}
return $this->success([], 'No valid filter specified');
}
private function prepareItemsData($items, $type)
{
if (count($items) === 0) return [];
// Optimize: Calculate route only once for the first item
$firstItem = $items[0];
$sharedRoute = $this->route(get_class($firstItem), $firstItem);
$law = Law::find($firstItem->law_id);
$isLocked = auth()->user()->isSubscriber() !== false ? false : $law?->is_locked;
$data = [];
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
// Ensure correct frontend naming
'type' => in_array($type, ['art', 'arts']) ? 'art' : $type,
'route' => $sharedRoute,
'is_locked' => $isLocked,
'law' => $law?->title,
'image' => $law?->image,
'count_art' => $law?->arts?->count() ?? 0,
'count_volums' => $law?->volums?->count() ?? 0,
'price' => $law?->price,
];
}
return $data;
}
private function checkRelations($model, $relations, $perPage)
{
foreach ($relations as $relation) {
// Safety check: ensure relation exists on model
if (method_exists($model, $relation) && $model->{$relation}()->count() > 0) {
$items = $model->{$relation}()->paginate($perPage);
return $this->prepareItemsData($items, $relation);
}
}
return null;
}
private function convertValueTo($var)
{
switch ($var) {
case 1:
return 'hagigi';
case 2:
return 'kifari';
default:
return 'kifari';
}
}
private function route($modelClass, $query)
{
$route = [];
switch ((new $modelClass)->getTable()) {
case 'laws':
$route = array_filter([
Law::find($query->id)?->title,
]);
break;
case 'volums':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->id)?->title,
]);
break;
case 'books':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
]);
break;
case 'divisions':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
]);
break;
case 'sections':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
]);
break;
case 'chapters':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
Section::find($query->section_id)?->title,
]);
break;
case 'parts':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
Section::find($query->section_id)?->title,
Chapter::find($query->chapter_id)?->title,
]);
break;
case 'gates':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
Section::find($query->section_id)?->title,
Chapter::find($query->chapter_id)?->title,
Part::find($query->part_id)?->title,
]);
break;
case 'branches':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
Section::find($query->section_id)?->title,
Chapter::find($query->chapter_id)?->title,
Part::find($query->part_id)?->title,
Gate::find($query->gate_id)?->title,
]);
break;
case 'art':
$route = array_filter([
Law::find($query->law_id)?->title,
Volum::find($query->volum_id)?->title,
Book::find($query->book_id)?->title,
Division::find($query->division_id)?->title,
Section::find($query->section_id)?->title,
Chapter::find($query->chapter_id)?->title,
Part::find($query->part_id)?->title,
Gate::find($query->gate_id)?->title,
Branch::find($query->branch_id)?->title,
]);
break;
default:
$route = [];
break;
}
return array_values($route);
}
}