FilterLast.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. Class FilterLast {
  3. var $args;
  4. var $args_default;
  5. var $filters;
  6. var $types;
  7. var $labels;
  8. var $_key;
  9. var $_storage;// enum('session', 'cookie')
  10. /**
  11. * @param $args - args table: $_GET, $_POST, $_REQUEST, etc
  12. * @param $key - kay to store values in @storage, if not set then args not saved
  13. * @param $storage - where to store state - cookie or session, default is cookie
  14. */
  15. function __construct($args, $key = null, $storage = 'cookie') {
  16. $this->args = $args;
  17. $this->values = array();
  18. $this->filters = array();
  19. $this->labels = array();
  20. $this->types = array();
  21. $this->_key = 'filter-last-'.$key;
  22. $this->_storage = $storage;
  23. $this->_init_args();
  24. $this->_values_limit = 10;
  25. }
  26. function get_values($name) {
  27. if (array_key_exists($name, $this->values)) {
  28. return $this->values[$name];
  29. }
  30. return array();
  31. }
  32. function get_arg($name) {
  33. // try load from @storage and save
  34. if (array_key_exists($name, $this->args)) {
  35. return $this->args[$name];
  36. }
  37. return null;
  38. }
  39. function _init_args() {
  40. $this->_read_args();
  41. }
  42. function add_filter($name, $arg_names, $label = '', $type = 'string') {
  43. $this->filters[$name] = $arg_names;
  44. $this->labels[$name] = ($label)? $label : $name;
  45. $this->types[$name] = $type;
  46. }
  47. function _save_args() {
  48. if (!$this->_key) { return; }
  49. if ($this->_storage == 'cookie') {
  50. $this->_save_args_in_cookie();
  51. } else {
  52. $this->_save_args_in_session();
  53. }
  54. }
  55. function _save_args_in_session() {
  56. if (!$this->_key) { return; }
  57. $save_args = array();
  58. foreach ($this->filters as $name => $arg_names) {
  59. $type = $this->types[$name];
  60. if ($type != 'int') {// type: 'int' or 'string'
  61. $type = 'string';
  62. }
  63. foreach ($arg_names as $arg_name) {
  64. if (($arg_val = V::get($arg_name, '', $this->args, $type)) != '') {
  65. if ($type == 'int') {
  66. if ($arg_val < 0) {
  67. continue;
  68. }
  69. }
  70. $cur_values = V::get($name, array(), $this->values, 'array');
  71. if (!in_array($arg_val, $cur_values)) {
  72. $cur_values[] = $arg_val;
  73. if (count($cur_values) > $this->_values_limit) {
  74. array_shift($cur_values);// - Shift an element off the beginning of array
  75. }
  76. $this->values[$name] = $cur_values;
  77. }
  78. }
  79. }
  80. }
  81. foreach ($this->values as $name => $v_arr) {
  82. if (!empty($v_arr)) {
  83. $save_args[] = ''.urlencode($name).'='.urlencode(implode(';', $v_arr));
  84. }
  85. }
  86. $save_args = implode(',', $save_args);
  87. $_SESSION[$this->_key] = $save_args;
  88. }
  89. function _save_args_in_cookie() {
  90. return;// TODO: ...
  91. if (!$this->_key) { return; }
  92. $save_args = array();
  93. foreach ($this->filters as $name => $options) {
  94. $arg = (isset($this->args[$name]))? $this->args[$name] : '';
  95. if (count($options) == 1 && reset($options) == 'search') {
  96. if ($arg != $this->args_default[$name]) {
  97. $option = $arg;
  98. $save_args[] = ''.urlencode($name).'='.urlencode($option);
  99. }
  100. } else {
  101. foreach($options as $option => $field_name) {
  102. if (isset($this->args[$name]) && $this->args[$name] == $option) {
  103. $save_args[] = ''.urlencode($name).'='.urlencode($option);
  104. }
  105. }
  106. }
  107. }
  108. // if ($save_args) {
  109. $save_args = implode(',', $save_args);
  110. echo'<script type="text/javascript">';echo"
  111. (function(){
  112. var exdate=new Date()
  113. , exdays = 14
  114. , c_name='".$this->_key."'
  115. , value='".$save_args."'
  116. ;
  117. exdate.setDate(exdate.getDate() + exdays);
  118. var c_value=escape(value) + ((exdays==null) ? '' : '; expires='+exdate.toUTCString());
  119. document.cookie=c_name + '=' + c_value;
  120. })();
  121. ";echo'</script>';
  122. // }
  123. }
  124. function _read_args($force = false) {
  125. if (!$this->_key) { return; }
  126. if ($this->_storage == 'cookie') {
  127. $this->_read_args_from_cookie($force);
  128. } else {
  129. $this->_read_args_from_session($force);
  130. }
  131. }
  132. function _read_args_from_session($force = false) {
  133. if (!$this->_key) { return; }
  134. if (!array_key_exists($this->_key, $_SESSION)) { return; }
  135. $c_args = explode(',', $_SESSION[$this->_key]);
  136. foreach ($c_args as $c_val) {
  137. $c_val = explode('=', $c_val);
  138. if (count($c_val) != 2) continue;
  139. $name = urldecode($c_val[0]);
  140. $values = urldecode($c_val[1]);
  141. if ($force || !array_key_exists($name, $this->values)) {
  142. $this->values[$name] = explode(';', $values);
  143. }
  144. }
  145. }
  146. /**
  147. * Read args from cookie.
  148. */
  149. function _read_args_from_cookie($force = false) {
  150. return;// TODO: ...
  151. if (!$this->_key) { return; }
  152. if (!array_key_exists($this->_key, $_COOKIE)) { return; }
  153. $c_args = explode(',', $_COOKIE[$this->_key]);
  154. foreach ($c_args as $c_val) {
  155. $c_val = explode('=', $c_val);
  156. if (count($c_val) != 2) continue;
  157. $name = urldecode($c_val[0]);
  158. $option = urldecode($c_val[1]);
  159. if ($force || !array_key_exists($name, $this->args)) {
  160. $this->args[$name] = $option;
  161. }
  162. }
  163. }
  164. /**
  165. * Print form fields. Must be inside <form> tag.
  166. */
  167. function show_filters() {
  168. // show filters
  169. foreach ($this->filters as $name => $v_values) {
  170. $out = new stdClass();
  171. $out->cls = '';// class
  172. $out->cnt = '';// content
  173. $vals = V::get($name, array(), $this->values, 'array');
  174. if (empty($vals)) {
  175. continue;
  176. }
  177. $out->cnt_arr = array();
  178. foreach ($vals as $val) {
  179. $js = "return scrollToProces('".$val."');";
  180. $out->cnt_arr[] = '<a href="'."#TREE".$val.'" onclick="'.$js.'">'.$val.'</a>';// TODO: read from add_arg
  181. }
  182. $out->cnt = implode(', ', $out->cnt_arr);
  183. echo' <nobr'.(($out->cls)? ' class="'.$out->cls.'"' : '').'>';
  184. echo $this->labels[$name];
  185. echo ": ";
  186. echo $out->cnt;
  187. echo'</nobr>';
  188. echo $this->separator();
  189. unset($out);
  190. }
  191. }
  192. function separator() {
  193. return '';//'<div class="btnseparator"></div>';// separator
  194. }
  195. }// class