헬퍼

// 멘토링분야 종류별로 가져오기
if (! function_exists('MENTORING_KINDS_SORT')) {

    // isGraduate=true면 졸업생, false면 재학생
    function MENTORING_KINDS_SORT($isGraduate=true)
    {
        $mentoringKinds = [];

        $config = config("services");

        foreach ($config['_MENTOR_OPTIONS_']['mentoringKinds'] as $key => $val) {
            $search = $isGraduate ? "졸업생" : "재학생";
            if ($val['type'] == $search) $mentoringKinds[] =  $val;
        }

        return $mentoringKinds;
    }

}

// 쿼리스트링 만들어서 리턴. 기존쿼리스트링에서 $_GET[$type]=$value 만 바꿔줌.
// $value가 null이라면 $_GET[$type] 을 쿼리스트링에서 아예 제외함.
if (! function_exists('CREATE_QUERY_STRING')) {
    
    function CREATE_QUERY_STRING($type, $value=null)
    {
        global $_GET;

        $query_string_arr = $_GET;
        if ($value != null) {
            $query_string_arr[$type] = $value;
        } else {
            unset($query_string_arr[$type]);
        }

        $query_string = "";
        foreach ($query_string_arr as $key => $val) {
            if ($query_string != "") $query_string .= "&";
            $query_string .= "{$key}={$val}";
        }
        return $query_string;
    }

}

// $_GET[$type] == $id 면 $classname 스트링을 리턴
if (! function_exists('class_operator')) {

    function class_operator($type, $id, $classname, $else=false)
    {
        global $_GET;

        if (isset($_GET[$type])) {

            if ($_GET[$type] == $id) {
                return $classname;
            }

        }

        // $_GET[$type] 이 존재하지 않을때 무조건 classname 리턴
        if ($else) {

            if (!isset($_GET[$type])) {
                return $classname;
            }

        }

        return "";
    }

}

// 직무유형, 멘토링유형 id값 주면 name으로 리턴.
// $type=1 : 직무유형
// $type=2 : 멘토링유형
if (! function_exists('EXCHANGE_ID_TO_NAME')) {

    function EXCHANGE_ID_TO_NAME($id, $type=1)
    {
        $data = config("services._MENTOR_OPTIONS_");
        $data = $type == 1 ? $data['jobTypes'] : $data['mentoringKinds'];

        $str = "";

        $ids = explode(",", $id);
        foreach ($ids as $key => $value) {
            if ($value == "") continue;
            if ($str != "") $str .= ", ";
            $str .= $data[array_search($value, array_column($data, "id"))]['name'];
        }

        return $str;
    }

}

// get 페이지네이션
if (! function_exists("pagination")) {

    function pagination($write_pages, $total_page, $add="")
    {

        $qstr = $_SERVER['QUERY_STRING'];
        $cur_page = $_REQUEST['page'] ?? 1;

        if ($qstr != "") {
            $qstr = "";
            foreach ($_REQUEST as $key=>$val) {
                if ($key == "page") continue;
                if ($qstr != "") $qstr .= "&";
                $qstr .= "{$key}={$val}";
            }
        }

        $pageAddress = explode("?", $_SERVER['REQUEST_URI'])[0];

        $url = $_SERVER['REQUEST_SCHEME'] . "://" . $_SERVER['SERVER_NAME'] . $pageAddress . "?" . $qstr;

        $str = '';
        if ($cur_page > 1) {
            $str .= "<a href='{$url}&page=1{$add}' class='pg_page pg_start'></a>".PHP_EOL;
        }

        $start_page = ( ( (int)( ($cur_page - 1 ) / $write_pages ) ) * $write_pages ) + 1;

        $end_page = $start_page + $write_pages - 1;

        if ($end_page >= $total_page) $end_page = $total_page;

        if ($cur_page > 1) {
            $cur_page_sub = $cur_page-1;
            $str .= "<a href='{$url}&page={$cur_page_sub}{$add}' class='pg_page pg_prev'></a>".PHP_EOL;
        }

        if ($total_page > 1) {
            for ($k=$start_page;$k<=$end_page;$k++) {
                if ($cur_page != $k)
                    $str .= '<a href="'.$url.'&page='.$k.$add.'" class="pg_page">'.$k.'<span class="sound_only"></span></a>'.PHP_EOL;
                else
                    $str .= '<span class="sound_only"></span><strong class="pg_current">'.$k.'</strong><span class="sound_only"></span>'.PHP_EOL;
            }
        }

        if ($total_page > $cur_page) $str .= '<a href="'.$url.'&page='.($cur_page+1).$add.'" class="pg_page pg_next"></a>'.PHP_EOL;

        if ($cur_page < $total_page) {
            $str .= '<a href="'.$url.'&page='.$total_page.$add.'" class="pg_page pg_end"></a>'.PHP_EOL;
        }

        if ($str)
            return "<nav class=\\"pg_wrap\\"><span class=\\"pg\\">{$str}</span></nav>";
        else
            return "";
    }

}

