Last active
February 3, 2022 04:55
-
-
Save Robiussani152/a11e8887186767f3a8a1789aeb081510 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Carbon\Carbon; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Http\Request; | |
use Illuminate\Support\Facades\Log; | |
use Illuminate\Support\Facades\Session; | |
use Illuminate\Support\Facades\Storage; | |
use Illuminate\Support\Facades\Validator; | |
if (!function_exists('formateDate')) { | |
/** | |
* formateDate | |
* | |
* @param mixed $date | |
* @param bool $withTime | |
* | |
* @return string | |
*/ | |
function formateDate($date, $withTime = false) | |
{ | |
if ($withTime) { | |
return \Carbon\Carbon::parse($date)->format('Y-m-d h:i a'); | |
} | |
return \Carbon\Carbon::parse($date)->format('Y-m-d'); | |
} | |
} | |
if (!function_exists('formateDateFromCarbon')) { | |
/** | |
* custom_datetime | |
* | |
* @param string $format | |
* @param mixed $datetime | |
* | |
* @return mixed | |
*/ | |
function formateDateFromCarbon($format = "Y-m-d g:i a", $datetime = null) | |
{ | |
return Carbon::parse($datetime ?? now())->format($format); | |
} | |
} | |
if (!function_exists('storagelink')) { | |
/** | |
* storageLink | |
* | |
* @param mixed $url | |
* @param string $type | |
* | |
* @return string | |
*/ | |
function storagelink($url) | |
{ | |
$defaultImage = 'settings/logo.png'; | |
if (Storage::exists(config('settings.default_image'))) { | |
$defaultImage = config('settings.default_image'); | |
} | |
// dd(Storage::disk('public')->exists($url)); | |
if ($url && Storage::disk('public')->exists($url)) { | |
return Storage::url($url); | |
} else { | |
return Storage::url($defaultImage); | |
} | |
} | |
} | |
if (!function_exists('downloadableLink')) { | |
/** | |
* downloadableLink | |
*/ | |
function downloadableLink($url, $disk = 'public') | |
{ | |
return Storage::disk($disk)->url($url); | |
} | |
} | |
if (!function_exists('deleteFileFromStorage')) { | |
/** | |
* deleteFileFromStorage | |
*/ | |
function deleteFileFromStorage($url, $disk = 'public') | |
{ | |
if (Storage::disk($disk)->exists($url)) { | |
Storage::disk($disk)->delete($url); | |
return true; | |
} | |
return false; | |
} | |
} | |
if (!function_exists('getRandomNumber')) { | |
/** | |
* getRandomNumber | |
* | |
* @param int $length | |
* | |
* @return string | |
*/ | |
function getRandomNumber($length = 8) | |
{ | |
$characters = '0123456789'; | |
$string = ''; | |
for ($i = 0; $i < $length; $i++) { | |
$string .= $characters[mt_rand(0, strlen($characters) - 1)]; | |
} | |
return $string; | |
} | |
} | |
if (!function_exists('checkPermission')) { | |
/** | |
* checkPermission | |
* | |
* @param mixed $permissions | |
* | |
* @return void | |
*/ | |
function checkPermission($permissions) | |
{ | |
if (!auth()->user()->can($permissions)) { | |
abort(403); | |
} | |
} | |
} | |
if (!function_exists('prefixGenerator')) { | |
/** | |
* prefixGenerator | |
* | |
* @param Model $model | |
* @param string $prefix | |
* | |
* @return string | |
*/ | |
function prefixGenerator(Model $model, $prefix = 'IC-') | |
{ | |
$countNumber = $model::count(); | |
return $prefix . sprintf('%06d', $countNumber + 1); | |
} | |
} | |
if (!function_exists('getToday')) { | |
/** | |
* getToday | |
* | |
* @return mixed | |
*/ | |
function getToday() | |
{ | |
return \Carbon\Carbon::parse(now())->format('Y-m-d'); | |
} | |
} | |
if (!function_exists('sendFlash')) { | |
/** | |
* sendFlash | |
* | |
* @param mixed $message | |
* @param string $type | |
* | |
* @return void | |
*/ | |
function sendFlash($message, $type = 'success') | |
{ | |
if ($type == 'error') { | |
flash($message)->error(); | |
} elseif ($type == 'important') { | |
flash($message)->important(); | |
} elseif ($type == 'info') { | |
flash($message)->info(); | |
} elseif ($type == 'warning') { | |
flash($message)->warning(); | |
} else { | |
flash($message)->success(); | |
} | |
} | |
} | |
if (!function_exists('systemSettings')) { | |
/** | |
* systemSettings | |
* | |
* @param string $columnName | |
* @param string $configName | |
* | |
* @return string | |
*/ | |
function systemSettings($columnName = '', $configName = "settings") | |
{ | |
return config($configName . '.' . $columnName); | |
} | |
} | |
if (!function_exists('getPageMeta')) { | |
/** | |
* get_page_meta | |
* | |
* @param string $metaName | |
* | |
* @return mixed | |
*/ | |
function getPageMeta($metaName = "title", $default = "") | |
{ | |
if (session()->has('page_meta_' . $metaName)) { | |
$title = session()->get("page_meta_" . $metaName); | |
session()->forget("page_meta_" . $metaName); | |
return $title ?? $default; | |
} | |
return $default; | |
} | |
} | |
if (!function_exists('setPageMeta')) { | |
/** | |
* set_page_meta | |
* | |
* @param null $content | |
* @param string $metaName | |
* | |
* @return void | |
*/ | |
function setPageMeta($content = null, $metaName = "title") | |
{ | |
if ($metaName == 'title') | |
session()->put('page_meta_header', $content); | |
session()->put('page_meta_' . $metaName, $content); | |
} | |
} | |
if (!function_exists('apiJsonResponse')) { | |
/** | |
* send api response | |
* | |
* @param string $status | |
* @param array|null $data | |
* @param string $message | |
* @param integer $statusCode | |
* @return response | |
*/ | |
function apiJsonResponse($status = 'success', $data = null, $message = '', $statusCode = 200) | |
{ | |
return response() | |
->json([ | |
'status' => $status, | |
'data' => $data ?? [], | |
'message' => $message | |
], $statusCode); | |
} | |
} | |
if (!function_exists('apiValidation')) { | |
/** | |
* validate api request | |
* | |
* @param Request $request | |
* @param array $rule | |
* @param array $attributes | |
* @return void | |
*/ | |
function apiValidation(Request $request, $rule = [], $attributes = []) | |
{ | |
$validator = Validator::make($request->all(), $rule, $attributes); | |
if ($validator->fails()) { | |
return apiJsonResponse('error', $validator->errors(), __('validation.default_message'), 422); | |
} else { | |
return null; | |
} | |
} | |
} | |
if (!function_exists('log_error')) { | |
/** | |
* Log error | |
* | |
* @param \Exception $e | |
* @return void | |
*/ | |
function log_error(\Exception $e) | |
{ | |
Log::error($e->getMessage()); | |
} | |
} | |
if (!function_exists('something_wrong_flash')) { | |
/** | |
* send flash message when error happened | |
* | |
* @param string|null $message | |
* @return void | |
*/ | |
function something_wrong_flash($message = null) | |
{ | |
Session::flash('error', $message ?? 'Something wrong!'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment