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

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


// ############################## UPDATE MESSAGES ##############################

function UpdateMessages($start)
{
  global $DB, $refreshpage;

  $messageid       = $_POST['messageid'];
  $noactivated       = $_POST['noactivated'];
  $username        = $_POST['username'];
  $websitename     = $_POST['websitename'];
  $website         = $_POST['website'];
  $message         = $_POST['message'];
  $deletemessageid = $_POST['deletemessageid'];

  for($i = 0; $i < count($message); $i++)
  {
    $DB->query("UPDATE " . TABLE_PREFIX . "m4_guestbook SET username    = '$username[$i]',
                                                            noactivated = '$noactivated[$i]',
                                                            websitename = '$websitename[$i]',
                                                            website     = '$website[$i]',
                                                            message     = '$message[$i]'
                                                        WHERE messageid = '$messageid[$i]'");
  }

  for($i = 0; $i < count($deletemessageid); $i++)
  {
    $DB->query("DELETE FROM " . TABLE_PREFIX . "m4_guestbook WHERE messageid = '" . $deletemessageid[$i] . "'");
  }

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



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

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

  PrintModuleSettings($moduleid, '模塊設置', $refreshpage);

  $getmessages  = $DB->query("SELECT * FROM " . TABLE_PREFIX . "m4_guestbook ORDER BY messageid DESC LIMIT $start, 10");

  PrintSection('內容條目');

  echo '<form method="post" action="'.$refreshpage.'&start='.$start.'" name="deletemessages">
        <input type="hidden" name="action" value="updatemessages" />

        <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">單位</td>
          <td class="tdrow1">網址</td>
          <td class="tdrow1">內容</td>
          <td class="tdrow1"><input type="checkbox" checkall="group" onclick="javascript: return select_deselectAll (\'deletemessages\', this, \'group\');">刪除</td>
        </tr>';

  while($message = $DB->fetch_array($getmessages))
  {

    $messagedate = DisplayDate($message['datecreated']);
    echo '<tr>
            <td class="tdrow2">'.iif($message['noactivated'],"<span style=\"color:red\"><b>".$messagedate."</b></span>","<span style=\"color:green\">".$messagedate."</span>").'</td>			
            </td>
              <td class="tdrow3">
                <select name="noactivated[]">
                  <option value="0">發佈</option>
                  <option value="1" ' . iif($message['noactivated'], 'SELECTED') . '>隱藏</option>
                </select>
              </td>
            <td class="tdrow2">
              <input type="hidden" name="messageid[]" value="'.$message['messageid'].'" />
              <input type="text" name="username[]" value="'.CleanFormValue($message['username']).'" size="6" />
            </td>
            <td class="tdrow3">
              <input type="text" name="websitename[]" value="'.CleanFormValue($message['websitename']).'" size="10" />
            </td>
            <td class="tdrow2">
              <input type="text" name="website[]" value="'.CleanFormValue($message['website']).'" size="18"  />
            </td>
            <td class="tdrow3">
              <textarea name="message[]" cols="32" rows="6">'.$message['message'].'</textarea>
            </td>
            <td class="tdrow2">
              <input type="checkbox" name="deletemessageid[]" value="' . $message['messageid'] . '" checkme="group" />
            </td>
          </tr>';
  }

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

		$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 'updatemessages':
    UpdateMessages($start);
  break;

  default:
    DisplayDefault($start);
}


?>
