mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/unity.git
synced 2026-08-25 15:24:10 +00:00
setup
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace App\Http\Helpers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ExcelHandel
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static function generateFolder($set_path = '')
|
||||
{
|
||||
$folder = public_path() . $set_path;
|
||||
\File::isDirectory($folder) or \File::makeDirectory($folder, 0777, true, true); //check folder is exist
|
||||
return $folder;
|
||||
}
|
||||
|
||||
public static function insertExcel($path = '', $base64Set = [])
|
||||
{
|
||||
$file_info = [];
|
||||
$folder_path = ExcelHandel::generateFolder('/share/excels/' . $path);
|
||||
|
||||
foreach ($base64Set as $key => $row) {
|
||||
|
||||
$exceldata = $row;
|
||||
$filename = (string) Str::uuid();
|
||||
|
||||
$f = finfo_open();
|
||||
$mime_type = finfo_file($f, $exceldata, FILEINFO_MIME_TYPE);
|
||||
|
||||
$extension = 'xls';
|
||||
|
||||
switch ($mime_type) {
|
||||
case 'application/vnd.ms-excel':
|
||||
$extension = 'xls';
|
||||
break;
|
||||
case 'application/zip':
|
||||
$extension = 'xlsx';
|
||||
break;
|
||||
}
|
||||
|
||||
if (empty($extension)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$exceldata = explode('base64,', $exceldata);
|
||||
$exceldata = base64_decode($exceldata[1]);
|
||||
|
||||
$filename_with_ext = $filename . '.' . $extension;
|
||||
|
||||
$file = ExcelHandel::generateExcel($path, $exceldata, $filename, $extension);
|
||||
|
||||
$file_info[] = [
|
||||
'path' => $path,
|
||||
'filename' => $filename_with_ext,
|
||||
'mime_type' => $mime_type,
|
||||
'extension' => $extension,
|
||||
'file_info' => empty($file) ? [] : $file,
|
||||
];
|
||||
}
|
||||
|
||||
return $file_info;
|
||||
}
|
||||
|
||||
public static function generateExcel($path = '', $exceldata = '', $filename = '', $extension = '')
|
||||
{
|
||||
$file_info = [];
|
||||
$file = \File::put(public_path('share/excels/' . $path) . '/' . $filename . '.' . $extension, $exceldata);
|
||||
$file_info['original']['file'] = public_path('/share/excels/' . $path . '/' . $filename . '.' . $extension);
|
||||
$file_info['large']['file'] = public_path('/share/excels/' . $path . '/' . $filename . '.' . $extension);
|
||||
$file_info['medium']['file'] = public_path('/share/excels/' . $path . '/' . $filename . '.' . $extension);
|
||||
$file_info['small']['file'] = public_path('/share/excels/' . $path . '/' . $filename . '.' . $extension);
|
||||
|
||||
return $file_info;
|
||||
}
|
||||
|
||||
public static function removeExcel($excel_info = [])
|
||||
{
|
||||
$excel_info = json_decode(json_encode($excel_info), true);
|
||||
foreach ($excel_info as $key => $row) {
|
||||
if (!empty($row['path'])) {
|
||||
\File::delete([$row['file_info']['original']['file']]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
<?php
|
||||
namespace App\Http\Helpers;
|
||||
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class General
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static function storeCachePerviousQuery($query = '', $name = '')
|
||||
{
|
||||
$query_string = urldecode(preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', http_build_query($query)));
|
||||
Cache::put($name, $query_string, 1800);
|
||||
}
|
||||
|
||||
public static function getCachePerviousQuery($name = '')
|
||||
{
|
||||
return Cache::get($name);
|
||||
}
|
||||
|
||||
public static function returnTokenData($object = [], $token = '')
|
||||
{
|
||||
$data = (object)[
|
||||
'status' => true,
|
||||
'access_token' => $token,
|
||||
'data' => $object
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function returnErrorData($error = [])
|
||||
{
|
||||
$data = (object)[
|
||||
'status' => false,
|
||||
'errors' => $error
|
||||
];
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function returnData($object = [], $query_string = '')
|
||||
{
|
||||
if ($query_string) {
|
||||
$data = (object)[
|
||||
'status' => true,
|
||||
'pervious_query' => $query_string,
|
||||
'data' => $object
|
||||
];
|
||||
}
|
||||
else {
|
||||
$data = (object)[
|
||||
'status' => true,
|
||||
'data' => $object
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public static function paginationGenerator($data = [], $params = [], $extract = [])
|
||||
{
|
||||
$pagination = [];
|
||||
|
||||
$data->appends($params)->links();
|
||||
$pagination = [
|
||||
'status' => true,
|
||||
'total' => $data->total(),
|
||||
'has_more_page' => $data->hasMorePages(),
|
||||
'query_string' => urldecode(preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', http_build_query($params))),
|
||||
'page' => [
|
||||
'next_page' => urldecode(preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', $data->nextPageUrl())),
|
||||
'pervious_page' => urldecode(preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', $data->previousPageUrl())),
|
||||
],
|
||||
'data' => $extract
|
||||
];
|
||||
|
||||
return $pagination;
|
||||
}
|
||||
|
||||
public static function returnMimeType($file = [])
|
||||
{
|
||||
$f = finfo_open();
|
||||
$mime_type = finfo_file($f, $file[0], FILEINFO_MIME_TYPE);
|
||||
return $mime_type;
|
||||
}
|
||||
|
||||
public static function returnQueryString($params = [])
|
||||
{
|
||||
$query_string = urldecode(preg_replace('/(%5B)\d+(%5D=)/i', '$1$2', http_build_query($params)));
|
||||
return $query_string;
|
||||
}
|
||||
|
||||
public static function seoURL($string)
|
||||
{
|
||||
//Lower case everything
|
||||
$string = strtolower($string);
|
||||
//Remove all url format string
|
||||
$string =preg_replace("/\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i", "", $string);
|
||||
|
||||
$string = preg_replace("~[^\p{L}\n\s_]+~u", "", $string);
|
||||
|
||||
//Clean up multiple dashes or whitespaces
|
||||
$string = preg_replace("/[\s-]+/", " ", $string);
|
||||
//Trim first - avoid spaces on beginning or end of the text
|
||||
$string = trim($string);
|
||||
|
||||
// Check whether is English
|
||||
$match = preg_match('/^[A-Za-z0-9 _]+$/', $string);
|
||||
|
||||
$isEnglish = $match == 1;
|
||||
|
||||
if ($isEnglish == 1) {
|
||||
$string = mb_substr($string, 0, 60);
|
||||
$strArray = explode(" ",$string);
|
||||
if (sizeof($strArray) != 1) {
|
||||
// unset($strArray[intval(sizeof($strArray))-1]);
|
||||
}
|
||||
$string = implode(' ',$strArray);
|
||||
|
||||
}else{
|
||||
$string = mb_substr($string, 0, 20,'UTF8');
|
||||
}
|
||||
|
||||
//Convert whitespaces and underscore to dash
|
||||
$string = preg_replace("/[\s_]/", "-", $string);
|
||||
|
||||
//Check empty string
|
||||
if (empty($string)) {
|
||||
$string = 'untitled';
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
public static function shortenNumber($number){
|
||||
if ($number < 1000) {
|
||||
$nformat = number_format($number);
|
||||
}else if ($number < 1000000) {
|
||||
// Anything less than a million
|
||||
$nformat = number_format($number/1000,1) . 'k';
|
||||
} else if ($number < 1000000000) {
|
||||
// Anything less than a billion
|
||||
$nformat = number_format($number / 1000000, 3) . 'm';
|
||||
} else {
|
||||
// At least a billion
|
||||
$nformat = number_format($number / 1000000000, 3) . 'b';
|
||||
}
|
||||
|
||||
return $nformat;
|
||||
}
|
||||
|
||||
public static function getExcerpt($str, $maxLength = 60, $startPos = 0)
|
||||
{
|
||||
$str = strip_tags($str);
|
||||
$str = html_entity_decode($str, ENT_QUOTES);
|
||||
$excerpt = $str;
|
||||
if(strlen($str) > $maxLength) {
|
||||
$excerpt = '';
|
||||
|
||||
$excerpt = mb_substr($str, $startPos, $maxLength-3, 'UTF-8');
|
||||
|
||||
$excerpt .= '...';
|
||||
}
|
||||
return $excerpt;
|
||||
}
|
||||
|
||||
public static function convertDayTime($date_time)
|
||||
{
|
||||
$date_time = date('Y-m-d H:i:s', strtotime($date_time));
|
||||
$dteStart = new \DateTime(date('Y-m-d H:i:s'));
|
||||
$dteEnd = new \DateTime($date_time);
|
||||
$dteDiff = $dteStart->diff($dteEnd);
|
||||
|
||||
if($dteEnd->format('Y') < $dteStart->format('Y')) {
|
||||
return 'Active ' . $dteEnd->format('d M Y');
|
||||
}
|
||||
elseif($dteDiff->days > 0) { //already posted one day
|
||||
return 'Active ' . $dteEnd->format('d M');
|
||||
}
|
||||
elseif($dteDiff->format('%H') > 0) { //already posted one hours
|
||||
$text = $dteDiff->format('%h') != 1 ? 'hrs ago' : 'hr ago';
|
||||
return 'Active ' . $dteDiff->format('%h') . $text;
|
||||
}
|
||||
elseif($dteDiff->format('%I') > 0) { //already posted one min
|
||||
return 'Active ' . $dteDiff->format('%i') . ' mins ago';
|
||||
}
|
||||
else{
|
||||
return 'Active ' . $dteDiff->format('%s') . ' sec ago'; //already posted one sec
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
namespace App\Http\Helpers;
|
||||
|
||||
use Hashids\Hashids;
|
||||
|
||||
class Hasher
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static function encode($entity = '', ...$args)
|
||||
{
|
||||
$hash = new Hashids($entity, 11);
|
||||
return $hash->encode(...$args);
|
||||
}
|
||||
|
||||
public static function decode($entity = '', $enc)
|
||||
{
|
||||
if (is_int($enc)) {
|
||||
return $enc;
|
||||
}
|
||||
|
||||
$hash = new Hashids($entity, 11);
|
||||
return $hash->decode($enc) ? $hash->decode($enc)[0] : '';
|
||||
}
|
||||
|
||||
public static function decodeArray($entity = '', $set = [])
|
||||
{
|
||||
$return_set = [];
|
||||
foreach ($set as $key => $row) {
|
||||
$return_set[] = Hasher::decode($entity, $row);
|
||||
}
|
||||
|
||||
return $return_set;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
namespace App\Http\Helpers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Image;
|
||||
|
||||
class ImageHandel
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static function getImgFullPath($img_info = [], $type = 'object')
|
||||
{
|
||||
$default_file = asset('share/no-image.png');
|
||||
|
||||
if ($type == 'object') {
|
||||
$default_file = asset('share/non-image.png');
|
||||
}
|
||||
else if ($type == 'person') {
|
||||
$default_file = asset('share/non-person.png');
|
||||
}
|
||||
else if ($type == 'logo') {
|
||||
$default_file = asset('share/non-logo.png');
|
||||
}
|
||||
|
||||
$img_info = json_decode($img_info);
|
||||
if (!empty($img_info)) {
|
||||
foreach ($img_info as $key_img => $row_img) {
|
||||
$img_info[$key_img]->file_info->original->file = asset('storage/' . $row_img->file_info->original->file);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$img_info = [(object)[
|
||||
'empty' => true,
|
||||
'file_info' => (object)[
|
||||
'original' => (object) [
|
||||
'file' => $default_file
|
||||
],
|
||||
'large' => (object) [
|
||||
'file' => $default_file
|
||||
],
|
||||
'medium' => (object) [
|
||||
'file' => $default_file
|
||||
],
|
||||
'small' => (object) [
|
||||
'file' => $default_file
|
||||
],
|
||||
]
|
||||
]];
|
||||
}
|
||||
return $img_info;
|
||||
}
|
||||
|
||||
public static function extractFullPath($file = '')
|
||||
{
|
||||
|
||||
$file = explode(asset('/storage'), $file);
|
||||
|
||||
|
||||
return $file[1];
|
||||
}
|
||||
|
||||
public static function generateFolder($path = '')
|
||||
{
|
||||
$folder = \Storage::disk('public')->makeDirectory($path);
|
||||
return $folder;
|
||||
}
|
||||
|
||||
public static function insertImage($path = '', $file = '')
|
||||
{
|
||||
$image = ImageHandel::base64ToImage($path, $file);
|
||||
return $image;
|
||||
}
|
||||
|
||||
public static function base64ToImage($path = '', $base64Set = [])
|
||||
{
|
||||
$img_info = [];
|
||||
$folder_path = ImageHandel::generateFolder('images/' . $path);
|
||||
|
||||
foreach ($base64Set as $key => $row) {
|
||||
|
||||
$imgdata = $row;
|
||||
$filename = (string) Str::uuid();
|
||||
|
||||
$f = finfo_open();
|
||||
$mime_type = finfo_file($f, $imgdata, FILEINFO_MIME_TYPE);
|
||||
|
||||
$extension = '';
|
||||
switch ($mime_type) {
|
||||
case 'image/gif':
|
||||
$extension = 'gif';
|
||||
break;
|
||||
case 'image/png':
|
||||
$extension = 'png';
|
||||
break;
|
||||
case 'image/jpeg':
|
||||
$extension = 'jpg';
|
||||
break;
|
||||
}
|
||||
|
||||
if (empty($extension)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$file_info = [];
|
||||
|
||||
$filename = $filename . '.' . $extension;
|
||||
$img = Image::make($row)->save(storage_path('app/public/images/' . $path) . '/' . $filename);
|
||||
|
||||
$file_info['original']['file'] = 'images/' . $path . '/' . $filename;
|
||||
$file_info['original']['width'] = $img->width();
|
||||
$file_info['original']['height'] = $img->height();
|
||||
|
||||
$img_info[] = [
|
||||
'on_cloud' => false,
|
||||
'path' => $path,
|
||||
'filename' => $filename,
|
||||
'mime_type' => $mime_type,
|
||||
'extension' => $extension,
|
||||
'file_info' => empty($file_info) ? [] : $file_info,
|
||||
];
|
||||
}
|
||||
|
||||
return $img_info;
|
||||
}
|
||||
|
||||
public static function removeImage($img_info = [])
|
||||
{
|
||||
foreach ($img_info as $key_img => $row_img) {
|
||||
if (!empty($row_img->path) && $row_img->path != 'seeder') {
|
||||
$img_original = ImageHandel::extractFullPath($row_img->file_info->original->file);
|
||||
\Storage::disk('public')->delete([$img_original]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
namespace App\Http\Helpers;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class PdfHandel
|
||||
{
|
||||
function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public static function getPdfFullPath($video_info = [])
|
||||
{
|
||||
$video_info = json_decode($video_info);
|
||||
if (!empty($video_info)) {
|
||||
foreach ($video_info as $key_img => $row_img) {
|
||||
$video_info[$key_img]->file_info->original->file = asset($row_img->file_info->original->file);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$video_info = [(object)[
|
||||
'empty' => true,
|
||||
'file_info' => (object)[
|
||||
'original' => (object) [
|
||||
'file' => asset('share/no-image.png')
|
||||
]
|
||||
]
|
||||
]];
|
||||
}
|
||||
return $video_info;
|
||||
}
|
||||
|
||||
public static function extractFullPath($file = '')
|
||||
{
|
||||
$file = explode(asset('/'), $file);
|
||||
return $file[1];
|
||||
}
|
||||
|
||||
public static function generateFolder($set_path = '')
|
||||
{
|
||||
$folder = public_path() . $set_path;
|
||||
\File::isDirectory($folder) or \File::makeDirectory($folder, 0777, true, true); //check folder is exist
|
||||
return $folder;
|
||||
}
|
||||
|
||||
public static function insertPdf($path = '', $base64Set = [])
|
||||
{
|
||||
$file_info = [];
|
||||
$folder_path = PdfHandel::generateFolder('/share/images/' . $path);
|
||||
|
||||
foreach ($base64Set as $key => $row) {
|
||||
|
||||
$pdfdata = $row;
|
||||
$filename = (string) Str::uuid();
|
||||
|
||||
$f = finfo_open();
|
||||
$mime_type = finfo_file($f, $pdfdata, FILEINFO_MIME_TYPE);
|
||||
|
||||
$extension = 'pdf';
|
||||
switch ($mime_type) {
|
||||
case 'application/pdf':
|
||||
$extension = 'pdf';
|
||||
break;
|
||||
}
|
||||
|
||||
if (empty($extension)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$pdfdata = explode('base64,', $pdfdata);
|
||||
$pdfdata = base64_decode($pdfdata[1]);
|
||||
|
||||
$filename_with_ext = $filename . '.' . $extension;
|
||||
|
||||
$file = PdfHandel::generatePdf($path, $pdfdata, $filename, $extension);
|
||||
|
||||
$file_info[] = [
|
||||
'path' => $path,
|
||||
'filename' => $filename_with_ext,
|
||||
'mime_type' => $mime_type,
|
||||
'extension' => $extension,
|
||||
'file_info' => empty($file) ? [] : $file,
|
||||
];
|
||||
}
|
||||
|
||||
return $file_info;
|
||||
}
|
||||
|
||||
public static function generatePdf($path = '', $pdfdata = '', $filename = '', $extension = '')
|
||||
{
|
||||
$file_info = [];
|
||||
$file = \File::put(public_path('share/images/' . $path) . '/' . $filename . '.' . $extension, $pdfdata);
|
||||
$file_info['original']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
|
||||
$file_info['large']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
|
||||
$file_info['medium']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
|
||||
$file_info['small']['file'] = asset('/share/images/' . $path . '/' . $filename . '.' . $extension);
|
||||
|
||||
return $file_info;
|
||||
}
|
||||
|
||||
public static function removePdf($img_info = [])
|
||||
{
|
||||
foreach ($img_info as $key_img => $row_img) {
|
||||
if (!empty($row_img->path)) {
|
||||
$img_original = PdfHandel::extractFullPath($row_img->file_info->original->file);
|
||||
\File::delete([$img_original]);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user