<?php
/* 
 * Author   : Dhidhik , 18 Feb 2016
 * Desc     : untuk beberapa function global untuk pengecekan khusus string
 * 
 */
function notCaseSensitifNotSpace($dataString){
    $dataString = str_replace(' ','',strtoupper($dataString));
    return $dataString;
}

function upperNotSpace($dataString){
    $dataString = str_replace(' ','',strtoupper($dataString));
    return $dataString;
}

function dateMMMtoMM($strMMM){
    $Bulan_ex = "";
    switch($strMMM){
        case 'Jan' : $Bulan_ex = '01'; break;
        case 'Feb' : $Bulan_ex = '02'; break;
        case 'Mar' : $Bulan_ex = '03'; break;
        case 'Apr' : $Bulan_ex = '04'; break;
        case 'May' : $Bulan_ex = '05'; break;
        case 'Jun' : $Bulan_ex = '06'; break;
        case 'Jul' : $Bulan_ex = '07'; break;
        case 'Aug' : $Bulan_ex = '08'; break;
        case 'Sep' : $Bulan_ex = '09'; break;
        case 'Oct' : $Bulan_ex = '10'; break;
        case 'Nov' : $Bulan_ex = '11'; break;
        case 'Dec' : $Bulan_ex = '12'; break;
    }
    return $Bulan_ex;
}

function code4Digit($strCode){
    $intCode    = $strCode * 1;
    $rstCode    = "";
    $lenCode    = strlen($intCode);
    switch($lenCode){
        case 1 : $rstCode = "000".$intCode; break;
        case 2 : $rstCode = "00".$intCode; break;
        case 3 : $rstCode = "0".$intCode; break;
        case 4 : $rstCode = $intCode; break;
    }
    return $rstCode;
}

function rangeMonths($date1,$date2){//format date yyyy-mm-dd, $date1 harus <= $date2
    $exDate1    = explode("-", $date1);
    $exDate2    = explode("-", $date2);
    $month1     = $exDate1[1];
    $year1      = $exDate1[0];
    $month2     = $exDate2[1];
    $year2      = $exDate2[0];
    if($year1==$year2){
        $rstMonth = $month2 - $month1;
        $rstMonth<0?$rstMonth=($rstMonth*(-1)+ 1):$rstMonth=$rstMonth+ 1;
    }else if(($year1+1)==$year2){
        $rangeMonth1= 12-$month1;
        $rstMonth   = $rangeMonth1 + $month2 + 1;
    }else if(($year1+1)<$year2){
        $rangeMonth1= 12-$month1;
        $rangeYear  = $year2 - $year1 - 1;
        $rstMonth   = $rangeMonth1+ ($rangeYear*12) + $month2 + 1;
    }
    return $rstMonth;
}

function cropCompanyOfBranch($branchCode){
    $arBranch = explode("-", $branchCode);
    $company  = $arBranch[0];
    return $company;
}
function numberInd($strNum){
    $rst = str_replace(",", "", $strNum);
    return $rst;
}
function checkNumberInd($strNum){
    $rst = numberInd($strNum);
    if(is_numeric($rst)){
        return true;
    }else{
        return false;
    }    
}

function arrayInUrlToStr($data_grid){ 
    if($data_grid!=null){
        $data_grid = str_replace('__', '/', $data_grid);
        $data_grid = str_replace('_', '-', $data_grid);
        $data_grid = str_replace('%20', ' ', $data_grid);
        $arr_grid_lv1 = explode('linebaru', $data_grid);

        $obj_string = '[';
        for ($x = 0; $x < count($arr_grid_lv1) - 1; $x++) {
            $arr_data_lv1 = $arr_grid_lv1[$x];
            $arr_grid_lv2 = explode('spasi', $arr_data_lv1);

            $obj_string .= '{';
            for ($i = 0; $i < count($arr_grid_lv2); $i++) {
                $arr_grid_lv3 = explode(':', $arr_grid_lv2[$i]);
                if ($i < count($arr_grid_lv2) - 1) {
                    $obj_string .= '"' . $arr_grid_lv3[0] . '":"' . $arr_grid_lv3[1] . '",';
                } else {
                    $obj_string .= '"' . $arr_grid_lv3[0] . '":"' . $arr_grid_lv3[1] . '"';
                }
            }

            if ($x < count($arr_grid_lv1) - 2) {
                $obj_string .= '},';
            } else {
                $obj_string .= '}';
            }
        }
        $obj_string .= ']';
    }else{
        $obj_string = '[]';
    }
    return $obj_string;
}

function arrayParamInUrlToStr($obj_string){  
    $data_filter = [];
    if($obj_string!=null){
        $data_filter    = str_replace('%252D', '-', $obj_string);
        $data_filter    = json_decode(urldecode($data_filter), TRUE);    
    }
    return $data_filter;
}

function get_user_browser()
{
    $u_agent = $_SERVER['HTTP_USER_AGENT'];
    $ub = '';
    if(preg_match('/MSIE/i',$u_agent))
    {
        $ub = "ie";
    }
    elseif(preg_match('/Firefox/i',$u_agent))
    {
        $ub = "firefox";
    }
    elseif(preg_match('/Safari/i',$u_agent))
    {
        $ub = "safari";
    }
    elseif(preg_match('/Chrome/i',$u_agent))
    {
        $ub = "chrome";
    }
    elseif(preg_match('/Flock/i',$u_agent))
    {
        $ub = "flock";
    }
    elseif(preg_match('/Opera/i',$u_agent))
    {
        $ub = "opera";
    }

    return $ub;
}