<?
/**
 * Bechu-Basic Skin for Gnuboard4
 *
 * Copyright (c) 2008 Choi Jae-Young <www.miwit.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

include_once("_common.php");
include_once("$board_skin_path/mw.lib/mw.skin.basic.lib.php");
include_once("$g4[path]/head.sub.php");

function week_begin($d) {
    $t = strtotime("$d 00:00:00");
    $w = date("w", $t);
    $n = $w - 1;
    return date("Y-m-d", strtotime("-$n day", $t));
}

function week_end($d) {
    $t = strtotime("$d 00:00:00");
    $w = date("w", $t);
    $n = 7 - $w;
    return date("Y-m-d", strtotime("$n day", $t));
}

if ($is_admin != "super")
    alert("접근 권한이 없습니다.");

if (!$sdate)
    $sdate = date("Y-m-01", $g4[server_time]);

if (!$edate)
    $edate = date("Y-m-t", $g4[server_time]);

if (!$limit)
    $limit = 10;

$colspan = 3;
?>
<script type="text/javascript" src="<?=$g4[path]?>/js/sideview.js"></script>

<style type="text/css">
input.ed { height:20px; border:1px solid #9A9A9A; border-right:1px solid #D8D8D8; border-bottom:1px solid #D8D8D8; padding:0 0 0 3px; }
textarea { border:1px solid #9A9A9A; border-right:1px solid #D8D8D8; border-bottom:1px solid #D8D8D8; padding:0 0 0 3px; }
input.bt { background-color:#efefef; height:20px; cursor:pointer; font-size:11px; font-family:dotum; }
</style>

<script type="text/javascript">
function change_date(sd, ed) {
    fsearch.sdate.value = sd;
    fsearch.edate.value = ed;
    fsearch.submit();
}
</script>

<div style="padding:10px; background-color:#fff;">
<form name="fsearch" method=post action="mw.stats.php">
<input type="hidden" name="bo_table" value="<?=$bo_table?>"/>
<div style="height:30px;">
    기간 : <input type="text" class="ed" size="10" name="sdate" id="sdate" value="<?=$sdate?>" required itemname="기간">
    <a href="javascript:win_calendar('sdate', $('#sdate').val(), '');"><!--
    --><img src='<?=$board_skin_path?>/img/calendar.gif' border=0 align=absmiddle title='달력 - 날짜를 선택하세요'></a>
    ~
    <input type="text" class="ed" size="10" name="edate" id="edate" value="<?=$edate?>" required itemname="기간">
    <a href="javascript:win_calendar('edate', $('#edate').val(), '');"><!--
    --><img src='<?=$board_skin_path?>/img/calendar.gif' border=0 align=absmiddle title='달력 - 날짜를 선택하세요'></a>

    <input type="button" class="bt" value="지난달"
        onclick="change_date('<?=date("Y-m-01", strtotime("-1 month", strtotime("$sdate 00:00:00")))?>',
        '<?=date("Y-m-t", strtotime("-1 month", strtotime("$sdate 00:00:00")))?>')">
    <input type="button" class="bt" value="이번달"
        onclick="change_date('<?=date("Y-m-01", $g4[server_time])?>','<?=date("Y-m-t", $g4[server_time])?>')">
    <input type="button" class="bt" value="다음달"
        onclick="change_date('<?=date("Y-m-01", strtotime("+1 month", strtotime("$sdate 00:00:00")))?>',
        '<?=date("Y-m-t", strtotime("+1 month", strtotime("$sdate 00:00:00")))?>')">
    &nbsp;

    <? $d = date("Y-m-d", strtotime("$sdate 00:00:00")-(86400*7)); ?>
    <input type="button" class="bt" value="지난주"
        onclick="change_date('<?=week_begin($d)?>','<?=week_end($d)?>')">
    <input type="button" class="bt" value="이번주"
        onclick="change_date('<?=week_begin($g4[time_ymd])?>','<?=week_end($g4[time_ymd])?>')">
    <? $d = date("Y-m-d", strtotime("$sdate 00:00:00")+(86400*7)); ?>
    <input type="button" class="bt" value="다음주"
        onclick="change_date('<?=week_begin($d)?>','<?=week_end($d)?>')">
</div>
<div style="height:30px;">
    통계 : 
    <select name="stype" required itemname="통계">
    <option value=""> </option>
    <option value="1"> 최다 글+댓글 작성 </option>
    <option value="2"> 최다 글작성 </option>
    <option value="3"> 최다 댓글작성 (본인글 포함) </option>
    <option value="4"> 최다 댓글작성 (본인글 제외) </option>
    </select>
    <script type="text/javascript"> fsearch.stype.value = "<?=$stype?>"; </script>
    순위 : <input type="text" name="limit" size="5" class="ed" required itemname="순위" value="<?=$limit?>"> 까지
    <input type="submit" class="bt" value="검색">
</div>
</form>
</div>

<table border=0 width=100% align=center cellspacing=1 bgcolor="#dddddd" style="padding:10px;">
<colgroup width=60>
<colgroup width=''>
<colgroup width=200>
<tr align=center height=30 bgcolor="#efefef" style="font-weight:bold;">
    <td> 순위 </td>
    <td> 회원ID </td>
    <td> 횟수 </td>
</tr>
<?
$sql_common = " select count(*) as cnt, mb_id, wr_hit from $write_table as w ";

$sql_where = " where wr_datetime between '$sdate 00:00:00' and '$edate 23:59:59' ";


if ($stype == '1') ; // 최다 글+댓글 작성

if ($stype == '2') // 최다 글작성
    $sql_where.= " and wr_is_comment = '' ";

if ($stype == '3') // 최다 댓글작성 (본인글 포함)
    $sql_where.= " and wr_is_comment = '1' ";

if ($stype == '4') { // 최다 댓글작성 (본인글 제외)
    $sql_where.= " and wr_parent not in (select wr_id from $write_table $sql_where and wr_is_comment = '' and mb_id = w.mb_id)  ";
    $sql_where.= " and wr_is_comment = '1' ";
}

$sql_group = " group by mb_id ";
$sql_order = " order by cnt desc ";

$sql = " $sql_common $sql_where $sql_group $sql_order limit $limit ";

if ($stype)
    $qry = sql_query($sql);

for ($i=0; $row=sql_fetch_array($qry); $i++) {
    $mb = get_member($row[mb_id], "mb_id, mb_nick, mb_homepage, mb_email");
    $name = get_sideview($mb[mb_id], $mb[mb_nick], $mb[mb_homepage], $mb[mb_email]);
?>
<tr align=center height=30 bgcolor="#ffffff">
    <td> <?=($i+1)?> </td>
    <td> <?=$name?> </td>
    <td> <?=$row[cnt]?> </td>
</tr>
<? } ?>

<? if (!$i) { ?>
<tr><td colspan=<?=$colspan?> height=100 align=center bgcolor="#ffffff">자료가 없습니다.</td></tr>
<? } ?>
</table>

<div style="height:20px;"></div>
<?
include_once("$g4[path]/tail.sub.php");
?>
