Files
law-api/app/Http/Controllers/api/VolumController.php

865 lines
31 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\Order;
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 = [];
$foundRelation = false;
foreach (['divisions', 'sections', 'gates', 'parts', 'chapters', '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) {
$division = Division::with(['sections', 'chapters', 'parts', 'gate', 'branch', 'art'])
->find($division_id);
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');
}
}
if ($section_id) {
$section = Section::with(['chapters', 'parts', 'gate', 'branch', 'art'])
->find($section_id);
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');
}
}
if ($volumeId) {
$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');
}
}
if ($part_id) {
$part = Part::with(['gate', 'branch', 'art'])
->find($part_id);
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');
}
}
if ($chapter_id) {
$chapter = Chapter::with(['parts', 'gate', 'branch', 'art'])
->find($chapter_id);
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');
}
}
if ($branch_id) {
$branch = Branch::with(['art'])
->find($branch_id);
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');
}
}
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);
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
]);
$data = [];
if (count($arts) > 0) {
foreach ($arts as $item) {
$data[] = [
'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
];
}
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)
->get();
foreach ($arts as $item) {
$data[] = [
'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
];
}
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 : Law::where('id', $item->id)->first()?->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 ($lawId) {
$law = Law::with(['volums', 'divisions', 'sections', 'gates', 'parts', 'chapters', '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);
if ($data) {
return $this->success($data, 'Success');
}
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);
if (!$volume) {
return $this->success([], 'Volume not found');
}
$data = $this->checkRelations($volume, ['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 volume');
}
if ($sectionId) {
$section = Section::with(['gates', 'parts', 'chapters', 'branchs', 'art'])->find($sectionId);
if (!$section) {
return $this->success([], 'Section not found');
}
$data = $this->checkRelations($section, ['gates', 'parts', 'chapters', 'branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the section');
}
if ($divisionId) {
$division = Division::with(['gates', 'parts', 'chapters', 'branchs', 'art'])->find($divisionId);
if (!$division) {
return $this->success([], 'Division not found');
}
$data = $this->checkRelations($division, ['gates', 'parts', 'chapters', 'branchs', 'art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
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');
}
$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);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the branch');
}
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);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the chapter');
}
if ($gateId) {
$gate = Gate::with(['art'])->find($gateId);
if (!$gate) {
return $this->success([], 'Gate not found');
}
$data = $this->checkRelations($gate, ['art'], $perPage);
if ($data) {
return $this->success($data, 'Success');
}
return $this->success([], 'No related records found in the gate');
}
return $this->success([], 'No valid filter specified');
}
private function prepareItemsData($items, $type)
{
$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,
'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) {
if ($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';
break;
case 2:
return 'kifari';
break;
default:
return 'kifari';
break;
}
}
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);
}
}