|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ka4ivan\ViewSortable\Support; |
|
4
|
|
|
|
|
5
|
|
|
class Sort |
|
6
|
|
|
{ |
|
7
|
|
|
protected $sortUrl; |
|
8
|
|
|
protected $orderUrl; |
|
9
|
|
|
|
|
10
|
|
|
public function __construct() |
|
11
|
|
|
{ |
|
12
|
|
|
if (is_null($this->sortUrl) || is_null($this->orderUrl)) { |
|
|
|
|
|
|
13
|
|
|
$this->sortUrl = config('view-sortable.url.sort', 'sort'); |
|
|
|
|
|
|
14
|
|
|
$this->orderUrl = config('view-sortable.url.order', 'order'); |
|
15
|
|
|
} |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param string $sort |
|
20
|
|
|
* @param string|null $text |
|
21
|
|
|
* @param string|null $order |
|
22
|
|
|
* @param string $class |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getSortLink(string $sort, string $text = null, string $order = null, string $class = 'lte-sort-link'): string |
|
26
|
|
|
{ |
|
27
|
|
|
$order = $order ?: ((request()->{$this->orderUrl} === 'asc') ? 'desc' : 'asc'); |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
$url = $this->buildUrl([$this->sortUrl => $sort, $this->orderUrl => $order]); |
|
30
|
|
|
$image = $this->getIcon($sort); |
|
31
|
|
|
$text = $text ?: $sort; |
|
32
|
|
|
|
|
33
|
|
|
return "<a class='{$class}' href='{$url}' style='position: relative'>{$text} {$image}</a>"; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $sort |
|
38
|
|
|
* @param string|null $order |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getSortUrl(string $sort, string $order = null): string |
|
42
|
|
|
{ |
|
43
|
|
|
$order = $order ?: ((request()->{$this->orderUrl} === 'asc') ? 'desc' : 'asc'); |
|
|
|
|
|
|
44
|
|
|
$url = $this->buildUrl(['sort' => $sort, 'order' => $order]); |
|
45
|
|
|
|
|
46
|
|
|
return $url; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $queryString |
|
51
|
|
|
* @return string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function buildUrl(array $queryString): string |
|
54
|
|
|
{ |
|
55
|
|
|
return url(request()->fullUrlWithQuery($queryString)); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $sort |
|
60
|
|
|
* @return string |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function getIcon(string $sort): string |
|
63
|
|
|
{ |
|
64
|
|
|
if (request()->{$this->sortUrl} === $sort) { |
|
|
|
|
|
|
65
|
|
|
$icon = request()->{$this->orderUrl} === 'asc' |
|
66
|
|
|
? config('view-sortable.icons.asc', 'fas fa-long-arrow-alt-up') |
|
|
|
|
|
|
67
|
|
|
: config('view-sortable.icons.desc', 'fas fa-long-arrow-alt-down'); |
|
68
|
|
|
} else { |
|
69
|
|
|
$icon = config('view-sortable.icons.default', ''); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $icon ? "<i class='{$icon}' style='position: absolute; top: 3px; right: -10px'></i>" : ''; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|