From f6bf37e76c550f30b95afa065fa1d0498a71ea37 Mon Sep 17 00:00:00 2001 From: khamyl <4025815+khamyl@users.noreply.github.com> Date: Fri, 21 Jan 2022 08:49:43 +0100 Subject: [PATCH 1/5] Overriding the direction parameter --- src/ColumnSortable/SortableLink.php | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index 986b0f2..2ccb5a9 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 { From 8a88f96733b360299f37f897cdeb50e92f3c63fc Mon Sep 17 00:00:00 2001 From: khamyl <4025815+khamyl@users.noreply.github.com> Date: Fri, 21 Jan 2022 21:13:15 +0100 Subject: [PATCH 2/5] Readme edit (Overriding direction) --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 93e0eb9..7b144a9 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 From 866a0885e8a49ca1917ab5639a51618caa067827 Mon Sep 17 00:00:00 2001 From: khamyl <4025815+khamyl@users.noreply.github.com> Date: Sat, 22 Jan 2022 20:52:47 +0100 Subject: [PATCH 3/5] Reset sorting link support --- README.md | 1 + src/ColumnSortable/SortableLink.php | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7b144a9..db11dc4 100755 --- a/README.md +++ b/README.md @@ -136,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. diff --git a/src/ColumnSortable/SortableLink.php b/src/ColumnSortable/SortableLink.php index 2ccb5a9..7a75671 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -258,8 +258,11 @@ 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); + + if(is_null($sortParameter)) $direction = null; + + $queryString = http_build_query(array_merge($queryParameters, $persistParameters, [ 'sort' => $sortParameter, 'direction' => $direction, ])); From f28a076d39f2eec701791fc5ad13a661fcc897dc Mon Sep 17 00:00:00 2001 From: khamyl <4025815+khamyl@users.noreply.github.com> Date: Sat, 22 Jan 2022 22:51:12 +0100 Subject: [PATCH 4/5] Enable saving sorting to session --- src/ColumnSortable/Sortable.php | 41 +++++++++++++++++++++++++++++ src/ColumnSortable/SortableLink.php | 6 ++++- src/config/columnsortable.php | 10 +++++++ 3 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/ColumnSortable/Sortable.php b/src/ColumnSortable/Sortable.php index 43dcc0f..81ac13b 100755 --- a/src/ColumnSortable/Sortable.php +++ b/src/ColumnSortable/Sortable.php @@ -25,10 +25,20 @@ trait Sortable */ public function scopeSortable($query, $defaultParameters = null) { + if (request()->allFilled(['sort', 'direction'])) { // allFilled() is macro + $this->saveToSession(request()->only(['sort', 'direction'])); return $this->queryOrderBuilder($query, request()->only(['sort', 'direction'])); } + if(request()->has('sort') && request()->only(['sort'])['sort'] == config('columnsortable.reset_value')){ + $this->removeFromSession(); + } + + $session_sort = $this->getSessionSort(); + if($session_sort !== false) return $this->queryOrderBuilder($query, $session_sort); + + if (is_null($defaultParameters)) { $defaultParameters = $this->getDefaultSortable(); } @@ -45,6 +55,37 @@ public function scopeSortable($query, $defaultParameters = null) return $query; } + 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 7a75671..93cf313 100644 --- a/src/ColumnSortable/SortableLink.php +++ b/src/ColumnSortable/SortableLink.php @@ -260,7 +260,11 @@ private static function buildQueryString($queryParameters, $sortParameter, $dire $persistParameters = array_filter(request()->except('sort', 'direction', 'page'), $checkStrlenOrArray); - if(is_null($sortParameter)) $direction = null; + //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, 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 From 421920d0ac1acf9bc708333a93b228395f29472d Mon Sep 17 00:00:00 2001 From: khamyl <4025815+khamyl@users.noreply.github.com> Date: Sun, 30 Jan 2022 13:01:00 +0100 Subject: [PATCH 5/5] Enable saving sorting params to Session --- README.md | 6 ++++++ src/ColumnSortable/Sortable.php | 33 ++++++++++++++++++--------------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index db11dc4..22f5b9a 100755 --- a/README.md +++ b/README.md @@ -386,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 81ac13b..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,20 +24,12 @@ trait Sortable */ public function scopeSortable($query, $defaultParameters = null) { - + $this->Init(); + if (request()->allFilled(['sort', 'direction'])) { // allFilled() is macro - $this->saveToSession(request()->only(['sort', 'direction'])); return $this->queryOrderBuilder($query, request()->only(['sort', 'direction'])); } - if(request()->has('sort') && request()->only(['sort'])['sort'] == config('columnsortable.reset_value')){ - $this->removeFromSession(); - } - - $session_sort = $this->getSessionSort(); - if($session_sort !== false) return $this->queryOrderBuilder($query, $session_sort); - - if (is_null($defaultParameters)) { $defaultParameters = $this->getDefaultSortable(); } @@ -55,6 +46,18 @@ 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', []); @@ -63,16 +66,16 @@ private function getSessionSort(){ return ['sort' => key($sortParams), 'direction' => reset($sortParams)]; } - } + } return false; } - private function saveToSession($sortParameters){ + 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()->session()->get('sort', []); $sess_sort[request()->path()] = $sort_rec; session(['sort' => $sess_sort]); } @@ -80,7 +83,7 @@ private function saveToSession($sortParameters){ private function removeFromSession(){ if(config('columnsortable.to_session')){ - $sess_sort = request()->session()->get('sort', []); + $sess_sort = request()->session()->get('sort', []); unset($sess_sort[request()->path()]); session(['sort' => $sess_sort]); }