Filter.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. Class Filter {
  3. var $args;
  4. var $args_default;
  5. var $filters;
  6. var $labels;
  7. var $callbacks;
  8. var $_key;
  9. var $_storage;// enum('session', 'cookie')
  10. var $_trash_filtr;// Tree trash
  11. /**
  12. * @param $args - args table: $_GET, $_POST, $_REQUEST, etc
  13. * @param $key - kay to store values in @storage, if not set then args not saved
  14. * @param $storage - where to store state - cookie or session, default is cookie
  15. */
  16. function __construct( $args, $key = null, $storage = 'cookie' ) {
  17. $this->args = $args;
  18. $this->args_default = array();
  19. $this->filters = array();
  20. $this->labels = array();
  21. $this->callbacks = array();
  22. $this->_key = 'filter-'.$key;
  23. $this->_storage = $storage;
  24. $this->_init_args();
  25. $this->_trash_filtr = null;
  26. }
  27. function get_arg( $name ) {
  28. // try load from @storage and save
  29. if (array_key_exists($name, $this->args)) {
  30. return $this->args[ $name ];
  31. } else if (array_key_exists($name, $this->args_default)) {
  32. return $this->args_default[ $name ];
  33. }
  34. return null;
  35. }
  36. function _init_args() {
  37. if (array_key_exists('filtr_clear', $this->args) && $this->args['filtr_clear'] == 1) {
  38. $this->args = array();
  39. } else {
  40. $this->_read_args();
  41. }
  42. }
  43. function add_filter( $name, $values, $default, $label = '', $callback = null ) {
  44. $this->filters[ $name ] = $values;
  45. $this->labels[ $name ] = ($label)? $label : $name;
  46. $this->args_default[ $name ] = $default;
  47. if ($callback) $this->callbacks[ $name ] = $callback;
  48. }
  49. function _save_args() {
  50. if (!$this->_key) { return; }
  51. if ($this->_storage == 'cookie') {
  52. $this->_save_args_in_cookie();
  53. } else {
  54. $this->_save_args_in_session();
  55. }
  56. }
  57. function _save_args_in_session() {
  58. if (!$this->_key) { return; }
  59. $save_args = array();
  60. foreach ($this->filters as $name => $options) {
  61. $arg = (isset($this->args[$name]))? $this->args[$name] : '';
  62. if (count($options) == 1 && reset($options) == 'search') {
  63. if ($arg != $this->args_default[$name]) {
  64. $option = $arg;
  65. $save_args []= ''.urlencode($name).'='.urlencode($option);
  66. }
  67. } else {
  68. foreach($options as $option => $field_name) {
  69. if (isset($this->args[$name]) && $this->args[$name] == $option) {
  70. $save_args []= ''.urlencode($name).'='.urlencode($option);
  71. }
  72. }
  73. }
  74. }
  75. $save_args = implode(',', $save_args);
  76. $_SESSION[ $this->_key ] = $save_args;
  77. }
  78. function _save_args_in_cookie() {
  79. if (!$this->_key) { return; }
  80. $save_args = array();
  81. foreach ($this->filters as $name => $options) {
  82. $arg = (isset($this->args[$name]))? $this->args[$name] : '';
  83. if (count($options) == 1 && reset($options) == 'search') {
  84. if ($arg != $this->args_default[$name]) {
  85. $option = $arg;
  86. $save_args []= ''.urlencode($name).'='.urlencode($option);
  87. }
  88. } else {
  89. foreach($options as $option => $field_name) {
  90. if (isset($this->args[$name]) && $this->args[$name] == $option) {
  91. $save_args []= ''.urlencode($name).'='.urlencode($option);
  92. }
  93. }
  94. }
  95. }
  96. // if ($save_args) {
  97. $save_args = implode(',', $save_args);
  98. echo'<script type="text/javascript">';echo"
  99. (function(){
  100. var exdate=new Date()
  101. , exdays = 14
  102. , c_name='".$this->_key."'
  103. , value='".$save_args."'
  104. ;
  105. exdate.setDate(exdate.getDate() + exdays);
  106. var c_value=escape(value) + ((exdays==null) ? '' : '; expires='+exdate.toUTCString());
  107. document.cookie=c_name + '=' + c_value;
  108. })();
  109. ";echo'</script>';
  110. // }
  111. }
  112. function _read_args($force = false) {
  113. if (!$this->_key) { return; }
  114. if ($this->_storage == 'cookie') {
  115. $this->_read_args_from_cookie($force);
  116. } else {
  117. $this->_read_args_from_session($force);
  118. }
  119. }
  120. function _read_args_from_session($force = false) {
  121. if (!$this->_key) { return; }
  122. if (!array_key_exists($this->_key, $_SESSION)) { return; }
  123. $c_args = explode(',', $_SESSION[$this->_key]);
  124. //echo'<p>read from session c_args: '.$_SESSION[$this->_key].'</p>';
  125. foreach ($c_args as $c_val) {
  126. $c_val = explode('=', $c_val);
  127. if (count($c_val) != 2) continue;
  128. $name = urldecode($c_val[0]);
  129. $option = urldecode($c_val[1]);
  130. if ($force || !array_key_exists($name, $this->args)) {
  131. //echo'<p>read from session: '.$name.' / '.$this->args[ $name ].' / set '.$option.'</p>';
  132. $this->args[ $name ] = $option;
  133. }
  134. }//end foreach
  135. }
  136. /**
  137. * Read args from cookie.
  138. */
  139. function _read_args_from_cookie($force = false) {
  140. if (!$this->_key) { return; }
  141. if (!array_key_exists($this->_key, $_COOKIE)) { return; }
  142. $c_args = explode(',', $_COOKIE[$this->_key]);
  143. foreach ($c_args as $c_val) {
  144. $c_val = explode('=', $c_val);
  145. if (count($c_val) != 2) continue;
  146. $name = urldecode($c_val[0]);
  147. $option = urldecode($c_val[1]);
  148. if ($force || !array_key_exists($name, $this->args)) {
  149. $this->args[ $name ] = $option;
  150. }
  151. }//end foreach
  152. }
  153. /**
  154. * Print form fields. Must be inside <form> tag.
  155. */
  156. function show_filters() {
  157. // read args from cookie if exists
  158. $selected_defaults = true;
  159. // bug fix in browser, run onclick action on first submit/image/button element in html code
  160. echo'<nobr style="float:right;margin:-9999px 0 0 -9999px;">';
  161. echo'<input type="submit" value="'."submit".'" />';
  162. echo'</nobr>';
  163. // show trash if exists
  164. echo $this->show_trash();
  165. // show filters
  166. foreach ($this->filters as $name => $options) {
  167. $out = new stdClass();
  168. $out->cls = '';// class
  169. $out->cnt = '';// content
  170. $selected_option = (isset($this->args[$name]))? $this->args[$name] : $this->args_default[$name];
  171. if (count($options) == 1 && reset($options) == 'search') {
  172. $size = strlen($selected_option); $size = ($size < 4)? 4 : $size + 2;// size min 4
  173. $out->cnt .= '<input type="text" name="'.$name.'" value="'.$selected_option.'" class="i" size="'.$size.'" />';
  174. if ($selected_option != $this->args_default[$name]) {
  175. $selected_defaults = false;
  176. $out->cls = 'active';
  177. //echo' <input type="submit" value="'."x".'" onclick="'."this.form.".$name.".value='';".'" title="'."Usuń".'" />';
  178. // po kliknięciu ENTER w polu wymuszało usuwanie zawartości pola - 1st submit btn in form
  179. $out->cnt .= '<input type="button" value="'."x".'" onclick="'."this.form.".$name.".value='';this.form.submit();".'" title="'."Usuń".'" />';
  180. }
  181. //$out->cnt .= '<input type="submit" value="'."Szukaj".'" />';
  182. $out->cnt .= '<input type="image" src="' . "icon/search.png" . '" ale="'."Szukaj".'" title="'."Szukaj".'" />';
  183. }
  184. else if (count($options) == 2) {// 2 opcje do wyboru to button
  185. $selected_button = $selected_option;
  186. if (!array_key_exists($selected_button, $options)) {
  187. $selected_button = $this->args_default[$name];
  188. }
  189. if ($selected_button != $this->args_default[$name]) {
  190. $selected_defaults = false;
  191. $out->cls = 'active';
  192. }
  193. foreach ($options as $option => $val) {
  194. if ($selected_button != $option) {
  195. $out->cnt .= '<input type="submit" name="'.$name.'" value="'.$option.'" />';
  196. }
  197. }
  198. }
  199. else {// ponad 2 opcje to select
  200. $out->cnt .= '<select name="'.$name.'" onchange="this.form.submit();">';
  201. if ($selected_option != $this->args_default[$name]) {
  202. $selected_defaults = false;
  203. $out->cls = 'active';
  204. }
  205. foreach ($options as $option => $field_name) {
  206. $sel = ($selected_option == $option)? 'selected' : '';
  207. $out->cnt .= '<option value="'.$option.'" '.$sel.'>'.$option.'</option>';
  208. }
  209. $out->cnt .= '</select>';
  210. }
  211. echo' <nobr'.(($out->cls)? ' class="'.$out->cls.'"' : '').'>';
  212. echo $this->labels[$name];
  213. echo $out->cnt;
  214. echo'</nobr>';
  215. echo $this->separator();
  216. unset($out);
  217. }
  218. if (!$selected_defaults) {
  219. echo'<nobr>';
  220. echo'<input type="hidden" name="filtr_clear" value="0" />';
  221. echo' <input type="button" value="'."Wyczyść filtr".'" onclick="this.form.filtr_clear.value=1;this.form.submit()" /> ';
  222. echo'</nobr>';
  223. }
  224. $this->_save_args();
  225. }
  226. function set_trash( $filtr_key = '', $value = 0 ) {
  227. $this->_trash_filtr = array('filtr_key'=>$filtr_key, 'value'=>$value);
  228. }
  229. function is_trash() {
  230. if ($this->_trash_filtr === null) {
  231. return false;
  232. }
  233. $trash_filtr_key = $this->_trash_filtr['filtr_key'];
  234. $trash_filtr_value = $this->_trash_filtr['value'];
  235. $filtr_value = $this->get_arg($trash_filtr_key);
  236. if ($filtr_value === null) {
  237. return false;
  238. }
  239. if ($filtr_value == $trash_filtr_value) {
  240. return true;
  241. }
  242. return false;
  243. }
  244. function show_trash() {
  245. $out = '';
  246. if ($this->_trash_filtr === null) {
  247. return $out;
  248. }
  249. $trash_filtr_key = $this->_trash_filtr['filtr_key'];
  250. $trash_filtr_value = $this->_trash_filtr['value'];
  251. $filtr_value = $this->get_arg($trash_filtr_key);
  252. if ($filtr_value === null) {// filtr not exists, TODO: throw error?
  253. return $out;
  254. }
  255. $out .= '<nobr>';
  256. if ($this->is_trash()) {
  257. //echo App::link("Drzewo", array('task'=>App::get_task()), array('ico'=>'trash_out.gif', 'title'=>'Wroc do drzewa'));
  258. $trash_ico = 'icon/trash_out.gif';
  259. $onclick = ' onclick="'."this.form.".$trash_filtr_key.".value='';this.form.submit();".'"';
  260. $out .= '<input type="image" src="'.$trash_ico.'" value="'."Drzewo".'" class="i"'.$onclick.' title="'."Wroc do drzewa".'" />';
  261. } else {
  262. //echo App::link("Kosz", array('task'=>App::get_task(), 'filtr_id'=>"-1"), array('ico'=>'trash.gif', 'title'=>'Kosz'));
  263. $trash_ico = 'icon/trash.gif';
  264. $onclick = ' onclick="'."this.form.".$trash_filtr_key.".value='".$trash_filtr_value."';this.form.submit();".'"';
  265. $out .= '<input type="image" src="'.$trash_ico.'" value="'."Kosz".'" class="i"'.$onclick.' title="'."Kosz".'" />';
  266. }
  267. $out .= '</nobr>';
  268. $out .= $this->separator();
  269. return $out;
  270. }
  271. function separator() {
  272. return '<div class="btnseparator"></div>';// separator
  273. }
  274. }