/home/mip/mip/app/Modules/helpers.php
<?php
use Hashids\Hashids;
use Illuminate\Support\Str;
function str_random($data) {
    return Str::random($data);
}

function get_website_assets($file_path = "")
{
    $path = config('website.path');
    return url($path.'/'.$file_path);
}

function get_template($file_path = "")
{
    $path = config('template.path');
    $name = config('template.name');
    return url($path.'/'.$name.'/'.$file_path);
}

function hashid($id = 0)
{
    $hashids = new Hashids();
    $id = $hashids->encode($id);
    return $id;
}

function decode($hashid = "")
{
    if($hashid <> "")
    {
        $hashids = new Hashids();
        $ids = $hashids->decode($hashid);
        if(isset($ids[0])) return $ids[0];
    }
    return;   
}

function yearArray($start_year = 2010)
{
    $years = array();
    $end_year = (int) Carbon\Carbon::now()->format('Y');
    while($end_year >= $start_year)  {
        $years[$end_year] = $end_year;
        $end_year--;
    }
   
    return $years;
}

function getYear($personal_id)
{
    return substr($personal_id, 0,4);
}

function lists($datas = array())
{
    $rows = array();
    if(!empty($datas)) {
        foreach ($datas as $data_key => $data) {
           $rows[$data] = $data;
        }
    }
    return $rows;
}

function format_date($sqldate = '0000-00-00', $format = null, $datetype = 'single', $guard = 'client') {
    if(empty($format) || $format == null) $dateformats = auth($guard)->user()->client->date_picker_display;
    else $dateformats = array('php' => $format);
    if($datetype == 'single' || $datetype == null) {
        if($sqldate <> '0000-00-00' && !empty($sqldate)) return Carbon\Carbon::parse($sqldate)->format($dateformats['php']);
    } else if($datetype=='range'){
        $date = explode('-', preg_replace('/\s+/', '', $sqldate));
        if(isset($date[0]) && isset($date[1])) {
            $from = Carbon\Carbon::parse($date[0])->format($dateformats['php']);
            $to = Carbon\Carbon::parse($date[1])->format($dateformats['php']);

            if($from == $to) return $from;
            return $from. ' to '. $to;
        }
    }
    return '';
}

function get_applicant_attachment($personal_id = 0, $file = "", $guard = 'client')
{
    $client = auth($guard)->user()->client;
    if(!empty($file) && $personal_id != 0) {
        if($client->storage_type == "public") {
            return url(\Storage::disk($client->storage_type)->url($client->root_dir.'/attachments/'.getYear($personal_id).'/'.$personal_id.'/'.$file));
        }
        if($client->storage_type == "s3") {
            return \Storage::disk($client->storage_type)->url($client->root_dir.'/attachments/'.getYear($personal_id).'/'.$personal_id.'/'.$file);
        }
    }
}

function get_extension($file = "")
{
    if($file != "") {
        $chunks = explode('.', $file);
        return strtolower(end($chunks));
    }
    return '';
}

function getDateFormatConfig($method = 'get')
{
    /*
       Form Accessor and Mutator
       use for adding or updating dates
    */
    if(in_array(strtolower($method), array('form','set'))) {
        $dateformats = config('account.dateformat.'.config('account.dateformat.default'));
    }

    /*
       Laravel built-in Accessor
       use for displaying dates
    */
    if(in_array(strtolower($method), array('get'))){
       $dateformats = config('account.displaydateformat.'.config('account.displaydateformat.default'));
    }

    return $dateformats;

}

function get_link($file_path = '')
{
    if(auth()->check()) {
        $client = auth()->user()->client;
        if(!empty($file_path)) {
            $storage_type = $client->storage_type;

            $url = Storage::disk($storage_type)->url($client->root_dir.'/'.$file_path);
            if($storage_type == "public") {
                return url($url);
            }

            if($storage_type == "s3") {
                return $url;
            }
            
        }
    }
}

function get_photo_link($filename = '', $thumbs = false)
{
   if(!empty($filename)){
        $url = '/photos/'.env('CLIENT_ROOT_DIR').'/shares/'.(($thumbs) ? 'thumbs/':'').$filename;
        if(file_exists($url)){
            return $url;
        }
        
       return $url = '/photos/'.env('CLIENT_ROOT_DIR').'/shares/'.$filename;
    }

    return '';
}


function remove_script_tags($text) 
{ 
    $text = preg_replace("/(\<script)(.*?)(script>)/si", "", "$text"); 
    return $text; 
}