Init(Core): Change repo

This commit is contained in:
2026-04-24 15:29:37 +03:30
commit ededb41a3a
1499 changed files with 199187 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Actions\Fortify;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\CreatesNewUsers;
use Laravel\Jetstream\Jetstream;
class CreateNewUser implements CreatesNewUsers
{
use PasswordValidationRules;
/**
* Validate and create a newly registered user.
*
* @param array<string, string> $input
*/
public function create(array $input): User
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();
return User::create([
'name' => $input['name'],
'email' => $input['email'],
'password' => Hash::make($input['password']),
]);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace App\Actions\Fortify;
use Illuminate\Validation\Rules\Password;
trait PasswordValidationRules
{
/**
* Get the validation rules used to validate passwords.
*
* @return array<int, \Illuminate\Contracts\Validation\Rule|array|string>
*/
protected function passwordRules(): array
{
return ['required', 'string', Password::default(), 'confirmed'];
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace App\Actions\Fortify;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\ResetsUserPasswords;
class ResetUserPassword implements ResetsUserPasswords
{
use PasswordValidationRules;
/**
* Validate and reset the user's forgotten password.
*
* @param array<string, string> $input
*/
public function reset(User $user, array $input): void
{
Validator::make($input, [
'password' => $this->passwordRules(),
])->validate();
$user->forceFill([
'password' => Hash::make($input['password']),
])->save();
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Actions\Fortify;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
class UpdateUserPassword implements UpdatesUserPasswords
{
use PasswordValidationRules;
/**
* Validate and update the user's password.
*
* @param array<string, string> $input
*/
public function update(User $user, array $input): void
{
Validator::make($input, [
'current_password' => ['required', 'string', 'current_password:web'],
'password' => $this->passwordRules(),
], [
'current_password.current_password' => __('The provided password does not match your current password.'),
])->validateWithBag('updatePassword');
$user->forceFill([
'password' => Hash::make($input['password']),
])->save();
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App\Actions\Fortify;
use App\Models\User;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
/**
* Validate and update the given user's profile information.
*
* @param array<string, mixed> $input
*/
public function update(User $user, array $input): void
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', Rule::unique('users')->ignore($user->id)],
'photo' => ['nullable', 'mimes:jpg,jpeg,png', 'max:1024'],
])->validateWithBag('updateProfileInformation');
if (isset($input['photo'])) {
$user->updateProfilePhoto($input['photo']);
}
if ($input['email'] !== $user->email &&
$user instanceof MustVerifyEmail) {
$this->updateVerifiedUser($user, $input);
} else {
$user->forceFill([
'name' => $input['name'],
'email' => $input['email'],
])->save();
}
}
/**
* Update the given verified user's profile information.
*
* @param array<string, string> $input
*/
protected function updateVerifiedUser(User $user, array $input): void
{
$user->forceFill([
'name' => $input['name'],
'email' => $input['email'],
'email_verified_at' => null,
])->save();
$user->sendEmailVerificationNotification();
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Actions\Jetstream;
use App\Models\User;
use Laravel\Jetstream\Contracts\DeletesUsers;
class DeleteUser implements DeletesUsers
{
/**
* Delete the given user.
*/
public function delete(User $user): void
{
$user->deleteProfilePhoto();
$user->tokens->each->delete();
$user->delete();
}
}

View File

@@ -0,0 +1,135 @@
<?php
namespace App\Console\Commands;
use App\Models\SubscribePlan;
use App\Models\User;
use App\Models\UserSubscriber;
use Illuminate\Console\Command;
class AddSubscriptionDays extends Command
{
protected $signature = 'subscriptions:add-days
{days=3 : Number of days to add}
{--all-users : Ensure every user gets days even if they have no subscription}
{--only-without-subscription : Process only users who do not have any subscription}
{--plan-id= : Subscribe plan id to use for users without a subscription}
{--include-expired : Also extend expired subscriptions}
{--chunk=1000 : Chunk size for processing}
{--dry-run : Show how many records would change without saving}';
protected $description = 'Add days to all user subscriptions';
public function handle(): int
{
$days = (int) $this->argument('days');
$chunkSize = max(1, (int) $this->option('chunk'));
$includeExpired = (bool) $this->option('include-expired');
$allUsers = (bool) $this->option('all-users');
$onlyWithoutSubscription = (bool) $this->option('only-without-subscription');
$dryRun = (bool) $this->option('dry-run');
if ($days <= 0) {
$this->error('Days must be a positive integer.');
return self::FAILURE;
}
if ($onlyWithoutSubscription && !$allUsers) {
$this->error('The --only-without-subscription option can only be used with --all-users.');
return self::FAILURE;
}
if ($allUsers) {
$planId = $this->option('plan-id');
if (!$planId) {
$this->error('When using --all-users you must provide --plan-id for users without a subscription.');
return self::FAILURE;
}
$plan = SubscribePlan::find($planId);
if (!$plan) {
$this->error('Subscribe plan not found for the given --plan-id.');
return self::FAILURE;
}
}
if ($allUsers) {
if ($onlyWithoutSubscription) {
$this->info("Granting {$days} day(s) only to users without a subscription." . ($dryRun ? ' (dry-run)' : ''));
} else {
$this->info("Granting {$days} day(s) to every user." . ($dryRun ? ' (dry-run)' : ''));
}
} else {
$scopeLabel = $includeExpired ? 'all' : 'active';
$this->info("Extending {$scopeLabel} subscriptions by {$days} day(s)." . ($dryRun ? ' (dry-run)' : ''));
}
$touched = 0;
if ($allUsers) {
$planId = (int) $this->option('plan-id');
$isFree = (bool) $plan->is_free;
User::query()->orderBy('id')->chunkById($chunkSize, function ($users) use (&$touched, $days, $dryRun, $planId, $isFree, $onlyWithoutSubscription) {
foreach ($users as $user) {
$latest = $user->userSubscribers()->orderByDesc('expired_at')->first();
if ($onlyWithoutSubscription && $latest) {
continue;
}
$touched++;
if ($dryRun) {
continue;
}
if ($latest) {
$baseDate = $latest->expired_at && $latest->expired_at->greaterThan(now())
? $latest->expired_at
: now();
$latest->expired_at = $baseDate->copy()->addDays($days);
$latest->save();
} else {
$user->userSubscribers()->create([
'subscribe_plan_id' => $planId,
'expired_at' => now()->addDays($days),
'is_free' => $isFree,
]);
}
}
});
} else {
$query = UserSubscriber::query();
if (!$includeExpired) {
$query->where('expired_at', '>', now());
}
$query->orderBy('id')->chunkById($chunkSize, function ($subscribers) use (&$touched, $days, $dryRun) {
foreach ($subscribers as $subscriber) {
$touched++;
if ($dryRun) {
continue;
}
if ($subscriber->expired_at === null) {
$subscriber->expired_at = now()->addDays($days);
} else {
$subscriber->expired_at = $subscriber->expired_at->copy()->addDays($days);
}
$subscriber->save();
}
});
}
if ($dryRun) {
$label = $allUsers ? 'user(s)' : 'subscription record(s)';
$this->info("{$touched} {$label} would be processed.");
} else {
$label = $allUsers ? 'user(s)' : 'subscription record(s)';
$this->info("{$touched} {$label} processed.");
}
return self::SUCCESS;
}
}

27
app/Console/Kernel.php Normal file
View File

@@ -0,0 +1,27 @@
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule): void
{
// $schedule->command('inspire')->hourly();
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* The list of the inputs that are never flashed to the session on validation exceptions.
*
* @var array<int, string>
*/
protected $dontFlash = [
'current_password',
'password',
'password_confirmation',
];
/**
* Register the exception handling callbacks for the application.
*/
public function register(): void
{
$this->reportable(function (Throwable $e) {
//
});
}
}

View File

@@ -0,0 +1,125 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Art;
use App\Models\Book;
use App\Models\Branch;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Gate;
use App\Models\Law;
use App\Models\Part;
use App\Models\Section;
use App\Models\Volum;
use Illuminate\Http\Request;
class ArtController extends Controller
{
public function index(Request $request)
{
$query = Art::query();
if ($request->filled('q')) {
$q = $request->q;
$query->where(function ($qry) use ($q) {
$qry->where('title', 'like', "%{$q}%")->orWhere('text', 'like', "%{$q}%");
});
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$arts = $query->paginate($perPage)->withQueryString();
return view('admin.art.index', compact('arts'));
}
public function create()
{
$chapters = Chapter::all();
$parts = Part::all();
$gates = Gate::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$sections = Section::all();
$divisions = Division::all();
$branches = Branch::all();
return view('admin.art.create', compact('chapters', 'parts', 'gates', 'books', 'volums', 'laws', 'sections','branches','divisions'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'text' => 'required',
'number' => 'required',
'chapter_id' => 'nullable',
'part_id' => 'nullable',
'gate_id' => 'nullable',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'required',
'division_id' => 'nullable',
'branch_id' => 'nullable',
'is_free' => 'nullable'
]);
$validated['is_free'] = $request->input('is_free') ? true : false;
art::query()->create($validated);
return redirect(route('art.index'));
}
public function edit(art $art)
{
$chapters = Chapter::all();
$parts = Part::all();
$gates = Gate::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$sections = Section::all();
$divisions = Division::all();
$branches = Branch::all();
return view('admin.art.update', compact('art', 'chapters', 'parts', 'gates', 'books', 'volums', 'laws', 'sections','branches','divisions'));
}
public function update(Request $request, Art $art)
{
$validated = $request->validate([
'title' => 'required',
'text' => 'required',
'number' => 'required',
'chapter_id' => 'nullable',
'part_id' => 'nullable',
'gate_id' => 'nullable',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'required',
'division_id' => 'nullable',
'branch_id' => 'nullable',
'is_free' => 'nullable'
]);
$validated['is_free'] = $request->input('is_free') ? true : false;
$art->update($validated);
return redirect(route('art.edit',$art->id));
}
public function destroy(Art $art)
{
$art->delete();
return redirect(route('art.index'));
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Book;
use App\Models\Law;
use App\Models\Volum;
use Illuminate\Http\Request;
class BookController extends Controller
{
public function index(Request $request)
{
$query = Book::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$books = $query->paginate($perPage)->withQueryString();
return view('admin.book.index', compact('books'));
}
public function create()
{
$volums = Volum::all();
$laws = Law::all();
return view('admin.book.create', compact('volums', 'laws'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'volum_id' => 'required',
'law_id' => 'nullable',
]);
book::query()->create($validated);
return redirect(route('book.index'));
}
public function edit(Book $book)
{
$volums = Volum::all();
$laws = Law::all();
return view('admin.book.update', compact('book', 'volums', 'laws'));
}
public function update(Request $request, Book $book)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'volum_id' => 'required',
'law_id' => 'nullable',
]);
$book->update($validated);
return redirect(route('book.index'));
}
public function destroy(Book $book)
{
$book->delete();
return redirect(route('book.index'));
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Book;
use App\Models\Branch;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Gate;
use App\Models\Law;
use App\Models\Part;
use App\Models\Section;
use App\Models\Volum;
use Illuminate\Http\Request;
class BranchController extends Controller
{
public function index(Request $request)
{
$query = Branch::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$branchs = $query->paginate($perPage)->withQueryString();
return view('admin.branch.index', compact('branchs'));
}
public function create()
{
$chapters = Chapter::all();
$parts = Part::all();
$gates = Gate::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$sections = Section::all();
$divisions = Division::all();
return view('admin.branch.create', compact('chapters', 'parts', 'gates', 'books', 'volums', 'laws', 'sections','divisions'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'chapter_id' => 'nullable',
'part_id' => 'nullable',
'gate_id' => 'nullable',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'required',
'law_id' => 'required',
'division' => 'nullable',
]);
branch::query()->create($validated);
return redirect(route('branch.index'));
}
public function edit(branch $branch)
{
$chapters = Chapter::all();
$parts = Part::all();
$gates = Gate::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$sections = Section::all();
$divisions = Division::all();
return view('admin.branch.update', compact('branch', 'chapters', 'parts', 'gates', 'books', 'volums', 'laws', 'sections','divisions'));
}
public function update(Request $request, Branch $branch)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'chapter_id' => 'nullable',
'part_id' => 'nullable',
'gate_id' => 'nullable',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'required',
'law_id' => 'required',
'division_id' => 'nullable',
]);
$branch->update($validated);
return redirect(route('branch.index'));
}
public function destroy(Branch $branch)
{
$branch->delete();
return redirect(route('branch.index'));
}
}

View File

@@ -0,0 +1,49 @@
<?php
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\category\CreateCategoryRequest;
use App\Http\Requests\admin\category\UpdateCategoryRequest;
use App\Models\Category;
class CategoriesController extends Controller
{
public function index()
{
$categories = Category::query()->select('id', 'name', 'label')->get();
return view('admin.categories.index', compact('categories'));
}
public function create()
{
return view('admin.categories.create');
}
public function store(CreateCategoryRequest $request)
{
Category::query()->create($request->validated());
return redirect(route('categories.index'));
}
public function edit(Category $category)
{
return view('admin.categories.update', compact('category'));
}
public function update(UpdateCategoryRequest $request, Category $category)
{
$category->update($request->validated());
return redirect(route('categories.index'));
}
public function destroy(Category $category)
{
$category->delete();
return back();
}
}

View File

@@ -0,0 +1,69 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
public function index(Request $request)
{
$query = Category::query();
if ($request->filled('q')) {
$query->where('name', 'like', '%' . $request->q . '%');
}
if ($request->filled('type')) {
$query->where('type', $request->type);
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$categories = $query->paginate($perPage)->withQueryString();
return view('admin.categories.index', compact('categories'));
}
public function create()
{
return view('admin.categories.create');
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required',
'type' => 'required'
]);
Category::create($validated);
return redirect(route('categories.index'));
}
public function edit(Category $category)
{
return view('admin.categories.update',compact('category'));
}
public function update(Request $request, Category $category)
{
$validated = $request->validate([
'name' => 'required',
'type' => 'required'
]);
$category->update($validated);
return redirect(route('categories.index'));
}
public function destroy(Category $category)
{
$category->delete();
return redirect(route('categories.index'));
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Book;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Law;
use App\Models\Section;
use App\Models\Volum;
use Illuminate\Http\Request;
class ChapterController extends Controller
{
public function index(Request $request)
{
$query = Chapter::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$chapters = $query->paginate($perPage)->withQueryString();
return view('admin.chapter.index', compact('chapters'));
}
public function create()
{
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$sections = Section::all();
$divisions = Division::all();
return view('admin.chapter.create', compact('sections', 'books', 'volums', 'laws','divisions'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'nullable',
'division_id' => 'nullable'
]);
Chapter::query()->create($validated);
return redirect(route('chapter.index'));
}
public function edit(Chapter $chapter)
{
$sections = Section::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$divisions = Division::all();
return view('admin.chapter.update', compact('sections', 'chapter', 'books', 'volums', 'laws','divisions'));
}
public function update(Request $request, Chapter $chapter)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'nullable',
'division_id' => 'nullable'
]);
$chapter->update($validated);
return redirect(route('chapter.index'));
}
public function destroy(Chapter $chapter)
{
$chapter->delete();
return redirect(route('chapter.index'));
}
}

