Skip to content
This repository has been archived by the owner on Apr 17, 2019. It is now read-only.

Commit

Permalink
fixed #61, fixed #63, fixed #65, fixed testcases to work with the con…
Browse files Browse the repository at this point in the history
…fig option
  • Loading branch information
Chumper committed May 11, 2014
1 parent 3b7404f commit cff8427
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 32 deletions.
13 changes: 10 additions & 3 deletions composer.json
Expand Up @@ -16,16 +16,23 @@
"php": ">=5.3.0",
"illuminate/support": "4.*",
"illuminate/view": "4.*",
"orchestra/testbench": "2.1.*"
"illuminate/config": "4.*"
},
"require-dev": {
"phpunit/phpunit": "3.7.*",
"mockery/mockery": "dev-master"
"mockery/mockery": "dev-master",
"orchestra/testbench": "2.1.*"
},
"autoload": {
"psr-0": {
"Chumper\\Datatable": "src/"
}
},
"minimum-stability": "dev"
"minimum-stability": "dev",
"repositories": [
{
"type": "vcs",
"url": "git://github.com/orchestral/phpseclib.git"
}
]
}
2 changes: 1 addition & 1 deletion src/Chumper/Datatable/Datatable.php
Expand Up @@ -45,7 +45,7 @@ public function table()
public function shouldHandle()
{
$echo = Input::get('sEcho',null);
if(Request::ajax() && !is_null($echo) && is_numeric($echo))
if(/*Request::ajax() && */!is_null($echo) && is_numeric($echo))
{
return true;
}
Expand Down
41 changes: 28 additions & 13 deletions src/Chumper/Datatable/Engines/BaseEngine.php
@@ -1,13 +1,15 @@
<?php namespace Chumper\Datatable\Engines;

use Exception, \Config;
use Exception;
use Assetic\Extension\Twig\AsseticFilterFunction;
use Chumper\Datatable\Columns\DateColumn;
use Chumper\Datatable\Columns\FunctionColumn;
use Chumper\Datatable\Columns\TextColumn;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Config;


/**
* Class BaseEngine
Expand Down Expand Up @@ -86,6 +88,11 @@ abstract class BaseEngine {

/**
* @var null
* Will be an array if order is set
* array(
* 0 => column
* 1 => name:cast:length
* )
*/
protected $orderColumn = null;

Expand Down Expand Up @@ -233,11 +240,8 @@ public function searchColumns($cols)
$cols = func_get_args();
}

$this->searchColumns = array();
$this->searchColumns = $cols;

foreach ($cols as $property) {
$this->searchColumns[] = $property;
}
return $this;
}

Expand All @@ -251,11 +255,7 @@ public function orderColumns($cols)
$cols = func_get_args();
}

$this->orderColumns = array();

foreach ($cols as $property) {
$this->orderColumns[] = $property;
}
$this->orderColumns = $cols;
return $this;
}

Expand Down Expand Up @@ -357,16 +357,30 @@ protected function handleiSortCol_0($value)
//check if order is allowed
if(empty($this->orderColumns))
{
$this->order($value, $direction);
$this->order(array(0 => $value, 1 => $this->getNameByIndex($value)), $direction);
return;
}

//prepare order array
$cleanNames = array();
foreach($this->orderColumns as $c)
{
if(strpos($c,':') !== FALSE)
{
$cleanNames[] = substr($c, 0, strpos($c,':'));
}
else
{
$cleanNames[] = $c;
}
}

