Skip to content

Commit 6bfa376

Browse files
author
unknown
committed
new methods: setPreResultCallback, setColumnSearchType, getColumnSearchType
updated readme with details on new methods
1 parent 5deb55c commit 6bfa376

File tree

2 files changed

+130
-8
lines changed

2 files changed

+130
-8
lines changed

README.md

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ This library requires **DataTables 1.10 >**
1919

2020
* Drastically Reduces PHP Code Necessary To Generate a Server Side Table
2121

22+
Chnage Log
23+
-----
24+
* **v1.1**
25+
* New Method `setPreResultCallback(function())`
26+
* New Method `setColumnSearchType(colName, type)` / `getColumnSearchType(colName)`
27+
2228
Install
2329
-----
2430

@@ -54,9 +60,47 @@ Name | Description | Return
5460
`joinArray` | Join additional tables for DataTable columns to reference. *Tip: Join Types CAN be specifed by using a pipe in the key value `'table_to_join b|left outer'`*| **Assoc. Array** *Key*=Table To Join *Value*=SQL Join Expression.
5561
`whereClauseArray`| Append Static SQL to the generated Where Clause| **Assoc. Array** *Key*= Column Name *Value*=Value To Filter **OR** *NULL*
5662

63+
Methods
64+
----
65+
`setPreResultCallback(function)`
66+
This will get called after the library constructs the associative array that will get converted into JSON. This allows for the manipulation of the data
67+
in the JSON or to add custom properties to the JSON before it is pushed to the browser. Make sure that you use the & when getting the data rows, otherwise,
68+
you will end up with a copy of the array and it will not affect the json. Below is an example of how it is used.
69+
```php
70+
$this -> datatable -> setPreResultCallback(
71+
function(&$json) {
72+
$rows =& $json['data'];
73+
foreach($rows as &$r) {
74+
// example of nested object in row when the
75+
// data propterty in the javascript looks like "$.url"
76+
if(empty($r['$']['url']) === FALSE) {
77+
$newUrl = 'http://www.changeurl.com';
78+
$r['$']['url'] = $newUrl;
79+
}
80+
81+
// change the value of the gender in the Json. data property in the javascript looks like "gender"
82+
$r['gender'] = $r['gender'] == 'M' ? 'Male' : 'Female';
83+
84+
85+
}
86+
87+
88+
$json['a_custom_property'] = 'Check me out with firebug!';
89+
}
90+
);
91+
```
5792

93+
`setColumnSearchType(columnName, type)`
94+
Set the matching type to be done for a given column. This will default to "after" if not specified
95+
*columnName*(string) - Name of the column matching what was passed in from the JavaScript in the data property
96+
*type*(string) - Type of search to perform. This will control the % wildcard on the like. valid values are: before, after, both, none
97+
```php
98+
$this -> datatable -> setColumnSearchType('$.url', 'both');
99+
```
58100

59-
101+
`getColumnSearchType(columnName)`
102+
Returns the current matching for a given column
103+
*columnName*(string) - Name of the column matching what was passed in from the JavaScript in the data property
60104

61105
Basic DatatableModel Implementation
62106
--------