View File

@@ -0,0 +1,83 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Book;
use App\Models\Division;
use App\Models\Law;
use App\Models\Volum;
use Illuminate\Http\Request;
class DivisionController extends Controller
{
public function index(Request $request)
{
$query = Division::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$divisions = $query->paginate($perPage)->withQueryString();
return view('admin.division.index', compact('divisions'));
}
public function create()
{
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
return view('admin.division.create', compact('books', 'volums', 'laws'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'book_id' => 'nullable',
'volum_id' => 'required',
'law_id' => 'required',
]);
division::query()->create($validated);
return redirect(route('division.index'));
}
public function edit(division $division)
{
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
return view('admin.division.update', compact('division','books', 'volums', 'laws'));
}
public function update(Request $request, Division $division)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'book_id' => 'nullable',
'volum_id' => 'required',
'law_id' => 'required',
]);
$division->update($validated);
return redirect(route('division.index'));
}
public function destroy(Division $division)
{
$division->delete();
return redirect(route('division.index'));
}
}

View File

@@ -0,0 +1,92 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Book;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Gate;
use App\Models\Law;
use App\Models\Part;
use App\Models\Section;
use App\Models\Volum;
use Illuminate\Http\Request;
class GateController extends Controller
{
public function index(Request $request)
{
$query = Gate::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$gates = $query->paginate($perPage)->withQueryString();
return view('admin.gate.index', compact('gates'));
}
public function create()
{
$parts = Part::all();
$chapters = Chapter::all();
$sections = Section::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$divisions = Division::all();
return view('admin.gate.create', compact('parts', 'chapters', 'sections', 'books', 'volums', 'laws','divisions'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'part_id' => 'nullable',
'division_id' => 'nullable',
]);
Gate::query()->create($validated);
return redirect(route('gate.index'));
}
public function edit(Gate $gate)
{
$parts = Part::all();
$chapters = Chapter::all();
$sections = Section::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$divisions = Division::all();
return view('admin.gate.update', compact('gate', 'parts', 'chapters', 'sections', 'books', 'volums', 'laws','divisions'));
}
public function update(Request $request, Gate $gate)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'part_id' => 'nullable',
'division_id' => 'nullable',
]);
$gate->update($validated);
return redirect(route('gate.index'));
}
public function destroy(Gate $gate)
{
$gate->delete();
return redirect(route('gate.index'));
}
}

View File

