<?php
/*
###################################################################################
Copyright (c) 2012 Bigware LTD
##################################################################################
*/

require('load_this/load_bigware_11.php');

($row_by_page) ? define('MAX_SHOW_ROW_BY_PAGE', $row_by_page) : $row_by_page = MAX_SHOW_SEARCH_RESULTS;

define('MAX_SHOW_ROW_BY_PAGE', MAX_SHOW_SEARCH_RESULTS);

$shipping_range_query = go_db_query("select id, description from shipping_range_groups where id > 0 order by description");
while ($shipping_range = go_db_fetch_array($shipping_range_query)) {
	$shipping_range_array[] = array('id' => $shipping_range['id'],
			'text' => $shipping_range['description']);
}

$group_id = (int)$_GET['groupId'];
$group_id = $group_id < 1 ? 1 : $group_id;

switch ($_GET['action']) {
	case 'update':
		$count_update = 0;
		$item_updated = array();
		if($_POST['range_quantity']){
			foreach($_POST['range_quantity'] as $id => $new_quantity) {
				if (trim($_POST['range_quantity'][$id]) != trim($_POST['old_quantity'][$id])) {
					$id = (int)$id;
					$count_update++;
					$item_updated[$id] = 'updated';
					$new_quantity = (int)$new_quantity;
					$update_query = "UPDATE shipping_range SET quantity = '$new_quantity' WHERE id = '$id'";
					$result = go_db_query($update_query);
				}
			}
		}
		if($_POST['range_delivery_price']){
			foreach($_POST['range_delivery_price'] as $id => $new_delivery_price) {
				if (trim($_POST['range_delivery_price'][$id]) != trim($_POST['old_delivery_price'][$id])) {
					$id = (int)$id;
					$count_update++;
					$item_updated[$id] = 'updated';
					$new_delivery_price = str_replace(',','.',$new_delivery_price) + 0;
					$update_query = "UPDATE shipping_range SET delivery_price = '$new_delivery_price' WHERE id = '$id'";
					$result = go_db_query($update_query);
				}
			}
		}
		if($_POST['new_range_quantity'] && $_POST['new_range_delivery_price']){
			$quantity = (int)$_POST['new_range_quantity'];
			$delivery_price = str_replace(',','.',$_POST['new_range_delivery_price']) + 0;
			if ($quantity > 0 && $delivery_price >= 0) {
				$insert_query = "INSERT INTO shipping_range (group_id, quantity, delivery_price) VALUES ('$group_id','$quantity','$delivery_price')";
				$result = go_db_query($insert_query);
			}
		}
		break;
	case 'delete':
		$id = (int)$_GET['id'];
		$delete_query = "DELETE FROM shipping_range WHERE id = '$id'";
		$result = go_db_query($delete_query);
		break;
}

if ($info_back && $info_back != "-") {
	$infoback    = explode('-', $info_back);
	$sort_by     = $infoback[0];
	$page        = $infoback[1];
	$group_id    = $infoback[2];
	$row_by_page = $infoback[3];
}

$row_bypage_array = array(
	array()
);

for ($i = 10; $i <= 300; $i = $i + 10) {
	$row_bypage_array[] = array(
		'id' => $i,
		'text' => $i
	);
}

