This repository has been archived by the owner on Jul 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Filter.php
119 lines (104 loc) · 3.01 KB
/
Filter.php
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?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 $view;
function init()
{
parent::init();
// set default values on non-yet initialized fields
$this->api->addHook('post-init', array($this, 'postInit'));
}
/**
* Set view on which conditions will be applied
*
* @param object $view
* @return Filter $this
*/
function useWith($view)
{
// Apply our condition on the view
$this->view = $view;
return $this;
}
/**
* Remembers values and uses them as condition
*
* @return void
*/
function postInit()
{
foreach ($this->elements as $x=>$field)
{
if($field instanceof Form_Field)
{
$field->set($val = $this->recall($x));
if($field->no_save || !$field->get()) {
continue;
}
// also apply the condition
if($this->view->model && $this->view->model->hasElement($x)) {
// take advantage of field normalization
$this->view->model->addCondition($x, $field->get());
}
}
}
// call applyFilter hook if such exist, pass model of associated view as parameter
$this->hook('applyFilter',array($this->view->model));
}
/**
* Memorize filtering parameters
*
* @return void
*/
function memorizeAll()
{
// memorize() method doesn't memorize anything if value is null
foreach ($this->get() as $field=>$value) {
if ($this->isClicked('Clear') || is_null($value)) {
$this->forget($field);
} else {
$this->memorize($field, $value);
}
}
}
/**
* Add Save and Reset buttons
*
* @return void
*/
function addButtons()
{
$this->save = $this->addSubmit('Save');
$this->reset = $this->addSubmit('Reset');
}
/**
* On form submit memorize or forget filtering parameters
*
* @return void
*/
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();
}
}
}