From baf9281468bd787c51b5374a179ee27c90d9d0c1 Mon Sep 17 00:00:00 2001 From: Todd Date: Thu, 21 Nov 2013 12:27:05 -0500 Subject: [PATCH] Added clean columns function --- src/Bllim/Datatables/Datatables.php | 46 ++++++++++++++++++++++++----- 1 file changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Bllim/Datatables/Datatables.php b/src/Bllim/Datatables/Datatables.php index 033507a2..6192f91c 100644 --- a/src/Bllim/Datatables/Datatables.php +++ b/src/Bllim/Datatables/Datatables.php @@ -28,10 +28,10 @@ class Datatables protected $edit_columns = array(); protected $sColumns = array(); - public $columns = array(); + public $columns = array(); public $last_columns = array(); - protected $count_all = 0; + protected $count_all = 0; protected $result_object; protected $result_array = array(); @@ -342,19 +342,48 @@ private function ordering() if(!is_null(Input::get('iSortCol_0'))) { + $columns = $this->cleanColumns( $this->last_columns ); for ( $i=0, $c=intval(Input::get('iSortingCols')); $i<$c ; $i++ ) { if ( Input::get('bSortable_'.intval(Input::get('iSortCol_'.$i))) == "true" ) { - if(isset($this->last_columns[intval(Input::get('iSortCol_'.$i))])) - $this->query->orderBy($this->last_columns[intval(Input::get('iSortCol_'.$i))],Input::get('sSortDir_'.$i)); + if(isset($columns[intval(Input::get('iSortCol_'.$i))])) + $this->query->orderBy($columns[intval(Input::get('iSortCol_'.$i))],Input::get('sSortDir_'.$i)); } } } } + private function cleanColumns( $cols ) + { + $_search = [ + 'GROUP_CONCAT( ', + 'CONCAT( ', + 'DISTINCT( ', + ',', + ' )', + 'as', + ]; + + foreach ( $cols as $col ) + { + $_column = explode( ' ' , str_replace( $_search, '', $col, $count ) ); + + if ( $count > 0 ) + { + $columns[] = array_shift( $_column ); + } + else + { + $columns[] = end( $_column ); + } + } + + return $columns; + } + /** * Datatable filtering * @@ -363,11 +392,12 @@ private function ordering() private function filtering() { - + $columns = $this->cleanColumns( $this->columns ); if (Input::get('sSearch','') != '') { $copy_this = $this; + $copy_this->columns = $columns; $this->query->where(function($query) use ($copy_this) { @@ -416,7 +446,7 @@ private function filtering() $db_prefix = $this->database_prefix(); - for ($i=0,$c=count($this->columns);$i<$c;$i++) + for ($i=0,$c=count($columns);$i<$c;$i++) { if (Input::get('bSearchable_'.$i) == "true" && Input::get('sSearch_'.$i) != '') { @@ -427,10 +457,10 @@ private function filtering() } if(Config::get('datatables.search.case_insensitive', false)) { - $column = $db_prefix . $this->columns[$i]; + $column = $db_prefix . $columns[$i]; $this->query->where(DB::raw('LOWER('.$column.')'),'LIKE', $keyword); } else { - $this->query->where($this->columns[$i], 'LIKE', $keyword); + $this->query->where($columns[$i], 'LIKE', $keyword); } } }