@@ -0,0 +1,13 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
class HomeController extends Controller
{
public function index()
{
return view('admin.dashboard');
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Art;
use App\Models\JudicialPrecedent;
use Illuminate\Http\Request;
class JudicialPrecedentController extends Controller
{
public function index(Request $request)
{
$query = JudicialPrecedent::query();
if ($request->filled('q')) {
$q = $request->q;
$query->where(function ($qry) use ($q) {
$qry->where('ruling_number', 'like', "%{$q}%")
->orWhere('subject', 'like', "%{$q}%")
->orWhere('full_text', 'like', "%{$q}%");
});
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$judicialPrecedents = $query->paginate($perPage)->withQueryString();
return view('admin.judicial-precedent.index', compact('judicialPrecedents'));
}
public function create()
{
$arts = Art::all();
return view('admin.judicial-precedent.create', compact('arts'));
}
public function store(Request $request)
{
$validated = $request->validate([
'ruling_number' => 'required|unique:judicial_precedents,ruling_number',
'ruling_date' => 'required|date',
'subject' => 'required|string',
'full_text' => 'required|string',
'issuing_authority' => 'nullable|string',
'art_ids' => 'nullable|array',
'art_ids.*' => 'exists:arts,id'
]);
$judicialPrecedent = JudicialPrecedent::create([
'ruling_number' => $validated['ruling_number'],
'ruling_date' => $validated['ruling_date'],
'subject' => $validated['subject'],
'full_text' => $validated['full_text'],
'issuing_authority' => $validated['issuing_authority'] ?? 'هیأت عمومی دیوان عالی کشور'
]);
if (!empty($validated['art_ids'])) {
$judicialPrecedent->arts()->attach($validated['art_ids']);
}
return redirect(route('judicial-precedent.index'));
}
public function edit(JudicialPrecedent $judicialPrecedent)
{
$arts = Art::all();
$selectedArtIds = $judicialPrecedent->arts->pluck('id')->toArray();
return view('admin.judicial-precedent.update', compact('judicialPrecedent', 'arts', 'selectedArtIds'));
}
public function update(Request $request, JudicialPrecedent $judicialPrecedent)
{
$validated = $request->validate([
'ruling_number' => 'required|unique:judicial_precedents,ruling_number,' . $judicialPrecedent->id,
'ruling_date' => 'required|date',
'subject' => 'required|string',
'full_text' => 'required|string',
'issuing_authority' => 'nullable|string',
'art_ids' => 'nullable|array',
'art_ids.*' => 'exists:arts,id'
]);
$judicialPrecedent->update($validated);
$judicialPrecedent->arts()->sync($validated['art_ids'] ?? []);
return redirect(route('judicial-precedent.edit', $judicialPrecedent->id));
}
public function destroy(JudicialPrecedent $judicialPrecedent)
{
$judicialPrecedent->delete();
return redirect(route('judicial-precedent.index'));
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Law;
use Illuminate\Http\Request;
class LawController extends Controller
{
public function index(Request $request)
{
$query = Law::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
if ($request->filled('locked')) {
$query->where('is_locked', $request->locked === '1');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$laws = $query->paginate($perPage)->withQueryString();
return view('admin.law.index', compact('laws'));
}
public function create()
{
$categories = Category::all();
return view('admin.law.create', compact('categories'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'is_locked' => 'nullable',
'category_id' => 'nullable|integer|exists:categories,id',
'price' => 'nullable|numeric',
'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$validated['is_locked'] = $request->has('is_locked', false);
if ($request->hasFile('image') && $request->file('image')->isValid()) {
dd($request->has('image'));
$imageName = uniqid('image_', true) . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->move(public_path('images'), $imageName);
$validated['image'] = $imageName;
}
Law::create($validated);
return redirect()->route('law.index')->with('success', 'Law created successfully.');
}
public function edit(Law $law)
{
$categories = Category::all();
return view('admin.law.update', compact('law', 'categories'));
}
public function update(Request $request, Law $law)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'is_locked' => 'sometimes',
'category_id' => 'nullable|integer',
'price' => 'nullable|numeric',
'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$validated['is_locked'] = $request->boolean('is_locked');
if ($request->hasFile('image') && $request->file('image')->isValid()) {
$imageName = uniqid('image_', true) . '.' . $request->file('image')->getClientOriginalExtension();
$request->file('image')->move(public_path('images'), $imageName);
$validated['image'] = $imageName;
}
$law->update($validated);
return redirect()->route('law.index');
}
public function destroy(Law $law)
{
$law->delete();
return redirect(route('law.index'));
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\Notification\StoreRequest;
use App\Http\Requests\admin\Notification\UpdateRequest;
use App\Models\Notification;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
public function index(Request $request)
{
$query = Notification::query();
if ($request->filled('q')) {
$q = $request->q;
$query->where(function ($qry) use ($q) {
$qry->where('title', 'like', "%{$q}%")
->orWhere('description', 'like', "%{$q}%");
});
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$notifications = $query->latest()->paginate($perPage)->withQueryString();
return view('admin.notifications.index', compact('notifications'));
}
public function create()
{
return view('admin.notifications.create');
}
public function store(StoreRequest $request)
{
Notification::create($request->validated());
return redirect()->route('notifications.index');
}
public function edit(Notification $notification)
{
return view('admin.notifications.update', compact('notification'));
}
public function update(UpdateRequest $request, Notification $notification)
{
$notification->update($request->validated());
return redirect()->route('notifications.index');
}
public function destroy(Notification $notification)
{
$notification->delete();
return redirect()->route('notifications.index');
}
}

View File

@@ -0,0 +1,97 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Book;
use App\Models\Chapter;
use App\Models\Division;
use App\Models\Law;
use App\Models\Part;
use App\Models\Section;
use App\Models\Volum;
use Illuminate\Http\Request;
class PartController extends Controller
{
public function index(Request $request)
{
$query = Part::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$parts = $query->paginate($perPage)->withQueryString();
return view('admin.part.index', compact('parts'));
}
public function create()
{
$chapters = Chapter::all();
$sections = Section::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$divisions = Division::all();
return view('admin.part.create', compact('chapters', 'sections', 'books', 'volums', 'laws','divisions'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'chapter_id' => 'nullable',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'nullable',
'division_id' => 'nullable'
]);
Part::query()->create($validated);
return redirect(route('part.index'));
}
public function edit(Part $part)
{
$chapters = Chapter::all();
$sections = Section::all();
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$divisions = Division::all();
return view('admin.part.update', compact('part', 'chapters', 'sections', 'books', 'volums', 'laws','divisions'));
}
public function update(Request $request, Part $part)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'chapter_id' => 'nullable',
'section_id' => 'nullable',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'nullable',
'division_id' => 'nullable'
]);
$part->update($validated);
return redirect(route('part.index'));
}
public function destroy(Part $part)
{
$part->delete();
return redirect(route('part.index'));
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Book;
use App\Models\Division;
use App\Models\Law;
use App\Models\Section;
use App\Models\Volum;
use Illuminate\Http\Request;
class SectionController extends Controller
{
public function index(Request $request)
{
$query = Section::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$sections = $query->paginate($perPage)->withQueryString();
return view('admin.section.index', compact('sections'));
}
public function create()
{
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$sections = Section::all();
$divisions = Division::all();
return view('admin.section.create', compact('books', 'volums', 'laws','sections','divisions'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'nullable',
'division_id' => 'nullable'
]);
section::query()->create($validated);
return redirect(route('section.index'));
}
public function edit(Section $section)
{
$books = Book::all();
$volums = Volum::all();
$laws = Law::all();
$sections = Section::all();
$divisions = Division::all();
return view('admin.section.update', compact('section', 'books', 'volums', 'laws','sections','divisions'));
}
public function update(Request $request, Section $section)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'book_id' => 'nullable',
'volum_id' => 'nullable',
'law_id' => 'nullable',
'division_id' => 'nullable'
]);
$section->update($validated);
return redirect(route('section.index'));
}
public function destroy(Section $section)
{
$section->delete();
return redirect(route('section.index'));
}
}

View File

@@ -0,0 +1,64 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\SubscribePlan\CreateRequest;
use App\Http\Requests\admin\SubscribePlan\UpdateRequest;
use App\Models\SubscribePlan;
use Illuminate\Http\Request;
class SubscribePlanController extends Controller
{
public function index(Request $request)
{
$query = SubscribePlan::query();
if ($request->filled('q')) {
$q = $request->q;
$query->where('name', 'like', "%{$q}%");
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$subscribePlans = $query->paginate($perPage)->withQueryString();
return view('admin.subscribe-plans.index', compact('subscribePlans'));
}
public function create()
{
return view('admin.subscribe-plans.create');
}
public function store(CreateRequest $request)
{
$validated = $request->validated();
$validated['is_active'] = $request->has('is_active');
$validated['is_free'] = $request->has('is_free');
SubscribePlan::create($validated);
return redirect()->route('subscribe-plans.index');
}
public function edit(SubscribePlan $subscribePlan)
{
return view('admin.subscribe-plans.update', compact('subscribePlan'));
}
public function update(UpdateRequest $request, SubscribePlan $subscribePlan)
{
$validated = $request->validated();
$validated['is_active'] = $request->has('is_active');
$validated['is_free'] = $request->has('is_free');
$subscribePlan->update($validated);
return redirect()->route('subscribe-plans.index');
}
public function destroy(SubscribePlan $subscribePlan)
{
$subscribePlan->delete();
return redirect()->route('subscribe-plans.index');
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Suggestion;
use Illuminate\Http\Request;
class SuggestionController extends Controller
{
public function index(Request $request)
{
$query = Suggestion::query()->with('user');
if ($request->filled('q')) {
$q = $request->q;
$query->where(function ($qry) use ($q) {
$qry->where('text', 'like', "%{$q}%")
->orWhereHas('user', fn ($u) => $u->where('mobile', 'like', "%{$q}%"));
});
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$suggestions = $query->paginate($perPage)->withQueryString();
return view('admin.suggestions.index', compact('suggestions'));
}
}

View File

@@ -0,0 +1,74 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\user\CreateUserRequest;
use App\Http\Requests\admin\user\UpdateUserRequest;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class UsersController extends Controller
{
public function index(Request $request)
{
$query = User::select('id', 'name', 'email', 'mobile', 'is_admin')
->with(['userSubscribers' => function ($q) {
$q->where('expired_at', '>=', now());
}]);
if ($request->filled('q')) {
$q = $request->q;
$query->where(function ($qry) use ($q) {
$qry->where('name', 'like', "%{$q}%")
->orWhere('email', 'like', "%{$q}%")
->orWhere('mobile', 'like', "%{$q}%");
});
}
if ($request->filled('type') && in_array($request->type, ['0', '1'])) {
$query->where('is_admin', (bool) $request->type);
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$users = $query->paginate($perPage)->withQueryString();
return view('admin.users.index', compact('users'));
}
public function create()
{
return view('admin.users.create');
}
public function store(CreateUserRequest $request)
{
$validated = $request->validated();
$validated['password'] = Hash::make(fake()->password);
$validated['is_admin'] = $validated['is_admin'] == 'on' ? true : false;
User::query()->create($validated);
return redirect(route('users.index'));
}
public function edit(User $user)
{
return view('admin.users.update', compact('user'));
}
public function update(UpdateUserRequest $request, User $user)
{
$validated = $request->validated();
$validated['is_admin'] = $validated['is_admin'] == 'on' ? true : false;
$user->update($validated);
return redirect(route('users.index'));
}
public function destroy(User $user)
{
$user->delete();
return back();
}
}

View File

@@ -0,0 +1,67 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Requests\admin\CreateVersionStore;
use App\Http\Requests\admin\UpdateVersionStore;
use App\Models\Version;
use Illuminate\Http\Request;
class VersionController extends Controller
{
public function index(Request $request)
{
$query = Version::query();
if ($request->filled('q')) {
$q = $request->q;
$query->where(function ($qry) use ($q) {
$qry->where('code', 'like', "%{$q}%")
->orWhere('number', 'like', "%{$q}%")
->orWhere('type', 'like', "%{$q}%");
});
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$versions = $query->paginate($perPage)->withQueryString();
return view('admin.versions.index', compact('versions'));
}
public function create()
{
return view('admin.versions.create');
}
public function store(CreateVersionStore $request)
{
$validated = $request->validated();
if (isset($validated['force_update'])) {
$validated['force_update'] = $validated['force_update'] == 'on' ? 1 : 0;
}
Version::create($validated);
return redirect(route('versions.index'));
}
public function edit(Version $version)
{
return view('admin.versions.update', compact('version'));
}
public function update(UpdateVersionStore $request, Version $version)
{
$validated = $request->validated();
if (isset($validated['force_update'])) {
$validated['force_update'] = $validated['force_update'] == 'on' ? 1 : 0;
}
$version->update($validated);
return redirect(route('versions.index'));
}
public function destroy(Version $version)
{
$version->delete();
return redirect(route('versions.index'));
}
}

View File

@@ -0,0 +1,72 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Law;
use App\Models\Volum;
use Illuminate\Http\Request;
class VolumController extends Controller
{
public function index(Request $request)
{
$query = Volum::query();
if ($request->filled('q')) {
$query->where('title', 'like', '%' . $request->q . '%');
}
$perPage = min(max((int) $request->input('per_page', 15), 10), 100);
$volums = $query->paginate($perPage)->withQueryString();
return view('admin.volum.index', compact('volums'));
}
public function create()
{
$laws = Law::all();
return view('admin.volum.create', compact('laws'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'law_id' => 'required',
]);
volum::query()->create($validated);
return redirect(route('volum.index'));
}
public function edit(Volum $volum)
{
$laws = Law::all();
return view('admin.volum.update', compact('volum', 'laws'));
}
public function update(Request $request, Volum $volum)
{
$validated = $request->validate([
'title' => 'required',
'number' => 'required',
'law_id' => 'required',
]);
$volum->update($validated);
return redirect(route('volum.index'));
}
public function destroy(Volum $volum)
{
$volum->delete();
return redirect(route('volum.index'));
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
class Controller extends BaseController
{
use AuthorizesRequests, ValidatesRequests;
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\SuggestionRequest;
use App\Models\Suggestion;
use App\Traits\BaseApiResponse;
class SuggestionController extends Controller
{
use BaseApiResponse;
public function index(SuggestionRequest $requst)
{
Suggestion::create([
'user_id' => auth()->user()->id,
'text' => $requst->text
]);
return $this->success(null,'موفقیت','پیشنهاد شما با موفقیت برای مدیر ارسال شد');
}
}

View File

@@ -0,0 +1,500 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\GetApiRequest;
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\LikeArt;
use App\Models\Note;
use App\Models\Part;
use App\Models\Section;
use App\Models\Volum;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class ArtController extends Controller
{
use BaseApiResponse;
public function index(GetApiRequest $request)
{
$arts = Art::with(['chapter', 'part', 'volum', 'law', 'book', 'section', 'gate'])->where('book_id', $request->book_id)->orderBy('number')->get();
$arts = $arts->map(function ($art) {
return [
'id' => $art->id,
'text' => $art->text,
'number' => $art->number,
'chapter' => $art->chapter != null ? [
'id' => $art->chapter->id,
'title' => $art->chapter->title,
'number' => $art->chapter->number,
] : null,
'part' => $art->part != null ? [
'id' => $art->part->id,
'title' => $art->part->title,
'number' => $art->part->number,
] : null,
'volum' => $art->volum != null ? [
'id' => $art->volum->id,
'title' => $art->volum->title,
'number' => $art->volum->number,
] : null,
'law' => $art->law != null ? [
'id' => $art->law->id,
'title' => $art->law->title,
'number' => $art->law->number,
] : null,
'book' => $art->book != null ? [
'id' => $art->book->id,
'title' => $art->book->title,
'number' => $art->book->number,
] : null,
'section' => $art->section != null ? [
'id' => $art->section->id,
'title' => $art->section->title,
'number' => $art->section->number,
] : null,
'gate' => $art->gate != null ? [
'id' => $art->gate->id,
'title' => $art->gate->title,
'number' => $art->gate->number,
] : null,
];
});
return response()->json(['arts' => $arts]);
}
public function single($id)
{
$art = Art::select('id', 'title', 'number', 'text', 'law_id')->find($id);
if (!$art) {
return $this->failed([], 'Art not found');
}
$law = Law::find($art?->law_id);
$art->is_like = $this->isLiked($art->id);
$art->note = Note::select('id', 'note', 'color_code','created_at')->where('user_id', auth()->user()->id)->where('art_id', $id)->get();
$art->category = $law?->category?->name;
$art->route = array_values($this->route(Art::class, $art));
$previousArt = Art::select('id', 'title', 'number', 'text', 'law_id')
->where('law_id',$art->law_id)
->where('number', '<', $art->number)
->orderBy('number', 'desc')
->first();
if ($previousArt) {
$previousLaw = Law::find($previousArt->law_id);
$previousArt->is_like = $this->isLiked($previousArt->id);
$previousArt->note = Note::select('id', 'note', 'color_code')->where('user_id', auth()->user()->id)->where('art_id', $previousArt->id)->get();
$previousArt->category = $previousLaw?->category?->name;
$previousArt->route = array_values($this->route(Art::class, $previousArt));
}
$nextArt = Art::select('id', 'title', 'number', 'text', 'law_id')
->where('law_id',$art->law_id)
->where('number', '>', $art->number)
->orderBy('number')
->first();
if ($nextArt) {
$nextLaw = Law::find($nextArt->law_id);
$nextArt->is_like = $this->isLiked($nextArt->id);
$nextArt->note = Note::select('id', 'note', 'color_code')->where('user_id', auth()->user()->id)->where('art_id', $nextArt->id)->get();
$nextArt->category = $nextLaw?->category?->name;
$nextArt->route = array_values($this->route(Art::class, $nextArt));
}
$results = array_values(array_filter([$previousArt, $art, $nextArt]));
return $this->success($results);
}
public function like(Art $art)
{
$userId = auth()->id();
$existingLike = LikeArt::query()->where('art_id', $art->id)
->where('user_id', $userId)
->first();
if ($existingLike) {
$existingLike->delete();
return $this->success(null, 'Success', 'Art unliked successfully');
}
LikeArt::create([
'art_id' => $art->id,
'user_id' => $userId,
]);
return $this->success(null, 'Success', 'Art liked successfully');
}
private function isLiked($art_id): bool
{
return LikeArt::query()->where('art_id', $art_id)
->where('user_id', auth()->user()->id)
->exists();
}
public function likes()
{
$likes = LikeArt::query()->where('user_id', auth()->user()->id)
->with('art')
->get()
->map(function ($q) {
return [
'id' => $q->art->id,
'title' => $q->art->title,
'text' => $q->art->text
];
});
return $this->success($likes);
}
public function search(Request $request)
{
$validated = $request->validate([
'book_id' => 'nullable|array',
'volume_id' => 'nullable|array',
'division_id' => 'nullable|array',
'section_id' => 'nullable|array',
'part_id' => 'nullable|array',
'gate_id' => 'nullable|array',
'law_id' => 'nullable|array',
'chapter_id' => 'nullable|array',
'branch_id' => 'nullable|array',
'category_id' => 'nullable|int',
'per_page' => 'nullable|integer',
'search' => 'nullable|string'
]);
$models = [
Law::class,
Volum::class,
Book::class,
Division::class,
Section::class,
Chapter::class,
Part::class,
Gate::class,
Branch::class,
Art::class
];
$results = [];
foreach ($models as $modelClass) {
$results = array_merge($results, $this->searchModel($modelClass, $validated));
}
return $this->success($results);
}
private function searchModel($modelClass, $validated)
{
$query = $modelClass::query();
$filters = [
'law_id',
'volume_id',
'division_id',
'section_id',
'part_id',
'gate_id',
'chapter_id',
'branch_id'
];
$model = new $modelClass;
if ($model->getTable() == 'art') {
$query->where(function ($query) use ($validated, $filters) {
if (!empty($validated['search'])) {
$query->where(function ($q) use ($validated) {
$q->where('title', 'LIKE', "%{$validated['search']}%")
->orWhere('text', 'LIKE', "%{$validated['search']}%");
});
}
foreach ($filters as $filter) {
if (!empty($validated[$filter])) {
$query->whereIn($filter, $validated[$filter]);
}
}
});
if (!empty($validated['category_id'])) {
$query->whereIn('law_id', Category::where('type', $this->convertValueTo($validated['category_id']))->get()->pluck('id'));
}
$items = $query->paginate($validated['per_page'] ?? 10);
return $items->map(function ($item) use ($validated, $modelClass) {
$text = $item->text ?? '';
$search = preg_quote($validated['search'] ?? '', '/');
if (!empty($text) && preg_match('/\b(.{0,50})\b(' . $search . ')\b(.{0,50})\b/i', $text, $matches)) {
$context = trim($matches[1] ?? '') . ' ' . $matches[2] . ' ' . trim($matches[3] ?? '');
} else {
$context = $text;
}
$law = Law::find($item->law_id);
return [
'id' => $item->id,
'title' => $item->title,
'text' => $context,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : optional($law)->is_locked,
'type' => 'art',
'route' => array_values($this->route($modelClass, $item)),
'category' => optional($law->category)->name,
'law' => $law?->title,
'count_art' => $law->arts->count(),
'count_volums' => $law->volums->count(),
'price' => $law->price,
'image' => $law->image
];
})->toArray();
} else {
$query = $query->where('title', 'LIKE', '%' . ($validated['search'] ?? '') . '%');
}
$items = $query->paginate($validated['per_page'] ?? 10);
return $items->map(function ($item) use ($modelClass) {
$law = Law::find($item?->law_id ?? $item->id);
if ((new $modelClass)->getTable() == 'laws') {
return [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => (new $modelClass)->getTable(),
'is_locked' => auth()->user()->isSubscriber() !== false ? false : optional($law)->is_locked,
'route' => array_values($this->route($modelClass, $item)),
'category' => optional($law?->category)?->name ?? '',
'image' => $law->image
];
} else if ((new $modelClass)->getTable() == 'art') {
return [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => (new $modelClass)->getTable(),
'is_locked' => auth()->user()->isSubscriber() !== false ? false : optional($law)->is_locked,
'route' => array_values($this->route($modelClass, $item)),
'law' => $law?->title ?? '',
'image' => $law->image
];
} else {
return [
'id' => $item->id,
'title' => $item->title,
'number' => $item->number,
'type' => (new $modelClass)->getTable(),
'is_locked' => auth()->user()->isSubscriber() !== false ? false : optional($law)->is_locked,
'route' => array_values($this->route($modelClass, $item)),
'law' => $law?->title ?? '',
];
}
})->toArray();
}
private function route($modelClass, $query)
{
if ((new $modelClass)->getTable() == '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
]);
$route = array_values($route);
return $route;
}
return [];
}
public function fash_search(Request $request)
{
$search = $request->input('search');
$models = [
Law::class,
Art::class
];
$results = [];
foreach ($models as $modelClass) {
$results = array_merge($results, $this->searchModelFastSearch($modelClass, $search));
}
$models_other = [
Volum::class,
Book::class,
Division::class,
Section::class,
Chapter::class,
Part::class,
Gate::class,
Branch::class,
];
$found = false;
foreach ($models_other as $modelClass) {
if (count($this->searchModelFastSearch($modelClass, $search)) > 0 && !$found) {
$results = array_merge($results, $this->searchModelFastSearch($modelClass, $search));
$found = true;
}
}
$models = [
Law::class,
Volum::class,
Book::class,
Division::class,
Section::class,
Chapter::class,
Part::class,
Gate::class,
Branch::class,
Art::class
];
$count_all = 0;
foreach ($models as $modelClass) {
$count_all += count($this->searchModel($modelClass, $search));
}
return $this->success(['items' => $results, 'count' => $count_all]);
}
private function searchModelFastSearch($modelClass, $search)
{
$results = $modelClass::searchLimit($search);
return $results->map(function ($q) use ($modelClass, $search) {
$instance = new $modelClass;
$table = $instance->getTable();
if ($table === 'art') {
return $this->formatArtResult($q, $search);
}
return $this->formatGenericResult($q, $table);
})->toArray();
}
private function formatGenericResult($q, $table)
{
$category = null;
if ($table === 'laws') {
$category = Law::where('id', $q->id)->first()?->category?->name;
} else {
$category = Law::where('id', $q->law_id)->first()?->category?->name;
}
if ($table !== 'laws') {
return [
'id' => $q->id,
'title' => $q->title,
'type' => $table === 'section' ? 'sections' : $table,
'category' => $category,
'law' => Law::where('id', $q->law_id)->first()?->title
];
}
return [
'id' => $q->id,
'title' => $q->title,
'type' => $table === 'section' ? 'sections' : $table,
'category' => $category,
'image' => Law::where('id', $q->id)->first()?->image
];
}
private function formatArtResult($q, $search)
{
$law = Law::where('id', $q->law_id)->first();
$route = array_filter([
$law?->title,
Volum::find($q->volum_id)?->title,
Book::find($q->book_id)?->title,
Division::find($q->division_id)?->title,
Section::find($q->section_id)?->title,
Chapter::find($q->chapter_id)?->title,
Part::find($q->part_id)?->title,
Gate::find($q->gate_id)?->title,
Branch::find($q->branch_id)?->title
]);
$text = $q->text;
$search = preg_quote($search, '/');
if (preg_match('/\b(.{0,50})\b(' . $search . ')\b(.{0,50})\b/i', $text, $matches)) {
$context = trim($matches[1]) . ' ' . $matches[2] . ' ' . trim($matches[3]);
} else {
$context = $text;
}
return [
'id' => $q->id,
'title' => $q->title,
'text' => $context,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : $law->is_locked,
'type' => 'art',
'route' => $route,
'category' => $law?->category?->name,
'law' => $law?->title,
'count_art' => $law->arts->count(),
'count_volum' => $law->volums->count(),
'price' => $law->price,
'image' => $law->image
];
}
private function convertValueTo($var)
{
switch ($var) {
case 1:
return 'hagigi';
break;
case 2:
return 'kifari';
break;
default:
return 'kifari';
break;
}
}
}

View File

@@ -0,0 +1,96 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\AuthRequest;
use App\Models\User;
use App\Models\UserCode;
use App\Traits\BaseApiResponse;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Http;
class AuthController extends Controller
{
use BaseApiResponse;
public function login(AuthRequest $request)
{
$user = User::query()
->where('mobile', $request->mobile)
->firstOr(function () use ($request) {
return User::create([
'name' => 'Law',
'mobile' => $request->mobile,
'email' => 'law' . $request->mobile . '@law.com',
'password' => Hash::make($request->mobile),
]);
});
$code = random_int(1000, 9999);
UserCode::create([
'user_id' => $user->id,
'code' => $code,
'expired_at' => Carbon::now()->addMinutes(10),
]);
$url = 'https://api.sms.ir/v1/send/verify';
$payload = [
'mobile' => $request->mobile,
'templateId' => '383927',
'parameters' => [
[
'name' => 'CODE',
'value' => strval($code),
],
],
];
$response = Http::withHeaders([
'Content-Type' => 'application/json',
'Accept' => 'text/plain',
'x-api-key' => 'VNXiXc1ERkFUwifxfYFzOIOCjNow9E8as1NpUE5EtkDkaWUlmC09nGxJCFX3kSqD',
])
->post('https://api.sms.ir/v1/send/verify', $payload);
if ($response->successful()) {
return $this->success([], 'موفق', 'کد ورود به شماره موبایل شما ارسال شد');
} else {
return $this->failed([], 'نا موفق', 'در ارسال کد به شماره شما مشکلی وجود دارد');
}
}
public function verify(Request $request)
{
$request->validate([
'mobile' => 'required',
'code' => 'required',
]);
$user = User::where('mobile', $request->mobile)->first();
if (! $user) {
return response()->json(['error' => 'User not found'], 404);
}
$latestCode = UserCode::where('user_id', $user->id)
->where('code', $request->code)
->latest()
->first();
if (! $latestCode) {
return $this->failed([], 'کد ورود اشتباه است', 404);
}
if ($latestCode->code === $request->code) {
$token = $user->createToken('mobile')->plainTextToken;
return $this->success(['token' => $token], 'ورود موفقیت امیز بود');
} else {
return $this->failed([], 'کد ورود اشتباه است', 404);
}
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\BookRequest;
use App\Models\Book;
use App\Models\Law;
use App\Models\Order;
use App\Traits\BaseApiResponse;
class BookController extends Controller
{
use BaseApiResponse;
public function index(BookRequest $request)
{
$validated = $request->validated();
$perPage = $validatedData['per_page'] ?? 15;
$page = $validatedData['page'] ?? 1;
$books = Book::where('volum_id', $validated['volum_id'])->paginate($perPage, ['*'], 'page', $page);
$books->getCollection()->transform(function ($section) {
$section['is_locked'] = auth()->user()->isSubscriber() !== true ? true : Law::where('is_locked',$section['law_id'])->first()?->is_locked ?? false;
unset($section['law_id']);
unset($section['volum_id']);
return $section;
});
return $this->success($books->items(), 'Success');
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class CategoriesController extends Controller
{
use BaseApiResponse;
public function index(Request $request)
{
$perPage = $request->input('per_page', 15);
$page = $request->input('page', 1);
$categories = Category::paginate($perPage, ['*'], 'page', $page);
return $this->success($categories->items());
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\ChapterRequest;
use App\Models\Chapter;
use App\Models\Law;
use App\Models\Order;
use App\Traits\BaseApiResponse;
class ChapterController extends Controller
{
use BaseApiResponse;
public function index(ChapterRequest $request)
{
$validated = $request->validated();
$perPage = $validated['per_page'] ?? 15;
$page = $validated['page'] ?? 1;
$chapters = Chapter::where('book_id' , $validated['book_id'])->paginate($perPage, ['*'], 'page', $page);
$chapters->getCollection()->transform(function ($section) {
$section['is_locked'] = auth()->user()->isSubscriber() !== false ? false : Law::where('is_locked', $section['law_id'])->first()?->is_locked;
unset($section['law_id']);
unset($section['section_id']);
return $section;
});
return $this->success($chapters->items());
}
}

View File

@@ -0,0 +1,151 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\Folder;
use App\Models\FolderArt;
use App\Models\Law;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class FolderController extends Controller
{
use BaseApiResponse;
public function index()
{
$folder = Folder::query()->where('user_id', auth()->id())->get()
->map(function ($q) {
return [
"id" => $q->id,
"name" => $q->name,
"count_of_arts" => $q->arts->count(),
"created_at" => $q->created_at,
];
});
return $this->success($folder);
}
public function create(Request $request)
{
$validted = $request->validate([
'name' => 'required'
]);
Folder::create([
'user_id' => auth()->user()->id,
'name' => $validted['name']
]);
return $this->success([], 'ایجاد شد', 'پوشه با موفقیت ایجاد شد.');
}
public function assign(Request $request)
{
$validted = $request->validate([
'folder_id' => 'required',
'art_id' => 'required'
]);
if (Folder::query()->where('id', $validted['folder_id'])->count() == 0) {
return $this->failed([], 'پوشه مورد نظر یافت نشد', 404);
}
if (FolderArt::query()->where('folder_id', $validted['folder_id'])->where('art_id', $validted['art_id'])->count() > 0) {
return $this->failed([], 'ارت مورد نظر قبلا اضافه شده است', 400);
}
FolderArt::create([
'folder_id' => $validted['folder_id'],
'art_id' => $validted['art_id']
]);
return $this->success([], 'اضافه شد', 'ارت با موفیت اضافه شد');
}
public function folder($id)
{
$arts = FolderArt::where('folder_id', $id)
->with('arts.chapter', 'arts.part', 'arts.volum', 'arts.law', 'arts.book', 'arts.section', 'arts.gate')
->get()
->pluck('arts')
->flatten();
$arts = $arts->map(function ($art) {
$text = $art->text ?? '';
$shortText = '';
if (strlen($text) > 50) {
$shortText = substr($text, 0, 50);
$shortText = substr($shortText, 0, strrpos($shortText, ' ')) . '...';
} else {
$shortText = $text;
}
return [
'id' => $art->id,
'title' => $art->title,
'text' => $shortText,
'number' => $art->number,
'is_locked' => auth()->user()->isSubscriber() !== false ? false : Law::where('is_locked', $art->law->id)->first()?->is_locked,
'chapter' => $art->chapter != null ? [
'id' => $art->chapter->id,
'title' => $art->chapter->title,
'number' => $art->chapter->number,
] : null,
'part' => $art->part != null ? [
'id' => $art->part->id,
'title' => $art->part->title,
'number' => $art->part->number,
] : null,
'volum' => $art->volum != null ? [
'id' => $art->volum->id,
'title' => $art->volum->title,
'number' => $art->volum->number,
] : null,
'law' => $art->law != null ? [
'id' => $art->law->id,
'title' => $art->law->title,
'number' => $art->law->number,
] : null,
'book' => $art->book != null ? [
'id' => $art->book->id,
'title' => $art->book->title,
'number' => $art->book->number,
] : null,
'section' => $art->section != null ? [
'id' => $art->section->id,
'title' => $art->section->title,
'number' => $art->section->number,
] : null,
'gate' => $art->gate != null ? [
'id' => $art->gate->id,
'title' => $art->gate->title,
'number' => $art->gate->number,
] : null,
];
});
return $this->success($arts);
}
public function delete_folder($id)
{
if (Folder::query()->where('id', $id)->count() == 0) {
return $this->failed('پوشه یافت نشد', 'پوشه مورد نظر یافت نشد', 404);
}
Folder::query()->where('id', $id)->delete();
return $this->success([], 'حذف شد', 'پوشه با موفقیت حذف شد');
}
public function delete_art($id,$art_id)
{
if (FolderArt::query()->where('folder_id', $id)->where('art_id', $art_id)->count() == 0) {
return $this->failed([], 'ارت مورد نظر یافت نشد', 404);
}
FolderArt::query()->where('folder_id', $id)->where('art_id', $art_id)->delete();
return $this->success([], 'حذف شد', 'ارت با موفقیت حذف شد');
}
}

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\ChapterRequest;
use App\Models\Gate;
use App\Models\Law;
use App\Models\Order;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class GateController extends Controller
{
use BaseApiResponse;
public function index(ChapterRequest $request)
{
$validated = $request->validated();
$gates = Gate::where('book_id', $validated['book_id'])
->paginate($validated['per_page'], ['*'], 'page', $validated['page']);
$gates->getCollection()->transform(function ($gate) {
unset($gate['book_id']);
$gate['is_locked'] = auth()->user()->isSubscriber() !== false ? false : Law::where('is_locked', $gate['law_id'])->first()?->is_locked;
unset($gate['law_id']);
return $gate;
});
return $this->success($gates->items());
}
}

View File

@@ -0,0 +1,131 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
// 1. IMPORT THE JOB WE CREATED
use App\Jobs\CheckBazaarSubscription;
// These are your existing imports
use App\Models\Law;
use App\Models\Notification;
use App\Models\RecentArt;
use App\Models\UserSubscriber;
use App\Traits\BaseApiResponse;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
class HomeController extends Controller
{
use BaseApiResponse;
public function index()
{
$user = auth()->user();
// 2. DISPATCH THE JOB AT THE VERY BEGINNING
// ===================================================================
// Find the user's latest subscription that has a purchase token
$latestSubscription = $user->userSubscribers()->whereNotNull('purchase_token')->latest()->first();
// If such a subscription exists, create a new job and hand it to the queue
// if ($latestSubscription) {
// CheckBazaarSubscription::dispatch($latestSubscription);
// }
// ===================================================================
// Your API now continues immediately without waiting for the check to finish.
// --- ALL THE REST OF YOUR CODE REMAINS EXACTLY THE SAME ---
$recent = RecentArt::query()->where('user_id', $user->id)->get()->map(function ($q) {
return [
'id' => $q->law?->id,
'name' => $q->law?->title,
'is_locked' => $q->law?->is_locked
];
});
$laws = Law::orderBy('created_at')->get()->map(function ($q) {
return [
'id' => $q->id,
'name' => $q->title,
'is_locked' => $q->is_locked
];
});
$categories = ['hagigi', 'kifari'];
$lawsByCategory = [];
foreach ($categories as $category) {
$lawsByCategory[$category] = Law::whereHas('category', function ($q) use ($category) {
$q->where('type', $category);
})->get()->map(function ($q) {
return [
"id" => $q->id,
"title" => $q->title,
"is_locked" => $q->is_locked,
"category_id" => $q->category_id,
"price" => $q->price,
"image" => $q->image,
"type" => 'law'
];
});
}
$free_law = Law::where('is_locked', false)->orderBy('created_at')->get()->map(function ($q) {
return [
'id' => $q->id,
'name' => $q->title,
'is_locked' => $q->is_locked
];
});
$current_plan = null;
$freeSubscription = $user->userSubscribers()
->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})
->where('expired_at', '>=', now())
->first();
$expiredAt = null;
$current_plan = null;
if ($freeSubscription) {
$expiredAt = $freeSubscription->expired_at;
$current_plan = [
'id' => $freeSubscription->id,
'name' => $freeSubscription->subscribe->name,
'price' => $freeSubscription->subscribe->price,
'expired_day' => $freeSubscription->expired_at->diffInDays(now()),
'is_free' => true
];
}
$latestSubscription = $user->userSubscribers()->latest()->first();
$expiredDays = UserSubscriber::query()->where('user_id', $user->id)->where('expired_at', '>=', now())->get()->sum(function ($subscriber) {return $subscriber->expired_at->diffInDays(now());});
$purchase_token = $latestSubscription?->purchase_token;
$current_plan = [
'id' => $latestSubscription->id,
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
'price' => $latestSubscription->subscribe->price ?? 100,
'expired_day' => $expiredDays,
'is_free' => false
];
$unread_notifications_count = Notification::unreadForUser($user->id)->count();
return $this->success([
'recent' => $recent,
'laws' => $lawsByCategory,
'last_law' => $laws,
'free' => $free_law,
'current_plan' => $current_plan,
'unread_notifications_count' => $unread_notifications_count,
]);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\Law;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class LawController extends Controller
{
use BaseApiResponse;
public function index(Request $request)
{
$perPage = $request->input('per_page', 15);
$page = $request->input('page', 1);
$laws = Law::simplePaginate($perPage, ['*'], 'page', $page);
return $this->success($laws->items(), 'Success');
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\Note;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class NoteController extends Controller
{
use BaseApiResponse;
public function create(Request $request)
{
$validated = $request->validate([
'art_id' => 'required',
'note' => 'required',
'color_code' => 'nullable'
]);
Note::query()->create(
[
'user_id' => auth()->user()->id,
'art_id' => $validated['art_id'],
'note' => $validated['note'],
'color_code' => $validated['color_code'],
]
);
return $this->success([], 'successfully created.');
}
public function update($id, Request $request)
{
$validated = $request->validate([
'note' => 'required',
'color_code' => 'nullable'
]);
$note = Note::findOrFail($id);
$note->update([
'note' => $validated['note'],
'color_code' => $validated['color_code'],
]);
return $this->success([], 'successfully updated.');
}
public function destroy($id)
{
$note = Note::findOrFail($id);
$note->delete();
return $this->success([], 'successfully deleted.');
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\Notification;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class NotificationController extends Controller
{
use BaseApiResponse;
public function index(Request $request)
{
$user = auth()->user();
$all = Notification::query()->latest()->get();
$now = now();
foreach ($all as $notification) {
$user->notifications()->syncWithoutDetaching([
$notification->id => ['read_at' => $now],
]);
}
$notifications = $all->map(function ($notification) {
return [
'id' => $notification->id,
'title' => $notification->title,
'description' => $notification->description,
'created_at' => $notification->created_at->toIso8601String(),
'is_read' => true,
];
});
return $this->success([
'notifications' => $notifications,
]);
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\ChapterRequest;
use App\Models\Law;
use App\Models\Order;
use App\Models\Part;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
class PartController extends Controller
{
use BaseApiResponse;
public function index(ChapterRequest $request)
{
$validated = $request->validated();
$perPage = $request->input('per_page', 15);
$page = $request->input('page', 1);
$parts = Part::where('book_id' , $request->input('book_id'))->paginate($perPage, ['*'], 'page', $page);
$parts->getCollection()->transform(function ($part) {
unset($part['book_id']);
$gate['is_locked'] = auth()->user()->isSubscriber() !== false ? false : Law::where('is_locked', $part['law_id'])->first()?->is_locked;
unset($gate['law_id']);
return $gate;
});
return $this->success($parts->items());
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\PaymentTransaction;
use App\Models\SubscribePlan;
use App\Models\UserSubscriber;
use App\Traits\BaseApiResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Shetabit\Multipay\Invoice;
use Shetabit\Payment\Facade\Payment;
class PayController extends Controller
{
use BaseApiResponse;
public function pay(Request $request)
{
$validation = $request->validate([
'subscribe_plan_id' => 'required|exists:subscribe_plans,id',
]);
$subscribePlan = SubscribePlan::findOrFail($request->subscribe_plan_id);
try {
$invoice = (new Invoice)->amount($subscribePlan->price);
$callback = 'https://ghaafapp.ir';
$payment = Payment::callbackUrl($callback . '/payment/callback')
->purchase($invoice, function ($driver, $transaction_id) use ($subscribePlan) {
PaymentTransaction::create([
'user_id' => auth()->user()->id,
'subscribe_plan_id' => $subscribePlan->id,
'transaction_id' => $transaction_id,
'amount' => $subscribePlan->price,
'status' => 'pending',
]);
})
->pay();
return $this->success(['url' => $payment]);
} catch (\Exception $e) {
Log::error('Payment initiation failed: ' . $e->getMessage());
return $this->failed('Payment initialization failed', $e->getMessage());
}
}
public function callback(Request $request)
{
try {
$url = 'http://bitpay.ir/payment/gateway-result-second';
$api = '066fd-d622e-690a9-be618-ddf02bc6059bbbd67c317bb340d1';
$trans_id = $request->input('trans_id');
$id_get = $request->input('id_get');
$result = $this->get($url, $api, $trans_id, $id_get);
$parseDecode = json_decode($result);
if ($parseDecode->status == 1) {
$transaction = PaymentTransaction::where('transaction_id', $id_get)->first();
$transaction->update([
'status' => 'success',
]);
$expiredAt = now()->addDays($transaction->subscribePlan->expired_day);
UserSubscriber::where('user_id', $transaction->user_id)->delete();
$transaction->user->userSubscribers()->create([
'subscribe_plan_id' => $transaction->subscribe_plan_id,
'expired_at' => $expiredAt,
]);
return $this->success([], 'Payment Successful', 'Subscription successfully activated.');
}
return $this->failed([], ['title' => 'Payment Failed', 'message' => 'Payment verification failed.']);
} catch (\Exception $e) {
Log::info('error in callback function', ['error' => $e->getMessage()]);
return $this->failed([], ['title' => 'Payment Failed', 'message' => 'Payment verification failed.']);
}
}
private function send($url, $api, $amount, $redirect, $factorId, $name, $email, $description)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, "api=$api&amount=$amount&redirect=$redirect&factorId=$factorId&name=$name&email=$email&description=$description");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
private function get($url, $api, $trans_id, $id_get)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, "api=$api&id_get=$id_get&trans_id=$trans_id&json=1");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}

View File

@@ -0,0 +1,59 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\SectionRequest;
use App\Models\Law;
use App\Models\LikeSection;
use App\Models\Order;
use App\Models\Section;
use App\Traits\BaseApiResponse;
class SectionController extends Controller
{
use BaseApiResponse;
public function index(SectionRequest $request)
{
$validatedData = $request->validated();
$perPage = $validatedData['per_page'] ?? 15;
$page = $validatedData['page'] ?? 1;
$section = Section::where('book_id', $validatedData['book_id'])->paginate($perPage, ['*'], 'page', $page);
$section->getCollection()->transform(function ($section) {
unset($section['book_id']);
$section['is_locked'] = auth()->user()->isSubscriber() !== false ? false : Law::where('is_locked',$section['law_id'])->first()?->is_locked;
unset($section['law_id']);
return $section;
});
return $this->success($section->items(), 'Success');
}
public function like(Section $section)
{
$userId = auth()->id();
$existingLike = LikeSection::query()->where('section_id', $section->id)
->where('user_id', $userId)
->first();
if ($existingLike) {
$existingLike->delete();
return $this->success(null, 'Success', 'Section unliked successfully');
}
LikeSection::create([
'section_id' => $section->id,
'user_id' => $userId,
]);
return $this->success(null, 'Success', 'Section liked successfully');
}
}

View File

@@ -0,0 +1,275 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Models\PaymentTransaction;
use App\Models\SubscribePlan;
use App\Models\UserSubscriber;
use App\Traits\BaseApiResponse;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Shetabit\Multipay\Invoice;
use Shetabit\Payment\Facade\Payment;
class SubscribePlanController extends Controller
{
use BaseApiResponse;
public function index()
{
$subscribePlans = SubscribePlan::query()->where('is_active', true)->get()->map(function ($q) {
if ($q->is_free) {
if (auth()->user()->subscribePlans()->first() !== null) {
return null;
}
}
return [
'id' => $q->id,
'name' => $q->name,
'price' => $q->price,
'expired_day' => $q->expired_day,
'is_free' => $q->is_free == 1 ? true : false,
'type' => $q->type,
'transaction' => $q->transaction
];
})->filter(function ($q) {
return $q != null;
});
return $this->success(array_values($subscribePlans->toArray()), 'Subscribe Plan', 'Subscribe Plan List');
}
public function subscribe(Request $request)
{
$request->validate([
'subscribe_plan_id' => 'required|exists:subscribe_plans,id',
]);
$subscribePlan = SubscribePlan::findOrFail($request->subscribe_plan_id);
$user = auth()->user();
$activeSubscription = $user->userSubscribers()
->where('expired_at', '>', now())
->first();
if ($activeSubscription && !$activeSubscription?->subscribe?->is_free && !$subscribePlan->is_free) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'You already have an active subscription. Please wait until it expires.']);
}
if ($subscribePlan->is_free) {
$hasUsedFreePlan = $user->userSubscribers()
->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})->exists();
if ($hasUsedFreePlan) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'You have already used the free plan.']);
}
$expiredAt = now()->addDays($subscribePlan->expired_day + 1);
$user->userSubscribers()->create([
'subscribe_plan_id' => $subscribePlan->id,
'expired_at' => $expiredAt,
]);
return $this->success(null, 'Subscribe Plan', 'Free Subscribe Plan Successfully Activated');
}
try {
$invoice = (new Invoice)->amount($subscribePlan->price);
$callback = 'https://ghaafapp.ir';
$payment = Payment::callbackUrl($callback)
->purchase($invoice, function ($driver, $transaction_id) use ($subscribePlan) {
PaymentTransaction::create([
'user_id' => auth()->user()->id,
'subscribe_plan_id' => $subscribePlan->id,
'transaction_id' => $transaction_id,
'amount' => $subscribePlan->price,
'status' => 'pending',
]);
})
->pay();
return $this->success(['url' => $payment]);
} catch (\Exception $e) {
Log::error('Payment initiation failed: ' . $e->getMessage());
return $this->failed(null, 'درگاه پرداخت در دسترس نیست');
}
}
public function subscribe_new(Request $request)
{
$package_name_bazzar = 'com.razzaghi.lawbook.android';
$request->validate([
'subscribe_plan_id' => 'required|exists:subscribe_plans,id',
'subscription_id' => 'nullable',
'purchase_token' => 'nullable'
]);
$subscribePlan = SubscribePlan::findOrFail($request->subscribe_plan_id);
$user = auth()->user();
$activeSubscription = $user->userSubscribers()
->where('expired_at', '>', now())
->latest('expired_at')
->first();
if ($subscribePlan->is_free) {
$hasUsedFreePlan = $user->userSubscribers()
->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})
->exists();
if ($hasUsedFreePlan) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'You have already used the free plan.']);
}
$expiredAt = $activeSubscription ? $activeSubscription->expired_at->addDays($subscribePlan->expired_day) : now()->addDays($subscribePlan->expired_day);
$user->userSubscribers()->create([
'subscribe_plan_id' => $subscribePlan->id,
'expired_at' => $expiredAt,
'is_free' => true
]);
return $this->success(null, 'Subscribe Plan', 'Free Subscribe Plan Successfully Activated');
}
$subscription_id = $request->input('subscription_id');
$purchase_token = $request->input('purchase_token');
if (!$subscription_id || !$purchase_token) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Invalid subscription details.']);
}
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/applications/$package_name_bazzar/subscriptions/$subscription_id/purchases/$purchase_token";
$client = new \GuzzleHttp\Client();
try {
$response = $client->get($url, [
'headers' => [
'CAFEBAZAAR-PISHKHAN-API-SECRET' => 'eyJhbGciOiJIUzI1NiIsImtpZCI6ImFuY2llbnQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJuYXNoZXItcGlzaGtoYW4tYXBpIiwiaWF0IjoxNzQwMjQ3NTMzLCJleHAiOjQ4OTM4NDc1MzMsImFwaV9hZ2VudF9pZCI6MzQ2NX0.UCrr3IHxCqn77ckxfnaubrsyCfrhPm18gJgyg1qNqwA',
]
]);
$data = json_decode($response->getBody(), true);
if (!isset($data['validUntilTimestampMsec']) || $data['validUntilTimestampMsec'] < now()->timestamp * 1000) {
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Invalid or expired subscription.']);
}
$bazaarExpiredAt = Carbon::createFromTimestampMs($data['validUntilTimestampMsec']);
$expiredAt = $activeSubscription ? $activeSubscription->expired_at->addDays($subscribePlan->expired_day) : $bazaarExpiredAt;
$user->userSubscribers()->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})
->update(['expired_at' => now()]);
$user->userSubscribers()->create([
'subscribe_plan_id' => $subscribePlan->id,
'expired_at' => $expiredAt,
'subscription_id' => $subscription_id,
'purchase_token' => $purchase_token,
'is_free' => false
]);
return $this->success(null, 'Subscribe Plan', 'Subscription successfully activated.');
} catch (\Exception $e) {
Log::error('Error in subscription', ['Error' => $e->getMessage()]);
return $this->failed(null, ['title' => 'Subscribe Plan', 'message' => 'Failed to verify subscription.']);
}
}
public function paymentCallback(Request $request)
{
try {
$url = 'http://bitpay.ir/payment/gateway-result-second';
$api = '066fd-d622e-690a9-be618-ddf02bc6059bbbd67c317bb340d1';
$trans_id = $request->input('trans_id');
$id_get = $request->input('id_get');
$result = $this->get($url, $api, $trans_id, $id_get);
$parseDecode = json_decode($result);
if ($parseDecode->status == 1) {
$transaction = PaymentTransaction::where('transaction_id', $id_get)->firstOrFail();
$transaction->update([
'status' => 'success',
]);
$expiredAt = now()->addDays($transaction->subscribePlan->expired_day);
UserSubscriber::where('user_id', $transaction->user_id)->delete();
$transaction->user->userSubscribers()->create([
'subscribe_plan_id' => $transaction->subscribe_plan_id,
'expired_at' => $expiredAt,
]);
return $this->success([], 'Payment Successful', 'Subscription successfully activated.');
}
return $this->failed([], ['title' => 'Payment Failed', 'message' => 'Payment verification failed.']);
} catch (\Exception $e) {
return $this->failed([], ['title' => 'Payment Failed', 'message' => 'Payment verification failed.']);
}
}
public function current()
{
$user = auth()->user();
$subscribePlans = UserSubscriber::where('user_id', $user->id)
->where('expired_at', '>', now())
->orderBy('expired_at', 'desc')
->get();
if ($subscribePlans->isNotEmpty()) {
$totalExpiredDays = $subscribePlans->sum('expired_day');
$latestPlan = $subscribePlans->first();
$subscribePlanData = [
'id' => $latestPlan->id,
'name' => $latestPlan->subscribe->name,
'price' => $latestPlan->subscribe->price,
'expired_day' => $totalExpiredDays,
'is_free' => $latestPlan->subscribe->is_free == 1,
'expired_at' => Carbon::now()->addDays($totalExpiredDays)->format('Y-m-d'),
];
return $this->success($subscribePlanData, 'Subscribe Plan', 'Current Subscribe Plan');
}
return $this->success(null, 'Subscribe Plan', 'No Subscribe Plan');
}
private function send($url, $api, $amount, $redirect, $factorId, $name, $email, $description)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, "api=$api&amount=$amount&redirect=$redirect&factorId=$factorId&name=$name&email=$email&description=$description");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
private function get($url, $api, $trans_id, $id_get)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, "api=$api&id_get=$id_get&trans_id=$trans_id&json=1");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Http\Controllers\api;
use App\Http\Controllers\Controller;
use App\Http\Requests\VersionRequest;
use App\Models\Version;
use App\Traits\BaseApiResponse;
class VersionController extends Controller
{
use BaseApiResponse;
public function index(VersionRequest $versionRequest)
{
$version = Version::query()->where('type', $versionRequest->type)->orderBy('id', 'desc')->first();
if (!$version) {
return $this->failed([], 'Version not found');
}
if (intval($version->number) > $versionRequest->number) {
return $this->success([
'force_update' => $version->force_update == 1 ? true : false
], 'Your app need to be updated');
}
return $this->success([
'force_update' => false
], 'Version successfully');
}
}

View File

@@ -0,0 +1,864 @@
<?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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $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('is_locked', $item->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('is_locked', $item->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('is_locked', $item->law_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('is_locked', $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);
}
}

68
app/Http/Kernel.php Normal file
View File

@@ -0,0 +1,68 @@
<?php
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array<int, class-string|string>
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];
/**
* The application's route middleware groups.
*
* @var array<string, array<int, class-string|string>>
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
/**
* The application's middleware aliases.
*
* Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
*
* @var array<string, class-string|string>
*/
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Http\Request;
class Authenticate extends Middleware
{
/**
* Get the path the user should be redirected to when they are not authenticated.
*/
protected function redirectTo(Request $request): ?string
{
return $request->expectsJson() ? null : route('login');
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Cookie\Middleware\EncryptCookies as Middleware;
class EncryptCookies extends Middleware
{
/**
* The names of the cookies that should not be encrypted.
*
* @var array<int, string>
*/
protected $except = [
//
];
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance as Middleware;
class PreventRequestsDuringMaintenance extends Middleware
{
/**
* The URIs that should be reachable while maintenance mode is enabled.
*
* @var array<int, string>
*/
protected $except = [
//
];
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Http\Middleware;
use App\Providers\RouteServiceProvider;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next, string ...$guards): Response
{
$guards = empty($guards) ? [null] : $guards;
foreach ($guards as $guard) {
if (Auth::guard($guard)->check()) {
return redirect(RouteServiceProvider::HOME);
}
}
return $next($request);
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\TrimStrings as Middleware;
class TrimStrings extends Middleware
{
/**
* The names of the attributes that should not be trimmed.
*
* @var array<int, string>
*/
protected $except = [
'current_password',
'password',
'password_confirmation',
];
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Middleware\TrustHosts as Middleware;
class TrustHosts extends Middleware
{
/**
* Get the host patterns that should be trusted.
*
* @return array<int, string|null>
*/
public function hosts(): array
{
return [
$this->allSubdomainsOfApplicationUrl(),
];
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Http\Middleware\TrustProxies as Middleware;
use Illuminate\Http\Request;
class TrustProxies extends Middleware
{
/**
* The trusted proxies for this application.
*
* @var array<int, string>|string|null
*/
protected $proxies;
/**
* The headers that should be used to detect proxies.
*
* @var int
*/
protected $headers =
Request::HEADER_X_FORWARDED_FOR |
Request::HEADER_X_FORWARDED_HOST |
Request::HEADER_X_FORWARDED_PORT |
Request::HEADER_X_FORWARDED_PROTO |
Request::HEADER_X_FORWARDED_AWS_ELB;
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Routing\Middleware\ValidateSignature as Middleware;
class ValidateSignature extends Middleware
{
/**
* The names of the query string parameters that should be ignored.
*
* @var array<int, string>
*/
protected $except = [
// 'fbclid',
// 'utm_campaign',
// 'utm_content',
// 'utm_medium',
// 'utm_source',
// 'utm_term',
];
}

View File

@@ -0,0 +1,17 @@
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array<int, string>
*/
protected $except = [
//
];
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class AuthRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'mobile' => 'required',
];
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class BookRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'volum_id' => 'required',
// 'book_id' => 'required',
'per_page' => 'nullable|numeric',
'page' => 'nullable|numeric',
];
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class ChapterRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'book_id' => 'required',
'per_page' => 'nullable|numeric',
'page' => 'nullable|numeric',
];
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class CheckBookFilterRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'category_id' => 'nullable',
'book_id' => 'nullable',
'volume_id' => 'nullable',
'division_id' => 'nullable',
'section_id' => 'nullable',
'part_id' => 'nullable',
'gate_id' => 'nullable',
'law_id' => 'nullable',
'chapter_id' => 'nullable',
'branch_id' => 'nullable',
'per_page' => 'nullable',
'art_id' => 'nullable',
];
}
}

View File

@@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class CheckBookRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'book_id' => 'nullable',
'volume_id' => 'nullable',
'division_id' => 'nullable',
'section_id' => 'nullable',
'part_id' => 'nullable',
'gate_id' => 'nullable',
'law_id' => 'nullable',
'chapter_id' => 'nullable',
'branch_id' => 'nullable',
'per_page' => 'nullable',
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class GetApiRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'book_id' => 'required'
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class SectionRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'book_id' => 'required',
'per_page' => 'nullable|numeric',
'page' => 'nullable|numeric',
];
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class SuggestionRequest extends FormRequest
{
use FailValidation;
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'text' => 'required'
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class VersionRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'type' => 'required|in:ios,web,android',
'number' => 'required|integer',
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Http\Requests;
use App\Traits\FailValidation;
use Illuminate\Foundation\Http\FormRequest;
class VolumRequest extends FormRequest
{
use FailValidation;
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'law_id' => 'required',
'per_page' => 'nullable|numeric',
'page' => 'nullable|numeric',
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Http\Requests\admin;
use Illuminate\Foundation\Http\FormRequest;
class CreateVersionStore extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'code' => 'required',
'number' => 'required',
'log' => 'required',
'force_update' => 'nullable',
'type' => 'required'
];
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Http\Requests\admin\Notification;
use Illuminate\Foundation\Http\FormRequest;
class StoreRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'description' => 'nullable|string',
];
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Http\Requests\admin\Notification;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'title' => 'required|string|max:255',
'description' => 'nullable|string',
];
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Http\Requests\admin\SubscribePlan;
use Illuminate\Foundation\Http\FormRequest;
class CreateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string',
'price' => 'required|numeric',
'expired_day' => 'required|integer',
'is_active' => 'nullable',
'is_free' => 'nullable',
'type' => 'nullable',
'transaction' => 'nullable'
];
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Http\Requests\admin\SubscribePlan;
use Illuminate\Foundation\Http\FormRequest;
class UpdateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
'name' => 'required|string',
'price' => 'required|numeric',
'expired_day' => 'required|integer',
'is_active' => 'nullable',
'is_free' => 'nullable',
'type' => 'nullable',
'transaction' => 'nullable'
];
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace App\Http\Requests\admin;
use Illuminate\Foundation\Http\FormRequest;
class UpdateVersionStore extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'code' => 'required',
'number' => 'required',
'log' => 'required',
'force_update' => 'nullable',
'type' => 'required'
];
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Http\Requests\admin\category;
use Illuminate\Foundation\Http\FormRequest;
class CreateCategoryRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required',
'label' => 'required',
];
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Http\Requests\admin\category;
use Illuminate\Foundation\Http\FormRequest;
class UpdateCategoryRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required',
'label' => 'required',
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Requests\admin\user;
use Illuminate\Foundation\Http\FormRequest;
class CreateUserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required',
'email' => 'required|email|unique:users',
'mobile' => 'required|unique:users',
'is_admin' => 'nullable',
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Requests\admin\user;
use Illuminate\Foundation\Http\FormRequest;
class UpdateUserRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
public function rules(): array
{
return [
'name' => 'required',
'email' => 'required|email|unique:users',
'mobile' => 'required|unique:users',
'is_admin' => 'nullable',
];
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\ResourceCollection;
class ArtCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @return array<int|string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'text' => $this->text,
'number' => $this->number,
];
}
}

View File

@@ -0,0 +1,89 @@
<?php
namespace App\Jobs;
// These are the classes we need to import for our job to work
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\UserSubscriber; // Your model for tracking user subscriptions
use Carbon\Carbon;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
class CheckBazaarSubscription implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// This variable will hold the user's subscription details
protected $userSubscriber;
/**
* This is the constructor. It runs when we first create the job.
* We pass the user's subscription data here so the job knows what to check.
*/
public function __construct(UserSubscriber $userSubscriber)
{
$this->userSubscriber = $userSubscriber;
}
/**
* This is the main part of the job that does the work.
* Laravel will automatically run this 'handle' method in the background.
*/
public function handle()
{
// Get the purchase token from the subscription details we received
$purchase_token = $this->userSubscriber->purchase_token;
$package_name = 'com.razzaghi.lawbook.android'; // Your app's package name
// If there's no purchase token, we can't do anything, so we stop.
if (!$purchase_token) {
Log::info('Job skipped: No purchase token for UserSubscriber ID: ' . $this->userSubscriber->id);
return;
}
// This is the URL for the external subscription API
$url = "https://pardakht.cafebazaar.ir/devapi/v2/api/applications/{$package_name}/active-subscriptions/{$purchase_token}";
// We use a try-catch block to handle any potential errors gracefully
try {
// Make the API call using Laravel's built-in HTTP client
$response = Http::withHeaders([
// IMPORTANT: Replace this with your actual secret key!
'CAFEBAZAAR-PISHKHAN-API-SECRET' => 'eyJhbGciOiJIUzI1NiIsImtpZCI6ImFuY2llbnQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJuYXNoZXItcGlzaGtoYW4tYXBpIiwiaWF0IjoxNzQwMjQ3NTMzLCJleHAiOjQ4OTM4NDc1MzMsImFwaV9hZ2VudF9pZCI6MzQ2NX0.UCrr3IHxCqn77ckxfnaubrsyCfrhPm18gJgyg1qNqwA',
])->get($url);
// Check if the API call was successful
if ($response->successful()) {
$data = $response->json();
// Check if the subscription is still valid according to the API response
if (isset($data['subscriptions'][0]['validUntilTimestampMsec']) && $data['subscriptions'][0]['validUntilTimestampMsec'] > now()->timestamp * 1000) {
// Convert the expiration date from the API into a standard format
$bazaarExpiredAt = Carbon::createFromTimestampMs($data['subscriptions'][0]['validUntilTimestampMsec']);
// If the expiration date in our database is different, update it
if ($this->userSubscriber->expired_at->notEqualTo($bazaarExpiredAt)) {
$this->userSubscriber->update(['expired_at' => $bazaarExpiredAt]);
Log::info('Subscription date updated for user: ' . $this->userSubscriber->user_id);
}
} else {
// If the subscription is no longer valid, update it in our database to expire now
$this->userSubscriber->update(['expired_at' => now()]);
Log::warning('Subscription found expired on Bazaar for user: ' . $this->userSubscriber->user_id);
}
} else {
// If the API call failed, log the error for debugging
Log::error('Failed to fetch subscription from Bazaar', ['status' => $response->status(), 'body' => $response->body()]);
}
} catch (\Exception $e) {
// If something else went wrong (e.g., network error), log it
Log::error('Exception while checking Bazaar subscription', ['error' => $e->getMessage()]);
}
}
}

