<?php
// +---------------------------------------------+
// |     Copyright 2003-2005 weenCompany |
// |     http://www.weentech.com               |
// |     This file may not be redistributed.     |
// +---------------------------------------------+

if(!defined('IN_WEENCOMPANY'))
{
  die("File not found!");
}


// ############################## UPDATE COMMENTS ##############################

function UpdateComments($start)
{
  global $DB;
  global $refreshpage;

  // post vars
  $commentid       = $_POST['commentid'];
  $username        = $_POST['username'];
  $comment         = $_POST['comment'];
  $deletecommentid = $_POST['deletecommentid'];

  // first update comments
  for($i = 0; $i < count($comment); $i++)
  {
    $DB->query("UPDATE " . TABLE_PREFIX . "m7_chatterbox SET comment   = '$comment[$i]',
                                                             username  = '$username[$i]'
                                                       WHERE commentid = '$commentid[$i]'");
  }

  // now delete comments (if user selected to delete any)
  for($i = 0; $i < count($deletecommentid); $i++)
  {
    $DB->query("DELETE FROM " . TABLE_PREFIX . "m7_chatterbox WHERE commentid = '" . $deletecommentid[$i] . "'");
  }

  PrintRedirect($refreshpage.'&start='.$start, 1);
}

// ############################## DISPLAY DEFAULT ##############################

function DisplayDefault($start)
{
  global $DB;
  global $moduleid;
  global $refreshpage;

  PrintModuleSettings($moduleid, '模块设置', $refreshpage);

  $getchatterbox  = $DB->query("SELECT * FROM " . TABLE_PREFIX . "m7_chatterbox ORDER BY commentid DESC LIMIT $start, 10");
  $chatterboxrows = $DB->get_num_rows($getchatterbox);

  PrintSection("内容条目");

  echo '<form method="post" action="" name="deletemessages">
        <input type="hidden" name="action" value="updatecomments" />

        <table width="100%" border="0" cellpadding="5" cellspacing="0">
        <tr>
		  <td class="tdrow1">时间</td>
          <td class="tdrow1">用户名</td>
          <td class="tdrow1">内容</td>
          <td class="tdrow1" width="75"><input type="checkbox" checkall="group" onclick="javascript: return select_deselectAll (\'deletemessages\', this, \'group\');"> 删除</td>
        </tr>';

  while($chatterbox = $DB->fetch_array($getchatterbox))
  {
    echo '<tr>
	        <td class="tdrow3">'.DisplayDate($chatterbox['datecreated'], 'Y.m.d-H:i:s').'</td>
            <td class="tdrow2"><input type="text" name="username[]" size="15" value="' . CleanFormValue($chatterbox['username']) . '" /></td>
            <td class="tdrow3"><input type="text" name="comment[]" size="50" value="' . CleanFormValue($chatterbox['comment']) . '" />
              <input type="hidden" name="commentid[]" value="'.$chatterbox['commentid'].'" />
            </td>
            <td class="tdrow2"><input type="checkbox" name="deletecommentid[]" value="'.$chatterbox['commentid'].'" checkme="group" /></td>
          </tr>';
  }

  $maxrows = $DB->query_first("SELECT COUNT(*) AS value FROM " . TABLE_PREFIX . "m7_chatterbox");

		$limit = 10;
		$sectionimagesnum = $maxrows['value'];
		$rows = $maxrows['value'] - $start;
		$page_num = ceil($sectionimagesnum / $limit);
		$page_group = floor($start/$limit/8)*8+1;
		$now_page_num = $start/$limit+1;

      if(($start > 0) || ($rows > $limit))
      {

			echo '<tr><td class="tdrow3" colspan="7" align="center" height="48" valign="middle"><table width="100%" cellpadding="0" cellspacing="0" border="0">
              <tr>';

			if($start > 0)
			{
			  echo '<td style="padding-left: 3px; vertical-align:middle;" width="10%" nowrap="nowrap"><a href="'.$refreshpage.'&start='.($start - $limit).'">&lsaquo;&lsaquo; 上一页</a></td>';
			}else{
			  echo '<td style="padding-left: 3px; vertical-align:middle;" width="10%" nowrap="nowrap">&nbsp;</td>';
			}


			if($page_num - $now_page_num < 10){
				$start_page = $page_num - 9;
				if($start_page<1){
					$start_page = 1;
				}
				$end_page = $page_num + 1;
			}else{
				$start_page = $page_group;
				$end_page = $start_page + 8;
			}

			if($start_page > 1){
				$direct_bar = '<a href="'.$refreshpage.'&start=0">1</a><a href="'.$refreshpage.'&start='.($start_page-2)*$limit.'">&lsaquo;&lsaquo;...</a>';
			}

			for ($p=$start_page; $p < $end_page; $p++ ) 
			{	
				if (($start/$limit+1) == $p){
					$direct_bar .= '<STRONG>'.$p.'</STRONG>';
				}else{
					$direct_bar .= '<a href="'.$refreshpage.'&start='.($p-1)*$limit.'">'.$p.'</a>';
				}
				if($p >= $page_num){
					break;
				}
			}

			if($page_num - $now_page_num >= 10){
				$direct_bar .= '<a href="'.$refreshpage.'&start='.($page_group + 7)*$limit.'">...&rsaquo;&rsaquo;</a><a href="'.$refreshpage.'&start='.($page_num-1)*$limit.'">'.$page_num.'</a>';
			}

			echo '<td width="80%" style="text-align:center; vertical-align:middle;" class="pages_nav">'.$direct_bar.'</td>';

			if($rows > $limit)
			{
			  $start += $limit;
			  echo '<td style="padding-right: 3px; vertical-align:middle;" width="10%" nowrap="nowrap"><div align="right"><a href="'.$refreshpage.'&start='.$start.'">下一页 &rsaquo;&rsaquo;</a></div></td>';
			}else{
			  echo '<td style="padding-right: 3px; vertical-align:middle;" width="10%" nowrap="nowrap">&nbsp;</td>';
			}

			echo '</tr></table>';

	   }


        echo '<tr><td class="tdrow1" colspan="7" align="center"><input type="submit" onclick="return confirm(\'确定保存更新, 并删除选中的留言吗?\');" value=" 保存更新 " /></td></tr></table></form>';

        EndSection();

}

// ############################# FUNCTION CALLING ##############################

if(isset($_GET['start']))
  $start = $_GET['start'];
else
  $start = 0;



switch($action)
{
  case 'updatecomments':
    UpdateComments($start);
  break;

  default:
    DisplayDefault($start);
}


?>