?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
  <title><?php echo TITLE; ?></title>
  <script language="javascript" src="load_this/menu.js"></script>
  <script language="javascript" src="load_this/general.js"></script>
  <link rel="stylesheet" type="text/css" href="load_this/style.css">
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<?php require(FOLDER_RELATIV_INCLUDES . 'load_bigware_9.php'); ?>
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo FRAME_WIDTH; ?>" valign="top"></td>
<td width="100%" valign="top">
  <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td>
         <table border="0" width="100%" cellspacing="0" cellpadding="0">
            <tr><td class="pageGeneralsign" colspan="3" valign="top"><?php echo ABOVE_TITLE; ?></td></tr>
         </table>
        </td>
      </tr>
      <tr>
        <td align="center">
          <table width="90%" cellspacing="0" cellpadding="0" border="1" bgcolor="#F3F9FB" bordercolor="#D1E7EF" height="100">
            <tr align="center">
              <td valign="middle">
                <table width="100%" cellspacing="0" cellpadding="0" border="0">
                  <tr>
                    <td height="5"></td>
                  </tr>
                  <tr align="center">
                    <td class="smalltext">
                      <?php
                        echo go_fetch_form('row_by_page', 'shipping_range_editor_values.php', '', 'get');
                        echo go_fetch_hidden_field('groupId', $group_id);
                      ?>
                    </td>
                    <td class="smallText">
                      <?php
                        echo TEXT_MAXI_ROW_BY_PAGE . '&nbsp;&nbsp;' . go_fetch_pull_down_menu('row_by_page', $row_bypage_array, $row_by_page, 'onChange="this.form.submit();"');
                      ?>
                      </form>
                    </td>
                      <?php
                        echo go_fetch_form('categorie', 'shipping_range_editor_values.php', '', 'get');
                        echo go_fetch_hidden_field('row_by_page', $row_by_page);
                      ?>
                    <td class="smallText" align="center" valign="top">
                      <?php
                        echo SHOW_SHIPPING_RANGE_GROUPS . '&nbsp;&nbsp;' . go_fetch_pull_down_menu('groupId', $shipping_range_array, $group_id, 'onChange="this.form.submit();"');
                      ?>
                    </td>
                    </form>
                  </tr>
                </table>
              </td>
            </tr>
          </table>
          <br>
          <table width="100%" cellspacing="0" cellpadding="0" border="0">
            <tr align="center">
              <form name="update" method="POST" action="<?php echo "$PHP_SELF?action=update&page=$page&sort_by=$sort_by&groupId=$group_id&row_by_page=$row_by_page"; ?>">
              <td align="right" valign="middle">
              </td>
            </tr>
          </table>
        </td>
      </tr>
      <tr>
        <td>
          <table border="1" width="100%" cellspacing="0" cellpadding="0" style="border-collapse: collapse" id="AutoNumber1">
            <tr>
              <td valign="top">
                <table border="1" width="100%" cellspacing="0" cellpadding="2" style="border-collapse: collapse">
                  <tr class="tbUPCountData">
                    <td class="tbUPDataInside" align="left" valign="middle">
                      <table border="0" cellspacing="0" cellpadding="0">
                        <tr class="tbUPCountData">
                          <td class="tbUPDataInside" align="right" valign="middle">
                            <?php echo "<td class=\"tbUPDataInside\" align=\"center\" valign=\"middle\">" . DB_TBL_ABOVE_QUANTITY . "</td>"; ?>
                          </td>
                        </tr>
                      </table>
                    </td>
                    <td class="tbUPDataInside" align="left" valign="middle">
                      <table border="0" cellspacing="0" cellpadding="0">
                        <tr class="tbUPCountData">
                          <td class="tbUPDataInside" align="right" valign="middle">
                            <?php echo "<td class=\"tbUPDataInside\" align=\"center\" valign=\"middle\">" . DB_TBL_ABOVE_PRICE . "</td>"; ?>
                          </td>
                        </tr>
                      </table>
                    </td>
		  </td>
                  <td class="tbUPDataInside" align="center" valign="middle"></td>
                </tr>
              <tr class="datatableRow">
<?php

if ($sort_by && !preg_match('/order by/', $sort_by))
	$sort_by = 'order by ' . $sort_by;

$origin     = 'shipping_range_editor_values.php' . "?info_back=$sort_by-$page-$group_id-$row_by_page";
$split_page = $page;
if ($split_page > 1) $rows = $split_page * MAX_SHOW_ROW_BY_PAGE - MAX_SHOW_ROW_BY_PAGE;

