simedkom/app/Helpers/CustomHelper.php
2025-10-13 08:35:36 +07:00

552 lines
16 KiB
PHP

<?php
use App\Models\Media;
use App\Models\User;
use Carbon\Carbon;
use Hashids\Hashids;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
// Template
// if (! function_exists('my_custom_function')) {
// function my_custom_function($param) {
// }
// }
// if(env('FILESYSTEM_DISK') === 's3'){
// if (! function_exists('store_file')) {
// function store_file($file = null, $path, $disk = null)
// {
// $disk = $disk ?? env('FILESYSTEM_DISK', 'public');
// if (filled($file) && $file instanceof \Illuminate\Http\UploadedFile) {
// $randomFilename = Str::random(35) . time() . '.' . $file->getClientOriginalExtension();
// $originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)) . '.' . $file->getClientOriginalExtension();
// $stored = $file->storeAs($path, $randomFilename, $disk);
// if ($stored) {
// $url = Storage::disk($disk)->url($stored);
// return [
// 'randomFileName' => $randomFilename,
// 'originalFileName' => $originalFileName,
// 'url' => $url,
// 'isSuccess' => true,
// ];
// }
// }
// return [
// 'randomFileName' => null,
// 'originalFileName' => null,
// 'url' => null,
// 'isSuccess' => false,
// ];
// }
// }
// if (!function_exists('display_file')) {
// function display_file($dir, $filename, $disk = null, $expirationMinutes = 60): string
// {
// $disk = $disk ?? env('FILESYSTEM_DISK', 'public');
// if (!filled($filename)) {
// return '<p class="text-center">Belum ditambahkan.</p>';
// }
// $path = "uploads/{$dir}/{$filename}";
// try {
// $fileExists = is_file_exists($path, $disk);
// if (!$fileExists) {
// return '<p class="text-center">File tidak ditemukan.</p>';
// }
// if ($disk === 's3') {
// $fileUrl = Storage::disk($disk)->temporaryUrl($path, now()->addMinutes($expirationMinutes));
// } else {
// $fileUrl = Storage::disk($disk)->url($path);
// }
// $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
// $mimeType = match ($ext) {
// 'jpg', 'jpeg', 'png', 'gif', 'webp' => 'image/' . $ext,
// 'pdf' => 'application/pdf',
// default => 'unknown',
// };
// if (str_contains($mimeType, 'image')) {
// return "<img src='{$fileUrl}' alt='File Image' class='img-fluid'>";
// } elseif ($mimeType === 'application/pdf') {
// return "<iframe src='{$fileUrl}' width='100%' height='600px'></iframe>";
// }
// return '<p class="text-center">Format file tidak didukung</p>';
// } catch (\Exception $e) {
// return '<p class="text-center text-danger">Terjadi kesalahan saat menampilkan file.</p>';
// }
// }
// }
// if (! function_exists('delete_file')) {
// function delete_file($path, $disk = null): bool
// {
// $disk = $disk ?? env('FILESYSTEM_DISK', 'public');
// try {
// if(is_file_exists($path)){
// return Storage::disk($disk)->delete($path);
// }else{
// return false;
// }
// } catch (\Exception $e) {
// return false;
// }
// }
// }
// if (!function_exists('is_file_exists')) {
// function is_file_exists($path, $disk = null): bool
// {
// $disk = $disk ?? env('FILESYSTEM_DISK', 'public');
// try {
// $file = Storage::disk($disk)->get($path);
// return !empty($file);
// } catch (\Throwable $e) {
// return false;
// }
// }
// }
// }else if(env('FILESYSTEM_DISK') === 'local'){
if (! function_exists('store_file')) {
function store_file($file = null, $path = null, $disk = 'public')
{
if (filled($file) && $file && $file instanceof \Illuminate\Http\UploadedFile) {
$randomFilename = Str::random(35).time().'.'.$file->getClientOriginalExtension();
$originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)).'.'.$file->getClientOriginalExtension();
$file->storeAs($path, $randomFilename, $disk);
return [
'randomFileName' => $randomFilename,
'originalFileName' => $originalFileName,
'isSuccess' => true,
];
} else {
return [
'randomFileName' => null,
'originalFileName' => null,
'isSuccess' => false,
];
}
}
}
if (! function_exists('display_file')) {
function display_file($dir, $filename)
{
$path = storage_path("app/public/uploads/{$dir}/{$filename}");
if (! filled($filename)) {
return '<p class="text-center">Belum ditambahkan.</p>';
}
if (! file_exists($path)) {
return '<p class="text-center">File tidak ditemukan.</p>';
}
$mimeType = mime_content_type($path);
$fileUrl = asset("storage/uploads/{$dir}/{$filename}");
if (str_contains($mimeType, 'image')) {
return "<img src='{$fileUrl}' alt='File Image' class='img-fluid'>";
} elseif ($mimeType === 'application/pdf') {
return "<iframe src='{$fileUrl}' width='100%' height='600px'></iframe>";
}
return '<p class="text-center">Format file tidak didukung</p>';
}
}
if (! function_exists('delete_file')) {
function delete_file($path)
{
// Menggunakan public_path untuk file yang berada di dalam storage/public
$fullPath = public_path('storage/'.$path);
if (File::exists($fullPath)) {
File::delete($fullPath);
return true;
}
return false;
}
}
if (! function_exists('is_file_exists')) {
function is_file_exists($path)
{
return File::exists(public_path('storage/'.$path));
}
}
if (! function_exists('get_role_name')) {
function get_role_name($role = null)
{
if ($role === null) {
$role = Auth::user()->role_id;
}
switch ($role) {
case '1':
return 'Administrator';
case '2':
return 'Admin Monitoring';
case '3':
return 'Perusahaan';
case '4':
return 'Admin Konten';
case '5':
return 'Admin Keuangan';
default:
return 'Role Tidak Dikenal';
}
}
}
if (! function_exists('shorten_text')) {
function shorten_text($text, $maxLength = 100)
{
if (strlen($text) > $maxLength) {
return substr($text, 0, $maxLength).'...';
}
return $text;
}
}
if (! function_exists('is_role')) {
function is_role($roles)
{
if (Auth::check()) {
return in_array(Auth::user()->role_id, (array) $roles) || Auth::user()->role_id === '4';
}
return false;
}
}
if (! function_exists('encrypt_id')) {
function encrypt_id($id)
{
$hashids = new Hashids('alwayssaybismillahirrahmanirrahim', 15);
return $hashids->encode($id);
}
}
if (! function_exists('decrypt_id')) {
function decrypt_id($hash)
{
$hashids = new Hashids('alwayssaybismillahirrahmanirrahim', 15);
$result = $hashids->decode($hash);
return $result ? $result[0] : null;
}
}
if (! function_exists('idn_date')) {
function idn_date($tanggal, $format = 'd F Y')
{
return Carbon::parse($tanggal)->locale('id')->translatedFormat($format);
}
}
if (! function_exists('get_user_status')) {
function get_user_status($status)
{
switch ($status) {
case '0':
return 'Menunggu';
case '1':
return 'Diterima';
case '2':
return 'Ditolak';
default:
return 'Tidak Diketahui';
}
}
}
if (! function_exists('get_edit_status')) {
function get_edit_status($status)
{
switch ($status) {
case '0':
return 'Tidak Ada';
case '1':
return 'Menunggu';
case '2':
return 'Diterima';
case '3':
return 'Ditolak';
default:
return 'Tidak Diketahui';
}
}
}
if (! function_exists('get_cooperation_notif')) {
function get_cooperation_notif()
{
$user = User::findOrFail(Auth::id());
if ($user->role_id === 3) {
$mediaId = $user->company->media->id;
$media = Media::with('cooperations')->findOrFail($mediaId);
return $media->cooperations()
->where('start_date', '<=', Carbon::now()->locale('id')->toDateString())
->where('end_date', '>=', Carbon::now()->locale('id')->toDateString())
->count();
} else {
return 0;
}
}
}
if (! function_exists('slugify')) {
function slugify($param)
{
return Str::slug($param);
}
}
if (! function_exists('get_unread_announcements')) {
function get_unread_announcements()
{
$user = User::findOrFail(Auth::id());
if ($user->role_id === 3) {
$userId = $user->id;
$user = User::with('announcements')->findOrFail($userId);
return $user->announcements()
->wherePivot('status', '=', '0')
->count();
} else {
return 0;
}
}
}
if (! function_exists('display_image')) {
function display_image($name, $dir = 'defaults', $default = 'default.jpg')
{
$path = "uploads/{$dir}/{$name}";
$defaultPath = "assets/defaults/{$default}";
if (Storage::disk('public')->exists($path)) {
try {
if (Storage::disk('public')->get($path)) {
return "storage/{$path}";
} else {
return $defaultPath;
}
} catch (\Exception $e) {
return $defaultPath;
}
}
return $defaultPath;
}
}
if (! function_exists('get_user_agent')) {
function get_user_agent($userAgentParam): array
{
$userAgent = $userAgentParam;
// Deteksi Sistem Operasi
$osArray = [
'Windows' => 'Windows',
'Mac' => 'Macintosh',
'Linux' => 'Linux',
'Android' => 'Android',
'iOS' => 'iPhone|iPad|iPod',
];
$osDetected = 'Unknown OS';
foreach ($osArray as $os => $pattern) {
if (preg_match("/$pattern/i", $userAgent)) {
$osDetected = $os;
break;
}
}
// Deteksi Browser
$browserArray = [
'Chrome' => 'Chrome',
'Firefox' => 'Firefox',
'Safari' => 'Safari',
'Edge' => 'Edg', // Microsoft Edge pakai "Edg" di User-Agent
'Opera' => 'Opera|OPR',
'Internet Explorer' => 'MSIE|Trident',
];
$browserDetected = 'Unknown Browser';
foreach ($browserArray as $browser => $pattern) {
if (preg_match("/$pattern/i", $userAgent)) {
$browserDetected = $browser;
break;
}
}
return [
'os' => $osDetected,
'browser' => $browserDetected,
'user_agent' => $userAgent,
];
}
}
if (! function_exists('change_status')) {
function change_status($status, $category)
{
if (in_array($category, ['company_edit', 'media_edit'])) {
switch ($status) {
case '0':
return '1';
case '1':
return '2';
case '2':
return '3';
default:
return '0';
}
}
}
}
if (! function_exists('titlefy')) {
function titlefy($text)
{
if (filled($text)) {
return Str::title($text);
} else {
return false;
}
}
}
if (! function_exists('get_activity_log_name')) {
function get_activity_log_name($name)
{
switch ($name) {
case 'airing_proof_theme':
return [
'name' => 'Tema Bukti Tayang',
'route' => 'dashboard.airing.proof.themes.show',
];
case 'announcement':
return [
'name' => 'Pengumuman',
'route' => 'dashboard.announcements.show',
];
case 'classification':
return [
'name' => 'Klasifikasi',
'route' => 'dashboard.classifications.show',
];
case 'content_recap':
return [
'name' => 'Rekap Konten',
'route' => 'dashboard.content.recaps.show',
];
case 'content_recap_classification':
return [
'name' => 'Klasifikasi Rekap Konten',
'route' => 'dashboard.content.recap.classifications.show',
];
case 'cooperation':
return [
'name' => 'Kerja Sama',
'route' => 'dashboard.cooperations.show',
];
case 'cooperation_completion':
return [
'name' => 'Penyelesaian Kerja Sama',
'route' => 'dashboard.cooperation.completions.show',
];
case 'cooperation_proposal':
return [
'name' => 'Pengajuan Kerja Sama',
'route' => 'dashboard.cooperations.media.proposal.show',
];
case 'government_agency':
return [
'name' => 'OPD',
'route' => 'dashboard.government.agencies.show',
];
case 'issue_management':
return [
'name' => 'Manajemen Isu',
'route' => 'dashboard.issue.managements.show',
];
case 'location':
return [
'name' => 'Location',
'route' => 'dashboard.locations.show',
];
case 'media_monitoring':
return [
'name' => 'Monitoring Media',
'route' => 'dashboard.media.monitorings.show',
];
case 'news':
return [
'name' => 'Berita',
'route' => 'dashboard.news.show',
];
case 'order':
return [
'name' => 'Media Order',
'route' => 'dashboard.media.orders.show',
];
case 'airing_proof_report':
return [
'name' => 'Laporan Bukti Tayang',
'route' => 'dashboard.reports.show',
];
case 'user_role':
return [
'name' => 'Hak Akses',
'route' => 'dashboard.role.index',
];
case 'sub_classification':
return [
'name' => 'Sub Klasifikasi',
'route' => 'dashboard.sub.classifications.show',
];
case 'sub_location':
return [
'name' => 'Sub Lokasi',
'route' => 'dashboard.sub.locations.show',
];
default:
return [
'name' => 'Tidak Valid',
'route' => null,
];
}
}
}