nhance/app/Helpers/utility_helper.php

73 lines
2.4 KiB
PHP

<?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('fileUpload')) {
function fileUpload($fileToUpload, $imageDetails = null)
{
$fileName = $fileToUpload->getClientName();
if ($fileToUpload !== NULL && $fileName !== "") {
if ($fileToUpload->isValid() && !$fileToUpload->hasMoved()) {
// Check if the image exists in the upload folder
$existingImagePath = ROOTPATH . 'public/uploads/' . $imageDetails; // Adjust filename field based on your database structure
if ($imageDetails !== null && file_exists($existingImagePath)) {
// If the image exists, delete it
unlink($existingImagePath);
}
// Move the new image to the upload folder
$fileToUpload->move(ROOTPATH . 'public/uploads', $fileName);
}
} else {
$fileName = "";
}
return $fileName;
}
}