Compare commits

..

16 Commits

Author SHA1 Message Date
m.razaghi
4d15014621 Fixed api issue 2026-07-12 16:24:16 +03:30
m.razaghi
6c0b1f83f2 Fixed api issue 2026-07-12 16:19:20 +03:30
m.razaghi
cad3fa9e1f Fixed api issue 2026-07-12 16:11:38 +03:30
m.razaghi
1a3f41ca8a Merge remote-tracking branch 'upstream-local/new-api' into clean-main 2026-07-12 16:05:05 +03:30
16e68c3048 Merge pull request 'Fixed api issue' (#11) from new-api into clean-main
Reviewed-on: #11
2026-07-12 12:27:18 +00:00
ed95f5b3ee Merge pull request 'Fixed api issue' (#10) from new-api into clean-main
Reviewed-on: #10
2026-07-12 11:56:37 +00:00
0962801c96 Merge pull request 'Fixed api issue' (#9) from new-api into clean-main
Reviewed-on: #9
2026-07-12 10:42:17 +00:00
ecccab9edc Merge pull request 'new-api' (#8) from new-api into clean-main
Reviewed-on: #8
2026-07-12 09:50:32 +00:00
370f4c0a78 Merge pull request 'Fixed api issue' (#7) from new-api into clean-main
Reviewed-on: #7
2026-07-12 09:13:47 +00:00
4214a4b007 Merge pull request 'new-api' (#6) from new-api into clean-main
Reviewed-on: #6
2026-07-12 06:07:23 +00:00
2a6313802b Merge pull request 'updated apis' (#5) from new-api into clean-main
Reviewed-on: #5
2026-07-10 20:56:19 +00:00
d47f148f12 Merge pull request 'new-api' (#4) from new-api into clean-main
Reviewed-on: #4
2026-07-10 10:58:40 +00:00
4ed77dd060 Merge pull request 'Fixed major bugs' (#3) from new-api into clean-main
Reviewed-on: #3
2026-07-08 07:24:33 +00:00
0b6851ebe2 Update README.md 2026-07-08 05:44:27 +00:00
80a271d325 Merge pull request 'fixed major bugs' (#2) from new-api into clean-main
Reviewed-on: #2
2026-07-08 05:44:00 +00:00
d27c32c918 Merge pull request 'Added new api' (#1) from new-api into clean-main
Reviewed-on: #1
2026-07-05 10:22:22 +00:00
2 changed files with 142 additions and 148 deletions

View File

@@ -1 +1 @@
Persian ping
## Law

View File

@@ -7,7 +7,6 @@ use App\Models\Art;
use App\Models\Book;
use App\Models\Branch;
use App\Models\BylawDocument;
use App\Models\BylawNode;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Gate;
@@ -27,7 +26,7 @@ class GlobalSearchApiController extends Controller
use BaseApiResponse;
/**
* Massive Search across ALL data types (With dynamic Date/Number Sorting & Deep Art Search)
* Massive Search across ALL data types
*/
public function search(Request $request)
{
@@ -43,80 +42,31 @@ class GlobalSearchApiController extends Controller
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$page = (int) $request->input('page', 1);
$results = collect();
$staticResults = collect(); // For Art & Structure (IGNORES SORTING)
$sortableResults = collect(); // For Bylaws, Precedents, Opinions (SORTS BASED ON REQUEST)
if (!empty($searchTerm)) {
$limitPerTable = 100; // Performance safeguard
$limitPerTable = 100;
// 1. Search Bylaw Documents
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereIn('id', function($subQuery) use ($searchTerm) {
$subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
});
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatBylaw($item);
$formatted['type'] = 'bylaw';
return $formatted;
});
$results = $results->concat($bylaws); // Changed to concat to prevent overwriting
// 2. Search Judicial Precedents
$judicials = JudicialPrecedent::with('category')
->where(function ($q) use ($searchTerm) {
$q->where('ruling_number', 'LIKE', "%{$searchTerm}%")
->orWhere('subject', 'LIKE', "%{$searchTerm}%")
->orWhere('full_text', 'LIKE', "%{$searchTerm}%");
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatJudicial($item);
$formatted['type'] = 'judicial_precedent';
return $formatted;
});
$results = $results->concat($judicials);
// 3. Search Legal Opinions
$opinions = LegalOpinion::with('category')
->where(function ($q) use ($searchTerm) {
$q->where('opinion_number', 'LIKE', "%{$searchTerm}%")
->orWhere('subject', 'LIKE', "%{$searchTerm}%")
->orWhere('full_text', 'LIKE', "%{$searchTerm}%")
->orWhere('issuing_authority', 'LIKE', "%{$searchTerm}%");
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatOpinion($item);
$formatted['type'] = 'legal_opinion';
return $formatted;
});
$results = $results->concat($opinions);
// 4. Search Arts (Fully restored deep relational search without breaking relationships)
$arts = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
// =========================================================
// 1. Search Arts FIRST (Exact match to ArtController)
// =========================================================
$arts = Art::with(['law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%")
->orWhere('number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('gate', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); });
->orWhere('number', 'LIKE', "%{$searchTerm}%");
})
->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
$formatted = $this->formatArt($item, $searchTerm, Art::class);
$formatted['type'] = 'art';
return $formatted;
});
$results = $results->concat($arts);
$staticResults = $staticResults->concat($arts);
// 5. Search Laws and Structural Models
// =========================================================
// 2. Search Laws and Structural Models (Exact match to ArtController)
// =========================================================
$structuralModels = [
Law::class, Volum::class, Book::class, Division::class,
Section::class, Chapter::class, Part::class, Gate::class,
@@ -136,39 +86,95 @@ class GlobalSearchApiController extends Controller
return $formatted;
});
$results = $results->concat($items);
}
$staticResults = $staticResults->concat($items);
}
// --- Apply Dynamic Memory Sorting ---
if ($request->has('sort_by')) {
// =========================================================
// 3. Search Bylaw Documents
// =========================================================
$bylaws = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('description', 'LIKE', "%{$searchTerm}%")
->orWhere('promulgation_number', 'LIKE', "%{$searchTerm}%")
->orWhere('official_gazette_number', 'LIKE', "%{$searchTerm}%")
->orWhereIn('id', function($subQuery) use ($searchTerm) {
$subQuery->select('document_id')
->from('bylaw_nodes')
->where('node_content', 'LIKE', "%{$searchTerm}%");
});
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatBylaw($item);
$formatted['type'] = 'bylaw';
return $formatted;
});
$sortableResults = $sortableResults->concat($bylaws);
// =========================================================
// 4. Search Judicial Precedents
// =========================================================
$judicials = JudicialPrecedent::with('category')
->where(function ($q) use ($searchTerm) {
$q->where('ruling_number', 'LIKE', "%{$searchTerm}%")
->orWhere('subject', 'LIKE', "%{$searchTerm}%")
->orWhere('full_text', 'LIKE', "%{$searchTerm}%");
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatJudicial($item);
$formatted['type'] = 'judicial_precedent';
return $formatted;
});
$sortableResults = $sortableResults->concat($judicials);
// =========================================================
// 5. Search Legal Opinions
// =========================================================
$opinions = LegalOpinion::with('category')
->where(function ($q) use ($searchTerm) {
$q->where('opinion_number', 'LIKE', "%{$searchTerm}%")
->orWhere('subject', 'LIKE', "%{$searchTerm}%")
->orWhere('full_text', 'LIKE', "%{$searchTerm}%")
->orWhere('issuing_authority', 'LIKE', "%{$searchTerm}%");
})->take($limitPerTable)->get()->map(function($item) {
$formatted = $this->formatOpinion($item);
$formatted['type'] = 'legal_opinion';
return $formatted;
});
$sortableResults = $sortableResults->concat($opinions);
}
// --- Apply Sorting ONLY to $sortableResults (Arts and Structures are ignored!) ---
if ($request->has('sort_by') && $request->input('sort_by') != null) {
$sortBy = $request->input('sort_by');
$sortOrder = $request->input('sort', 'desc');
if ($sortBy === 'number') {
$sortCallback = function ($item) {
return (string) ($item['promulgation_number'] ?? $item['ruling_number'] ?? $item['opinion_number'] ?? $item['number'] ?? '');
return (string) ($item['promulgation_number'] ?? $item['ruling_number'] ?? $item['opinion_number'] ?? '0');
};
if ($sortOrder === 'asc') {
$results = $results->sortBy($sortCallback, SORT_NATURAL)->values();
$sortableResults = $sortableResults->sortBy($sortCallback, SORT_NATURAL)->values();
} else {
$results = $results->sortByDesc($sortCallback, SORT_NATURAL)->values();
$sortableResults = $sortableResults->sortByDesc($sortCallback, SORT_NATURAL)->values();
}
} elseif ($sortBy === 'date') {
$sortCallback = function ($item) {
return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? '');
return (string) ($item['approval_date'] ?? $item['ruling_date'] ?? $item['opinion_date'] ?? '1970-01-01');
};
if ($sortOrder === 'asc') {
$results = $results->sortBy($sortCallback)->values();
$sortableResults = $sortableResults->sortBy($sortCallback)->values();
} else {
$results = $results->sortByDesc($sortCallback)->values();
$sortableResults = $sortableResults->sortByDesc($sortCallback)->values();
}
}
}
// --- Execute Manual Pagination on the Sorted Collection ---
// Combine Results: Static (Arts/Structures) ALWAYS at the top, followed by Sorted Docs
$results = $staticResults->concat($sortableResults);
// --- Execute Manual Pagination on the final Collection ---
$total = $results->count();
$paginatedItems = $results->forPage($page, $perPage)->values();
@@ -204,7 +210,22 @@ class GlobalSearchApiController extends Controller
if (!empty($searchTerm)) {
// 1. Bylaw Documents
// 1. Search Arts FIRST for Fast Search
$artQuery = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%")
->orWhere('number', 'LIKE', "%{$searchTerm}%");
});
$totalCount += $artQuery->count();
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
$formatted = $this->formatArt($item, $searchTerm, Art::class);
$formatted['type'] = 'art';
return $formatted;
});
$results = $results->concat($arts);
// 2. Bylaw Documents
$bylawQuery = BylawDocument::with(['category', 'approvalAuthority', 'promulgationAuthority'])
->where('is_published', true)
->where(function ($q) use ($searchTerm) {
@@ -226,7 +247,7 @@ class GlobalSearchApiController extends Controller
});
$results = $results->concat($bylaws);
// 2. Judicial Precedents
// 3. Judicial Precedents
$judicialQuery = JudicialPrecedent::with('category')
->where(function ($q) use ($searchTerm) {
$q->where('ruling_number', 'LIKE', "%{$searchTerm}%")
@@ -241,7 +262,7 @@ class GlobalSearchApiController extends Controller
});
$results = $results->concat($judicials);
// 3. Legal Opinions
// 4. Legal Opinions
$opinionQuery = LegalOpinion::with('category')
->where(function ($q) use ($searchTerm) {
$q->where('opinion_number', 'LIKE', "%{$searchTerm}%")
@@ -257,28 +278,6 @@ class GlobalSearchApiController extends Controller
});
$results = $results->concat($opinions);
// 4. Search Arts
$artQuery = Art::with(['law', 'law.category', 'chapter', 'part', 'volum', 'book', 'section', 'gate'])
->where(function ($q) use ($searchTerm) {
$q->where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'LIKE', "%{$searchTerm}%")
->orWhere('number', 'LIKE', "%{$searchTerm}%")
->orWhereHas('law', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('volum', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('book', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('section', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('chapter', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('part', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); })
->orWhereHas('gate', function ($sub) use ($searchTerm) { $sub->where('title', 'LIKE', "%{$searchTerm}%"); });
});
$totalCount += $artQuery->count();
$arts = $artQuery->take($limitPerTable)->get()->map(function($item) use ($searchTerm) {
$formatted = $this->formatArt($item, $searchTerm, Art::class);
$formatted['type'] = 'art';
return $formatted;
});
$results = $results->concat($arts);
// 5. Structural Models
$structuralModels = [
Law::class, Volum::class, Book::class, Division::class,
@@ -324,7 +323,6 @@ class GlobalSearchApiController extends Controller
private function formatBylaw($item)
{
$isLocked = $this->isLockedForCurrentUserGeneric($item);
$description = $item->description;
$cleanDesc = trim(strip_tags($description ?? ''));
@@ -383,76 +381,72 @@ class GlobalSearchApiController extends Controller
];
}
private function formatArt($art, $searchTerm, $modelClass)
/**
* Exact 1:1 match with ArtController format mapping
*/
private function formatArt($q, $searchTerm, $modelClass)
{
$law = $art->law ?? Law::find($art->law_id);
$law = $q->law ?? Law::find($q->law_id);
$isLocked = ! auth()->user()->isSubscriber() && (bool) ($law?->is_locked ?? false);
$text = $art->text ?? '';
$text = $q->text ?? '';
$searchQuoted = preg_quote($searchTerm, '/');
if (!empty($searchTerm) && !empty($text)) {
$pos = mb_stripos($text, $searchTerm);
if ($pos !== false) {
$start = max(0, $pos - 50);
$length = mb_strlen($searchTerm) + 100;
$context = mb_substr($text, $start, $length);
if ($start > 0) $context = '...' . $context;
if ($start + $length < mb_strlen($text)) $context = $context . '...';
} else {
$context = $text;
}
// Exact regex match logic from ArtController
if (preg_match('/\b(.{0,50})\b('.$searchQuoted.')\b(.{0,50})\b/i', $text, $matches)) {
$context = trim($matches[1] ?? '').' '.($matches[2] ?? '').' '.trim($matches[3] ?? '');
} else {
$context = $text;
}
return [
'id' => $art->id,
'title' => $art->title,
'number' => $art->number,
'id' => $q->id,
'title' => $q->title,
'number' => $q->number,
'text' => $isLocked ? null : $context,
'is_locked' => $isLocked,
'route' => array_values($this->route($modelClass, $art)),
'route' => array_values($this->route($modelClass, $q)),
'category' => optional($law?->category)->name,
'law' => $law?->title,
'count_art' => $law?->arts?->count() ?? 0,
'count_volums'=> $law?->volums?->count() ?? 0,
'count_volum' => $law?->volums?->count() ?? 0,
'price' => $law?->price,
'image' => $law?->image,
'law' => $art->law != null ? [
'id' => $art->law->id,
'title' => $art->law->title,
'number' => $art->law->number,
'chapter' => $q->chapter != null ? [
'id' => $q->chapter->id,
'title' => $q->chapter->title,
'number' => $q->chapter->number,
] : null,
'chapter' => $art->chapter != null ? [
'id' => $art->chapter->id,
'title' => $art->chapter->title,
'number' => $art->chapter->number,
'part' => $q->part != null ? [
'id' => $q->part->id,
'title' => $q->part->title,
'number' => $q->part->number,
] : null,
'part' => $art->part != null ? [
'id' => $art->part->id,
'title' => $art->part->title,
'number' => $art->part->number,
'volum' => $q->volum != null ? [
'id' => $q->volum->id,
'title' => $q->volum->title,
'number' => $q->volum->number,
] : null,
'volum' => $art->volum != null ? [
'id' => $art->volum->id,
'title' => $art->volum->title,
'number' => $art->volum->number,
'law_object' => $q->law != null ? [
'id' => $q->law->id,
'title' => $q->law->title,
'number' => $q->law->number,
] : null,
'book' => $art->book != null ? [
'id' => $art->book->id,
'title' => $art->book->title,
'number' => $art->book->number,
'book' => $q->book != null ? [
'id' => $q->book->id,
'title' => $q->book->title,
'number' => $q->book->number,
] : null,
'section' => $art->section != null ? [
'id' => $art->section->id,
'title' => $art->section->title,
'number' => $art->section->number,
'section' => $q->section != null ? [
'id' => $q->section->id,
'title' => $q->section->title,
'number' => $q->section->number,
] : null,
'gate' => $art->gate != null ? [
'id' => $art->gate->id,
'title' => $art->gate->title,
'number' => $art->gate->number,
'gate' => $q->gate != null ? [
'id' => $q->gate->id,
'title' => $q->gate->title,
'number' => $q->gate->number,
] : null,
];
}