diff --git a/README.md b/README.md index 93e0eb9..22f5b9a 100755 --- a/README.md +++ b/README.md @@ -127,6 +127,8 @@ There is a blade extension for you to use **@sortablelink()** You can omit 2nd, 3rd and 4th parameter. +By using `['direction'=>'desc']` in 3rd parameter you can override the default toggle behavior. + Possible examples and usages of blade extension: ```blade @@ -134,6 +136,7 @@ Possible examples and usages of blade extension: @sortablelink('name', 'Username') @sortablelink('address', trans('fields.address'), ['filter' => 'active, visible']) @sortablelink('address', trans('fields.address'), ['filter' => 'active, visible'], ['class' => 'btn btn-block', 'rel' => 'nofollow', 'href' => route('my.custom.route')]) +@sortablelink(null, 'Reset') {{!-- back to default --}} ``` If you do not fill **Title** (2nd parameter) column name is used instead. @@ -383,3 +386,9 @@ try { ``` >**Note**: I strongly recommend to catch **ColumnSortableException** because there is a user input in question (GET parameter) and any user can modify it in such way that package throws ColumnSortableException with code `0`. + +# Session - persisting parameters + +Package supports saving the sortable parameters _sort_ and _direction_ into the session. To support this on the application level set the `to_session` configuration parameter to `true`. + + diff --git a/src/ColumnSortable/Sortable.php b/src/ColumnSortable/Sortable.php index 43dcc0f..20fec6d 100755 --- a/src/ColumnSortable/Sortable.php +++ b/src/ColumnSortable/Sortable.php @@ -15,7 +15,6 @@ */ trait Sortable { - /** * @param \Illuminate\Database\Query\Builder $query * @param array|null $defaultParameters @@ -25,6 +24,8 @@ trait Sortable */ public function scopeSortable($query, $defaultParameters = null) { + $this->Init(); + if (request()->allFilled(['sort', 'direction'])) { // allFilled() is macro return $this->queryOrderBuilder($query, request()->only(['sort', 'direction'])); } @@ -45,6 +46,49 @@ public function scopeSortable($query, $defaultParameters = null) return $query; } + private function Init(){ + if(request()->allFilled(['sort', 'direction'])){ + $this->saveToSession(request()->only(['sort', 'direction'])); + } + elseif(request()->has('sort') && request()->input('sort')==config('columnsortable.reset_value')){ + $this->removeFromSession(); + } + elseif($this->getSessionSort()!==false && !request()->allFilled(['sort', 'direction']) ){ + request()->merge($this->getSessionSort()); + } + } + + private function getSessionSort(){ + if(config('columnsortable.to_session')){ + $sess_sort = request()->session()->get('sort', []); + if(isset($sess_sort[request()->path()])) { + $sortParams = $sess_sort[request()->path()]; + return ['sort' => key($sortParams), + 'direction' => reset($sortParams)]; + } + } + + return false; + } + + private function saveToSession($sortParameters){ + if(config('columnsortable.to_session')){ + list($column, $direction) = $this->parseParameters($sortParameters); + $sort_rec[$column] = $direction; + $sess_sort = request()->session()->get('sort', []); + $sess_sort[request()->path()] = $sort_rec; + session(['sort' => $sess_sort]); + } + } + + private function removeFromSession(){ + if(config('columnsortable.to_session')){ + $sess_sort = request()->session()->get('sort', []); + unset($sess_sort[request()->path()]); + session(['sort' => $sess_sort]); + } + } + /** * Returns the first element of defined sortable columns from the Model diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index 986b0f2..93cf313 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -29,7 +29,7 @@ public static function render(array $parameters) request()->merge([$mergeTitleAs => $title]); } - list($icon, $direction) = self::determineDirection($sortColumn, $sortParameter); + list($icon, $direction) = self::determineDirection($sortColumn, $sortParameter, $queryParameters); $trailingTag = self::formTrailingTag($icon); @@ -125,14 +125,21 @@ private static function applyFormatting($title, $sortColumn) * * @return array */ - private static function determineDirection($sortColumn, $sortParameter) + private static function determineDirection($sortColumn, $sortParameter, $queryParameters) { $icon = self::selectIcon($sortColumn); - if (request()->get('sort') == $sortParameter && in_array(request()->get('direction'), ['asc', 'desc'])) { - $icon .= (request()->get('direction') === 'asc' ? config('columnsortable.asc_suffix', '-asc') : - config('columnsortable.desc_suffix', '-desc')); - $direction = request()->get('direction') === 'desc' ? 'asc' : 'desc'; + $override_direction = array_key_exists('direction', $queryParameters) && in_array($queryParameters['direction'], ['asc', 'desc']); + + if (request()->get('sort') == $sortParameter && in_array(request()->get('direction'), ['asc', 'desc']) || $override_direction) { + $icon .= (request()->get('direction') === 'asc' ? config('columnsortable.asc_suffix', '-asc') : config('columnsortable.desc_suffix', '-desc')); + + if($override_direction){ + //Override the direction with the query parameter + $direction = $queryParameters['direction']; + }else{ + $direction = request()->get('direction') === 'desc' ? 'asc' : 'desc'; + } return [$icon, $direction]; } else { @@ -251,8 +258,15 @@ private static function buildQueryString($queryParameters, $sortParameter, $dire return is_array($element) ? $element : strlen($element); }; - $persistParameters = array_filter(request()->except('sort', 'direction', 'page'), $checkStrlenOrArray); - $queryString = http_build_query(array_merge($queryParameters, $persistParameters, [ + $persistParameters = array_filter(request()->except('sort', 'direction', 'page'), $checkStrlenOrArray); + + //TODO: Test for one-to-one relations + if(is_null($sortParameter)){ + if(config('columnsortable.to_session')) $sortParameter = config('columnsortable.reset_value'); + $direction = null; + } + + $queryString = http_build_query(array_merge($queryParameters, $persistParameters, [ 'sort' => $sortParameter, 'direction' => $direction, ])); diff --git a/src/config/columnsortable.php b/src/config/columnsortable.php index 51b54b9..c8ec086 100755 --- a/src/config/columnsortable.php +++ b/src/config/columnsortable.php @@ -96,6 +96,16 @@ allow request modification, when default sorting is set but is not in URI (first load) */ 'allow_request_modification' => true, + + /* + Enable storing sorting to session + */ + 'to_session' => false, + + /* + Value indicating to reset sorting + */ + 'reset_value' => '__reset', /* default direction for: $user->sortable('id') usage