<?php


function SQL_WIEV() {
	global $thiss,$SQL_WIEV,$ILE,$SQL_WIEV_CMD,$ARG1,$ARG1_VAL;
	//DETECT_TABLE_COLUMN();

	$ses_p =& $_SESSION;
	if (!empty($thiss->DETECT_TABLE_COLUMN) && !empty($thiss->DETECT_TABLE_NAME)) {
		$ses_p =& $_SESSION[$thiss->DETECT_TABLE_COLUMN];
	}

	// TODO: $SQL_WIEV from $_GET - register globals
	if (!isset($ses_p['ILE'])) $ses_p['ILE'] = 100;
	if (!$ses_p['SQL_WIEV_CURR_SQL']) $SQL_WIEV = 'BEGIN';
	if (!$ses_p['SQL_WIEV_CURR']) $ses_p['SQL_WIEV_CURR'] = 0;

	if ($_SESSION['DEBUG']) echo "<HR>f.SQL_WIEV SQL_WIEV to $SQL_WIEV *";

	// TODO: funkcja SQL_WIEV moze nie byc uruchomiona za pomoca function_init i argumenty $ARG1 ('POSITION','ILE','LIMIT') moze byc uzywane przez inne funkcje!
	if (isset($ARG1_VAL)) {
		switch ($ARG1) {
			case 'POSITION':
				$ses_p['SQL_WIEV_CURR'] = $ARG1_VAL;
				$ses_p['SQL_WIEV_CURR_SQL'] = "limit " . $ses_p['SQL_WIEV_CURR'] . "," . $ses_p['ILE'];
				break;
			case 'ILE':
				$ses_p['ILE'] = $ARG1_VAL;
				// TODO: SQL_WIEV_CURR ?
				$ses_p['SQL_WIEV_CURR_SQL'] = "limit " . $ses_p['SQL_WIEV_CURR'] . "," . $ses_p['ILE'];
				break;
			case 'LIMIT':
				$ses_p['ILE'] = $ses_p['SQL_WIEV_CURR'] + $ses_p['ILE'];
				$ses_p['SQL_WIEV_CURR_SQL'] = "limit " . $ses_p['SQL_WIEV_CURR'] . "," . $ses_p['ILE'];
				break;
		}
	}

	switch ($SQL_WIEV) {
		case 'BEGIN':
			$ses_p['SQL_WIEV_CURR'] = 0;
			$ses_p['SQL_WIEV_CURR_SQL'] = "limit 0," . $ses_p['ILE'];
			break;
		case 'NEXT':
			$ses_p['SQL_WIEV_CURR'] = $ses_p['SQL_WIEV_CURR'] + $ses_p['ILE'];
			$ses_p['SQL_WIEV_CURR_SQL'] = "limit " . $ses_p['SQL_WIEV_CURR'] . "," . $ses_p['ILE'];
			break;
		case 'LAST':
			$ses_p['SQL_WIEV_CURR'] = $ses_p['MYSQL_NUM_ROWS_TOTAL'] - $ses_p['ILE'];
			//$_SESSION['SQL_WIEV_CURR'] = $_SESSION['MYSQL_NUM_ROWS_TOTAL']['USERS'] - $_SESSION['ILE'];
			$ses_p['SQL_WIEV_CURR_SQL'] = "limit " . $ses_p['SQL_WIEV_CURR'] . "," . $ses_p['ILE'];
			break;
		case 'PREV':
			$ses_p['SQL_WIEV_CURR'] = $ses_p['SQL_WIEV_CURR'] - $ses_p['ILE'];
			$ses_p['SQL_WIEV_CURR_SQL'] = "limit " . $ses_p['SQL_WIEV_CURR'] . "," . $ses_p['ILE'];
			if ($ses_p['SQL_WIEV_CURR'] < 0) {
				$SQL_WIEV = 'BEGIN';
				SQL_WIEV();
			}
			break;
		//default:
	}

	if ($_SESSION['DEBUG']) echo "<HR>LIMIT $_SESSION[SQL_WIEV_CURR_SQL] | ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL']."(for $thiss->DETECT_TABLE_COLUMN)*";
}


