<?php


Class FilterLast {


var $args;
var $args_default;
var $filters;
var $types;
var $labels;
var $_key;
var $_storage;// enum('session', 'cookie')


/**
 * @param $args - args table: $_GET, $_POST, $_REQUEST, etc
 * @param $key - kay to store values in @storage, if not set then args not saved
 * @param $storage - where to store state - cookie or session, default is cookie
 */
function __construct($args, $key = null, $storage = 'cookie') {
	$this->args = $args;
	$this->values = array();
	$this->filters = array();
	$this->labels = array();
	$this->types = array();
	$this->_key = 'filter-last-'.$key;
	$this->_storage = $storage;
	$this->_init_args();
	$this->_values_limit = 10;
}


function get_values($name) {
	if (array_key_exists($name, $this->values)) {
		return $this->values[$name];
	}
	return array();
}


function get_arg($name) {
	// try load from @storage and save
	if (array_key_exists($name, $this->args)) {
		return $this->args[$name];
	}
	return null;
}


function _init_args() {
	$this->_read_args();
}


function add_filter($name, $arg_names, $label = '', $type = 'string') {
	$this->filters[$name] = $arg_names;
	$this->labels[$name] = ($label)? $label : $name;
	$this->types[$name] = $type;
}


function _save_args() {
	if (!$this->_key) { return; }
	if ($this->_storage == 'cookie') {
		$this->_save_args_in_cookie();
	} else {
		$this->_save_args_in_session();
	}
}


function _save_args_in_session() {
	if (!$this->_key) { return; }

	$save_args = array();
	foreach ($this->filters as $name => $arg_names) {
		$type = $this->types[$name];
		if ($type != 'int') {// type: 'int' or 'string'
			$type = 'string';
		}
		foreach ($arg_names as $arg_name) {
			if (($arg_val = V::get($arg_name, '', $this->args, $type)) != '') {
				if ($type == 'int') {
					if ($arg_val < 0) {
						continue;
					}
				}
				$cur_values = V::get($name, array(), $this->values, 'array');
				if (!in_array($arg_val, $cur_values)) {
					$cur_values[] = $arg_val;
					if (count($cur_values) > $this->_values_limit) {
						array_shift($cur_values);// - Shift an element off the beginning of array
					}
					$this->values[$name] = $cur_values;
				}
			}
		}
	}

	foreach ($this->values as $name => $v_arr) {
		if (!empty($v_arr)) {
			$save_args[] = ''.urlencode($name).'='.urlencode(implode(';', $v_arr));
		}
	}
	$save_args = implode(',', $save_args);
	$_SESSION[$this->_key] = $save_args;
}


function _save_args_in_cookie() {
	return;// TODO: ...
	if (!$this->_key) { return; }

	$save_args = array();
	foreach ($this->filters as $name => $options) {
		$arg = (isset($this->args[$name]))? $this->args[$name] : '';
		if (count($options) == 1 && reset($options) == 'search') {
			if ($arg != $this->args_default[$name]) {
				$option = $arg;
				$save_args[] = ''.urlencode($name).'='.urlencode($option);
			}
		} else {
			foreach($options as $option => $field_name) {
				if (isset($this->args[$name]) && $this->args[$name] == $option) {
					$save_args[] = ''.urlencode($name).'='.urlencode($option);
				}
			}
		}
	}
//	if ($save_args) {
		$save_args = implode(',', $save_args);
		echo'<script type="text/javascript">';echo"
			(function(){
				var exdate=new Date()
					, exdays = 14
					, c_name='".$this->_key."'
					, value='".$save_args."'
				;
				exdate.setDate(exdate.getDate() + exdays);
				var c_value=escape(value) + ((exdays==null) ? '' : '; expires='+exdate.toUTCString());
				document.cookie=c_name + '=' + c_value;
			})();
		";echo'</script>';
//	}
}


function _read_args($force = false) {
	if (!$this->_key) { return; }
	if ($this->_storage == 'cookie') {
		$this->_read_args_from_cookie($force);
	} else {
		$this->_read_args_from_session($force);
	}
}


function _read_args_from_session($force = false) {
	if (!$this->_key) { return; }
	if (!array_key_exists($this->_key, $_SESSION)) { return; }
	$c_args = explode(',', $_SESSION[$this->_key]);
	foreach ($c_args as $c_val) {
		$c_val = explode('=', $c_val);
		if (count($c_val) != 2) continue;
		$name = urldecode($c_val[0]);
		$values = urldecode($c_val[1]);
		if ($force || !array_key_exists($name, $this->values)) {
			$this->values[$name] = explode(';', $values);
		}
	}
}


/**
 * Read args from cookie.
 */
function _read_args_from_cookie($force = false) {
	return;// TODO: ...
	if (!$this->_key) { return; }
	if (!array_key_exists($this->_key, $_COOKIE)) { return; }
	$c_args = explode(',', $_COOKIE[$this->_key]);
	foreach ($c_args as $c_val) {
		$c_val = explode('=', $c_val);
		if (count($c_val) != 2) continue;
		$name = urldecode($c_val[0]);
		$option = urldecode($c_val[1]);
		if ($force || !array_key_exists($name, $this->args)) {
			$this->args[$name] = $option;
		}
	}
}


/**
 * Print form fields. Must be inside <form> tag.
 */
function show_filters() {
	// show filters
	foreach ($this->filters as $name => $v_values) {
		$out = new stdClass();
		$out->cls = '';// class
		$out->cnt = '';// content

		$vals = V::get($name, array(), $this->values, 'array');
		if (empty($vals)) {
			continue;
		}
		$out->cnt_arr = array();
		foreach ($vals as $val) {
			$js = "return scrollToProces('".$val."');";
			$out->cnt_arr[] = '<a href="'."#TREE".$val.'" onclick="'.$js.'">'.$val.'</a>';// TODO: read from add_arg
		}
		$out->cnt = implode(', ', $out->cnt_arr);

		echo' <nobr'.(($out->cls)? ' class="'.$out->cls.'"' : '').'>';
			echo $this->labels[$name];
			echo ": ";
			echo $out->cnt;
		echo'</nobr>';
		echo $this->separator();
		unset($out);
	}
}


function separator() {
	return '';//'<div class="btnseparator"></div>';// separator
}


}// class
