Skip to content

Commit e25bd84

Browse files
committed
New: Support for ColumnControl with server-side processing
1 parent 57bf0a4 commit e25bd84

File tree

3 files changed

+192
-0
lines changed

3 files changed

+192
-0
lines changed

Editor.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
namespace DataTables;
1919

2020
use DataTables\Database\Query;
21+
use DataTables\Editor\ColumnControl;
2122
use DataTables\Editor\Field;
2223
use DataTables\Editor\Join;
2324

@@ -2001,6 +2002,8 @@ private function _ssp_filter($query, $http)
20012002
});
20022003
}
20032004

2005+
ColumnControl::ssp($this, $query, $http);
2006+
20042007
// if ( $http['search']['value'] ) {
20052008
// $words = explode(" ", $http['search']['value']);
20062009

@@ -2313,6 +2316,21 @@ private function _options($refresh)
23132316

23142317
$this->_out['searchBuilder']['options'][$field->name()] = $sbOpts;
23152318
}
2319+
2320+
// ContentControl - searchList content type
2321+
$options = $field->columnControl();
2322+
2323+
if ($options) {
2324+
$opts = $options->exec($this->_db, false);
2325+
2326+
if ($opts) {
2327+
if (!isset($this->_out['columnControl'])) {
2328+
$this->_out['columnControl'] = [];
2329+
}
2330+
2331+
$this->_out['columnControl'][$field->name()] = $opts;
2332+
}
2333+
}
23162334
}
23172335
}
23182336

