127 lines
3.6 KiB
PHP
127 lines
3.6 KiB
PHP
<?php
|
||
|
||
use App\Helpers\CalendarHelper;
|
||
|
||
if (!function_exists('calendar_date')) {
|
||
/**
|
||
* Format a date based on the user's calendar preference with Persian digits.
|
||
*/
|
||
function calendar_date($date, $format = 'Y/m/d')
|
||
{
|
||
if (!$date) return '';
|
||
|
||
$calendar = session('calendar', 'jalali');
|
||
|
||
try {
|
||
// Convert to date string if it's a Carbon/DateTime object
|
||
if ($date instanceof \DateTimeInterface) {
|
||
$dateString = $date->format('Y-m-d H:i:s');
|
||
} else {
|
||
$dateString = (string) $date;
|
||
}
|
||
|
||
if ($calendar === 'jalali') {
|
||
$jalali = \Morilog\Jalali\Jalalian::fromDateTime($dateString);
|
||
$result = $jalali->format($format);
|
||
} else {
|
||
$result = date($format, strtotime($dateString));
|
||
}
|
||
} catch (\Exception $e) {
|
||
// Fallback: try Carbon format
|
||
try {
|
||
if ($date instanceof \DateTimeInterface) {
|
||
$result = $date->format($format);
|
||
} else {
|
||
$result = date($format, strtotime((string) $date));
|
||
}
|
||
} catch (\Exception $e2) {
|
||
return (string) $date;
|
||
}
|
||
}
|
||
|
||
// Convert to Persian digits if locale is fa
|
||
if (app()->getLocale() === 'fa') {
|
||
$result = persian_num($result);
|
||
}
|
||
|
||
return $result;
|
||
}
|
||
}
|
||
|
||
if (!function_exists('calendar_date_raw')) {
|
||
/**
|
||
* Format a date based on calendar preference WITHOUT Persian digits.
|
||
* Useful for form values that need English digits.
|
||
*/
|
||
function calendar_date_raw($date, $format = 'Y/m/d')
|
||
{
|
||
if (!$date) return '';
|
||
|
||
$calendar = session('calendar', 'jalali');
|
||
|
||
try {
|
||
if ($date instanceof \DateTimeInterface) {
|
||
$dateString = $date->format('Y-m-d H:i:s');
|
||
} else {
|
||
$dateString = (string) $date;
|
||
}
|
||
|
||
if ($calendar === 'jalali') {
|
||
$jalali = \Morilog\Jalali\Jalalian::fromDateTime($dateString);
|
||
return $jalali->format($format);
|
||
} else {
|
||
return date($format, strtotime($dateString));
|
||
}
|
||
} catch (\Exception $e) {
|
||
if ($date instanceof \DateTimeInterface) {
|
||
return $date->format($format);
|
||
}
|
||
return (string) $date;
|
||
}
|
||
}
|
||
}
|
||
|
||
if (!function_exists('persian_num')) {
|
||
function persian_num($number)
|
||
{
|
||
$persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
|
||
$english = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
||
return str_replace($english, $persian, (string) $number);
|
||
}
|
||
}
|
||
|
||
if (!function_exists('format_currency')) {
|
||
function format_currency($amount, $currency = 'IRR')
|
||
{
|
||
$formatted = number_format((float) $amount, $currency === 'IRR' ? 0 : 2);
|
||
|
||
if (app()->getLocale() === 'fa') {
|
||
$formatted = persian_num($formatted);
|
||
}
|
||
|
||
$symbol = '';
|
||
switch ($currency) {
|
||
case 'IRR': $symbol = 'ریال'; break;
|
||
case 'USD': $symbol = '$'; break;
|
||
case 'EUR': $symbol = '€'; break;
|
||
case 'AED': $symbol = 'د.إ'; break;
|
||
}
|
||
|
||
return $formatted . ' ' . $symbol;
|
||
}
|
||
}
|
||
|
||
if (!function_exists('get_direction')) {
|
||
function get_direction()
|
||
{
|
||
return app()->getLocale() === 'fa' ? 'rtl' : 'ltr';
|
||
}
|
||
}
|
||
|
||
if (!function_exists('is_rtl')) {
|
||
function is_rtl()
|
||
{
|
||
return app()->getLocale() === 'fa';
|
||
}
|
||
}
|