diff --git a/composer.json b/composer.json index db2e0e6..df8ff45 100644 --- a/composer.json +++ b/composer.json @@ -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" + } + ] } diff --git a/src/Chumper/Datatable/Datatable.php b/src/Chumper/Datatable/Datatable.php index 9c16553..7c23b98 100644 --- a/src/Chumper/Datatable/Datatable.php +++ b/src/Chumper/Datatable/Datatable.php @@ -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; } diff --git a/src/Chumper/Datatable/Engines/BaseEngine.php b/src/Chumper/Datatable/Engines/BaseEngine.php index 7b54915..f7ff306 100644 --- a/src/Chumper/Datatable/Engines/BaseEngine.php +++ b/src/Chumper/Datatable/Engines/BaseEngine.php @@ -1,6 +1,6 @@ column + * 1 => name:cast:length + * ) */ protected $orderColumn = null; @@ -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; } @@ -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; } @@ -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++; @@ -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; diff --git a/src/Chumper/Datatable/Engines/CollectionEngine.php b/src/Chumper/Datatable/Engines/CollectionEngine.php index a732767..8811b41 100644 --- a/src/Chumper/Datatable/Engines/CollectionEngine.php +++ b/src/Chumper/Datatable/Engines/CollectionEngine.php @@ -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; @@ -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++; } @@ -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; @@ -174,12 +183,12 @@ 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; } } @@ -187,12 +196,12 @@ private function doInternalSearch(Collection $columns, array $searchColumns) { 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; } } @@ -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) { diff --git a/src/Chumper/Datatable/Engines/QueryEngine.php b/src/Chumper/Datatable/Engines/QueryEngine.php index dda42a3..3f45d60 100644 --- a/src/Chumper/Datatable/Engines/QueryEngine.php +++ b/src/Chumper/Datatable/Engines/QueryEngine.php @@ -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++; diff --git a/src/Chumper/Datatable/Table.php b/src/Chumper/Datatable/Table.php index 1e05543..c977c1c 100644 --- a/src/Chumper/Datatable/Table.php +++ b/src/Chumper/Datatable/Table.php @@ -1,8 +1,9 @@ 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'); } diff --git a/tests/Engines/BaseEngineTest.php b/tests/Engines/BaseEngineTest.php index 65b3f45..3099b1b 100644 --- a/tests/Engines/BaseEngineTest.php +++ b/tests/Engines/BaseEngineTest.php @@ -1,10 +1,10 @@ zeroOrMoreTimes()->with("datatable::engine")->andReturn( + array( + 'exactWordSearch' => false, + ) + ); + $this->collection = new Collection(); $this->engine = new CollectionEngine($this->collection); } diff --git a/tests/Engines/CollectionEngineTest.php b/tests/Engines/CollectionEngineTest.php index cbbb181..2e185a8 100644 --- a/tests/Engines/CollectionEngineTest.php +++ b/tests/Engines/CollectionEngineTest.php @@ -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 { @@ -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' diff --git a/tests/Engines/QueryEngineTest.php b/tests/Engines/QueryEngineTest.php index b5975d4..e9f5739 100644 --- a/tests/Engines/QueryEngineTest.php +++ b/tests/Engines/QueryEngineTest.php @@ -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); diff --git a/tests/TableTest.php b/tests/TableTest.php index c83c764..2e4b3d7 100644 --- a/tests/TableTest.php +++ b/tests/TableTest.php @@ -13,6 +13,22 @@ class TableTest extends PHPUnit_Framework_TestCase { protected function setUp() { parent::setUp(); + + 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->table = new Table(); } @@ -92,7 +108,9 @@ public function testRender() ->with('datatable::template', array( 'options' => array( 'sAjaxSource' => 'fooBar', - 'bServerSide' => true + 'bServerSide' => true, + 'sPaginationType'=>'full_numbers', + 'bProcessing'=>false ), 'callbacks' => array(), 'values' => array(), @@ -100,6 +118,7 @@ public function testRender() 'columns' => array(1=>'foo'), 'noScript' => false, 'class' => $this->table->getClass(), + 'id' => $this->table->getId(), ))->andReturn(true);