<?php
/*
 * g4m admin
 * board_list.php
 * 게시판 목록
 * 
 */
$sub_menu = "300100";
include_once("./_common.php");

auth_check($auth[$sub_menu], "r");

$token = get_token();

// DHTML 에디터 사용 필드 추가 : 061021
sql_query(" ALTER TABLE `{$g4['board_table']}` ADD `bo_use_dhtml_editor` TINYINT NOT NULL AFTER `bo_use_secret` ", false);
// RSS 보이기 사용 필드 추가 : 061106
sql_query(" ALTER TABLE `{$g4['board_table']}` ADD `bo_use_rss_view` TINYINT NOT NULL AFTER `bo_use_dhtml_editor` ", false);

$sql_common = " from {$g4['board_table']} a ";
$sql_search = " where (1) ";

if ($is_admin != "super") {
    $sql_common .= " , {$g4['group_table']} b ";
    $sql_search .= " and (a.gr_id = b.gr_id and b.gr_admin = '{$member['mb_id']}') ";
}

if ($stx) {
    $sql_search .= " and ( ";
    switch ($sfl) {
        case "bo_table" :
            $sql_search .= " ({$sfl} like '{$stx}%') ";
            break;
        case "a.gr_id" :
            $sql_search .= " ({$sfl} = '{$stx}') ";
            break;
        default :
            $sql_search .= " ({$sfl} like '%{$stx}%') ";
            break;
    }
    $sql_search .= " ) ";
}

if (!$sst) {
    $sst = "a.gr_id, a.bo_table";
    $sod = "asc";
}
$sql_order = " order by {$sst} {$sod} ";

$sql = " select count(*) as cnt {$sql_common} {$sql_search} {$sql_order} ";
$row = sql_fetch($sql);
$total_count = $row['cnt'];

$rows = $config['cf_page_rows'];
$total_page = ceil($total_count / $rows);  // 전체 페이지 계산
if ($page == "") {
    $page = 1;
} // 페이지가 없으면 첫 페이지 (1 페이지)
$from_record = ($page - 1) * $rows; // 시작 열을 구함

$sql = " select * {$sql_common} {$sql_search} {$sql_order} limit {$from_record}, {$rows} ";
$result = sql_query($sql);

$listall = "<a href='{$_SERVER['PHP_SELF']}'>처음</a>";

$g4[title] = "게시판관리";
include_once("./admin.head.php");

$colspan = 13;
?>

<script type="text/javascript">
    var list_update_php = 'board_list_update.php';
    var list_delete_php = 'board_list_delete.php';
</script>
<div class="row">
    <div class="round-box">
        <div class='col-sm-3' style="line-height: 34px">
            <?php echo $listall ?> (게시판수 : <?php echo number_format($total_count) ?>개)
        </div>
        <div class='col-sm-9' style="text-align: right">
            <form name='fsearch' method='get' class="form-inline" role='form'>
                <div class='form-group'>
                    <select name='sfl' class='form-control'>
                        <option value='bo_table'>TABLE</option>
                        <option value='bo_subject'>제목</option>
                        <option value='a.gr_id'>그룹ID</option>
                    </select>
                </div>
                <div class='form-group'>
                    <input type='text' name='stx' class='form-control' required itemname='검색어' value='<?php echo $stx ?>' placeholder="검색어"/>
                </div>
                <button type="submit" class="btn btn-primary">검색</button>
            </form>
        </div>
    </div>
    <div class='col-sm-12'>

        <form name='fboardlist' method='post' class='form-inline' role='form'>
            <input type='hidden' name=sst   value="<?php echo $sst ?>">
            <input type='hidden' name='sod'   value="<?php echo $sod ?>">
            <input type='hidden' name='sfl'   value="<?php echo $sfl ?>">
            <input type='hidden' name='stx'   value="<?php echo $stx ?>">
            <input type='hidden' name='page'  value="<?php echo $page ?>">
            <input type='hidden' name='token' value="<?php echo $token ?>">
            <table class='table table-striped hide-sm'>
                <thead>
                    <tr>
                        <th rowspan=2><input type=checkbox name=chkall value="1" onclick="check_all(this.form)"></th>
                        <th rowspan=2><?php echo subject_sort_link("bo_table") ?>TABLE</a></th>
                        <th colspan=2><?php echo subject_sort_link("bo_subject") ?>제목</a></th>
                        <th rowspan=2 title="글읽기 포인트"><?php echo subject_sort_link("bo_read_point") ?>읽기<br>포인트</a></th>
                        <th rowspan=2 title="글쓰기 포인트"><?php echo subject_sort_link("bo_write_point") ?>쓰기<br>포인트</a></th>
                        <th rowspan=2 title="코멘트쓰기 포인트"><?php echo subject_sort_link("bo_comment_point") ?>코멘트<br>포인트</a></th>
                        <th rowspan=2 title="다운로드 포인트"><?php echo subject_sort_link("bo_download_point") ?>다운<br>포인트</a></th>
                        <th rowspan=2 title="검색사용"><?php echo subject_sort_link("bo_use_search") ?>검색<br>사용</a></th>
                        <th rowspan=2 title="검색순서"><?php echo subject_sort_link("bo_order_search") ?>검색<br>순서</a></th>
                        <th rowspan=2><a href="./board_form.php" class="btn btn-primary">생성</a></th>
                    </tr>
                    <tr>
                        <th><?php echo subject_sort_link("a.gr_id") ?>그룹</a></th>
                        <th><?php echo subject_sort_link("bo_skin", "", "desc") ?>스킨</a></th>
                    </tr>
                </thead>
                <?php
