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; } } }//end foreach } foreach ($this->values as $name => $v_arr) { if (!empty($v_arr)) { $save_args []= ''.urlencode($name).'='.urlencode(implode(';', $v_arr)); } }//end foreach $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''; // } } 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); } }//end foreach } /** * 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; } }//end foreach } /** * Print form fields. Must be inside