Files

119 lines
2.7 KiB
PHP

<?php
use App\Models\Tour;
use App\Models\User;
use Carbon\Carbon;
if (!function_exists('generateSortRouteParameter')) {
function generateSortRouteParameter(string $sortKey): array
{
return array_merge(request()->query(), ['sort' => $sortKey]);
}
}
if (!function_exists('activeSort')) {
function activeSort(string $sortKey, string $default = ''): string
{
$currentSort = request()->query('sort', $default);
if ($currentSort === $sortKey) {
return 'bg-blue-500 text-white font-DanaMedium';
}
return 'text-gray-600 dark:text-gray-300 hover:text-blue-500';
}
}
if (!function_exists('tourDuration')) {
function tourDuration(Tour $tour): string
{
$departure = $tour->tourDepartures->first();
if (!$departure) {
return 'نامشخص';
}
$start = Carbon::parse($departure->start_date);
$end = Carbon::parse($departure->end_date);
$nights = $start->diffInDays($end);
$days = $nights + 1;
return "{$days} روز و {$nights} شب";
}
}
if (!function_exists('tourDateRange')) {
function tourDateRange(Tour $tour): string
{
$departure = $tour->tourDepartures->first();
if (!$departure) {
return 'تاریخ نامشخص';
}
$start = Verta::instance($departure->start_date);
$end = Verta::instance($departure->end_date);
return $start->format('Y/m/d') . ' - ' . $end->format('Y/m/d');
}
}
if (!function_exists('activeAdminSidebar')) {
function activeAdminSidebar(string|array $routeNames): string
{
$currentRouteName = Route::currentRouteName();
if (is_string($routeNames)) {
$routeNames = [$routeNames];
}
if (in_array($currentRouteName, $routeNames)) {
return 'active';
}
return '';
}
}
if (!function_exists('getUserFullName')) {
function getUserFullName(?User $user = null): string
{
if (!$user) {
$user = auth()->user();
}
return $user->first_name . ' ' . $user->last_name;
}
}
if (! function_exists('deleteFile')) {
function deleteFile(?\App\Models\File $file): void
{
if (! $file) {
return;
}
if (Storage::exists($file->path)) {
Storage::delete($file->path);
}
$file->delete();
}
}
if (!function_exists('getFileUrl')) {
function getFileUrl(?int $fileId): ?string
{
if (!$fileId){
return null;
}
$file = \App\Models\File::find($fileId);
if (!$file){
return null;
}
return \Illuminate\Support\Facades\Storage::url($file->path);
}
}