$i = 0;
foreach($this->columns as $name => $column)
{
if($i == $value && in_array($name, $this->orderColumns))
if($i == $value && in_array($name, $cleanNames))
{
$this->order($value, $direction);
$this->order(array(0 => $value, 1 => $this->orderColumns[array_search($name,$cleanNames)]), $direction);
return;
}
$i++;
Expand All @@ -381,6 +395,7 @@ protected function handleiSortCol_0($value)
*/
protected function handleSingleColumnSearch($columnIndex, $searchValue)
{
//dd($columnIndex, $searchValue, $this->searchColumns);
if (!isset($this->searchColumns[$columnIndex])) return;
if (empty($searchValue)) return;

Expand Down
27 changes: 18 additions & 9 deletions src/Chumper/Datatable/Engines/CollectionEngine.php
Expand Up @@ -129,7 +129,7 @@ protected function internalMake(Collection $columns, array $searchColumns = arra

private function doInternalSearch(Collection $columns, array $searchColumns)
{
if(is_null($this->search) or empty($this->search))
if((is_null($this->search) || empty($this->search)) && empty($this->fieldSearches))
return;

$value = $this->search;
Expand All @@ -141,9 +141,17 @@ private function doInternalSearch(Collection $columns, array $searchColumns)
$ii = 0;
foreach($columns as $i => $col)
{
if(in_array($columns->get($i)->getName(), $searchColumns))
if(in_array($columns->get($i)->getName(), $searchColumns) || in_array($columns->get($i)->getName(), $this->fieldSearches))
{
$toSearch[] = $ii;
// map values to columns, where there is no value use the global value
if(($field = array_search($columns->get($i)->getName(), $this->fieldSearches)) !== FALSE)
{
$toSearch[$ii] = $this->columnSearches[$field];
}
else
{
$toSearch[$ii] = $value;
}
}
$ii++;
}
Expand All @@ -153,7 +161,8 @@ private function doInternalSearch(Collection $columns, array $searchColumns)
{
for($i = 0; $i < count($row); $i++)
{
if(!in_array($i, $toSearch))
//$toSearch[$i] = value
if(!array_key_exists($i, $toSearch))
continue;

$column = $i;
Expand All @@ -174,25 +183,25 @@ private function doInternalSearch(Collection $columns, array $searchColumns)
{
if($self->exactWordSearch)
{
if($value === $search)
if($toSearch[$i] === $search)
return true;
}
else
{
if(str_contains($search,$value))
if(str_contains($search,$toSearch[$i]))
return true;
}
}
else
{
if($self->getExactWordSearch())
{
if(strtolower($value) === strtolower($search))
if(mb_strtolower($toSearch[$i]) === mb_strtolower($search))
return true;
}
else
{
if(str_contains(strtolower($search),strtolower($value)))
if(str_contains(mb_strtolower($search),mb_strtolower($toSearch[$i])))
return true;
}
}
Expand All @@ -205,7 +214,7 @@ private function doInternalOrder()
if(is_null($this->orderColumn))
return;

$column = $this->orderColumn;
$column = $this->orderColumn[0];
$stripOrder = $this->options['stripOrder'];
$self = $this;
$this->workingCollection->sortBy(function($row) use ($column,$stripOrder,$self) {
Expand Down
13 changes: 11 additions & 2 deletions src/Chumper/Datatable/Engines/QueryEngine.php
Expand Up @@ -216,14 +216,23 @@ private function compile($builder, $columns)

private function doInternalOrder($builder, $columns)
{
//var_dump($this->orderColumn);
if(!is_null($this->orderColumn))
{
$i = 0;
foreach($columns as $col)
{
if($i === (int) $this->orderColumn)

if($i === (int) $this->orderColumn[0])
{
$builder = $builder->orderBy($col->getName(), $this->orderDirection);
if(strrpos($this->orderColumn[1], ':')){
$c = explode(':', $this->orderColumn[1]);
if(isset($c[2]))
$c[1] .= "($c[2])";
$builder = $builder->orderByRaw("cast($c[0] as $c[1]) ".$this->orderDirection);
}
else
$builder = $builder->orderBy($col->getName(), $this->orderDirection);
return $builder;
}
$i++;
Expand Down
3 changes: 2 additions & 1 deletion src/Chumper/Datatable/Table.php
@@ -1,8 +1,9 @@
<?php namespace Chumper\Datatable;

use Exception, Config;
use Exception;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Config;

/**
* Class Table
Expand Down
22 changes: 22 additions & 0 deletions tests/DatatableTest.php
Expand Up @@ -2,6 +2,7 @@

use Chumper\Datatable\Datatable;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;

class DatatableTest extends PHPUnit_Framework_TestCase {

Expand All @@ -12,6 +13,27 @@ class DatatableTest extends PHPUnit_Framework_TestCase {

protected function setUp()
{
// set up config
Config::shouldReceive('get')->zeroOrMoreTimes()->with("datatable::engine")->andReturn(
array(
'exactWordSearch' => false,
)
);
Config::shouldReceive('get')->zeroOrMoreTimes()->with("datatable::table")->andReturn(
array(
'class' => 'table table-bordered',
'id' => '',
'options' => array(
"sPaginationType" => "full_numbers",
"bProcessing" => false
),
'callbacks' => array(),
'noScript' => false,
'table_view' => 'datatable::template',
'script_view' => 'datatable::javascript',
)
);

$this->dt = new Datatable;
$this->mock = Mockery::mock('Illuminate\Database\Query\Builder');
}
Expand Down
9 changes: 8 additions & 1 deletion tests/Engines/BaseEngineTest.php
@@ -1,10 +1,10 @@
<?php

use Chumper\Datatable\Columns\TextColumn;
use Chumper\Datatable\Engines\CollectionEngine;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Input;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Config;

class BaseEngineTest extends TestCase {

Expand All @@ -17,6 +17,13 @@ class BaseEngineTest extends TestCase {

public function setUp()
{
// set up config
Config::shouldReceive('get')->zeroOrMoreTimes()->with("datatable::engine")->andReturn(
array(
'exactWordSearch' => false,
)
);

$this->collection = new Collection();
$this->engine = new CollectionEngine($this->collection);
}
Expand Down
14 changes: 13 additions & 1 deletion tests/Engines/CollectionEngineTest.php
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Input;
use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Config;

class CollectionEngineTest extends TestCase {

Expand All @@ -25,15 +26,26 @@ class CollectionEngineTest extends TestCase {

public function setUp()
{
Config::shouldReceive('get')->zeroOrMoreTimes()->with("datatable::engine")->andReturn(
array(
'exactWordSearch' => false,
)
);

parent::setUp();

Config::shouldReceive('get')->zeroOrMoreTimes()->with("datatable::engine")->andReturn(
array(
'exactWordSearch' => false,
)
);

$this->collection = Mockery::mock('Illuminate\Support\Collection');
$this->c = new CollectionEngine($this->collection);
}

public function testOrder()
{

$should = array(
array(
'id' => 'eoo'
Expand Down
7 changes: 7 additions & 0 deletions tests/Engines/QueryEngineTest.php
Expand Up @@ -20,6 +20,13 @@ class QueryEngineTest extends PHPUnit_Framework_TestCase {

public function setUp()
{

Config::shouldReceive('get')->zeroOrMoreTimes()->with("datatable::engine")->andReturn(
array(
'exactWordSearch' => false,
)
);

$this->builder = Mockery::mock('Illuminate\Database\Query\Builder');

$this->c = new QueryEngine($this->builder);
Expand Down

0 comments on commit cff8427

Please sign in to comment.