Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added @sortableurl #112

Open
wants to merge 1 commit into
base: L5.5-7
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ If you do not fill **Title** (2nd parameter) column name is used instead.

>**Note**: you can set default formatting function that is applied on **Title** (2nd parameter), by default this is set to [`ucfirst`](http://php.net/manual/en/function.ucfirst.php).

Also there is a blade extension for you to use **@sortableurl()** for get only url-path.

```blade
@sortableurl('column')
```

`@sortableurl` has similar arguments like `@sortablelink`, but not using (2nd) and (4th) parameters!

## Configuration in few words

**Sortablelink** blade extension distinguishes between *types* (**numeric**, **amount** and **alpha**) and applies different class for each of them.
Expand Down
6 changes: 6 additions & 0 deletions src/ColumnSortable/ColumnSortableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public function boot()
return "<?php echo \Kyslik\ColumnSortable\SortableLink::render(array ({$expression}));?>";
});

Blade::directive('sortableurl', function ($expression) {
$expression = ($expression[0] === '(') ? substr($expression, 1, -1) : $expression;

return "<?php echo \Kyslik\ColumnSortable\SortableLink::url(array ({$expression}));?>";
});

request()->macro('allFilled', function (array $keys) {
foreach ($keys as $key) {
if ( ! $this->filled($key)) {
Expand Down
25 changes: 25 additions & 0 deletions src/ColumnSortable/SortableLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,31 @@ public static function render(array $parameters)
return '<a'.$anchorClass.' href="'.url(request()->path().'?'.$queryString).'"'.$anchorAttributesString.'>'.htmlentities($title).$trailingTag;
}

/**
* @param array $parameters
* @param bool $absolute
*
* @return \Illuminate\Contracts\Routing\UrlGenerator|string
* @throws \Kyslik\ColumnSortable\Exceptions\ColumnSortableException
*/
public static function url(array $parameters, $absolute = true)
{
list($sortColumn, $sortParameter, $title, $queryParameters, $anchorAttributes) = self::parseParameters($parameters);
$explodeResult = self::explodeSortParameter($parameters[0]);

$sortColumn = (empty($explodeResult)) ? $parameters[0] : $explodeResult[1];
$queryParameters = (isset($parameters[2]) && is_array($parameters[2])) ? $parameters[2] : [];
$anchorAttributes = (isset($parameters[3]) && is_array($parameters[3])) ? $parameters[3] : [];

list($icon, $direction) = self::determineDirection($sortColumn, $sortParameter);

$queryString = self::buildQueryString($queryParameters, $sortParameter, $direction);

$urlPath = request()->path().'?'.$queryString;

return $absolute ? url($urlPath) : $urlPath;
}


/**
* @param array $parameters
Expand Down