<?
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * -----------------------------------------------------------------------------
 * PURPOSE : 페이징
 * CREATE  : 2018-04-11
 * -----------------------------------------------------------------------------
 */
if ( ! function_exists('gf_paging') ) {
    function gf_paging($write_pages, $cur_page, $total_page, $url) {
        $str = "";
        if ($cur_page > 1) {
            //$str .= "<a href='" . $url . "1'><img src='/img/web/mypage_arrow_left.gif'></a>";
            //$str .= "<a href='" . $url . ($cur_page-1) . "' class='btn_page prev'>이전</a>";
        }


        $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 ($start_page > 1) $str .= "<a href='" . $url . ($start_page-1) . "'><img src='".imgSrc('/img/web/mypage_arrow_left.gif'). "'></a>";

        if ($total_page >= 1) {
            for ($k=$start_page;$k<=$end_page;$k++) {
                if ($cur_page != $k)
                    $str .= " <a href='$url$k'>$k</a> ";
                else
                    $str .= " <a class='current'>$k</a> ";
            }
        }

        if ($total_page > $end_page) $str .= "<a href='" . $url . ($end_page+1) . "'><img src='". imgSrc('/img/web/mypage_arrow_right.gif'). "'></a>";

        if ($cur_page < $total_page) {
            //$str .= "[<a href='$url" . ($cur_page+1) . "'>다음</a>]";
            //$str .= "<a href='$url$total_page' class='btn_page last'>마지막</a>";
        }
        //$str .= "</ul>";

        return $str;
    }
}


/**
 * -----------------------------------------------------------------------------
 * PURPOSE : 기사 시간 계산
 * CREATE  : 2018-05-03
 * -----------------------------------------------------------------------------
 */
if ( ! function_exists('gf_article_date_text') ) {
    function gf_article_date_text($article_reg_date) {
        $now_date =  date("Y-m-d H:i:s");
        $total_time = strtotime($now_date) - strtotime($article_reg_date);

        $year   = floor($total_time/(86400*30*12));
        $month  = floor($total_time/(86400*30));
        $days   = floor($total_time/86400);
        $hours  = floor($total_time/3600);
        $min    = floor($total_time/60);

        $show_time = '1분 전';
        if($year > 0) {
            $show_time = $year.'년 전';
        } else if($month > 0) {
            $show_time = $month.'개월 전';
        } else if($days > 0) {
            $show_time = $days.'일 전';
            if($days > 6) {
                $diff = ceil($days/7);
                $show_time = $diff.'주일 전';
            }
        } else if($hours > 0) {
            $show_time = $hours.'시간 전';
        } else if($min > 0) {
            $show_time = $min.'분 전';
        }

        return $show_time;
    }
}


/**
 * -----------------------------------------------------------------------------
 * PURPOSE : 검색어 fulltext string으로 변경
 * CREATE  : 2018-05-03
 * -----------------------------------------------------------------------------
 */
if ( ! function_exists('gf_search_keyword_fulltext_string') ) {
    function gf_search_keyword_fulltext_string($search_keyword) {

        $search_keyword_txt = $search_keyword;
        $arr_keyword = explode(" ", $search_keyword);
        if(count($arr_keyword) > 0) {
            if(count($arr_keyword)  > 1)    array_unshift($arr_keyword, $search_keyword);
            $arr_new_keyword = array();
            foreach ($arr_keyword as $key =>$val) {
                $arr_new_keyword[] = '*'.$val.'*';
            }
            $search_keyword_txt = implode(" ", $arr_new_keyword);
        }

        return $search_keyword_txt;
    }
}



/**
 * -----------------------------------------------------------------------------
 * PURPOSE : 랜덤문자 + 숫자
 * CREATE  : 2018-05-30
 * -----------------------------------------------------------------------------
 */
if ( ! function_exists('gf_generate_random_string') ) {
    function gf_generate_random_string($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }
}