// 영어이름주면 한글이름으로 리턴
if (! function_exists("conversion_eng_to_kor")) {

    function conversion_eng_to_kor ($str, $type=1) {

        $return = "";

        if ($type == 1)
        {
            switch ($str)
            {
                case "board_job":
                    $return = "직무이야기";
                    break;
                case "board_recruit":
                    $return = "채용공고";
                    break;
                case "board_news":
                    $return = "유멘토 소식";
                    break;
                case "board_qna":
                    $return = "QNA";
                    break;
                case "mentoring_comments":
                    $return = "멘토링이야기";
                    break;
                case "mentor_view_comments":
                    $return = "멘토정보";
                    break;
                default:
                    $return = "";
                    break;
            }

        }

        return $return;

    }
}

if (! function_exists("upload_path_auto")) {

    function upload_path_auto($path) {

        $path = explode("/", $path);
        array_shift($path);
        return "/storage/" . implode("/", $path);

    }

}

// option으로 job_type, mentoring_kind 다 뽑음. 1:잡타입, 2:멘토링종류
if (! function_exists("print_options")) {

    function print_options($type=1, $graduate=1) {

        $data = config("services._MENTOR_OPTIONS_");
        $data = $type == 1 ? $data['jobTypes'] : $data['mentoringKinds'];

        foreach ($data as $key => $val)
        {
            if ($graduate == 1) {
                if ($val['type'] == "재학생") continue;
            } else {
                if ($val['type'] == "졸업생") continue;
            }
            echo "<option value='{$val['id']}'>{$val['name']}</option>";
        }

    }

}

if (! function_exists("curl_post")) {

    function curl_post($url, $params) {

        $post_field_string = http_build_query($params, '', '&');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_field_string);
        curl_setopt($ch, CURLOPT_POST, true);
        $response = curl_exec($ch);
        curl_close ($ch);

        return $response;

    }

}

// 배열->stdClass안에 든 json 형태를 디코드해서 리턴.
if (! function_exists("convert_json_to_stdClass")) {

    function convert_json_to_stdClass($stdClass, $json_name)
    {
        $return = $stdClass;
        foreach ($return as $val)
        {
            if ($val->$json_name == "") continue;
            $val->$json_name = json_decode($val->$json_name);
        }
        return $return;
    }
}

// 게시판 db이름 -> 한글이름으로
if (! function_exists("boardname")) {

    function boardname($eng) {

        switch ($eng)
        {
            case "board_job":
                return "직무이야기";
                break;
            case "board_qna":
                return "QNA";
                break;
            case "board_news":
                return "유멘토소식";
                break;
            case "board_recruit":
                return "채용공고";
                break;
            case "board_mentoring_ask":
                return "멘토링이야기질문";
                break;
            case "board_mentoring_asnwer":
                return "멘토링이야기답변";
                break;
            case "board_mentoring":
                return "멘토링이야기";
                break;
        }

    }
}