Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Commit

Permalink
updated to latest
Browse files Browse the repository at this point in the history
  • Loading branch information
cnizzardini committed Jul 2, 2014
1 parent 3f25a31 commit a604f47
Showing 1 changed file with 52 additions and 45 deletions.
97 changes: 52 additions & 45 deletions test/app/Controller/Component/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
* @author chris
* @package DataTableComponent
* @link http://www.datatables.net/release-datatables/examples/server_side/server_side.html parts of code borrowed from dataTables example
* @since version 1.2.1
* Copyright (c) 2013 Chris Nizzardini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
* @since version 1.1.1
Copyright (c) 2013 Chris Nizzardini
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class DataTableComponent extends Component{

Expand All @@ -46,20 +46,6 @@ public function initialize(Controller $controller){
$this->model = $this->controller->{$modelName};
}

/**
* returns true if get request has any searching conditions
* @param object $httpGet
* @return bool
*/
public function whereConditions($httpGet){
foreach($httpGet as $request){
if(strpos($request,'sSearch') === false && !empty($request)) {
return true;
}
}
return false;
}

/**
* returns dataTables compatible array - just json_encode the resulting aray
* @param object $controller optional
Expand Down Expand Up @@ -109,8 +95,9 @@ public function getResponse($controller = null, $model=null){
}

// check for WHERE statement in GET request
if(isset($httpGet) && $this->whereConditions($httpGet)){
if( isset($httpGet) && $this->isSearchable($model) == true ){
$conditions = $this->getWhereConditions();

if( !empty($this->controller->paginate['contain']) ){
$this->controller->paginate = array_merge_recursive($this->controller->paginate, array('contain'=>$conditions));
}
Expand Down Expand Up @@ -146,6 +133,7 @@ public function getResponse($controller = null, $model=null){

// execute sql select
$data = $this->model->find('all', $parameters);

$this->setTimes('Find','stop');
$this->setTimes('Response','start','Formatting of response');
// dataTables compatible array
Expand Down Expand Up @@ -214,6 +202,22 @@ private function getOrderByStatements(){
return $orderBy;
}


/**
* returns true if get request has any searching conditions
* @param object $httpGet
* @return bool
*/
public function isSearchable($httpGet){
foreach($httpGet as $request){
if(strpos($request,'sSearch') === false && !empty($request)) {
return true;
}
}
return false;
}


/**
* returns sql conditions array after converting dataTables GET request into Cake style conditions
* will only search on fields with bSearchable set to true (which is the default value for bSearchable)
Expand All @@ -228,7 +232,6 @@ private function getWhereConditions(){

$conditions = array();


if($this->mDataProp == true){
for($i=0;$i<$this->controller->request->query['iColumns'];$i++){
if(!isset($this->controller->request->query['bSearchable_'.$i]) || $this->controller->request->query['bSearchable_'.$i] == true){
Expand All @@ -241,21 +244,24 @@ private function getWhereConditions(){
}

foreach($fields as $x => $column){
// only create conditions on bSearchable fields
if (isset($this->controller->request->query['bSearchable_'.$x])) { // check if the parameter exists in query.

// only create conditions on bSearchable fields (assume true if bSearchable is not set)
$bSearchable = isset($this->controller->request->query['bSearchable_'.$x]) ? $this->controller->request->query['bSearchable_'.$x] : 'true';
if( $bSearchable == 'true' ){

if($this->mDataProp == true){
if (!empty($this->controller->request->query['sSearch'])){
// check if table-wide search term was passed over - if so then add a condition for this field
if( isset($this->controller->request->query['sSearch']) && !empty($this->controller->request->query['sSearch']) ){
$conditions['OR'][] = array(
$this->controller->request->query['mDataProp_'.$x].' LIKE' => '%'.$this->controller->request->query['sSearch'].'%'
);
);
}
//check if specific column (i.e. sSearch_x) is empty, add it to the query if so
if(!empty($this->controller->request->query['sSearch_'.$x])){
if( isset($this->controller->request->query['sSearch_'.$x]) && !empty($this->controller->request->query['sSearch_'.$x])){
$conditions['OR'][] = array(
$this->controller->request->query['mDataProp_'.$x].' LIKE' => '%'.$this->controller->request->query['sSearch_'.$x].'%'
);
}

}
}
else{

Expand All @@ -274,6 +280,7 @@ private function getWhereConditions(){
}
}
else{

if( !empty($this->controller->paginate['contain']) ){
if(array_key_exists($table, $this->controller->paginate['contain']) && in_array($field, $this->controller->paginate['contain'][$table]['fields'])){
$conditions[$table]['conditions'][] = $column.' LIKE "%'.$this->controller->request->query['sSearch'].'%"';
Expand All @@ -286,7 +293,7 @@ private function getWhereConditions(){
}
}
}
}
}
}
return $conditions;
}
Expand Down Expand Up @@ -414,4 +421,4 @@ public function getTimes(){

return $times;
}
}
}

0 comments on commit a604f47

Please sign in to comment.