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
<?php
/* --------------------------------------------------------------
DataTableHelper.inc.php 2016-11-07
Gambio GmbH
http://www.gambio.de
Copyright (c) 2016 Gambio GmbH
Released under the GNU General Public License (Version 2)
[http://www.gnu.org/licenses/gpl-2.0.html]
--------------------------------------------------------------
*/
/**
* Class DataTableHelper
*
* This class contains helper methods for datatable manipulation. Create an instance and define the columns of
* your table and their respective database fields. You can also provide two fields separated with a space for
* concatenated string.
*
* @category System
* @package Extensions
* @subpackage Helpers
*/
class DataTableHelper
{
/**
* Get ORDER BY clause.
*
* This method will check the order parameter of the DataTables request and return the appropriate
* ORDER BY clause.
*
* Notice: DataTables sends the zero-based index of the table column and not the name. If the column order
* changes then the method needs to be updated.
*
* @param DataTableColumnCollection $columns Contains the definitions of the table columns.
*
* @return string
*
* @throws Exception If there is no order case for the provided column index.
* @throws InvalidArgumentException
*/
public function getOrderByClause(DataTableColumnCollection $columns)
{
$orderBy = $_REQUEST['order'];
if(empty($orderBy))
{
return '';
}
$orderByClause = array();
foreach($orderBy as $order)
{
$direction = strtoupper($order['dir']);
$column = $_REQUEST['columns'][$order['column']];
if(empty($column))
{
continue; // The ordered column does not exist within the columns of the table.
}
$field = $columns->findByName(new StringType($column['name']))->getField();
if($field === '')
{
continue; // No field value was set.
}
$exploded = explode(' ', $field);
foreach($exploded as $section)
{
$orderByClause[] = $section . ' ' . $direction;
}
}
return implode(', ', $orderByClause);
}
/**
* Get the filtering parameters of the request.
*
* Notice: Multiple string values need to be sent as concatenated strings with "||" as the delimiter.
*
* @param DataTableColumnCollection $columns Contains the definitions of the table columns.
*
* @return array
*
* @throws InvalidArgumentException
*/
public function getFilterParameters(DataTableColumnCollection $columns)
{
$filterParameters = [];
foreach($_REQUEST['columns'] as $index => $column)
{
$columnName = $column['name'];
$columnValue = $column['search']['value'];
$datatableColumn = $columns->findByName(new StringType($columnName));
if(empty($datatableColumn) || $columnValue === '' || $columnValue === null || $columnName === 'checkbox'
|| $columnName === 'actions'
)
{
continue; // Column definition was not found or no filter value was provided.
}
switch($datatableColumn->getType())
{
case DataTableColumnType::NUMBER:
case DataTableColumnType::DATE:
$columnValue = str_replace(',', '.', $columnValue);
$filterParameters[$columnName] = strpos($columnValue, '-') ? explode('-',
$columnValue) : $columnValue;
break;
case DataTableColumnType::STRING:
$filterParameters[$columnName] = $columnValue;
break;
}
}
return array_map([$this, '_trimArray'], $filterParameters);
}
/**
* Recursively trim the array values.
*
* @param string|array $entry
*
* @return array|string
*/
protected function _trimArray($entry)
{
if(!is_array($entry))
{
return trim($entry);
}
return array_map(array($this, '_trimArray'), $entry);
}
}