Skip to content

Commit

Permalink
remove direct access to $_POST, use symfony parameter bag instead.
Browse files Browse the repository at this point in the history
remove hardcoded dependencies
  • Loading branch information
ikhsan017 committed Dec 7, 2015
1 parent 4948844 commit ef3d9c9
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 93 deletions.
122 changes: 62 additions & 60 deletions src/LiveControl/EloquentDataTable/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,46 @@
use Exception;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Connection;
use Illuminate\Support\Str;
use Illuminate\Database\Query\Expression as raw;
use Symfony\Component\HttpFoundation\ParameterBag;
use LiveControl\EloquentDataTable\VersionTransformers\Version110Transformer;
use LiveControl\EloquentDataTable\VersionTransformers\VersionTransformerContract;


class DataTable
{
private $builder;
private $columns;
private $formatRowFunction;
protected $str;
protected $builder;
protected $columns;
protected $connection;
protected $rowFormatter;

/**
* @var VersionTransformerContract
*/
protected static $versionTransformer;
protected $versionTransformer;

private $rawColumns;
private $columnNames;
protected $rawColumns;
protected $columnNames;

private $total = 0;
private $filtered = 0;
protected $total = 0;
protected $filtered = 0;

private $rows = [];
protected $rows = [];

/**
* @param Builder|Model $builder
* @param array $columns
* @param null|callable $formatRowFunction
* @param null|callable $rowFormatter
* @throws Exception
*/
public function __construct($builder, $columns, $formatRowFunction = null)
public function __construct(Connection $connection, $builder, ParameterBag $param, Str $str)
{
$this->str = $str;
$this->param = $param;
$this->connection = $connection;
$this->setBuilder($builder);
$this->setColumns($columns);

if ($formatRowFunction !== null) {
$this->setFormatRowFunction($formatRowFunction);
}
}

/**
Expand All @@ -51,7 +53,7 @@ public function __construct($builder, $columns, $formatRowFunction = null)
*/
public function setBuilder($builder)
{
if ( ! ($builder instanceof Builder || $builder instanceof Model)) {
if (!($builder instanceof Builder || $builder instanceof Model)) {
throw new Exception('$builder variable is not an instance of Builder or Model.');
}

Expand All @@ -73,9 +75,9 @@ public function setColumns($columns)
* @param callable $function
* @return $this
*/
public function setFormatRowFunction($function)
public function setRowFormatter($function)
{
$this->formatRowFunction = $function;
$this->rowFormatter = $function;
return $this;
}

Expand All @@ -85,7 +87,7 @@ public function setFormatRowFunction($function)
*/
public function setVersionTransformer(VersionTransformerContract $versionTransformer)
{
static::$versionTransformer = $versionTransformer;
$this->versionTransformer = $versionTransformer;
return $this;
}

Expand All @@ -101,8 +103,8 @@ public function make()
$this->rawColumns = $this->getRawColumns($this->columns);
$this->columnNames = $this->getColumnNames();

if (static::$versionTransformer === null) {
static::$versionTransformer = new Version110Transformer();
if ($this->versionTransformer === null) {
$this->versionTransformer = new Version110Transformer();
}

$this->addSelect();
Expand All @@ -120,25 +122,25 @@ public function make()
$rows[] = $this->formatRow($row);
}

$drawKey = $this->versionTransformer->transform('draw');

return [
static::$versionTransformer->transform('draw') => (isset($_POST[static::$versionTransformer->transform(
'draw'
)]) ? (int)$_POST[static::$versionTransformer->transform('draw')] : 0),
static::$versionTransformer->transform('recordsTotal') => $this->total,
static::$versionTransformer->transform('recordsFiltered') => $this->filtered,
static::$versionTransformer->transform('data') => $rows
$this->versionTransformer->transform('draw') => $this->param->get($drawKey, 0),
$this->versionTransformer->transform('recordsTotal') => $this->total,
$this->versionTransformer->transform('recordsFiltered') => $this->filtered,
$this->versionTransformer->transform('data') => $rows
];
}

/**
* @param $data
* @return array|mixed
*/
private function formatRow($data)
protected function formatRow($data)
{
// if we have a custom format row function we trigger it instead of the default handling.
if ($this->formatRowFunction !== null) {
$function = $this->formatRowFunction;
if ($this->rowFormatter !== null) {
$function = $this->rowFormatter;

return call_user_func($function, $data);
}
Expand All @@ -158,18 +160,18 @@ private function formatRow($data)
* @param $data
* @return mixed
*/
private function formatRowIndexes($data)
protected function formatRowIndexes($data)
{
if (isset($data['id'])) {
$data[static::$versionTransformer->transform('DT_RowId')] = $data['id'];
$data[$this->versionTransformer->transform('DT_RowId')] = $data['id'];
}
return $data;
}

/**
* @return array
*/
private function getColumnNames()
protected function getColumnNames()
{
$names = [];
foreach ($this->columns as $index => $column) {
Expand All @@ -191,7 +193,7 @@ private function getColumnNames()
* @param $columns
* @return array
*/
private function getRawColumns($columns)
protected function getRawColumns($columns)
{
$rawColumns = [];
foreach ($columns as $column) {
Expand All @@ -204,7 +206,7 @@ private function getRawColumns($columns)
* @param $column
* @return raw|string
*/
private function getRawColumnQuery($column)
protected function getRawColumnQuery($column)
{
if ($column instanceof ExpressionWithName) {
return $column->getExpression();
Expand All @@ -217,26 +219,26 @@ private function getRawColumnQuery($column)
return 'CONCAT(' . implode(', " ", ', $this->getRawColumns($column)) . ')';
}

return Model::resolveConnection()->getQueryGrammar()->wrap($column);
return $this->connection->getQueryGrammar()->wrap($column);
}

/**
* @return string
*/
private function getDatabaseDriver()
protected function getDatabaseDriver()
{
return Model::resolveConnection()->getDriverName();
return $this->connection->getDriverName();
}

/**
*
*/
private function addSelect()
protected function addSelect()
{
$rawSelect = [];
foreach ($this->columns as $index => $column) {
if (isset($this->rawColumns[$index])) {
$rawSelect[] = $this->rawColumns[$index] . ' as ' . Model::resolveConnection()->getQueryGrammar()->wrap($this->columnNames[$index]);
$rawSelect[] = $this->rawColumns[$index] . ' as ' . $this->connection->getQueryGrammar()->wrap($this->columnNames[$index]);
}
}
$this->builder = $this->builder->select(new raw(implode(', ', $rawSelect)));
Expand All @@ -247,7 +249,7 @@ private function addSelect()
* @param bool|false $inForeach
* @return array|string
*/
private function arrayToCamelcase($array, $inForeach = false)
protected function arrayToCamelcase($array, $inForeach = false)
{
$result = [];
foreach ($array as $value) {
Expand All @@ -259,16 +261,16 @@ private function arrayToCamelcase($array, $inForeach = false)
$result[] = $value;
}

return (! $inForeach ? camel_case(implode('_', $result)) : $result);
return $inForeach ? $result : $this->str-camel(implode('_', $result));
}

/**
* Add the filters based on the search value given.
* @return $this
*/
private function addFilters()
protected function addFilters()
{
$search = static::$versionTransformer->getSearchValue();
$search = $this->versionTransformer->getSearchValue();
if ($search != '') {
$this->addAllFilter($search);
}
Expand All @@ -280,7 +282,7 @@ private function addFilters()
* Searches in all the columns.
* @param $search
*/
private function addAllFilter($search)
protected function addAllFilter($search)
{
$this->builder = $this->builder->where(
function ($query) use ($search) {
Expand All @@ -298,14 +300,14 @@ function ($query) use ($search) {
/**
* Add column specific filters.
*/
private function addColumnFilters()
protected function addColumnFilters()
{
foreach ($this->columns as $i => $column) {
if (static::$versionTransformer->isColumnSearched($i)) {
if ($this->versionTransformer->isColumnSearched($i)) {
$this->builder->where(
new raw($this->getRawColumnQuery($column)),
'like',
'%' . static::$versionTransformer->getColumnSearchValue($i) . '%'
'%' . $this->versionTransformer->getColumnSearchValue($i) . '%'
);
}
}
Expand All @@ -316,8 +318,8 @@ private function addColumnFilters()
*/
protected function addOrderBy()
{
if (static::$versionTransformer->isOrdered()) {
foreach (static::$versionTransformer->getOrderedColumns() as $index => $direction) {
if ($this->versionTransformer->isOrdered()) {
foreach ($this->versionTransformer->getOrderedColumns() as $index => $direction) {
if (isset($this->columnNames[$index])) {
$this->builder->orderBy(
$this->columnNames[$index],
Expand All @@ -331,15 +333,15 @@ protected function addOrderBy()
/**
* Adds the pagination limits to the builder
*/
private function addLimits()
protected function addLimits()
{
if (isset($_POST[static::$versionTransformer->transform(
'start'
)]) && $_POST[static::$versionTransformer->transform('length')] != '-1'
) {
$this->builder->skip((int)$_POST[static::$versionTransformer->transform('start')])->take(
(int)$_POST[static::$versionTransformer->transform('length')]
);
$start = $this->versionTransformer->transform('start');
$length = $this->versionTransformer->transform('length');

if ($this->param->get($start) && $this->param->get($length) != '-1') {
$this->builder
->skip((int) $this->param->get($start))
->take((int) $this->param->get($length));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,66 @@
<?php

namespace LiveControl\EloquentDataTable\VersionTransformers;

use Symfony\Component\HttpFoundation\ParameterBag;

class Version109Transformer implements VersionTransformerContract
{
private $translate = [
'draw' => 'sEcho',
'recordsTotal' => 'iTotalRecords',
'recordsFiltered' => 'iTotalDisplayRecords',
'data' => 'aaData',
'start' => 'iDisplayStart',
'length' => 'iDisplayLength',
protected $param;

protected static $translate = [
'draw' => 'sEcho',
'recordsTotal' => 'iTotalRecords',
'recordsFiltered' => 'iTotalDisplayRecords',
'data' => 'aaData',
'start' => 'iDisplayStart',
'length' => 'iDisplayLength',

];

public function __construct(ParameterBag $param)
{
$this->param = $param;
}

public function transform($name)
{
return (isset($this->translate[$name]) ? $this->translate[$name] : $name);
return (isset(static::$translate[$name]) ? static::$translate[$name] : $name);
}

public function getSearchValue()
{
if(isset($_POST['sSearch']))
return $_POST['sSearch'];
return '';
return $this->param->get('sSearch', '');
}

public function isColumnSearched($i)
{
return (isset($_POST['bSearchable_' . $i]) && $_POST['bSearchable_' . $i] == 'true' && $_POST['sSearch_' . $i] != '');
return ($this->param->get('bSearchable_' . $i) && $this->param->get('bSearchable_' . $i) == 'true' && $this->param->get('sSearch_' . $i) != '');
}

public function getColumnSearchValue($i)
{
return $_POST['sSearch_' . $i];
return $this->param->get('sSearch_' . $i);
}

public function isOrdered()
{
return isset($_POST['iSortCol_0']);
return (bool) $this->param->get('iSortCol_0');
}

public function getOrderedColumns()
{
$columns = [];
for ($i = 0; $i < (int) $_POST['iSortingCols']; $i ++) {
if ( $_POST['bSortable_' . ((int) $_POST['iSortCol_' . $i])] == 'true' ) {
$columns[(int) $_POST['iSortCol_' . $i]] = ($_POST['sSortDir_' . $i] == 'asc' ? 'asc' : 'desc');
$columns = [];
$sortingCols= (int) $this->param->get('iSortingCols');

for ($i = 0; $i < $sortingCols; $i ++) {
$sortCol = (int) $this->param->get('iSortCol_' . $i);

if ($this->param->get('bSortable_' . $sortCol) == 'true') {
$columns[$sortCol] = $this->param->get('sSortDir_' . $i) == 'asc' ? 'asc' : 'desc';
}
}

return $columns;
}
}
}
Loading

0 comments on commit ef3d9c9

Please sign in to comment.