function SQL_WIEV_bup_20120816() {
	global $thiss,$SQL_WIEV,$ILE,$SQL_WIEV_CMD,$ARG1,$ARG1_VAL;
echo '<p style="color:red">{F.'. __FUNCTION__ .'(): ...}</p>';
	//DETECT_TABLE_COLUMN();
	$ses_p =& $_SESSION;
	if (!empty($thiss->DETECT_TABLE_COLUMN) && !empty($thiss->DETECT_TABLE_NAME)) {
		$ses_p =& $_SESSION[$thiss->DETECT_TABLE_COLUMN];
	}

echo '<p style="color:red">{F.'. __FUNCTION__ .':before: ILE='.$ses_p['ILE'].', SQL_WIEV_CURR='.$ses_p['SQL_WIEV_CURR'].', SQL_WIEV_CURR_SQL='.$ses_p['SQL_WIEV_CURR_SQL'].' }</p>';
	if (($thiss->DETECT_TABLE_COLUMN) && ($thiss->DETECT_TABLE_NAME)) {

		// defaults
		if (!isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'])) {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'] = 100;
		}
		if (!$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL']) {
			$SQL_WIEV = 'BEGIN';
		}
		if (!$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR']) {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] = 0;
		}

		if ($_SESSION['DEBUG']) echo "<HR>f.SQL_WIEV SQL_WIEV to $SQL_WIEV *";

		if ($SQL_WIEV == 'BEGIN') {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'] = "limit 0," . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] = 0;
		}
		if ($SQL_WIEV == 'NEXT') {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] = $_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] + $_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'] = "limit ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'].",".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
		//echo "<hr>ILE TO [ILE]". $_SESSION[$thiss->DETECT_TABLE_COLUMN][ILE]."<hr>";
		}
		if ($SQL_WIEV == 'LAST') {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] = $_SESSION[$thiss->DETECT_TABLE_COLUMN]['MYSQL_NUM_ROWS_TOTAL'] - $_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'] = "limit ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'].",".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
		}

		if (($ARG1 == 'POSITION') && (isset($ARG1_VAL))) {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] = $ARG1_VAL;
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'] = "limit ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'].",".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
		}
		if (($ARG1 == 'ILE') && (isset($ARG1_VAL))) {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'] = $ARG1_VAL ;
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'] = "limit ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'].",".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
		}
		if (($ARG1 == 'LIMIT') && (isset($ARG1_VAL))) {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] = $_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] + $_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'] = "limit ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'].",".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
		}

		if ($SQL_WIEV == 'PREV') {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] = $_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] - $_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'] = "limit ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'].",".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['ILE'];
			if ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR'] < 0) {
				$SQL_WIEV = 'BEGIN';
				SQL_WIEV();
			}
		}
	}

	// defaults
	if (!isset($_SESSION['ILE'])) $_SESSION['ILE'] = 100;
	if (!$_SESSION['SQL_WIEV_CURR_SQL']) $SQL_WIEV = 'BEGIN';
	if (!$_SESSION['SQL_WIEV_CURR']) $_SESSION['SQL_WIEV_CURR'] = 0;

	if ($_SESSION['DEBUG']) echo "<HR>HERE SQL_WIEV to $SQL_WIEV *";

	if ($SQL_WIEV == 'BEGIN') {
		$_SESSION['SQL_WIEV_CURR_SQL'] = "limit 0," . $_SESSION['ILE'];
		$_SESSION['SQL_WIEV_CURR'] = 0;
	}
	if ($SQL_WIEV == 'NEXT') {
		$_SESSION['SQL_WIEV_CURR'] = $_SESSION['SQL_WIEV_CURR'] + $_SESSION['ILE'];
		$_SESSION['SQL_WIEV_CURR_SQL'] = "limit $_SESSION[SQL_WIEV_CURR],$_SESSION[ILE]";
	}
	if ($SQL_WIEV == 'LAST') {
		$_SESSION['SQL_WIEV_CURR'] = $_SESSION['MYSQL_NUM_ROWS_TOTAL']['USERS'] - $_SESSION['ILE'];
		$_SESSION['SQL_WIEV_CURR_SQL'] = "limit $_SESSION[SQL_WIEV_CURR],$_SESSION[ILE]";
	}

	if (($ARG1 == 'POSITION') && (isset($ARG1_VAL))) {
		$_SESSION['SQL_WIEV_CURR'] = $ARG1_VAL;
		$_SESSION['SQL_WIEV_CURR_SQL'] = "limit $_SESSION[SQL_WIEV_CURR],$_SESSION[ILE]";
	}
	if (($ARG1 == 'ILE') && (isset($ARG1_VAL))) {
		$_SESSION['ILE'] = $ARG1_VAL;
		$_SESSION['SQL_WIEV_CURR_SQL'] = "limit $_SESSION[SQL_WIEV_CURR],$_SESSION[ILE]";
	}
	if (($ARG1 == 'LIMIT') && (isset($ARG1_VAL))) {
		$_SESSION['SQL_WIEV_CURR'] = $_SESSION['SQL_WIEV_CURR'] + $_SESSION['ILE'];
		$_SESSION['SQL_WIEV_CURR_SQL'] = "limit $_SESSION[SQL_WIEV_CURR],$_SESSION[ILE]";
	}

	if ($SQL_WIEV == 'PREV') {
		$_SESSION['SQL_WIEV_CURR'] = $_SESSION['SQL_WIEV_CURR'] - $_SESSION['ILE'];
		$_SESSION['SQL_WIEV_CURR_SQL'] = "limit $_SESSION[SQL_WIEV_CURR],$_SESSION[ILE]";
		if ($_SESSION['SQL_WIEV_CURR'] < 0) {
			$SQL_WIEV = 'BEGIN';
			SQL_WIEV();
		}
	}
echo '<p style="color:red">{F.'. __FUNCTION__ .':end: ILE='.$ses_p['ILE'].', SQL_WIEV_CURR='.$ses_p['SQL_WIEV_CURR'].', SQL_WIEV_CURR_SQL='.$ses_p['SQL_WIEV_CURR_SQL'].' }</p>';

	if ($_SESSION['DEBUG']) echo "<HR>LIMIT $_SESSION[SQL_WIEV_CURR_SQL] | ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL']."(for $thiss->DETECT_TABLE_COLUMN)*";
}
