<?php
if (!defined('_GNUBOARD_')) exit;

function latest_group_hits($a, $b) {
    return $b['wr_hit'] - $a['wr_hit'];
}

// $not_table 게시판제외 설정 array('bo_table1','bo_table2','bo_table3')

//echo latest_group("스킨이름", "그룹ID", 게시물수, 제목글자수, "정렬방식", "이미지만", "작성기간설정", "회원아이디설정", "게시판제외", "옵션설정");

function latest_group($skin_dir='', $gr_id, $rows=10, $subject_len=40, $order='', $imgs='', $days='', $mbid='', $not_table=array(), $options='') { 

    global $g5;

    if (!$skin_dir) $skin_dir = 'basic';

    if (preg_match('#^theme/(.+)$#', $skin_dir, $match)) {
        if (G5_IS_MOBILE) {
            $latest_skin_path = G5_THEME_MOBILE_PATH . '/' . G5_SKIN_DIR . '/latest/' . $match[1];
            if (!is_dir($latest_skin_path))
                $latest_skin_path = G5_THEME_PATH . '/' . G5_SKIN_DIR . '/latest/' . $match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        } else {
            $latest_skin_path = G5_THEME_PATH . '/' . G5_SKIN_DIR . '/latest/' . $match[1];
            $latest_skin_url = str_replace(G5_PATH, G5_URL, $latest_skin_path);
        }
        $skin_dir = $match[1];
    } else {
        if (G5_IS_MOBILE) {
            $latest_skin_path = G5_MOBILE_PATH . '/' . G5_SKIN_DIR . '/latest/' . $skin_dir;
            $latest_skin_url = G5_MOBILE_URL . '/' . G5_SKIN_DIR . '/latest/' . $skin_dir;
        } else {
            $latest_skin_path = G5_SKIN_PATH . '/latest/' . $skin_dir;
            $latest_skin_url = G5_SKIN_URL . '/latest/' . $skin_dir;
        }
    }

    // $order 값에 따라 랜덤, 히트순, 날짜순(기본) 결정 - 20200116
    $order_sql = '';
    if (!$order) {
        $order_sql = 'wr_num desc';
    } else if ($order == "hit") {
        $order_sql = 'wr_hit desc';
    } else if ($order == "date") {
        $order_sql = 'wr_datetime desc';
    } else if($order == "rand") {
        $order_sql = 'rand()';
    } 

    $list = array();

    $sql_common = "";
    if ($imgs) {
        $sql_common .= " and wr_file > 0 ";
    } 
    if ($days) {
        $nowYmd = date('Ymd', G5_SERVER_TIME);
        $startYmd = date('Ymd', strtotime('-'.$days." day", G5_SERVER_TIME));
        $sql_common .= " and date_format(wr_datetime, '%Y%m%d') between '{$startYmd}' and '{$nowYmd}' ";			
    }
    if ($mbid) {
        $sql_common .= " and mb_id = '$mbid' ";
    }
    if ($options) {
        $sql_common .= " and ".$options;
    }

    $not_sql = ""; 
    $not_in = ""; 
    if($not_table!="") {
    	foreach ($not_table as $key => $val) {
    		$not_in .= "'$val',";
    	}
        $not_in .= "'1'";
        $not_sql = " and bo_table not in({$not_in}) "; 
    }

    $sql = " select bo_table, bo_subject from {$g5['board_table']} where gr_id = '{$gr_id}' {$not_sql}";
    $board_list = sql_query($sql);
    for ($i = 0; $board = sql_fetch_array($board_list); $i++) {
        $tmp_write_table = $g5['write_prefix'] . $board['bo_table'];

        $sql = " select * from {$tmp_write_table} where wr_is_comment = 0 {$sql_common} order by {$order_sql} limit 0, {$rows}";
        $result = sql_query($sql);
        for ($i = 0; $row = sql_fetch_array($result); $i++) {
            $tmp_list = get_list($row, $board, $latest_skin_url, $subject_len);
            $tmp_list['bo_table'] = $board['bo_table'];
            $tmp_list['bo_subject'] = $board['bo_subject'];
            $tmp_list['href'] = get_pretty_url($tmp_list['bo_table'], $row['wr_id']);
            
            array_push($list, $tmp_list);
        }
    }

    usort($list, 'latest_group_hits');
    
    $list = array_slice($list, 0, $rows);
    
    ob_start();
    include $latest_skin_path . '/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}