PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/atk4/lib/Filter.php

https://github.com/mahimarathore/mahi
PHP | 75 lines | 50 code | 4 blank | 21 comment | 12 complexity | 2957b9385bc151aa9709ab3ecd1151f5 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /***********************************************************
  3. ..
  4. Reference:
  5. http://agiletoolkit.org/doc/ref
  6. ==ATK4===================================================
  7. This file is part of Agile Toolkit 4
  8. http://agiletoolkit.org/
  9. (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
  10. Distributed under Affero General Public License v3 and
  11. commercial license.
  12. See LICENSE or LICENSE_COM for more information
  13. =====================================================ATK4=*/
  14. class Filter extends Form {
  15. public $limiters=array();
  16. public $memorize=true;
  17. public $view;
  18. function init(){
  19. parent::init();
  20. // set default values on non-yet initialized fields
  21. $this->api->addHook('post-init',array($this,'postInit'));
  22. }
  23. function useWith($view){
  24. // Apply our condition on the view
  25. $this->view=$view;
  26. return $this;
  27. }
  28. /** Remembers values and uses them as condition */
  29. function postInit(){
  30. foreach($this->elements as $x=>$field){
  31. if($field instanceof Form_Field){
  32. $field->set($val=$this->recall($x));
  33. if($field->no_save)continue;
  34. if(!$field->get())continue;
  35. // also apply the condition
  36. if($this->view->model && $this->view->model->hasElement($x) ){
  37. if($this->view->model->addCondition($x,$field->get())); // take advantage of field normalization
  38. }
  39. }
  40. }
  41. $this->hook('applyFilter',array($this->view->model));
  42. }
  43. function memorizeAll(){
  44. //by Camper: memorize() method doesn't memorize anything if value is null
  45. foreach($this->elements as $x=>$field){
  46. if($field instanceof Form_Field){
  47. if($this->isClicked('Clear')||is_null($this->get($x)))$this->forget($x);
  48. else $this->memorize($x,$this->get($x));
  49. }
  50. }
  51. }
  52. function addButtons(){
  53. $this->save=$this->addSubmit('Save');
  54. $this->reset=$this->addSubmit('Reset');
  55. }
  56. function submitted(){
  57. if(parent::submitted()){
  58. if(isset($this->reset) && $this->isClicked($this->reset)){
  59. $this->forget();
  60. $this->js(null,$this->view->js()->reload())->reload()->execute();
  61. }else{
  62. $this->memorizeAll();
  63. }
  64. $this->view->js()->reload()->execute();
  65. }
  66. }
  67. }