67
app/Models/Art.php Normal file
View File

@@ -0,0 +1,67 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Art extends Model
{
use HasFactory;
protected $fillable = ['title', 'text', 'is_free', 'number', 'chapter_id', 'part_id', 'volum_id', 'law_id', 'book_id', 'section_id', 'gate_id', 'division_id', 'branch_id'];
protected $hidden = ['created_at', 'updated_at'];
public function chapter()
{
return $this->belongsTo(Chapter::class);
}
public function part()
{
return $this->belongsTo(Part::class);
}
public function volum()
{
return $this->belongsTo(Volum::class);
}
public function law()
{
return $this->belongsTo(Law::class);
}
public function book()
{
return $this->belongsTo(Book::class);
}
public function section()
{
return $this->belongsTo(Section::class);
}
public function gate()
{
return $this->belongsTo(Gate::class);
}
public function judicialPrecedents()
{
return $this->belongsToMany(JudicialPrecedent::class, 'art_judicial_precedent');
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'like', "%{$searchTerm}%")->get();
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")
->orWhere('text', 'like', "%{$searchTerm}%")->limit(3)->get();
}
}

61
app/Models/Book.php Normal file
View File

@@ -0,0 +1,61 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Book extends Model
{
use HasFactory;
protected $fillable = ['title', 'number', 'volum_id', 'law_id'];
protected $hidden = ['created_at', 'updated_at'];
public function art()
{
return $this->hasMany(Art::class);
}
public function sections()
{
return $this->hasMany(Section::class);
}
public function gates()
{
return $this->hasMany(Gate::class);
}
public function parts()
{
return $this->hasMany(Part::class);
}
public function chapters()
{
return $this->hasMany(Chapter::class);
}
public function divisions()
{
return $this->hasMany(Division::class);
}
public function branchs()
{
return $this->hasMany(Branch::class);
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
}

29
app/Models/Branch.php Normal file
View File

@@ -0,0 +1,29 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Branch extends Model
{
use HasFactory;
protected $fillable = ['title', 'number', 'chapter_id', 'part_id', 'volum_id', 'law_id', 'book_id', 'section_id', 'gate_id','division_id'];
public function art()
{
return $this->hasMany(Art::class);
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
}

32
app/Models/Category.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
use HasFactory;
protected $fillable = [
'name',
'type'
];
public function laws()
{
return $this->hasMany(Law::class);
}
public static function getType()
{
return [
'hagigi' => 'حقیقی',
'kifari' => 'کیفری'
];
}
}

51
app/Models/Chapter.php Normal file
View File

@@ -0,0 +1,51 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Chapter extends Model
{
use HasFactory;
protected $fillable = ['title', 'number', 'section_id', 'book_id', 'volum_id', 'law_id','division_id'];
protected $hidden = ['created_at', 'updated_at'];
public function parts()
{
return $this->hasMany(Part::class);
}
public function gate()
{
return $this->hasMany(Gate::class);
}
public function gates()
{
return $this->hasMany(Gate::class);
}
public function branch()
{
return $this->hasMany(Branch::class);
}
public function art()
{
return $this->hasMany(Art::class);
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
}

71
app/Models/Division.php Normal file
View File

@@ -0,0 +1,71 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Division extends Model
{
use HasFactory;
protected $fillable = ['title', 'number', 'book_id', 'volum_id', 'law_id'];
public function sections()
{
return $this->hasMany(Section::class);
}
public function division()
{
return $this->hasMany(Division::class);
}
public function chapters()
{
return $this->hasMany(Chapter::class);
}
public function parts()
{
return $this->hasMany(Part::class);
}
public function gate()
{
return $this->hasMany(Gate::class);
}
public function branch()
{
return $this->hasMany(Branch::class);
}
public function art()
{
return $this->hasMany(Art::class);
}
public function gates()
{
return $this->hasMany(Gate::class);
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
public function branchs()
{
return $this->hasMany(Branch::class);
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
}

21
app/Models/Folder.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Folder extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'name'
];
public function arts()
{
return $this->belongsToMany(Art::class, 'folder_art');
}
}

21
app/Models/FolderArt.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class FolderArt extends Model
{
use HasFactory;
protected $fillable = [
'folder_id',
'art_id'
];
public function arts()
{
return $this->belongsTo(Art::class,'art_id');
}
}

43
app/Models/Gate.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Gate extends Model
{
use HasFactory;
protected $fillable = ['title', 'number', 'part_id', 'chapter_id', 'section_id', 'book_id', 'volum_id', 'law_id','division_id'];
protected $hidden = ['created_at', 'updated_at'];
public function branch()
{
return $this->hasMany(Branch::class);
}
public function art()
{
return $this->hasMany(Art::class);
}
public function parts()
{
return $this->hasMany(Part::class);
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class JudicialPrecedent extends Model
{
use HasFactory;
protected $fillable = [
'ruling_number',
'ruling_date',
'subject',
'full_text',
'issuing_authority'
];
protected $hidden = ['created_at', 'updated_at'];
public function arts()
{
return $this->belongsToMany(Art::class, 'art_judicial_precedent');
}
}

86
app/Models/Law.php Normal file
View File

@@ -0,0 +1,86 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Law extends Model
{
use HasFactory;
protected $fillable = ['title', 'is_locked', 'category_id', 'price','image'];
protected $hidden = ['created_at', 'updated_at'];
public function category()
{
return $this->belongsTo(Category::class, 'category_id');
}
public function sections()
{
return $this->hasMany(Section::class);
}
public function gates()
{
return $this->hasMany(Gate::class);
}
public function parts()
{
return $this->hasMany(Part::class);
}
public function chapters()
{
return $this->hasMany(Chapter::class);
}
public function divisions()
{
return $this->hasMany(Division::class);
}
public function branchs()
{
return $this->hasMany(Branch::class);
}
public function arts()
{
return $this->hasMany(Art::class);
}
public function volums()
{
return $this->hasMany(Volum::class);
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
public function getIsLockedAttribute($value)
{
return (bool) $value;
}
public function setIsLockedAttribute($value)
{
$this->attributes['is_locked'] = $value ? 1 : 0;
}
public function getImageAttribute($value)
{
return $value ? secure_asset('images/' . $value) : null;
}
}

21
app/Models/LikeArt.php Normal file
View File

@@ -0,0 +1,21 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LikeArt extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'art_id'
];
public function art()
{
return $this->belongsTo(Art::class);
}
}

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class LikeSection extends Model
{
use HasFactory;
protected $fillable = [
'section_id',
'user_id'
];
}

18
app/Models/Note.php Normal file
View File

@@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Note extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'art_id',
'note',
'color_code'
];
}

View File

@@ -0,0 +1,35 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
use HasFactory;
protected $table = 'notifications';
protected $fillable = [
'title',
'description',
];
public function users()
{
return $this->belongsToMany(User::class, 'notification_user')
->withPivot('read_at')
->withTimestamps();
}
/**
* Notifications that a user has not read (no pivot row with read_at set).
*/
public static function scopeUnreadForUser($query, $userId)
{
return $query->whereDoesntHave('users', function ($q) use ($userId) {
$q->where('user_id', $userId)->whereNotNull('notification_user.read_at');
});
}
}

