181 lines
4.5 KiB
PHP
181 lines
4.5 KiB
PHP
<?php
|
|
|
|
use App\Models\Media;
|
|
use App\Models\User;
|
|
use App\Settings\AppSettings;
|
|
use Carbon\Carbon;
|
|
use Hashids\Hashids;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\File;
|
|
use Illuminate\Support\Str;
|
|
|
|
// Template
|
|
// if (! function_exists('my_custom_function')) {
|
|
// function my_custom_function($param) {
|
|
// }
|
|
// }
|
|
|
|
if (! function_exists('store_file')) {
|
|
function store_file($file = null, $path, $disk = 'public')
|
|
{
|
|
if ($file && $file instanceof \Illuminate\Http\UploadedFile) {
|
|
$randomFilename = Str::random(35).time().'.'.$file->getClientOriginalExtension();
|
|
$originalFileName = Str::slug(pathinfo($file->getClientOriginalName(), PATHINFO_FILENAME)).'.'.$file->getClientOriginalExtension();
|
|
|
|
$file->storeAs($path, $randomFilename, $disk);
|
|
|
|
return [
|
|
'randomFileName' => $randomFilename,
|
|
'originalFileName' => $originalFileName,
|
|
'isSuccess' => true,
|
|
];
|
|
} else {
|
|
return [
|
|
'randomFileName' => null,
|
|
'originalFileName' => null,
|
|
'isSuccess' => false,
|
|
];
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! function_exists('delete_file')) {
|
|
function delete_file($path)
|
|
{
|
|
if (File::exists(storage_path($path))) {
|
|
File::delete(storage_path($path));
|
|
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! function_exists('is_file_exists')) {
|
|
function is_file_exists($path)
|
|
{
|
|
return File::exists(public_path('storage/' . $path));
|
|
}
|
|
}
|
|
|
|
if (!function_exists('get_role_name')) {
|
|
function get_role_name($role = null) {
|
|
|
|
if($role === null){
|
|
$role = Auth::user()->role_id;
|
|
}
|
|
|
|
switch($role) {
|
|
case '1':
|
|
return "Administrator";
|
|
case '2':
|
|
return "Admin Monitoring";
|
|
case '3':
|
|
return "Perusahaan";
|
|
case '4':
|
|
return "Admin Konten";
|
|
default:
|
|
return "Role Tidak Dikenal";
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!function_exists('shorten_text')) {
|
|
function shorten_text($text, $maxLength = 100) {
|
|
if (strlen($text) > $maxLength) {
|
|
return substr($text, 0, $maxLength) . '...';
|
|
}
|
|
|
|
return $text;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('is_role')) {
|
|
function is_role($roles) {
|
|
if (Auth::check()) {
|
|
return in_array(Auth::user()->role_id, (array) $roles);
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (! function_exists('encrypt_id')) {
|
|
function encrypt_id($id)
|
|
{
|
|
$hashids = new Hashids('alwayssaybismillahirrahmanirrahim', 15);
|
|
return $hashids->encode($id);
|
|
}
|
|
}
|
|
|
|
if (! function_exists('decrypt_id')) {
|
|
function decrypt_id($hash)
|
|
{
|
|
$hashids = new Hashids('alwayssaybismillahirrahmanirrahim', 15);
|
|
$result = $hashids->decode($hash);
|
|
return $result ? $result[0] : null;
|
|
}
|
|
}
|
|
|
|
if (!function_exists('idn_date')) {
|
|
function idn_date($tanggal, $format = 'd F Y')
|
|
{
|
|
return Carbon::parse($tanggal)->locale('id')->translatedFormat($format);
|
|
}
|
|
}
|
|
|
|
if (!function_exists('get_user_status')) {
|
|
function get_user_status($status)
|
|
{
|
|
switch($status){
|
|
case '0':
|
|
return 'Menunggu';
|
|
case '1':
|
|
return 'Aktif';
|
|
case '2':
|
|
return 'Nonaktif';
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! function_exists('get_cooperation_notif')) {
|
|
function get_cooperation_notif() {
|
|
$user = User::findOrFail(Auth::id());
|
|
if($user->role_id === 3){
|
|
$mediaId = $user->company->media->id;
|
|
|
|
$media = Media::with('cooperations')->findOrFail($mediaId);
|
|
return $media->cooperations()
|
|
->where('start_date', '<=', Carbon::now()->locale('id')->toDateString())
|
|
->where('end_date', '>=', Carbon::now()->locale('id')->toDateString())
|
|
->count();
|
|
|
|
}else{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (! function_exists('slugify')) {
|
|
function slugify($param) {
|
|
return Str::slug($param);
|
|
}
|
|
}
|
|
|
|
if (! function_exists('get_unread_announcements')) {
|
|
function get_unread_announcements() {
|
|
$user = User::findOrFail(Auth::id());
|
|
if($user->role_id === 3){
|
|
$userId = $user->id;
|
|
|
|
$user = User::with('announcements')->findOrFail($userId);
|
|
return $user->announcements()
|
|
->wherePivot('status', '=', '0')
|
|
->count();
|
|
|
|
}else{
|
|
return 0;
|
|
}
|
|
}
|
|
}
|