Files
new_raffles/app/Helpers/Helper.php

207 lines
5.8 KiB
PHP

<?php
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Modules\CCMS\Models\Setting;
if (!function_exists('setting')) {
function setting($key = null)
{
$setting = Cache::has('setting') ? Cache::get('setting') : Cache::rememberForever('setting', function () {
return Setting::get()->mapWithKeys(function (Setting $setting) {
return [$setting->key => $setting->value];
})->toArray();
});
return array_key_exists($key, $setting) ? $setting[$key] : null;
}
}
if (!function_exists('getFileIcon')) {
function getFileIcon($extension)
{
switch ($extension) {
case 'pdf':
return ['ri-file-pdf-line', 'danger'];
case 'doc':
case 'docx':
return ['ri-file-word-line', 'primary'];
case 'xls':
case 'xlsx':
return ['ri-file-excel-line', 'success'];
case 'ppt':
case 'pptx':
return ['ri-file-powerpoint-line', 'danger'];
case 'txt':
return ['ri-file-alt-line', 'secondary'];
case 'jpg':
case 'jpeg':
case 'png':
case 'gif':
return ['ri-file-image-line', 'info'];
case 'zip':
case 'rar':
return ['ri-file-archive-line', 'warning'];
case 'mp3':
case 'wav':
return ['ri-file-audio-line', 'warning'];
case 'mp4':
case 'mov':
case 'avi':
return ['ri-file-video-line', 'warning'];
default:
return ['ri-file-line', 'warning'];
}
// return collect([
// 'icon' => $res[0],
// 'color' => $res[1],
// ]);
}
}
if (!function_exists('uploadImage')) {
function uploadImage($file, $path = 'uploads')
{
$fileName = uniqid() . '.' . $file->getClientOriginalExtension();
$filePath = Storage::disk('public')->putFileAs($path, $file, $fileName);
return $filePath;
}
}
function getPageTemplateOptions()
{
$client = config('app.client');
$pageTemplateOptions = collect(File::files(resource_path("views/client/{$client}/pages")))
->mapWithKeys(function ($file) {
$template = Str::replace('.blade.php', '', $file->getFilename());
return [
$template => $template,
];
});
return $pageTemplateOptions->all();
}
if (!function_exists('getFormatted')) {
function getFormatted($dateTime = null, $date = null, $time = null, $format = null)
{
$data = null;
switch (true) {
case !is_null($dateTime):
$data = $dateTime;
$format ??= 'd M, Y h:i A';
break;
case !is_null($date) && !is_null($time):
$data = "{$date} {$time}";
$format ??= 'd M, Y h:i A';
break;
case !is_null($date):
$data = $date;
$format ??= 'd M, Y';
break;
case !is_null($time):
$data = $time;
$format ??= 'h:i A';
break;
default:
return null;
}
try {
$formatted = Carbon::parse($data)->format($format);
return $formatted;
} catch (\Exception $e) {
return null;
}
}
function getThemeColor($lightenFactor = 0.3)
{
$themeColor = setting('color') ?? '#be2400';
if (!preg_match('/^#[a-fA-F0-9]{6}$/', $themeColor)) {
return $themeColor;
}
$lighterColor = lightenColor($themeColor, $lightenFactor);
return [
'original' => $themeColor,
'lighter' => $lighterColor,
];
}
/**
* Lighten a hexadecimal color.
*
* @param string $hexColor
* @param float $factor
* @return string
*/
function lightenColor(string $hexColor, float $factor = 0.2): string
{
$hexColor = ltrim($hexColor, '#');
// Convert hex to RGB
$r = hexdec(substr($hexColor, 0, 2));
$g = hexdec(substr($hexColor, 2, 2));
$b = hexdec(substr($hexColor, 4, 2));
// Apply the lightening factor
$r = min(255, (int) ($r + (255 - $r) * $factor));
$g = min(255, (int) ($g + (255 - $g) * $factor));
$b = min(255, (int) ($b + (255 - $b) * $factor));
// Convert back to hex
return sprintf('#%02x%02x%02x', $r, $g, $b);
}
function setEnvIfNotExists($key, $value)
{
$envFile = app()->environmentFilePath(); // Path to the .env file
$str = file_get_contents($envFile);
// Check if the key exists in the .env file
$keyExists = preg_match("/^{$key}=.*/m", $str);
if (!$keyExists) {
// If the key doesn't exist, append it to the .env file
$str .= "\n{$key}={$value}";
file_put_contents($envFile, $str);
}
}
function isEmptyArray($array = [])
{
return is_array($array) && array_filter($array) === [];
}
if (!function_exists('sendNotification')) {
function sendNotification($user, $notification = [])
{
Notification::send($user, new App\Notifications\SendNotification($notification));
}
}
if (!function_exists('uploadImage')) {
function uploadImage($file, $path = 'uploads')
{
$fileName = time() . '_' . $file->getClientOriginalName();
$filePath = Storage::disk('public')->putFileAs($path, $file, $fileName);
return $filePath;
}
}
}