32
app/Models/Order.php Normal file
View File

@@ -0,0 +1,32 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Order extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'price',
'status',
'transaction_id',
];
public function user()
{
return $this->belongsTo(User::class);
}
public static function getStatues()
{
return [
'paid' => 'پرداخت شده',
'unpaid' => 'پرداخت نشده'
];
}
}

42
app/Models/Part.php Normal file
View File

@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Part extends Model
{
use HasFactory;
protected $fillable = ['title', 'number', 'chapter_id', 'section_id', 'book_id', 'volum_id', 'law_id','division_id'];
protected $hidden = ['created_at', 'updated_at'];
public function gates()
{
return $this->hasMany(Gate::class);
}
public function branch()
{
return $this->hasMany(Branch::class);
}
public function art()
{
return $this->hasMany(Art::class);
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class PaymentTransaction extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'subscribe_plan_id',
'transaction_id',
'reference_id',
'amount',
'status',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function subscribePlan()
{
return $this->belongsTo(SubscribePlan::class);
}
}

22
app/Models/RecentArt.php Normal file
View File

@@ -0,0 +1,22 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class RecentArt extends Model
{
use HasFactory;
protected $fillable = [
'user_id',
'law_id'
];
public function law()
{
return $this->belongsTo(Law::class);
}
}

63
app/Models/Section.php Normal file
View File

@@ -0,0 +1,63 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Section extends Model
{
use HasFactory;
protected $fillable = ['title', 'number', 'book_id', 'volum_id', 'law_id','division_id'];
protected $hidden = ['created_at', 'updated_at'];
public function chapters()
{
return $this->hasMany(Chapter::class);
}
public function parts()
{
return $this->hasMany(Part::class);
}
public function gate()
{
return $this->hasMany(Gate::class);
}
public function gates()
{
return $this->hasMany(Gate::class);
}
public function branch()
{
return $this->hasMany(Branch::class);
}
public function branchs()
{
return $this->hasMany(Branch::class);
}
public function art()
{
return $this->hasMany(Art::class);
}
public static function search($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->get();
}
public static function searchLimit($searchTerm)
{
return self::where('title', 'LIKE', "%{$searchTerm}%")->limit(3)->get();
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class SubscribePlan extends Model
{
use HasFactory;
protected $fillable = [
'name',
'price',
'expired_day',
'is_active',
'is_free',
'type',
'transaction'
];
public function users()
{
return $this->belongsToMany(User::class, 'user_subscribers');
}
public function userSubscribers()
{
return $this->hasMany(UserSubscriber::class);
}
}

Some files were not shown because too many files have changed in this diff Show More