// 스킨디렉토리
                $skin_options = "";
                $arr = get_skin_dir("board");
                for ($k = 0; $k < count($arr); $k++) {
                    $option = $arr[$k];
                    if (strlen($option) > 10)
                        $option = substr($arr[$k], 0, 18) . "…";

                    $skin_options .= "<option value='{$arr[$k]}'>{$option}</option>";
                }
                
                $show_sm = '<div class="panel panel-default show-sm">
                            <div class="panel-heading">게시판목록</div>
                            <div class="panel-body">
                            <a href="./board_form.php" class="btn btn-info">게시판생성</a>
                            </div><ul class="list-group">';
                for ($i = 0; $row = sql_fetch_array($result); $i++) {
                    //print_r2($row);
                    $s_upd = "<a href='./board_form.php?w=u&bo_table={$row['bo_table']}&{$qstr}'><img src='img/icon_modify.gif' border=0 title='수정'></a>";
                    $s_del = "";
                    if ($is_admin == "super") {
                        //$s_del = "<a href=\"javascript:del('./board_delete.php?bo_table=$row[bo_table]&$qstr');\"><img src='img/icon_delete.gif' border=0 title='삭제'></a>";
                        $s_del = "<a href=\"javascript:post_delete('board_delete.php', '{$row['bo_table']}');\"><img src='img/icon_delete.gif' border=0 title='삭제'></a>";
                    }
                    $s_copy = "<a href=\"javascript:board_copy('{$row['bo_table']}');\"><img src='img/icon_copy.gif' border=0 title='복사'></a>";

                    
                      // 스킨디렉토리
                    $skin_options = "";
                    $arr = get_skin_dir("board");
                    for ($k = 0; $k < count($arr); $k++) {
                        $option = $arr[$k];
                        if (strlen($option) > 10)
                            $option = substr($arr[$k], 0, 18) . "…";

                        $skin_options .= "<option value='$arr[$k]'";
                        if ($arr[$k] == $row['bo_m_skin'])
                            $skin_options .= " selected ";
                        $skin_options .= ">$option</option>";
                    }


    //$list = $i % 2;
                    echo "<tr>";
                    echo "<td rowspan=2 height=25><input type='hidden' name=board_table[{$i}] value='{$row['bo_table']}'><input type=checkbox name=chk[] value='{$i}'></td>";
                    echo "<td rowspan=2><a href='{$g4['g4m_bbs_path']}/board.php?bo_table={$row['bo_table']}'><b>{$row['bo_table']}</b></a></td>";
                    echo "<td colspan=2 align=left height=25><input type=text class=ed name=bo_subject[{$i}] value='" . get_text($row['bo_subject']) . "' style='width:99%'></td>";
                    echo "<td rowspan=2 title='읽기 포인트'><input type=text class=ed name=bo_read_point[{$i}] value='{$row['bo_read_point']}' style='width:33px;'></td>";
                    echo "<td rowspan=2 title='쓰기 포인트'><input type=text class=ed name=bo_write_point[{$i}] value='{$row['bo_write_point']}' style='width:33px;'></td>";
                    echo "<td rowspan=2 title='속글쓰기 포인트'><input type=text class=ed name=bo_comment_point[$i] value='{$row['bo_comment_point']}' style='width:33px;'></td>";
                    echo "<td rowspan=2 title='다운로드 포인트'><input type=text class=ed name=bo_download_point[$i] value='{$row['bo_download_point']}' style='width:33px;'></td>";
                    echo "<td rowspan=2 title='검색사용'><input type=checkbox name=bo_use_search[{$i}] " . ($row['bo_use_search'] ? 'checked' : '') . " value='1'></td>";
                    echo "<td rowspan=2 title='검색순서'><input type=text class=ed name=bo_order_search[{$i}] value='{$row['bo_order_search']}' size=2></td>";
                    echo "<td rowspan=2>{$s_upd} {$s_del} {$s_copy}</td>";
                    echo "</tr>";
                    echo "<tr>";
                    $show_sm .= "<li class='list-group-item'>";
                    $show_sm .= "<input type='hidden' name='board_table[{$i}]' value='{$row['bo_table']}'><input type='checkbox' name='chk[]' value='{$i}'>";
                    $show_sm .= "<a href='{$g4['g4m_bbs_path']}/board.php?bo_table={$row['bo_table']}'> {$row['bo_table']} </a>";
                    $show_sm .= "<div class='form-group'><input type='text' class='form-control' name='bo_subject[{$i}]' value='" . get_text($row['bo_subject']) . "'></div>";
                    if($is_admin = 'super'){
                        $show_sm .= "<div class='form-group'>그룹:". get_group_select("gr_id[$i]", $row['gr_id']) ."</div>";
                    }else{
                        $show_sm .= "<input type='hidden' name='gr_id[$i]' value='{$row['gr_id']}'>{$row['gr_subject']}";
                    }
                    
                    $show_sm .= "<div class='form-group'>스킨<select id='bo_skin_{$i}' name='bo_skin[{$i}]' class='form-control'>{$skin_options}</select></div>";
                    $show_sm .= "<div class='form-group'>읽기<input type='text' class='form-control' name='bo_read_point[{$i}]' value='{$row['bo_read_point']}'></div>";
                    $show_sm .= "<div class='form-group'>쓰기<input type='text' class='form-control' name='bo_write_point[{$i}]' value='{$row['bo_write_point']}'></div>";
                    $show_sm .= "<div class='form-group'>코멘트<input type='text' class='form-control' name='bo_comment_point[$i]' value='{$row['bo_comment_point']}'></div>";
                    $show_sm .= "<div class='form-group'>다운로드<input type='text' class='form-control' name='bo_download_point[$i]' value='{$row['bo_download_point']}'></div>";
                    $show_sm .= "<div class='form-group'>검색사용<input type='checkbox' name='bo_use_search[{$i}]' " . ($row['bo_use_search'] ? 'checked' : '') . " value='1'></div>";
                    
                    
                    $show_sm .= "{$s_upd} {$s_del} {$s_copy}";
                    $show_sm .= "";
                    $show_sm .= "";
                    $show_sm .= "";
                    $show_sm .= "";
                    $show_sm .= "";
                    $show_sm .= "</li>";
                    

                    if ($is_admin == "super")
                        echo "<td align=left>" . get_group_select("gr_id[$i]", $row['gr_id']) . "</td>";
                    else
                        echo "<td align=center><input type='hidden' name='gr_id[$i]' value='{$row['gr_id']}'>{$row['gr_subject']}</td>";

                    echo "<td align=left><select id='bo_skin_{$i}' name=bo_skin[{$i}] class='form-control'>{$skin_options}</select></td>";
                    echo "</tr>\n";
                    echo "<script type='text/javascript'>document.getElementById('bo_skin_{$i}').value='{$row['bo_skin']}';</script>";
                }

                if ($i == 0)
                    echo "<tr><td colspan='{$colspan}' align=center height=100 bgcolor=#ffffff>자료가 없습니다.</td></tr>";
                ?>
            </table>
            <?php
            $show_sm .= "</ul>";
            $show_sm .= '</div>';
            echo $show_sm;
            $pagelist = get_paging($config['cf_m_write_pages'], $page, $total_page, "{$_SERVER['PHP_SELF']}?{$qstr}&page=");
            ?>
            <div class='form-action col-sm-12'>
                <input type='button' class='btn btn-primary' value='선택수정' onclick="btn_check(this.form, 'update')">
                <?php
                if ($is_admin == "super")
                    echo "<input type='button' class='pull-right btn btn-danger' value='선택삭제' onclick=\"btn_check(this.form, 'delete')\">";
                ?>
            </div>
            
            <?php 
            if($pagelist){
                echo "<div class='page'>" . $pagelist . "</div>";
            }
            ?>
            <?php
            if ($stx)
                echo "<script>document.fsearch.sfl.value = '{$sfl}';</script>";
            ?>
        </form>

        <script type="text/javascript">
            function board_copy(bo_table) {
                window.open("./board_copy.php?bo_table=" + bo_table, "BoardCopy", "left=10,top=10,width=500,height=200");
            }
        </script>

        <script type='text/javascript'>
            // POST 방식으로 삭제
            function post_delete(action_url, val) {
                var f = document.fpost;

                if (confirm("한번 삭제한 자료는 복구할 방법이 없습니다.\n\n정말 삭제하시겠습니까?")) {
                    f.bo_table.value = val;
                    f.action = action_url;
                    f.submit();
                }
            }
        </script>

        <form name='fpost' method='post'>
            <input type='hidden' name='sst'   value='<?php echo $sst ?>'>
            <input type='hidden' name='sod'   value='<?php echo $sod ?>'>
            <input type='hidden' name='sfl'   value='<?php echo $sfl ?>'>
            <input type='hidden' name='stx'   value='<?php echo $stx ?>'>
            <input type='hidden' name='page'  value='<?php echo $page ?>'>
            <input type='hidden' name='token' value='<?php echo $token ?>'>
            <input type='hidden' name='bo_table'>
        </form>
    </div>
</div>
<?
include_once("./admin.tail.php");
?>
