PHP

그누보드5 최신글 커스텀 (게시판 여러개)

먹세 2020. 1. 27. 01:27

이전 글 (여러개 게시판의 글을 한개씩 로테이션으로 출력)을 최신글로 뽑아보기

 

/theme/해당테마이름/lib/new_latest.lib.php 안에 새로운 latest 함수명을 짓고 넣으면 됨

사용 : total_latest('theme/total_latest', 'total_board', 'free,humor,gallery', 10, 20);

 

 

// total_board 게시판 최신글 추출
function total_latest($skin_dir='', $bo_table, $boards, $rows=10, $subject_len=40, $cache_time=1, $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;
        }
    }

    $caches = null;

    if(G5_USE_CACHE) {
        $cache_file_name = "latest-{$bo_table}-{$skin_dir}-{$rows}-{$subject_len}-".g5_cache_secret_key();
        $caches = g5_get_cache($cache_file_name);
        $cache_list = isset($caches['list']) ? $caches['list'] : array();
        g5_latest_cache_data($bo_table, $cache_list);
    }

    if( $caches === false ){

        $list = array();
        $board = get_board_db($bo_table, true);
        $bo_subject = get_text($board['bo_subject']);
		$exp = explode(',',$boards);
		
		//통합게시판 순서정하기
		$total_count = 0;
		for($i=0;$i<count($exp);$i++){
			$boards_sql[$i] = "select * from g5_write_{$exp[$i]} where wr_is_comment = 0 order by wr_num, wr_reply";
			$b_latest[$i] = sql_fetch_array(sql_query($boards_sql[$i]));
			//테이블 이름 추가
			$dates[$i]['bo_table'] = $exp[$i];
			$dates[$i]['latest_datetime'] = $b_latest[$i]['wr_datetime'];

			//total count set
			$board_count[$i] = "select * from {$g5['board_table']} where bo_table = '{$exp[$i]}'";
			$board_count_row = sql_fetch_array(sql_query($board_count[$i]));
			$total_count += $board_count_row['bo_count_write'];
			$total_page  = ceil($total_count / $page_rows);  // 전체 페이지 계산
		}

		//내림차순 정렬
		for($i=count($dates)-1;$i>0;$i--){
			for($j=0;$j<$i;$j++){
				if($dates[$j]['latest_datetime'] < $dates[$j+1]['latest_datetime']){ //desc
					$temp = $dates[$j+1];
					$dates[$j+1] = $dates[$j];
					$dates[$j] = $temp;
				}
			}
		}
		

		//통합게시판 순서대로 최신글순으로 로테이션
		$i=0;
		$k=0;
		foreach($dates as $key => $value){
			$q[$key] = "select * from g5_write_{$value['bo_table']} where wr_is_comment = 0 order by wr_num, wr_reply";
			$q_result[$key] = sql_query($q[$key]);
			
			//list 게시판별 데이터 분리
			while($row2 = sql_fetch_array($q_result[$key])){
				if($list_arr[$value['bo_table']]){
					array_push($list_arr[$value['bo_table']], $row2);
				}else{
					$list_arr[$value['bo_table']][0]=$row2;
				}
			}

		}
		
		// list 게시판별 순서대로 한개씩 세팅
		$z=0; //게시판 갯수 체크
		$a=0; //배열 번호
		$rotation_list=[];
		$rotation_list_count=count($rotation_list);
		while($rotation_list_count<$total_count){
			$b_count=$a%count($dates);
			$board_name = $dates[$b_count]['bo_table'];

			if($list_arr[$board_name][$z]){
				$list_arr[$board_name][$z]['bo_table'] = $board_name; //게시판명 세팅
				array_push($rotation_list, $list_arr[$board_name][$z]);
			}
			
			if($a%count($dates)==count($dates)-1){
				$z++;
			}
			$a++;
			$rotation_list_count=count($rotation_list);
		}

		//list 세팅
		//배열 페이징
		$list = array_slice($rotation_list, 0, $rows);

		foreach($list as $key => $value){
			$board['bo_table'] = $value['bo_table'];//링크를 위해 각 테이블에 맞게 다시 세팅
			$list[$key] = get_list($value, $board, $latest_skin_url, $subject_len);
			$list[$key]['first_file_thumb'] = (isset($value['wr_file']) && $value['wr_file']) ? get_board_file_db($board['bo_table'], $value['wr_id'], 'bf_file, bf_content', "and bf_type between '1' and '3'", true) : array('bf_file'=>'', 'bf_content'=>'');

			// 썸네일 추가
			if($options && is_string($options)) {
				$options_arr = explode(',', $options);
				$thumb_width = $options_arr[0];
				$thumb_height = $options_arr[1];
				$thumb = get_list_thumbnail($board['bo_table'], $value['wr_id'], $thumb_width, $thumb_height, false, true);
				// 이미지 썸네일
				if($thumb['src']) {
					$img_content = '';
					$list[$i]['img_thumbnail'] = ''.$img_content.'';
				// } else {
				//     $img_content = '';
				}
			}
			
		}
	

        g5_latest_cache_data($bo_table, $list);

        if(G5_USE_CACHE) {

            $caches = array(
                'list' => $list,
                'bo_subject' => sql_escape_string($bo_subject),
            );

            g5_set_cache($cache_file_name, $caches, 3600 * $cache_time);
        }
    } else {
        $list = $cache_list;
        $bo_subject = (is_array($caches) && isset($caches['bo_subject'])) ? $caches['bo_subject'] : '';
    }

    ob_start();
    include $latest_skin_path.'/latest.skin.php';
    $content = ob_get_contents();
    ob_end_clean();

    return $content;
}
반응형