-
Notifications
You must be signed in to change notification settings - Fork 128
/
AllUlrTable.php
182 lines (164 loc) · 6.37 KB
/
AllUlrTable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace App\Livewire\Table;
use App\Helpers\Helper;
use App\Models\Url;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Str;
use PowerComponents\LivewirePowerGrid\{
Column, Footer, Header, PowerGrid, PowerGridColumns, PowerGridComponent
};
/**
* @codeCoverageIgnore
*/
final class AllUlrTable extends PowerGridComponent
{
const STR_LIMIT = 60;
public bool $showUpdateMessages = true;
public string $sortDirection = 'desc';
/*
|--------------------------------------------------------------------------
| Features Setup
|--------------------------------------------------------------------------
| Setup Table's general features
|
*/
public function setUp(): array
{
return [
Header::make()
->showToggleColumns()
->showSearchInput(),
Footer::make()
->showPerPage()
->showRecordCount('full'),
];
}
/*
|--------------------------------------------------------------------------
| Datasource
|--------------------------------------------------------------------------
| Provides data to your Table using a Model or Collection
|
*/
public function datasource(): ?Builder
{
return Url::query();
}
/*
|--------------------------------------------------------------------------
| Relationship Search
|--------------------------------------------------------------------------
| Configure here relationships to be used by the Search and Table Filters.
|
*/
/**
* Relationship search.
*
* @return array<string, array<int, string>>
*/
public function relationSearch(): array
{
return [];
}
/*
|--------------------------------------------------------------------------
| Add Column
|--------------------------------------------------------------------------
| Make Datasource fields available to be used as columns.
| You can pass a closure to transform/modify the data.
|
*/
public function addColumns(): PowerGridColumns
{
return PowerGrid::columns()
->addColumn('user_name', function (Url $url) {
return '<span class="font-semibold">'.$url->author->name.'</span>';
})
->addColumn('keyword', function (Url $url) {
return '<a href="'.$url->short_url.'" target="_blank"class="font-light text-sky-800">'.$url->keyword.'</a>';
})
->addColumn('destination', function (Url $url) {
return
'<span title="'.htmlspecialchars($url->title).'">'
.Str::limit($url->title, self::STR_LIMIT).
'</span>
<br>
<a href="'.$url->destination.'" target="_blank" title="'.$url->destination.'" rel="noopener noreferrer"
class="text-[#6c6c6c]"
>'
.Helper::urlDisplay($url->destination, self::STR_LIMIT).
'</a>';
})
->addColumn('t_clicks', function (Url $url) {
$uClick = Helper::compactNumber($url->uniqueClicks);
$tClick = Helper::compactNumber($url->clicks);
$icon = Blade::render('@svg(\'icon-bar-chart\', \'ml-2 text-amber-600\')');
$title = $uClick.' '.__('Uniques').' / '.$tClick.' '.__('Clicks');
return '<div title="'.$title.'">'.$uClick.' / '.$tClick.$icon.'</div>';
})
->addColumn('created_at_formatted', function (Url $url) {
return
'<span title="'.$url->created_at->toDayDateTimeString().'">'
.$url->created_at->shortRelativeDiffForHumans().
'</span>';
})
->addColumn('action', function (Url $url) {
return
'<a role="button" href="'.route('su_detail', $url->keyword).'" target="_blank" title="'.__('Open front page').'"
class="btn btn-secondary btn-sm"
>'
.Blade::render('@svg(\'icon-open-in-new\')').
'</a>
<a role="button" href="'.route('dashboard.su_duplicate', $url->keyword).'" title="'.__('Duplicate').'"
class="btn btn-secondary btn-sm"
>'
.Blade::render('@svg(\'icon-clone-alt\')').
'</a>
<a role="button" href="'.route('dashboard.su_edit', $url->keyword).'" title="'.__('Edit').'"
class="btn btn-secondary btn-sm"
>'
.Blade::render('@svg(\'icon-edit-alt\')').
'</a>
<a role="button" href="'.route('dashboard.su_delete', $url->getRouteKey()).'" title="'.__('Delete').'"
class="btn btn-secondary btn-sm hover:text-red-600 active:text-red-700"
>'
.Blade::render('@svg(\'icon-trash-alt\')').
'</a>';
});
}
/*
|--------------------------------------------------------------------------
| Include Columns
|--------------------------------------------------------------------------
| Include the columns added columns, making them visible on the Table.
| Each column can be configured with properties, filters, actions...
|
*/
/**
* PowerGrid Columns.
*
* @return array<int, Column>
*/
public function columns(): array
{
return [
Column::make('Owner', 'user_name', 'users.name')
->sortable()
->searchable(),
Column::make('Short URL', 'keyword')
->sortable()
->searchable(),
Column::make('Destination URL', 'destination')
->sortable()
->searchable(),
Column::make('CLICKS', 't_clicks')
->bodyAttribute(styleAttr: ';padding-left: 8px'),
Column::make('CREATED AT', 'created_at_formatted', 'created_at')
->searchable()
->sortable(),
Column::make('ACTIONS', 'action')
->bodyAttribute(styleAttr: ';padding-left: 8px'),
];
}
}