nhance/app/Helpers/utility_helper.php
2024-09-13 09:00:09 +05:30

312 lines
8.8 KiB
PHP
Executable File

<?php
// File: app/Helpers/Uuid_helper.php
if (!function_exists('generate_uuid')) {
function generate_uuid($version = 4, $format = 'hex')
{
$data = random_bytes(16);
// echo $data.'<br>';
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
return $uuid;
}
}
if (!function_exists('change_date_format')) {
function change_date_format($data, $source_format, $output_format)
{
// echo $data, $source_format, $output_format;return true;
try {
// Create DateTime object with the source format
$dateTime = DateTime::createFromFormat($source_format, $data);
// Check if the DateTime object is created successfully
if ($dateTime === false) {
throw new Exception('Invalid date or format');
}
// Format the DateTime object with the output format
$formattedDate = $dateTime->format($output_format);
return $formattedDate;
} catch (Exception $e) {
// Handle the exception (e.g., log it, show a user-friendly message)
return "Error: " . $e->getMessage();
return $data;
}
}
}
if (!function_exists('file_Upload')) {
function file_Upload($fileToUpload, $filepath)
{
if ($fileToUpload !== null && $fileToUpload->isValid() && !$fileToUpload->hasMoved()) {
$fileName = $fileToUpload->getName();
$fileToUpload->move($filepath, $fileName);
return $fileName;
} else {
return "";
}
}
}
if (!function_exists('file_unlink')) {
function file_unlink($filepath)
{
if (is_file($filepath) && file_exists($filepath)) {
unlink($filepath);
}
}
}
if (!function_exists('compressImage')) {
function compressImage($file, $destinationPath, $newWidth = 100, $newHeight = 100)
{
// Load the image manipulation library
$image = \Config\Services::image();
// Resize and compress the image
$image->withFile($file)
->fit($newWidth, $newHeight, 'center')
->save($destinationPath);
return true;
}
}
if (!function_exists('fancy_date_time_format'))
{
function fancy_date_time_format($datetime,$return_type = 'fancy') {
date_default_timezone_set('Asia/Kolkata');
$currentDateTime = new DateTime();
$passedDateTime = new DateTime($datetime);
// Calculate the interval between the current time and the passed datetime
$interval = $currentDateTime->diff($passedDateTime);
// If the interval is more than 1 month or the year is different, return the original datetime
if ($interval->m > 1 || $interval->y != 0) {
if ($return_type == 'fancy') {
return change_date_format($datetime, 'Y-m-d H:i:s', 'd M Y h:i a');
}
return $datetime;
} elseif ($interval->m == 1 && $interval->y == 0) {
return "1 month ago";
} elseif ($interval->d >= 7) {
$weeks = floor($interval->d / 7);
return $weeks == 1 ? "1 week ago" : "$weeks weeks ago";
} elseif ($interval->d >= 1) {
return $interval->d == 1 ? "1 day ago" : $interval->d . " days ago";
} elseif ($interval->h >= 1) {
return $interval->h == 1 ? "1 hour ago" : $interval->h . " hours ago";
} elseif ($interval->i >= 1) {
return $interval->i == 1 ? "1 minute ago" : $interval->i . " minutes ago";
} else {
return "Just now";
}
}
}
if(!function_exists('check_string_date'))
{
function check_string_date($str)
{
if (DateTime::createFromFormat('Y-m-d H:i:s', $str) !== false) {
return true;
}
return false;
}
}
if (!function_exists('generate_download_link')) {
function generate_download_link($rand_string) {
// Generate the link using provided emp_code and client_policy_id
$link = htmlspecialchars(base_url('download-e-card/' . $rand_string));
// Return the link wrapped in anchor tag
// return '<a href="' . $link . '" target="_blank">Click To </a>';
return $link;
}
}
if (!function_exists('generate_random_alphanumeric')) {
function generate_random_alphanumeric($length = 6) {
$random_string = '';
for ($i = 0; $i < $length; $i++) {
$random_ascii = rand(0, 61);
if ($random_ascii < 10) {
$random_character = chr($random_ascii + 48);
} elseif ($random_ascii < 36) {
$random_character = chr($random_ascii + 55);
} else {
$random_character = chr($random_ascii + 61);
}
$random_string .= $random_character;
}
return $random_string;
}
}
if (!function_exists('get_base64_image')) {
function get_base64_image($path)
{
// Get file extension
$type = pathinfo($path, PATHINFO_EXTENSION);
// Read file content
$dataContent = file_get_contents($path);
// Encode as base64
$base64Image = 'data:image/' . $type . ';base64,' . base64_encode($dataContent);
return $base64Image;
}
}
if (!function_exists('format_indian_number')) {
function format_indian_number($number) {
// Round the number to two decimal places
$number = isset($number) ? $number : 0;
$number = round($number, 2);
// Split the number into integer and decimal parts
$numberParts = explode('.', number_format($number, 2, '.', ''));
$integerPart = $numberParts[0];
$decimalPart = isset($numberParts[1]) ? $numberParts[1] : '00';
// Format the integer part with commas
$length = strlen($integerPart);
$formattedStr = '';
$counter = 0;
for ($i = $length - 1; $i >= 0; $i--) {
$formattedStr = $integerPart[$i] . $formattedStr;
$counter++;
if ($counter == 3 && $i != 0) {
$formattedStr = ',' . $formattedStr;
$counter = 0;
} elseif ($counter == 2 && $i != 0 && $length - $i > 3) {
$formattedStr = ',' . $formattedStr;
$counter = 0;
}
}
// Combine the integer and decimal parts
return $formattedStr . '.' . $decimalPart;
}
}
if (!function_exists('get_username')) {
function get_username($user_id) {
// Connect to the database
$db = \Config\Database::connect();
// Query the database
$query = $db->table('user_profiles')
->select('first_name')
->where('id', $user_id)
->get();
// Get the result
$result = $query->getRow();
// Return the username if found, otherwise return null
return $result ? $result->first_name : null;
}
}
if (!function_exists('get_role_id')) {
function get_role_id() {
$role_id = get_session_userdata()->role;
return $role_id;
}
}
function numberToWords($number)
{
$words = array(
'0' => 'Zero',
'1' => 'One',
'2' => 'Two',
'3' => 'Three',
'4' => 'Four',
'5' => 'Five',
'6' => 'Six',
'7' => 'Seven',
'8' => 'Eight',
'9' => 'Nine',
'10' => 'Ten',
'11' => 'Eleven',
'12' => 'Twelve',
'13' => 'Thirteen',
'14' => 'Fourteen',
'15' => 'Fifteen',
'16' => 'Sixteen',
'17' => 'Seventeen',
'18' => 'Eighteen',
'19' => 'Nineteen',
'20' => 'Twenty',
'30' => 'Thirty',
'40' => 'Forty',
'50' => 'Fifty',
'60' => 'Sixty',
'70' => 'Seventy',
'80' => 'Eighty',
'90' => 'Ninety'
);
if ($number < 21) {
return $words[$number];
}
if ($number < 100) {
$tens = (int)($number / 10) * 10;
$units = $number % 10;
return $words[$tens] . ($units ? ' ' . $words[$units] : '');
}
if ($number < 1000) {
$hundreds = (int)($number / 100);
$remainder = $number % 100;
return $words[$hundreds] . ' Hundred' . ($remainder ? ' and ' . numberToWords($remainder) : '');
}
$levels = array('', ' Thousand', ' Million', ' Billion', ' Trillion', ' Quadrillion', ' Quintillion');
for ($i = 0, $unit = 1; $i < count($levels); $i++, $unit *= 1000) {
if ($number < $unit * 1000) {
$current = (int)($number / $unit);
$remainder = $number % $unit;
return numberToWords($current) . $levels[$i] . ($remainder ? ' ' . numberToWords($remainder) : '');
}
}
return $number; // Fallback for numbers beyond the supported range
}