args = $args; $this->args_default = array(); $this->filters = array(); $this->labels = array(); $this->callbacks = array(); $this->_key = 'filter-'.$key; $this->_storage = $storage; $this->_init_args(); $this->_trash_filtr = null; } public function get_arg($name) { // TODO: allow only keys from $this->filters // try load from @storage and save if (array_key_exists($name, $this->args)) { return $this->args[$name]; } else if (array_key_exists($name, $this->args_default)) { return $this->args_default[$name]; } return null; } public function setArgs($args) { foreach ($args as $k => $v) { if (!array_key_exists($k, $this->filters)) continue; $this->args[$k] = $v; } } private function _init_args() { if (array_key_exists('filtr_clear', $this->args) && $this->args['filtr_clear'] == 1) { $this->args = array(); } else { $this->_read_args(); } } function add_filter($name, $values, $default, $label = '', $callback = null) { $this->filters[$name] = $values; $this->labels[$name] = ($label)? $label : $name; $this->args_default[$name] = $default; if ($callback) $this->callbacks[$name] = $callback; } public function _save_args() { if (!$this->_key) { return; } if ($this->_storage == 'cookie') { $this->_save_args_in_cookie(); } else { $this->_save_args_in_session(); } } private function _save_args_in_session() { 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); } } } } $save_args = implode(',', $save_args); $_SESSION[$this->_key] = $save_args; } private function _save_args_in_cookie() { 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''; // } } public 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); } } private 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]); //echo'

read from session c_args: '.$_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]); $option = urldecode($c_val[1]); if ($force || !array_key_exists($name, $this->args)) { //echo'

read from session: '.$name.' / '.$this->args[$name].' / set '.$option.'

'; $this->args[$name] = $option; } } } /** * Read args from cookie. */ private function _read_args_from_cookie($force = false) { 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
tag. */ public function show_filters() { // read args from cookie if exists $selected_defaults = true; // bug fix in browser, run onclick action on first submit/image/button element in html code echo''; echo''; echo''; // show trash if exists echo $this->show_trash(); // show filters foreach ($this->filters as $name => $options) { $out = new stdClass(); $out->cls = '';// class $out->cnt = '';// content $selected_option = (isset($this->args[$name]))? $this->args[$name] : $this->args_default[$name]; if (count($options) == 1 && reset($options) == 'search') { $size = strlen($selected_option); $size = ($size < 4)? 4 : $size + 2;// size min 4 $out->cnt .= ''; if ($selected_option != $this->args_default[$name]) { $selected_defaults = false; $out->cls = 'active'; //echo' '; // po kliknięciu ENTER w polu wymuszało usuwanie zawartości pola - 1st submit btn in form $out->cnt .= ''; } //$out->cnt .= ''; $out->cnt .= ''; } else if (count($options) == 2) {// 2 opcje do wyboru to button $selected_button = $selected_option; if (!array_key_exists($selected_button, $options)) { $selected_button = $this->args_default[$name]; } if ($selected_button != $this->args_default[$name]) { $selected_defaults = false; $out->cls = 'active'; } foreach ($options as $option => $val) { if ($selected_button != $option) { $out->cnt .= ''; } } } else {// ponad 2 opcje to select $out->cnt .= ''; } echo' cls)? ' class="'.$out->cls.'"' : '').'>'; echo $this->labels[$name]; echo $out->cnt; echo''; echo $this->separator(); unset($out); } if (!$selected_defaults) { echo''; echo''; echo' '; echo''; } $this->_save_args(); } public function set_trash($filtr_key = '', $value = 0) { $this->_trash_filtr = array('filtr_key'=>$filtr_key, 'value'=>$value); } public function is_trash() { if ($this->_trash_filtr === null) { return false; } $trash_filtr_key = $this->_trash_filtr['filtr_key']; $trash_filtr_value = $this->_trash_filtr['value']; $filtr_value = $this->get_arg($trash_filtr_key); if ($filtr_value === null) { return false; } if ($filtr_value == $trash_filtr_value) { return true; } return false; } public function show_trash() { $out = ''; if ($this->_trash_filtr === null) { return $out; } $trash_filtr_key = $this->_trash_filtr['filtr_key']; $trash_filtr_value = $this->_trash_filtr['value']; $filtr_value = $this->get_arg($trash_filtr_key); if ($filtr_value === null) {// filtr not exists, TODO: throw error? return $out; } $out .= ''; if ($this->is_trash()) { //echo App::link("Drzewo", array('task'=>App::get_task()), array('ico'=>'trash_out.gif', 'title'=>'Wroc do drzewa')); $trash_ico = 'icon/trash_out.gif'; $onclick = ' onclick="'."this.form.".$trash_filtr_key.".value='';this.form.submit();".'"'; $out .= ''; } else { //echo App::link("Kosz", array('task'=>App::get_task(), 'filtr_id'=>"-1"), array('ico'=>'trash.gif', 'title'=>'Kosz')); $trash_ico = 'icon/trash.gif'; $onclick = ' onclick="'."this.form.".$trash_filtr_key.".value='".$trash_filtr_value."';this.form.submit();".'"'; $out .= ''; } $out .= ''; $out .= $this->separator(); return $out; } function separator() { return '
';// separator } }