Updated VolumController.php

This commit is contained in:
m.razaghi
2026-08-18 14:35:18 +03:30
parent 4c6eb52716
commit f84ef8c067

View File

@@ -59,335 +59,108 @@ class VolumController extends Controller
$perPage = $request->per_page ?? 10; $perPage = $request->per_page ?? 10;
if ($bookId) { if ($bookId) {
$book = Book::with(['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art']) $book = Book::with(['divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])->find($bookId);
->find($bookId); if (! $book) return $this->success([], 'Book not found');
if (! $book) { $data = $this->checkRelations($book, ['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
return $this->success([], 'Book not found'); return $this->success($data ?? [], 'Success'); // Force return to prevent fall-through
}
$data = [];
$foundRelation = false;
foreach (['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'] as $relation) {
if ($book->{$relation}->count() > 0) {
$foundRelation = true;
$items = $book->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
$paginationData = [
'next_page_url' => $items->nextPageUrl(),
];
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($division_id) { if ($division_id) {
$division = Division::with(['sections', 'chapters', 'parts', 'gate', 'branch', 'art']) $division = Division::with(['sections', 'chapters', 'parts', 'gate', 'branch', 'art'])->find($division_id);
->find($division_id); if (! $division) return $this->success([], 'Division not found');
if (! $division) { $data = $this->checkRelations($division, ['sections', 'chapters', 'parts', 'gate', 'branch', 'art'], $perPage);
return $this->success([], 'Division not found'); return $this->success($data ?? [], 'Success');
}
$data = [];
$foundRelation = false;
foreach (['sections', 'chapters', 'parts', 'gate', 'branch', 'art'] as $relation) {
if ($division->{$relation}->count() > 0) {
$foundRelation = true;
$items = $division->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($section_id) { if ($section_id) {
$section = Section::with(['chapters', 'parts', 'gate', 'branch', 'art']) $section = Section::with(['chapters', 'parts', 'gate', 'branch', 'art'])->find($section_id);
->find($section_id); if (! $section) return $this->success([], 'Section not found');
if (! $section) { $data = $this->checkRelations($section, ['chapters', 'parts', 'gate', 'branch', 'art'], $perPage);
return $this->success([], 'Section not found'); return $this->success($data ?? [], 'Success');
}
$data = [];
$foundRelation = false;
foreach (['chapters', 'parts', 'gate', 'branch', 'art'] as $relation) {
if ($section->{$relation}->count() > 0) {
$foundRelation = true;
$items = $section->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($volumeId) { if ($volumeId) {
$volume = Volum::with(['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art']) $volume = Volum::with(['book', 'divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])->find($volumeId);
->find($volumeId); if (! $volume) return $this->success([], 'Volume not found');
if (! $volume) {
return $this->success([], 'Volume not found');
}
$data = []; $data = $this->checkRelations($volume, ['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
$foundRelation = false; return $this->success($data ?? [], 'Success');
foreach (['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'] as $relation) {
if ($volume->{$relation}->count() > 0) {
$foundRelation = true;
$items = $volume->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($chapter_id) { if ($chapter_id) {
$chapter = Chapter::with(['parts', 'gate', 'branch', 'art']) $chapter = Chapter::with(['parts', 'gate', 'branch', 'art'])->find($chapter_id);
->find($chapter_id); if (! $chapter) return $this->success([], 'Chapter not found');
if (! $chapter) { $data = $this->checkRelations($chapter, ['parts', 'gate', 'branch', 'art'], $perPage);
return $this->success([], 'Chapter not found'); return $this->success($data ?? [], 'Success');
}
$data = [];
$foundRelation = false;
foreach (['parts', 'gate', 'branch', 'art'] as $relation) {
if ($chapter->{$relation}->count() > 0) {
$foundRelation = true;
$items = $chapter->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($part_id) { if ($part_id) {
$part = Part::with(['gate', 'branch', 'art']) $part = Part::with(['gate', 'branch', 'art'])->find($part_id);
->find($part_id); if (! $part) return $this->success([], 'Part not found');
if (! $part) { $data = $this->checkRelations($part, ['gate', 'branch', 'art'], $perPage);
return $this->success([], 'Part not found'); return $this->success($data ?? [], 'Success');
}
$data = [];
$foundRelation = false;
foreach (['gate', 'branch', 'art'] as $relation) {
if ($part->{$relation}->count() > 0) {
$foundRelation = true;
$items = $part->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($gate_id) { if ($gate_id) {
$gate = Gate::with(['branch', 'art']) $gate = Gate::with(['branch', 'art'])->find($gate_id);
->find($gate_id); if (! $gate) return $this->success([], 'Gate not found');
if (! $gate) { $data = $this->checkRelations($gate, ['branch', 'art'], $perPage);
return $this->success([], 'Gate not found'); return $this->success($data ?? [], 'Success');
}
$data = [];
$foundRelation = false;
foreach (['branch', 'art'] as $relation) {
if ($gate->{$relation}->count() > 0) {
$foundRelation = true;
$items = $gate->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($branch_id) { if ($branch_id) {
$branch = Branch::with(['art']) $branch = Branch::with(['art'])->find($branch_id);
->find($branch_id); if (! $branch) return $this->success([], 'Branch not found');
if (! $branch) { $data = $this->checkRelations($branch, ['art'], $perPage);
return $this->success([], 'Branch not found'); return $this->success($data ?? [], 'Success');
}
$data = [];
$foundRelation = false;
foreach (['art'] as $relation) {
if ($branch->{$relation}->count() > 0) {
$foundRelation = true;
$items = $branch->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $relation,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
];
}
break;
}
}
if ($foundRelation) {
return $this->success($data, 'Success');
} else {
return $this->success([], 'No relations found');
}
} }
if ($law_Id) { if ($law_Id) {
$law = Law::find($law_Id); $law = Law::find($law_Id);
if (! $law) return $this->success([], 'Law not found');
if (! $law) { $arts = Volum::query()->where('law_id', $law->id)->get();
return $this->success([], 'Law not found');
}
$arts = Volum::query() RecentArt::query()->updateOrCreate(
->where('law_id', $law->id) ['user_id' => auth()->user()->id, 'law_id' => $law_Id],
->get(); ['user_id' => auth()->user()->id, 'law_id' => $law_Id]
);
RecentArt::query()->updateOrCreate([
'user_id' => auth()->user()->id,
'law_id' => $law_Id,
], [
'user_id' => auth()->user()->id,
'law_id' => $law_Id,
]);
$data = [];
if (count($arts) > 0) { if (count($arts) > 0) {
foreach ($arts as $item) { $data = $arts->map(function ($item) use ($law) {
$data[] = [ return [
'id' => $item->id, 'id' => $item->id,
'title' => $item->title, 'title' => $item->title,
'number' => $item->number, 'number' => $item->number,
'type' => 'volume', 'type' => 'volume',
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $law->id)->first()?->is_locked, 'is_locked' => auth()->user()->isSubscriber() !== false ? false : $law->is_locked,
]; ];
} })->toArray();
return $this->success($data, 'Success'); return $this->success($data, 'Success');
} }
$data = [];
$arts = Art::query() $arts = Art::query()
->where('law_id', $law->id) ->where('law_id', $law->id)
->where('section_id', null) ->whereNull('section_id')->whereNull('gate_id')->whereNull('part_id')
->where('gate_id', null) ->whereNull('chapter_id')->whereNull('book_id')->whereNull('volum_id')
->where('part_id', null)
->where('chapter_id', null)
->where('book_id', null)
->where('volum_id', null)
->get(); ->get();
foreach ($arts as $item) { $data = $arts->map(function ($item) use ($law) {
$data[] = [ return [
'id' => $item->id, 'id' => $item->id,
'title' => $item->title, 'title' => $item->title,
'number' => $item->number, 'number' => $item->number,
'type' => 'art', 'type' => 'art',
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $law->id)->first()?->is_locked, 'is_locked' => auth()->user()->isSubscriber() !== false ? false : $law->is_locked,
]; ];
} })->toArray();
return $this->success($data, 'Success'); return $this->success($data, 'Success');
} }
@@ -521,9 +294,9 @@ class VolumController extends Controller
$categoryId = $request->category_id; $categoryId = $request->category_id;
$perPage = $request->per_page ?? 10; $perPage = $request->per_page ?? 10;
if ($categoryId) { if ($categoryId) {
$categories = Category::with(['laws'])->where('type', $this->convertValueTo($categoryId))->get()->pluck('id'); $categories = Category::with(['laws'])->where('type', $this->convertValueTo($categoryId))->get()->pluck('id');
$items = Law::query()->whereIn('category_id', $categories)->get(); $items = Law::query()->whereIn('category_id', $categories)->get();
$data = []; $data = [];
foreach ($items as $item) { foreach ($items as $item) {
@@ -532,7 +305,7 @@ class VolumController extends Controller
'title' => $item->title, 'title' => $item->title,
'number' => $item->number, 'number' => $item->number,
'type' => 'laws', 'type' => 'laws',
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->id)->first()?->is_locked, 'is_locked' => auth()->user()->isSubscriber() !== false ? false : $item->is_locked,
'image' => $item?->image, 'image' => $item?->image,
'law' => $item?->title, 'law' => $item?->title,
'count_art' => $item->arts->count(), 'count_art' => $item->arts->count(),
@@ -540,134 +313,70 @@ class VolumController extends Controller
'price' => $item->price, 'price' => $item->price,
]; ];
} }
return $this->success($data, 'Success'); return $this->success($data, 'Success');
} }
if ($lawId) { if ($branchId) {
$law = Law::with(['volums', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'arts'])->find($lawId); $branch = Branch::with(['gates', 'chapters', 'art'])->find($branchId);
if (! $law) { if (! $branch) return $this->success([], 'Branch not found');
return $this->success([], 'Law not found'); $data = $this->checkRelations($branch, ['gates', 'chapters', 'art'], $perPage);
} return $this->success($data ?? [], 'Success');
$data = $this->checkRelations($law, ['volums', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'arts'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the law');
}
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);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the volume');
}
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);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the book');
}
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);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the division');
}
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);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the section');
}
if ($chapterId) {
$chapter = Chapter::with(['parts', 'gate', 'art'])->find($chapterId);
if (! $chapter) {
return $this->success([], 'Chapter not found');
}
$data = $this->checkRelations($chapter, ['parts', 'gate', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the chapter');
}
if ($partId) {
$part = Part::with(['gates', 'art'])->find($partId);
if (! $part) {
return $this->success([], 'Part not found');
}
$data = $this->checkRelations($part, ['gates', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the part');
} }
if ($gateId) { if ($gateId) {
$gate = Gate::with(['art'])->find($gateId); $gate = Gate::with(['art'])->find($gateId);
if (! $gate) { if (! $gate) return $this->success([], 'Gate not found');
return $this->success([], 'Gate not found');
}
$data = $this->checkRelations($gate, ['art'], $perPage); $data = $this->checkRelations($gate, ['art'], $perPage);
if ($data) { return $this->success($data ?? [], 'Success');
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the gate');
} }
if ($branchId) { if ($partId) {
$branch = Branch::with(['chapters', 'gates', 'art'])->find($branchId); $part = Part::with(['gates', 'art'])->find($partId);
if (! $branch) { if (! $part) return $this->success([], 'Part not found');
return $this->success([], 'Branch not found'); $data = $this->checkRelations($part, ['gates', 'art'], $perPage);
} return $this->success($data ?? [], 'Success');
}
$data = $this->checkRelations($branch, ['chapters', 'gates', 'art'], $perPage); if ($chapterId) {
if ($data) { $chapter = Chapter::with(['gate', 'parts', 'art'])->find($chapterId);
return $this->success($data, 'Success'); if (! $chapter) return $this->success([], 'Chapter not found');
} $data = $this->checkRelations($chapter, ['gate', 'parts', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
return $this->success([], 'No related records found in the branch'); 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'); return $this->success([], 'No valid filter specified');
@@ -675,16 +384,24 @@ class VolumController extends Controller
private function prepareItemsData($items, $type) 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 = []; $data = [];
foreach ($items as $item) { foreach ($items as $item) {
$law = Law::find($item->law_id);
$data[] = [ $data[] = [
'id' => $item->id, 'id' => $item->id,
'title' => $item->title, 'title' => $item->title,
'number' => $item->number, 'number' => $item->number,
'type' => $type, // Ensure correct frontend naming
'route' => $this->route($item, $item), 'type' => in_array($type, ['art', 'arts']) ? 'art' : $type,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked, 'route' => $sharedRoute,
'is_locked' => $isLocked,
'law' => $law?->title, 'law' => $law?->title,
'image' => $law?->image, 'image' => $law?->image,
'count_art' => $law?->arts?->count() ?? 0, 'count_art' => $law?->arts?->count() ?? 0,
@@ -699,13 +416,12 @@ class VolumController extends Controller
private function checkRelations($model, $relations, $perPage) private function checkRelations($model, $relations, $perPage)
{ {
foreach ($relations as $relation) { foreach ($relations as $relation) {
if ($model->{$relation} && $model->{$relation}->count() > 0) { // Safety check: ensure relation exists on model
if (method_exists($model, $relation) && $model->{$relation}()->count() > 0) {
$items = $model->{$relation}()->paginate($perPage); $items = $model->{$relation}()->paginate($perPage);
return $this->prepareItemsData($items, $relation); return $this->prepareItemsData($items, $relation);
} }
} }
return null; return null;
} }
@@ -714,14 +430,10 @@ class VolumController extends Controller
switch ($var) { switch ($var) {
case 1: case 1:
return 'hagigi'; return 'hagigi';
break;
case 2: case 2:
return 'kifari'; return 'kifari';
break;
default: default:
return 'kifari'; return 'kifari';
break;
} }
} }
@@ -823,5 +535,4 @@ class VolumController extends Controller
return array_values($route); return array_values($route);
} }
} }