From 212b602bb212e42b14b97664e857a9b2e05b6c6a Mon Sep 17 00:00:00 2001 From: gbrock Date: Thu, 6 Aug 2015 10:43:55 -0400 Subject: [PATCH] Callback triggers first; some Sortable reorganization --- src/Traits/Sortable.php | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/Traits/Sortable.php b/src/Traits/Sortable.php index 8f0fae4..0dfadb6 100644 --- a/src/Traits/Sortable.php +++ b/src/Traits/Sortable.php @@ -35,13 +35,9 @@ public function scopeSorted($query, $field = false, $direction = false) // is there a custom function for sorting this column? $isCallableFunction = method_exists($this, $sortFunctionName); - // If the field requested isn't known to be sortable by our model, fail silently. - if( - !$isValueOfSortable && - !$isKeyOfSortable && - !$isCallableFunction - ) + if(!$isValueOfSortable && !$isKeyOfSortable && !$isCallableFunction) { + // If the field requested isn't known to be sortable by our model, fail silently. return $query; } @@ -51,21 +47,20 @@ public function scopeSorted($query, $field = false, $direction = false) $direction = config('gbrock-tables.default_direction'); } + if($isCallableFunction) + { + // Call custom function and return immediately + return call_user_func([$this, $sortFunctionName], $query, $direction); + } + + // By default assume the $field is a member of the $sortable array + $sortField = strpos($field, '.') === FALSE ? $this->getTable() . '.' . $field : $field; + if($isKeyOfSortable) { // Set via key $sortField = $this->sortable[$field]; } - elseif($isValueOfSortable) - { - // Does the passed field contain a period character? - $sortField = strpos($field, '.') === FALSE ? $this->getTable() . '.' . $field : $field; - } - elseif($isCallableFunction) - { - // Call custom function and return immediately - return call_user_func([$this, $sortFunctionName], $query, $direction); - } // At this point, all should be well, continue. return $query