Updated VolumController.php

This commit is contained in:
m.razaghi
2026-08-18 13:39:05 +03:30
parent 464ed71360
commit 8b853a6f8f

View File

@@ -58,8 +58,8 @@ class VolumController extends Controller
$perPage = $request->per_page ?? 10;
if ($bookId) {
$book = Book::with(['divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])
// ترتیب درست اعمال شد
$book = Book::with(['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'])
->find($bookId);
if (! $book) {
@@ -68,7 +68,7 @@ class VolumController extends Controller
$data = [];
$foundRelation = false;
foreach (['divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'] as $relation) {
foreach (['divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'] as $relation) {
if ($book->{$relation}->count() > 0) {
$foundRelation = true;
$items = $book->{$relation}()->paginate($perPage);
@@ -96,7 +96,7 @@ class VolumController extends Controller
}
if ($division_id) {
$division = Division::with(['sections', 'chapters', 'parts', 'gate', 'branch', 'art'])
$division = Division::with(['sections', 'chapters', 'parts', 'gates', 'branchs', 'art'])
->find($division_id);
if (! $division) {
@@ -105,7 +105,7 @@ class VolumController extends Controller
$data = [];
$foundRelation = false;
foreach (['sections', 'chapters', 'parts', 'gate', 'branch', 'art'] as $relation) {
foreach (['sections', 'chapters', 'parts', 'gates', 'branchs', 'art'] as $relation) {
if ($division->{$relation}->count() > 0) {
$foundRelation = true;
$items = $division->{$relation}()->paginate($perPage);
@@ -130,7 +130,7 @@ class VolumController extends Controller
}
if ($section_id) {
$section = Section::with(['chapters', 'parts', 'gate', 'branch', 'art'])
$section = Section::with(['chapters', 'parts', 'gates', 'branchs', 'art'])
->find($section_id);
if (! $section) {
@@ -139,7 +139,7 @@ class VolumController extends Controller
$data = [];
$foundRelation = false;
foreach (['chapters', 'parts', 'gate', 'branch', 'art'] as $relation) {
foreach (['chapters', 'parts', 'gates', 'branchs', 'art'] as $relation) {
if ($section->{$relation}->count() > 0) {
$foundRelation = true;
$items = $section->{$relation}()->paginate($perPage);
@@ -164,7 +164,7 @@ class VolumController extends Controller
}
if ($volumeId) {
$volume = Volum::with(['book', 'divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])
$volume = Volum::with(['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'])
->find($volumeId);
if (! $volume) {
return $this->success([], 'Volume not found');
@@ -196,8 +196,42 @@ class VolumController extends Controller
}
}
if ($chapter_id) {
$chapter = Chapter::with(['parts', 'gates', 'branchs', 'art'])
->find($chapter_id);
if (! $chapter) {
return $this->success([], 'Chapter not found');
}
$data = [];
$foundRelation = false;
foreach (['parts', 'gates', 'branchs', '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) {
$part = Part::with(['gate', 'branch', 'art'])
$part = Part::with(['gates', 'branchs', 'art'])
->find($part_id);
if (! $part) {
@@ -206,7 +240,7 @@ class VolumController extends Controller
$data = [];
$foundRelation = false;
foreach (['gate', 'branch', 'art'] as $relation) {
foreach (['gates', 'branchs', 'art'] as $relation) {
if ($part->{$relation}->count() > 0) {
$foundRelation = true;
$items = $part->{$relation}()->paginate($perPage);
@@ -230,20 +264,20 @@ class VolumController extends Controller
}
}
if ($chapter_id) {
$chapter = Chapter::with(['parts', 'gate', 'branch', 'art'])
->find($chapter_id);
if ($gate_id) {
$gate = Gate::with(['branchs', 'art'])
->find($gate_id);
if (! $chapter) {
return $this->success([], 'Chapter not found');
if (! $gate) {
return $this->success([], 'Gate not found');
}
$data = [];
$foundRelation = false;
foreach (['parts', 'gate', 'branch', 'art'] as $relation) {
if ($chapter->{$relation}->count() > 0) {
foreach (['branchs', 'art'] as $relation) {
if ($gate->{$relation}->count() > 0) {
$foundRelation = true;
$items = $chapter->{$relation}()->paginate($perPage);
$items = $gate->{$relation}()->paginate($perPage);
foreach ($items as $item) {
$data[] = [
'id' => $item->id,
@@ -298,74 +332,6 @@ class VolumController extends Controller
}
}
if ($gate_id) {
$gate = Gate::with(['branch', 'art'])
->find($gate_id);
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');
}
}
if ($volumeId) {
$volume = Volum::with(['book', 'divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs'])
->find($volumeId);
if (! $volume) {
return $this->success([], 'Volume not found');
}
$data = [];
$foundRelation = false;
foreach (['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs'] 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 ($law_Id) {
$law = Law::find($law_Id);
@@ -579,12 +545,12 @@ class VolumController extends Controller
}
if ($lawId) {
$law = Law::with(['volums', 'divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'arts'])->find($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', 'gates', 'parts', 'chapters', 'branchs', 'arts'], $perPage);
$data = $this->checkRelations($law, ['volums', 'book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'arts'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
@@ -592,27 +558,13 @@ class VolumController extends Controller
return $this->success([], 'No related records found in the law');
}
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', 'gates', 'parts', 'chapters', 'branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the book');
}
if ($volumeId) {
$volume = Volum::with(['book', 'divisions', 'sections', 'gates', 'parts', 'chapters', 'branchs', 'art'])->find($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', 'gates', 'parts', 'chapters', 'branchs', 'art'], $perPage);
$data = $this->checkRelations($volume, ['book', 'divisions', 'sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
@@ -620,27 +572,27 @@ class VolumController extends Controller
return $this->success([], 'No related records found in the volume');
}
if ($sectionId) {
$section = Section::with(['gates', 'parts', 'chapters', 'branchs', 'art'])->find($sectionId);
if (! $section) {
return $this->success([], 'Section not found');
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($section, ['gates', 'parts', 'chapters', 'branchs', 'art'], $perPage);
$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 section');
return $this->success([], 'No related records found in the book');
}
if ($divisionId) {
$division = Division::with(['gates', 'parts', 'chapters', 'branchs', 'art'])->find($divisionId);
$division = Division::with(['sections', 'chapters', 'parts', 'gates', 'branchs', 'art'])->find($divisionId);
if (! $division) {
return $this->success([], 'Division not found');
}
$data = $this->checkRelations($division, ['gates', 'parts', 'chapters', 'branchs', 'art'], $perPage);
$data = $this->checkRelations($division, ['sections', 'chapters', 'parts', 'gates', 'branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
@@ -648,41 +600,27 @@ class VolumController extends Controller
return $this->success([], 'No related records found in the division');
}
if ($partId) {
$part = Part::with(['gates', 'art'])->find($partId);
if (! $part) {
return $this->success([], 'Part not found');
if ($sectionId) {
$section = Section::with(['chapters', 'parts', 'gates', 'branchs', 'art'])->find($sectionId);
if (! $section) {
return $this->success([], 'Section not found');
}
$data = $this->checkRelations($part, ['gates', 'art'], $perPage);
$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 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);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the branch');
return $this->success([], 'No related records found in the section');
}
if ($chapterId) {
$chapter = Chapter::with(['gate', 'parts', 'art'])->find($chapterId);
$chapter = Chapter::with(['parts', 'gates', 'branchs', 'art'])->find($chapterId);
if (! $chapter) {
return $this->success([], 'Chapter not found');
}
$data = $this->checkRelations($chapter, ['gate', 'parts', 'art'], $perPage);
$data = $this->checkRelations($chapter, ['parts', 'gates', 'branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
@@ -690,13 +628,27 @@ class VolumController extends Controller
return $this->success([], 'No related records found in the chapter');
}
if ($partId) {
$part = Part::with(['gates', 'branchs', 'art'])->find($partId);
if (! $part) {
return $this->success([], 'Part not found');
}
$data = $this->checkRelations($part, ['gates', 'branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the part');
}
if ($gateId) {
$gate = Gate::with(['art'])->find($gateId);
$gate = Gate::with(['branchs', 'art'])->find($gateId);
if (! $gate) {
return $this->success([], 'Gate not found');
}
$data = $this->checkRelations($gate, ['art'], $perPage);
$data = $this->checkRelations($gate, ['branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
@@ -704,6 +656,20 @@ class VolumController extends Controller
return $this->success([], 'No related records found in the gate');
}
if ($branchId) {
$branch = Branch::with(['art'])->find($branchId);
if (! $branch) {
return $this->success([], 'Branch not found');
}
$data = $this->checkRelations($branch, ['art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the branch');
}
return $this->success([], 'No valid filter specified');
}
@@ -857,4 +823,5 @@ class VolumController extends Controller
return array_values($route);
}
}