libraries/Datatable.php

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,19 @@
3232
*/
3333
class Datatable{
3434

35+
private static $VALID_MATCH_TYPES = array('before', 'after', 'both', 'none');
3536

36-
var $model;
37+
private $model;
3738

38-
var $CI;
39+
private $CI;
40+
41+
private $rowIdCol;
42+
43+
private $preResultFunc = FALSE;
44+
45+
// assoc. array. key is column name being passed from the DataTables data property and value is before, after, both, none
46+
private $matchType = array();
3947

40-
var $rowIdCol;
4148

4249
/**
4350
* @params
@@ -67,13 +74,72 @@ public function __construct($params) {
6774

6875
}
6976

77+
/**
78+
* Register a function that will fire after the JSON object is put together
79+
* in the library, but before sending it to the browser. The function should accept 1 parameter
80+
* for the JSON object which is stored as associated array.
81+
*
82+
* IMPORTANT: Make sure to add a & in front of the parameter to get a reference of the Array,otherwise
83+
* your changes will not be picked up by the library
84+
*
85+
* function(&$json) {
86+
* //do some work and add to the json if you wish.
87+
* }
88+
*/
89+
public function setPreResultCallback($func) {
90+
if(is_object($func) === FALSE || ($func instanceof Closure) === FALSE) {
91+
throw new Exception('Expected Anonymous Function Parameter Not Received');
92+
}
93+
94+
$this -> preResultFunc = $func;
95+
96+
return $this;
97+
}
98+
99+
100+
/**
101+
* Sets the wildcard matching to be a done on a specific column in the search
102+
*
103+
* @param col
104+
* column sepcified in the DataTables "data" property
105+
* @param type
106+
* Type of wildcard search before, after, both, none. Default is after if not specified for a column.
107+
* @return Datatable
108+
*/
109+
public function setColumnSearchType($col, $type) {
110+
$type = trim(strtolower($type));
111+
//make sure we have a valid type
112+
if(in_array($type, self :: $VALID_MATCH_TYPES) === FALSE) {
113+
throw new Exception('[' . $type . '] is not a valid type. Must Use: ' . implode(', ', self :: $VALID_MATCH_TYPES));
114+
}
115+
116+
$this -> matchType[$col] = $type;
117+
118+
// log_message('info', 'setColumnSearchType() ' . var_export($this -> matchType, TRUE));
119+
120+
return $this;
121+
}
122+
123+
/**
124+
* Get the current search type for a column
125+
*
126+
* @param col
127+
* column sepcified in the DataTables "data" property
128+
*
129+
* @return search type string
130+
*/
131+
public function getColumnSearchType($col) {
132+
// log_message('info', 'getColumnSearchType() ' . var_export($this -> matchType, TRUE));
133+
return isset($this -> matchType[$col]) ? $this -> matchType[$col] : 'after';
134+
}
135+
70136
/**
71137
* @param formats
72138
* Associative array.
73139
* Key is column name
74140
* Value format: percent, currency, date, boolean
75141
*/
76-
public function datatableJson($formats = array()) {
142+
public function datatableJson($formats = array(), $debug = FALSE) {
77143

78144
$f = $this -> CI -> input;
79145
$start = (int)$f -> post('start');
@@ -150,6 +216,9 @@ public function datatableJson($formats = array()) {
150216
return $jsonArry;
151217
}
152218

219+
if($debug === TRUE) {
220+
$jsonArry['debug_sql'] = $this -> CI -> db -> last_query();
221+
}
153222

154223
//process the results and create the JSON objects
155224
$dataArray = array();
@@ -207,14 +276,19 @@ public function datatableJson($formats = array()) {
207276
$jsonArry['recordsTotal'] = $totalRecords;
208277
$jsonArry['recordsFiltered'] = $totalRecords;
209278
$jsonArry['data'] = $dataArray;
210-
$jsonArry['debug'] = $whereDebug;
279+
//$jsonArry['debug'] = $whereDebug;
280+
281+
if($this -> preResultFunc !== FALSE) {
282+
$func = $this -> preResultFunc;
283+
$func($jsonArry);
284+
}
211285

212286
return $jsonArry;
213287

214288
}
215289

216290
private function formatValue($formats, $column, $value) {
217-
if (isset($formats[$column]) === FALSE) {
291+
if (isset($formats[$column]) === FALSE || trim($value) == '') {
218292
return $value;
219293
}
220294

@@ -254,6 +328,7 @@ private function formatValue($formats, $column, $value) {
254328
//fetch the data and get a total record count
255329
private function sqlJoinsAndWhere() {
256330
$debug = '';
331+
$this -> CI -> db-> _protect_identifiers = FALSE;
257332
$this -> CI -> db -> from($this -> model -> fromTableStr());
258333

259334
$joins = $this -> model -> joinArray() === NULL ? array() : $this -> model -> joinArray();
@@ -275,6 +350,8 @@ private function sqlJoinsAndWhere() {
275350
foreach($f -> post('columns') as $c) {
276351
if($c['search']['value'] !== '') {
277352
$colName = $c['data'];
353+
$searchType = $this -> getColumnSearchType($colName);
354+
//log_message('info', 'colname[' . $colName . '] searchtype[' . $searchType . ']');
278355
//handle custom sql expressions/subselects
279356
if(substr($colName, 0, 2) === '$.') {
280357
$aliasKey = substr($colName, 2);
@@ -285,7 +362,8 @@ private function sqlJoinsAndWhere() {
285362
$colName = $customExpArray[$aliasKey];
286363
}
287364
$debug .= 'col[' . $c['data'] .'] value[' . $c['search']['value'] . '] ' . PHP_EOL;
288-
$this -> CI -> db -> like($colName, $c['search']['value'], 'after');
365+
// log_message('info', 'colname[' . $colName . '] searchtype[' . $searchType . ']');
366+
$this -> CI -> db -> like($colName, $c['search']['value'], $searchType);
289367
}
290368
}
291369

0 commit comments

Comments
 (0)