Skip to content

Commit a9c75f2

Browse files
committed
Merge pull request bgultekin#68 from yajra/bllim
case insensitive filtering with config and unit testing added
2 parents 0c21b70 + ccd7bb5 commit a9c75f2

File tree

9 files changed

+303
-23
lines changed

9 files changed

+303
-23
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,17 @@ Composer will download the package. After package downloaded, open "app/config/a
3131
),
3232

3333

34-
34+
3535
'aliases' => array(
3636
...
3737
'Datatables' => 'Bllim\Datatables\Datatables',
3838
),
3939

40+
Finally you need to publish a configuration file by running the following Artisan command.
41+
42+
```php
43+
$ php artisan config:publish bllim/datatables
44+
```
4045

4146
### Usage
4247

@@ -49,7 +54,7 @@ It is better, you know these:
4954
- You can remove any column by using remove_column($column) method
5055
- You can add columns by using add_column($column_name, $content, $order)
5156
- You can use Blade Template Engine in your $content values
52-
- The name of columns is set by returned array.
57+
- The name of columns is set by returned array.
5358
- That means, for 'posts.id' it is 'id' and also for 'owner.name as ownername' it is 'ownername'
5459

5560

@@ -72,18 +77,18 @@ It is better, you know these:
7277
->add_column('operations','<a href="{{ URL::route( \'admin.post\', array( \'edit\',$id )) }}">edit</a>
7378
<a href="{{ URL::route( \'admin.post\', array( \'delete\',$id )) }}">delete</a>
7479
')
75-
->edit_column('status','@if($status)
76-
Active
77-
@else
78-
Passive
80+
->edit_column('status','@if($status)
81+
Active
82+
@else
83+
Passive
7984
@endif')
8085
// you can also give a function as parameter to edit_column and add_column instead of blade string
8186
->edit_column('ownername','Author of this post is {{ $ownername }}')
8287
->remove_column('id')
8388
->make();
8489

85-
**Notice:** If you use double quotes while giving content of add_column or edit_column, you should escape variables with backslash (\) else you get error. For example:
86-
90+
**Notice:** If you use double quotes while giving content of add_column or edit_column, you should escape variables with backslash (\) else you get error. For example:
91+
8792
edit_column('id',"- {{ \$id }}") .
8893

8994

composer.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
"require": {
1313
"php": ">=5.3.0",
1414
"illuminate/support": ">=4.0.0",
15+
"illuminate/database": ">=4.0.0",
1516
"illuminate/view": ">=4.0.0",
1617
"illuminate/filesystem": ">=4.0.0"
1718
},
19+
"require-dev": {
20+
"laravel/laravel": ">=4.0.0",
21+
"mockery/mockery": "0.7.2",
22+
"phpunit/phpunit": "3.7.*"
23+
},
1824
"autoload": {
1925
"classmap": [
2026
"src/migrations"

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<phpunit backupGlobals="false"
33
backupStaticAttributes="false"
4-
bootstrap="vendor/autoload.php"
4+
bootstrap="tests/bootstrap/autoload.php"
55
colors="true"
66
convertErrorsToExceptions="true"
77
convertNoticesToExceptions="true"

src/Bllim/Datatables/Datatables.php

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ private function clean_columns( $cols, $use_alias = true)
369369
foreach ( $cols as $i=> $col )
370370
{
371371
preg_match('#^(.*?)\s+as\s+(\S*?)$#si',$col,$matches);
372-
$return[$i] = empty($matches) ? $col : $matches[$use_alias?2:1];
372+
$return[$i] = empty($matches) ? $col : $matches[$use_alias?2:1];
373373
}
374374

375375
return $return;
@@ -384,7 +384,7 @@ private function clean_columns( $cols, $use_alias = true)
384384
private function filtering()
385385
{
386386
$columns = $this->clean_columns( $this->columns, false );
387-
387+
388388
if (Input::get('sSearch','') != '')
389389
{
390390
$copy_this = $this;
@@ -393,25 +393,25 @@ private function filtering()
393393
$this->query->where(function($query) use ($copy_this) {
394394

395395
$db_prefix = $copy_this->database_prefix();
396-
397-
396+
397+
398398

399399
for ($i=0,$c=count($copy_this->columns);$i<$c;$i++)
400400
{
401401
if (Input::get('bSearchable_'.$i) == "true")
402402
{
403403
$column = $copy_this->columns[$i];
404-
405-
if (stripos($column, ' AS ') !== false){
404+
405+
if (stripos($column, ' AS ') !== false){
406406
$column = substr($column, stripos($column, ' AS ')+4);
407407
}
408-
408+
409409
$keyword = '%'.Input::get('sSearch').'%';
410410

411411
if(Config::get('datatables.search.use_wildcards', false)) {
412412
$keyword = $copy_this->wildcard_like_string(Input::get('sSearch'));
413413
}
414-
414+
415415
// Check if the database driver is PostgreSQL
416416
// If it is, cast the current column to TEXT datatype
417417
$cast_begin = null;
@@ -420,10 +420,10 @@ private function filtering()
420420
$cast_begin = "CAST(";
421421
$cast_end = " as TEXT)";
422422
}
423-
423+
424424
$column = $db_prefix . $column;
425425
if(Config::get('datatables.search.case_insensitive', false)) {
426-
$query->orwhere(DB::raw('LOWER('.$cast_begin.$column.$cast_end.')'), 'LIKE', $keyword);
426+
$query->orwhere(DB::raw('LOWER('.$cast_begin.$column.$cast_end.')'), 'LIKE', strtolower($keyword));
427427
} else {
428428
$query->orwhere(DB::raw($cast_begin.$column.$cast_end), 'LIKE', $keyword);
429429
}
@@ -432,7 +432,7 @@ private function filtering()
432432
});
433433

434434
}
435-
435+
436436
$db_prefix = $this->database_prefix();
437437

438438
for ($i=0,$c=count($columns);$i<$c;$i++)
@@ -447,7 +447,7 @@ private function filtering()
447447

448448
if(Config::get('datatables.search.case_insensitive', false)) {
449449
$column = $db_prefix . $columns[$i];
450-
$this->query->where(DB::raw('LOWER('.$column.')'),'LIKE', $keyword);
450+
$this->query->where(DB::raw('LOWER('.$column.')'),'LIKE', strtolower($keyword));
451451
} else {
452452
$col = strstr($columns[$i],'(')?DB::raw($columns[$i]):$columns[$i];
453453
$this->query->where($col, 'LIKE', $keyword);
@@ -546,8 +546,8 @@ private function output()
546546
"sColumns" => $sColumns
547547
);
548548

549-
if(Config::get('application.profiler', false)) {
550-
Log::write('$this->result_array', '<pre>'.print_r($this->result_array, true).'</pre>');
549+
if(Config::get('app.debug', false)) {
550+
$output['aQueries'] = DB::getQueryLog();
551551
}
552552
return Response::json($output);
553553
}

src/config/datatables.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
return array(
4+
5+
'search' => array(
6+
'case_insensitive' => true,
7+
'use_wildcards' => false,
8+
)
9+
10+
);

tests/Datatables.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
use Mockery as m;
4+
use Bllim\Datatables\Datatables as Datatables;
5+
6+
/**
7+
* Demo Table
8+
*/
9+
class Demo extends Illuminate\Database\Eloquent\Model {}
10+
11+
class DatatablesTest extends Illuminate\Foundation\Testing\TestCase {
12+
13+
public function createApplication()
14+
{
15+
$unitTesting = true;
16+
17+
$testEnvironment = 'testing';
18+
19+
return require __DIR__.'/bootstrap/start.php';
20+
}
21+
22+
public function setUp()
23+
{
24+
parent::setUp();
25+
26+
Config::set('database.default', 'sqlite');
27+
Config::set('database.sqlite.database', ':memory:');
28+
29+
Schema::dropIfExists('demos');
30+
Schema::create('demos', function($table){
31+
$table->increments('id');
32+
$table->string('name');
33+
});
34+
Demo::insert(array('name'=>'demo datatables'));
35+
}
36+
37+
public function tearDown()
38+
{
39+
Schema::dropIfExists('demos');
40+
}
41+
42+
public function test_demo_count()
43+
{
44+
$this->assertEquals(1, Demo::count());
45+
}
46+
47+
public function test_datatables_make_function()
48+
{
49+
$demo = DB::table('demos')->select('id','name');
50+
$output = Datatables::of($demo)->make();
51+
$this->assertInstanceOf('Illuminate\Http\JsonResponse', $output);
52+
}
53+
54+
55+
}

tests/bootstrap/autoload.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?php
2+
3+
define('LARAVEL_START', microtime(true));
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Register The Composer Auto Loader
8+
|--------------------------------------------------------------------------
9+
|
10+
| Composer provides a convenient, automatically generated class loader
11+
| for our application. We just need to utilize it! We'll require it
12+
| into the script here so that we do not have to worry about the
13+
| loading of any our classes "manually". Feels great to relax.
14+
|
15+
*/
16+
17+
require __DIR__.'/../../vendor/autoload.php';
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| Include The Compiled Class File
22+
|--------------------------------------------------------------------------
23+
|
24+
| To dramatically increase your application's performance, you may use a
25+
| compiled class file which contains all of the classes commonly used
26+
| by a request. The Artisan "optimize" is used to create this file.
27+
|
28+
*/
29+
30+
if (file_exists($compiled = __DIR__.'/compiled.php'))
31+
{
32+
require $compiled;
33+
}
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| Setup Patchwork UTF-8 Handling
38+
|--------------------------------------------------------------------------
39+
|
40+
| The Patchwork library provides solid handling of UTF-8 strings as well
41+
| as provides replacements for all mb_* and iconv type functions that
42+
| are not available by default in PHP. We'll setup this stuff here.
43+
|
44+
*/
45+
46+
Patchwork\Utf8\Bootup::initMbstring();
47+
48+
/*
49+
|--------------------------------------------------------------------------
50+
| Register The Laravel Auto Loader
51+
|--------------------------------------------------------------------------
52+
|
53+
| We register an auto-loader "behind" the Composer loader that can load
54+
| model classes on the fly, even if the autoload files have not been
55+
| regenerated for the application. We'll add it to the stack here.
56+
|
57+
*/
58+
59+
Illuminate\Support\ClassLoader::register();
60+
61+
/*
62+
|--------------------------------------------------------------------------
63+
| Register The Workbench Loaders
64+
|--------------------------------------------------------------------------
65+
|
66+
| The Laravel workbench provides a convenient place to develop packages
67+
| when working locally. However we will need to load in the Composer
68+
| auto-load files for the packages so that these can be used here.
69+
|
70+
*/
71+
72+
if (is_dir($workbench = __DIR__.'/../../workbench'))
73+
{
74+
Illuminate\Workbench\Starter::start($workbench);
75+
}

tests/bootstrap/paths.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
return array(
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Application Path
8+
|--------------------------------------------------------------------------
9+
|
10+
| Here we just defined the path to the application directory. Most likely
11+
| you will never need to change this value as the default setup should
12+
| work perfectly fine for the vast majority of all our applications.
13+
|
14+
*/
15+
16+
'app' => __DIR__.'/../../vendor/laravel/laravel/app',
17+
18+
/*
19+
|--------------------------------------------------------------------------
20+
| Public Path
21+
|--------------------------------------------------------------------------
22+
|
23+
| The public path contains the assets for your web application, such as
24+
| your JavaScript and CSS files, and also contains the primary entry
25+
| point for web requests into these applications from the outside.
26+
|
27+
*/
28+
29+
'public' => __DIR__.'/../../vendor/laravel/laravel/public',
30+
31+
/*
32+
|--------------------------------------------------------------------------
33+
| Base Path
34+
|--------------------------------------------------------------------------
35+
|
36+
| The base path is the root of the Laravel installation. Most likely you
37+
| will not need to change this value. But, if for some wild reason it
38+
| is necessary you will do so here, just proceed with some caution.
39+
|
40+
*/
41+
42+
'base' => __DIR__.'/../..',
43+
44+
/*
45+
|--------------------------------------------------------------------------
46+
| Storage Path
47+
|--------------------------------------------------------------------------
48+
|
49+
| The storage path is used by Laravel to store cached Blade views, logs
50+
| and other pieces of information. You may modify the path here when
51+
| you want to change the location of this directory for your apps.
52+
|
53+
*/
54+
55+
'storage' => __DIR__.'/../../vendor/laravel/laravel/app/storage',
56+
57+
);

0 commit comments

Comments
 (0)