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;
if ($bookId) {
$book = Book::with(['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'])
->find($bookId);
$book = Book::with(['divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])->find($bookId);
if (! $book) return $this->success([], 'Book not found');
if (! $book) {
return $this->success([], 'Book not found');
}
$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');
}
$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);
$division = Division::with(['sections', 'chapters', 'parts', 'gate', 'branch', 'art'])->find($division_id);
if (! $division) return $this->success([], 'Division not found');
if (! $division) {
return $this->success([], 'Division not found');
}
$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');
}
$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);
$section = Section::with(['chapters', 'parts', 'gate', 'branch', 'art'])->find($section_id);
if (! $section) return $this->success([], 'Section not found');
if (! $section) {
return $this->success([], 'Section not found');
}
$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');
}
$data = $this->checkRelations($section, ['chapters', 'parts', 'gate', 'branch', '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');
}
$volume = Volum::with(['book', 'divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])->find($volumeId);
if (! $volume) return $this->success([], 'Volume not found');
$data = [];
$foundRelation = false;
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');
}
$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);
$chapter = Chapter::with(['parts', 'gate', 'branch', 'art'])->find($chapter_id);
if (! $chapter) return $this->success([], 'Chapter not found');
if (! $chapter) {
return $this->success([], 'Chapter not found');
}
$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');
}
$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);
$part = Part::with(['gate', 'branch', 'art'])->find($part_id);
if (! $part) return $this->success([], 'Part not found');
if (! $part) {
return $this->success([], 'Part not found');
}
$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');
}
$data = $this->checkRelations($part, ['gate', 'branch', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($gate_id) {
$gate = Gate::with(['branch', 'art'])
->find($gate_id);
$gate = Gate::with(['branch', 'art'])->find($gate_id);
if (! $gate) return $this->success([], 'Gate not found');
if (! $gate) {
return $this->success([], 'Gate not found');
}
$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');
}
$data = $this->checkRelations($gate, ['branch', 'art'], $perPage);
return $this->success($data ?? [], 'Success');
}
if ($branch_id) {
$branch = Branch::with(['art'])
->find($branch_id);
$branch = Branch::with(['art'])->find($branch_id);
if (! $branch) return $this->success([], 'Branch not found');
if (! $branch) {
return $this->success([], 'Branch not found');
}
$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');
}
$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');
if (! $law) {
return $this->success([], 'Law not found');
}
$arts = Volum::query()->where('law_id', $law->id)->get();
$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,
]);
$data = [];
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) {
foreach ($arts as $item) {
$data[] = [
$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::where('id', $law->id)->first()?->is_locked,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : $law->is_locked,
];
}
})->toArray();
return $this->success($data, 'Success');
}
$data = [];
$arts = Art::query()
->where('law_id', $law->id)
->where('section_id', null)
->where('gate_id', null)
->where('part_id', null)
->where('chapter_id', null)
->where('book_id', null)
->where('volum_id', null)
->whereNull('section_id')->whereNull('gate_id')->whereNull('part_id')
->whereNull('chapter_id')->whereNull('book_id')->whereNull('volum_id')
->get();
foreach ($arts as $item) {
$data[] = [
$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::where('id', $law->id)->first()?->is_locked,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : $law->is_locked,
];
}
})->toArray();
return $this->success($data, 'Success');
}
@@ -521,9 +294,9 @@ class VolumController extends Controller
$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) {
@@ -532,7 +305,7 @@ class VolumController extends Controller
'title' => $item->title,
'number' => $item->number,
'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,
'law' => $item?->title,
'count_art' => $item->arts->count(),
@@ -540,134 +313,70 @@ class VolumController extends Controller
'price' => $item->price,
];
}
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', '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 ($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');
}
if (! $gate) return $this->success([], 'Gate not found');
$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 ($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 ($branchId) {
$branch = Branch::with(['chapters', 'gates', 'art'])->find($branchId);
if (! $branch) {
return $this->success([], 'Branch not found');
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');
}
$data = $this->checkRelations($branch, ['chapters', 'gates', 'art'], $perPage);
if ($data) {
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');
}
return $this->success([], 'No related records found in the branch');
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');
@@ -675,16 +384,24 @@ class VolumController extends Controller
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) {
$law = Law::find($item->law_id);
$data[] = [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => $type,
'route' => $this->route($item, $item),
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('id', $item->law_id)->first()?->is_locked,
// 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,
@@ -699,13 +416,12 @@ class VolumController extends Controller
private function checkRelations($model, $relations, $perPage)
{
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);
return $this->prepareItemsData($items, $relation);
}
}
return null;
}
@@ -714,14 +430,10 @@ class VolumController extends Controller
switch ($var) {
case 1:
return 'hagigi';
break;
case 2:
return 'kifari';
break;
default:
return 'kifari';
break;
}
}
@@ -823,5 +535,4 @@ class VolumController extends Controller
return array_values($route);
}
}