mahi /atk4/lib/Filter.php

Language PHP Lines 76
MD5 Hash 2957b9385bc151aa9709ab3ecd1151f5
Repository https://github.com/mahimarathore/mahi.git View Raw File
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/***********************************************************
  ..

  Reference:
  http://agiletoolkit.org/doc/ref

==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
=====================================================ATK4=*/
class Filter extends Form {
    public $limiters=array();
    public $memorize=true;
    public $view;
    function init(){
        parent::init();

        // set default values on non-yet initialized fields
        $this->api->addHook('post-init',array($this,'postInit'));
    }
    function useWith($view){
        // Apply our condition on the view
        $this->view=$view;
        return $this;
    }
    /** Remembers values and uses them as condition */
    function postInit(){
        foreach($this->elements as $x=>$field){
            if($field instanceof Form_Field){

                $field->set($val=$this->recall($x));

                if($field->no_save)continue;
                if(!$field->get())continue;

                // also apply the condition
                if($this->view->model && $this->view->model->hasElement($x) ){
                    if($this->view->model->addCondition($x,$field->get())); // take advantage of field normalization
                }
            }
        }
        $this->hook('applyFilter',array($this->view->model));
    }
    function memorizeAll(){
        //by Camper: memorize() method doesn't memorize anything if value is null
        foreach($this->elements as $x=>$field){
            if($field instanceof Form_Field){
                if($this->isClicked('Clear')||is_null($this->get($x)))$this->forget($x);
                else $this->memorize($x,$this->get($x));
            }
        }
    }
    function addButtons(){
        $this->save=$this->addSubmit('Save');
        $this->reset=$this->addSubmit('Reset');
    }
    function submitted(){
        if(parent::submitted()){
            if(isset($this->reset) && $this->isClicked($this->reset)){
                $this->forget();
                $this->js(null,$this->view->js()->reload())->reload()->execute();
            }else{
                $this->memorizeAll();
            }
            $this->view->js()->reload()->execute();
        }
    }
}
Back to Top