Editor/ColumnControl.php

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
namespace DataTables\Editor;
4+
5+
/**
6+
* Column search methods for server-side processing
7+
*/
8+
class ColumnControl
9+
{
10+
/**
11+
* Apply conditions to a query for a ColumnControl search
12+
*
13+
* @param \DataTables\Editor $editor Host Editor instance
14+
* @param \DataTables\Database\Query $query Query to add conditions to
15+
* @param mixed $http Request object
16+
* @return void
17+
*/
18+
public static function ssp(&$editor, &$query, $http)
19+
{
20+
for ($i = 0; $i < count($http['columns']); $i++) {
21+
$column = $http['columns'][$i];
22+
23+
if (isset($column['columnControl'])) {
24+
$field = $editor->field($column['data']);
25+
26+
// `<input>` based searches
27+
if (isset($column['columnControl']['search'])) {
28+
$search = $column['columnControl']['search'];
29+
30+
$value = $search['value'];
31+
$logic = $search['logic'];
32+
$type = $search['type'];
33+
34+
if ($type === 'num') {
35+
self::_sspNumber($query, $field, $value, $logic);
36+
} else if ($type === 'date') {
37+
self::_sspDate($query, $field, $value, $logic);
38+
} else {
39+
self::_sspText($query, $field, $value, $logic);
40+
}
41+
}
42+
43+
// SearchList
44+
if (isset($column['columnControl']['list'])) {
45+
$list = $column['columnControl']['list'];
46+
47+
$query->where_in($field->dbField(), $list);
48+
}
49+
}
50+
}
51+
}
52+
53+
/**
54+
* Add conditions to a query for a ColumnControl date search
55+
*
56+
* @param \DataTables\Database\Query $query Query to add the conditions to
57+
* @param \DataTables\Editor\Field $field Field for the column in question
58+
* @param string $value Search term
59+
* @param string $logic Search logic
60+
* @return void
61+
*/
62+
private static function _sspDate(&$query, $field, $value, $logic)
63+
{
64+
if ($logic === 'empty') {
65+
$query->where($field->dbField(), null);
66+
} else if ($logic === 'notEmpty') {
67+
$query->where($field->dbField(), null, '!=');
68+
} else if ($value === '') {
69+
// Empty search value means no search for the other logic operators
70+
return;
71+
} else if ($logic === 'equal') {
72+
$query->where($field->dbField(), $value);
73+
} else if ($logic === 'notEqual') {
74+
$query->where($field->dbField(), $value, '!=');
75+
} else if ($logic === 'greater') {
76+
$query->where($field->dbField(), $value, '>');
77+
} else if ($logic === 'less') {
78+
$query->where($field->dbField(), $value, '<');
79+
}
80+
}
81+
82+
/**
83+
* Add conditions to a query for a ColumnControl number search
84+
*
85+
* @param \DataTables\Database\Query $query Query to add the conditions to
86+
* @param \DataTables\Editor\Field $field Field for the column in question
87+
* @param string $value Search term
88+
* @param string $logic Search logic
89+
* @return void
90+
*/
91+
private static function _sspNumber(&$query, $field, $value, $logic)
92+
{
93+
if ($logic === 'empty') {
94+
$query->where(function ($q) use ($field) {
95+
$q->where($field->dbField(), null);
96+
$q->or_where($field->dbField(), '');
97+
});
98+
} else if ($logic === 'notEmpty') {
99+
$query->where(function ($q) use ($field) {
100+
$q->where($field->dbField(), null, '!=');
101+
$q->where($field->dbField(), '', '!=');
102+
});
103+
} else if ($value === '') {
104+
// Empty search value means no search for the other logic operators
105+
return;
106+
} else if ($logic === 'equal') {
107+
$query->where($field->dbField(), $value);
108+
} else if ($logic === 'notEqual') {
109+
$query->where($field->dbField(), $value, '!=');
110+
} else if ($logic === 'greater') {
111+
$query->where($field->dbField(), $value, '>');
112+
} else if ($logic === 'greaterOrEqual') {
113+
$query->where($field->dbField(), $value, '>=');
114+
} else if ($logic === 'less') {
115+
$query->where($field->dbField(), $value, '<');
116+
} else if ($logic === 'lessOrEqual') {
117+
$query->where($field->dbField(), $value, '<=');
118+
}
119+
}
120+
121+
/**
122+
* Add conditions to a query for a ColumnControl text search
123+
*
124+
* @param \DataTables\Database\Query $query Query to add the conditions to
125+
* @param \DataTables\Editor\Field $field Field for the column in question
126+
* @param string $value Search term
127+
* @param string $logic Search logic
128+
* @return void
129+
*/
130+
private static function _sspText(&$query, $field, $value, $logic)
131+
{
132+
if ($logic === 'empty') {
133+
$query->where(function ($q) use ($field) {
134+
$q->where($field->dbField(), null);
135+
$q->or_where($field->dbField(), '');
136+
});
137+
} else if ($logic === 'notEmpty') {
138+
$query->where(function ($q) use ($field) {
139+
$q->where($field->dbField(), null, '!=');
140+
$q->where($field->dbField(), '', '!=');
141+
});
142+
} else if ($value === '') {
143+
// Empty search value means no search for the other logic operators
144+
return;
145+
} else if ($logic === 'equal') {
146+
$query->where($field->dbField(), $value);
147+
} else if ($logic === 'notEqual') {
148+
$query->where($field->dbField(), $value, '!=');
149+
} else if ($logic === 'contains') {
150+
$query->where($field->dbField(), '%' . $value . '%', 'like');
151+
} else if ($logic === 'notContains') {
152+
$query->where($field->dbField(), '%' . $value . '%', 'not like');
153+
} else if ($logic === 'starts') {
154+
$query->where($field->dbField(), $value . '%', 'like');
155+
} else if ($logic === 'ends') {
156+
$query->where($field->dbField(), '%' . $value, 'like');
157+
}
158+
}
159+
}

Editor/Field.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ public function __construct($dbField = null, $name = null)
123123
/** @var Options|null */
124124
private $_opts;
125125

126+
/** @var Options|null */
127+
private $_columnControl;
128+
126129
/** @var SearchPaneOptions|null */
127130
private $_spopts;
128131

@@ -202,6 +205,18 @@ public function dbField($_ = null)
202205
return $this;
203206
}
204207

208+
/**
209+
* Options for ColumnControl's searchList content type
210+
*
211+
* @param Options $_ Options
212+
*
213+
* @return ($_ is null ? Options|null : $this)
214+
*/
215+
public function columnControl($_ = null)
216+
{
217+
return $this->_getSet($this->_columnControl, $_);
218+
}
219+
205220
/**
206221
* Get / set the 'get' property of the field.
207222
*

0 commit comments

Comments
 (0)