-
Notifications
You must be signed in to change notification settings - Fork 36
/
FilterHelper.php
192 lines (169 loc) · 4.63 KB
/
FilterHelper.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
class FilterHelper extends AppHelper{
public $helpers = array(
'Form', 'Html'
);
public $count = 0;
public function form($model, $filter = array()) {
if (empty($filter) || !isset($filter['fields'])) {
$this->errors[] = 'There is no filters';
return false;
}
$output = '<div class="filter-form"><h1>'.__('Search').'</h1>';
foreach($filter['fields'] as $field => $options) {
if (is_array($options)) {
switch($field) {
case 'active':
$emptyText = __('status');
break;
default:
$emptyText = __($field);
break;
}
$emptyText = sprintf(__('Select the %s'), Inflector::humanize(str_replace('_id', '', $emptyText)));
$output .= $this->Form->input(
$field,
array(
'type' => 'select',
'div' => false,
'options' => $options,
'empty' => $emptyText,
'label' => false
)
);
}
else if(strstr($options, 'date')) {
$output .= $this->Html->datePicker(array($options));
}
else{
$output .= $this->Form->input(
$options,
array(
'type' => 'text',
'div' => false,
'label' => false,
'placeholder' => Inflector::humanize($options)
)
);
}
}
$output .= $this->Form->button(
$this->Image->image(
'actions',
'filter',
array(
'width' => '16px'
)
),
array(
'value' => 'filter',
'name' => 'action',
'title' => $this->niceTitleText('Filter results'),
'div' => false
)
);
$output .= '</div>';
return $output;
}
/**
* Comment Template.
*
* @todo -c Implement .this needs to be sorted out.
*
* Copyright (c) 2009 Carl Sutton ( dogmatic69 )
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright (c) 2009 Carl Sutton ( dogmatic69 )
* @link http://infinitas-cms.org
* @package sort
* @subpackage sort.comments
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
* @since 0.5a
*/
public function clear($filter, $div = false) {
if (!isset($filter['url'][0]) || empty($filter['url'][0]) || $filter['url'][0] == '/') {
$filter['url'][0] = '/';
}
$out = '';
if ($div) {
$out .= '<div class="filter">';
}
$out .= '<div class="wrap">';
$parts = explode( '/', $filter['url'][0] );
$done = array();
foreach($parts as $_f) {
if (empty($_f) || in_array($_f, $done)) {
continue;
}
$done[] = $_f;
$text = explode('.', current(explode(':', $_f)));
$text = (count($text ) > 1) ? $text[1] : $text[0];
$text = $this->stripPluginName($text);
if(substr($text, -3) == '_id') {
$text = substr($text, 0, -3);
}
$out .= '<div class="left">'.
'<div class="remove">'.
$this->Html->link(
Inflector::humanize($text),
str_replace($_f, '', '/' . $this->request->url)
).
'</div>'.
'</div>';
}
$out .= '</div>';
if ($div) {
$out .= '</div>';
}
return $out;
}
/**
* bulid a list of leters and numbers with filtered links to rows that
* start with that letter or number
*
* @return string ul->li list of things found
*/
public function alphabetFilter() {
if(empty($this->request->params['models'])) {
return false;
}
$model = implode('.', current($this->request->params['models']));
$letters = ClassRegistry::init($model)->getLetterList();
$return = array();
foreach($letters as $key => $value) {
$url = ($value == true) ? $this->__filterLink($key) : $key;
if(is_array($url)) {
$url = $this->Html->link(
$key,
InfinitasRouter::url($url),
array(
'title' => sprintf(__('Rows starting with "%s"'), $key)
)
);
}
$return[] = sprintf('<li>%s</li>', $url);
}
$return[] = sprintf('<li>%s</li>', $this->Html->link('All', $this->cleanCurrentUrl()));
return '<div class="alphabet-filter"><ul>' . implode('', $return) . '</ul></div>';
}
/**
* create a link for some text against the display field
*
* @param string $text the text to show/filter with
* @return array the url cake style
*/
private function __filterLink($text = null) {
if(!$text) {
return false;
}
$model = implode('.', current($this->request->params['models']));
$filter = array(
ClassRegistry::init($model)->alias . '.' . ClassRegistry::init($model)->displayField => $text
);
$params = array_merge(parent::cleanCurrentUrl(), $this->request->params['named'], $this->request->params['pass'], $filter);
return $params;
}
}