$ranges_query_raw = "select id, quantity, delivery_price from shipping_range where group_id = '" . $group_id . "' ORDER BY quantity";
$ranges_split = new splitPageResults($split_page, MAX_SHOW_ROW_BY_PAGE, $ranges_query_raw, $ranges_query_numrows);
$ranges_query = go_db_query($ranges_query_raw);
while ($ranges = go_db_fetch_array($ranges_query)) {
	$rows++;
	if (strlen($rows) < 2) {
		$rows = '0' . $rows;
	}
	echo '<tr class="tbCountData" onmouseover="this.className=\'tbCountDataOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'tbCountData\'">';
	echo "  <td class=\"smallText\" align=\"center\">" . go_fetch_inputfeld('range_quantity[' . $ranges['id'] . ']', $ranges['quantity']) . "</td>\n";
	echo "  <td class=\"smallText\" align=\"center\">" . go_fetch_inputfeld('range_delivery_price[' . $ranges['id'] . ']', $ranges['delivery_price']) . "</td>\n";
	echo "  <td class=\"smallText\" align=\"center\"><a href=\"" . go_href_link('shipping_range_editor_values.php', 'id=' . $ranges['id'] . '&action=delete&read=only&sort_by=' . $sort_by . '&page=' . $split_page . '&origin=' . $origin) . "\">" . go_picture(FOLDER_RELATIV_ICONS . 'delete.gif', PICTURE_DELETE) . "</a></td>\n";
	echo go_fetch_hidden_field('old_quantity[' . $ranges['id'] . '] ', $ranges['quantity']);
	echo go_fetch_hidden_field('old_delivery_price[' . $ranges['id'] . '] ', $ranges['delivery_price']);
	echo '</tr>';
}

echo '<tr class="tbCountData" onmouseover="this.className=\'tbCountDataOver\';this.style.cursor=\'hand\'" onmouseout="this.className=\'tbCountData\'">';
echo "  <td class=\"smallText\" align=\"center\">" . go_fetch_inputfeld('new_range_quantity', '') . "</td>\n";
echo "  <td class=\"smallText\" align=\"center\">" . go_fetch_inputfeld('new_range_delivery_price', '') . "</td>\n";
echo "  <td class=\"smallText\" align=\"center\">" . go_picture_submit('add.gif', PICTURE_ADD) . "</td>\n";
echo go_fetch_hidden_field('row_by_page', $row_by_page);
echo go_fetch_hidden_field('sort_by', $sort_by);
echo go_fetch_hidden_field('page', $split_page);
echo '</tr>';

echo "</table>\n";
?>
          </td>
        </tr>
       </table></td>
      </tr>
<tr>
<td align="right">
<?php
echo go_picture_submit('button_update.gif', PICTURE_UPDATE);
echo '&nbsp;&nbsp;<a href="' . go_href_link('shipping_range_editor_values.php', "row_by_page=$row_by_page") . '">' . go_picture_button('button_cancel.gif', PICTURE_CANCEL) . '</a>';
?>
</form>
</td>
</tr>
<tr>
<td>
  <table border="0" width="100%" cellspacing="0" cellpadding="2">
    <td class="smallText" valign="top">
      <?php echo $ranges_split->display_count($ranges_query_numrows, MAX_SHOW_ROW_BY_PAGE, $split_page, TEXT_SHOW_NUMBER_OF_ITEMS); ?>
    </td>
    <td class="smallText" align="right">
      <?php echo $ranges_split->display_links($ranges_query_numrows, MAX_SHOW_ROW_BY_PAGE, MAX_SHOW_PAGE_LINKS, $split_page, '&groupId=' . $group_id . '&sort_by=' . $sort_by . '&row_by_page=' . $row_by_page); ?>
    </td>
  </table>
</td>
</tr>
</table></td>
</tr>
</table>
<?php require(FOLDER_RELATIV_INCLUDES . 'load_bigware_7.php'); ?>
</body>
</html>
<?php require(FOLDER_RELATIV_INCLUDES . 'load_bigware_6.php'); ?>
