Skip to content

Commit

Permalink
Merge branch 'table-sorting'
Browse files Browse the repository at this point in the history
  • Loading branch information
NFarrington committed Nov 3, 2019
2 parents da77aff + d53ba37 commit 6f64d47
Show file tree
Hide file tree
Showing 13 changed files with 286 additions and 37 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Platform/OrganizationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function index(Request $request)
$organizations = Organization::with('owners', 'managers', 'members', 'users')
->whereHas('users', function ($query) use ($request) {
$query->where('users.id', $request->user()->id);
})->orderBy('created_at')->paginate(20);
})->sortable('name')->paginate(20);

return view('platform.organizations.index')->with([
'organizations' => $organizations,
Expand Down
5 changes: 1 addition & 4 deletions app/Http/Controllers/Platform/UrlController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ public function index(Request $request)
->where('user_id', $request->user()->id)
->orWhereHas('organization', function ($query) use ($user) {
$query->whereIn('id', $user->organizations->pluck('id'));
})->join('domains', 'urls.domain_id', 'domains.id')
->orderBy('organization_id')
->orderBy('domains.url')
->orderBy('urls.url')
})->sortable('url')
->paginate(20, ['urls.*']);

$publicUrls = Url::public()
Expand Down
16 changes: 15 additions & 1 deletion app/Models/Organization.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;

/**
* App\Models\Organization
Expand All @@ -26,6 +27,7 @@
* @method static \Illuminate\Database\Query\Builder|\App\Models\Organization onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Organization query()
* @method static bool|null restore()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Organization sortable($defaultParameters = null)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Organization whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Organization whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Organization whereId($value)
Expand All @@ -38,7 +40,7 @@
*/
class Organization extends Model
{
use SoftDeletes;
use SoftDeletes, Sortable;

/**
* The attributes that are trackable.
Expand All @@ -47,6 +49,18 @@ class Organization extends Model
*/
protected $tracked = ['name'];

/**
* Sortable attributes.
*
* @var array
*/
public $sortable = [
'id',
'name',
'created_at',
'updated_at',
];

/**
* The organization's URLs.
*
Expand Down
36 changes: 35 additions & 1 deletion app/Models/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace App\Models;

use DB;
use Illuminate\Database\Eloquent\SoftDeletes;
use Kyslik\ColumnSortable\Sortable;

/**
* App\Models\Url
Expand Down Expand Up @@ -30,6 +32,7 @@
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url public()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url query()
* @method static bool|null restore()
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url sortable($defaultParameters = null)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url whereAnalyticsDisabled($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Url whereDeletedAt($value)
Expand All @@ -47,7 +50,7 @@
*/
class Url extends Model
{
use SoftDeletes;
use SoftDeletes, Sortable;

/**
* The attributes that should be cast to native types.
Expand All @@ -65,6 +68,19 @@ class Url extends Model
*/
protected $tracked = ['user_id', 'organization_id', 'domain_id', 'url', 'redirect_url'];

/**
* Sortable attributes.
*
* @var array
*/
public $sortable = [
'id',
'url',
'redirect_url',
'created_at',
'updated_at',
];

/**
* The relations to eager load on every query.
*
Expand Down Expand Up @@ -124,4 +140,22 @@ public function scopePublic($query)
{
return $query->whereNull('user_id')->whereNull('organization_id');
}

/**
* Override default URL sorting in order to sort URLs by their
* full host and path, instead of just their path.
*
* @param $query
* @param $direction
* @return mixed
*/
public function urlSortable($query, $direction)
{
$column = DB::connection()->getDriverName() == 'sqlite'
? DB::raw('domains.url || urls.url')
: DB::raw('CONCAT(domains.url, urls.url)');

return $query->join('domains', 'urls.domain_id', 'domains.id')
->orderBy($column, $direction);
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"fideloper/proxy": "^4.0",
"graham-campbell/markdown": "^10.0",
"guzzlehttp/guzzle": "^6.3",
"kyslik/column-sortable": "5.8.*",
"laravel/framework": "5.8.*",
"laravel/tinker": "^1.0",
"pragmarx/google2fa-qrcode": "^1.0",
Expand Down
79 changes: 68 additions & 11 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

116 changes: 116 additions & 0 deletions config/columnsortable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

return [

/*
spec columns
*/
'columns' => [
'alpha' => [
'rows' => [],
'class' => 'fas fa-sort-alpha',
],
'amount' => [
'rows' => [],
'class' => 'fas fa-sort-amount',
],
'numeric' => [
'rows' => [],
'class' => 'fas fa-sort-numeric',
],
],

/*
whether icons should be enabled
*/
'enable_icons' => true,

/*
defines icon set to use when sorted data is none above (alpha nor amount nor numeric)
*/
'default_icon_set' => 'fa fa-sort',

/*
icon that shows when generating sortable link while column is not sorted
*/
'sortable_icon' => 'fa fa-sort',

/*
generated icon is clickable non-clickable (default)
*/
'clickable_icon' => true,

/*
icon and text separator (any string)
in case of 'clickable_icon' => true; separator creates possibility to style icon and anchor-text properly
*/
'icon_text_separator' => ' ',

/*
suffix class that is appended when ascending direction is applied
*/
'asc_suffix' => '-up',

/*
suffix class that is appended when descending direction is applied
*/
'desc_suffix' => '-down',

/*
default anchor class, if value is null none is added
*/
'anchor_class' => null,

/*
default active anchor class, if value is null none is added
*/
'active_anchor_class' => null,

/*
default sort direction anchor class, if value is null none is added
*/
'direction_anchor_class_prefix' => null,

/*
relation - column separator ex: detail.phone_number means relation "detail" and column "phone_number"
*/
'uri_relation_column_separator' => '.',

/*
formatting function applied to name of column, use null to turn formatting off
*/
'formatting_function' => 'ucfirst',

/*
inject title parameter in query strings, use null to turn injection off
example: 'inject_title' => 't' will result in ..user/?t="formatted title of sorted column"
*/
'inject_title_as' => null,

/*
allow request modification, when default sorting is set but is not in URI (first load)
*/
'allow_request_modification' => true,

/*
default direction for: $user->sortable('id') usage
*/
'default_direction' => 'asc',

/*
default direction for non-sorted columns
*/
'default_direction_unsorted' => 'asc',

/*
use the first defined sortable column (Model::$sortable) as default
also applies if sorting parameters are invalid for example: 'sort' => 'name', 'direction' => ''
*/
'default_first_column' => false,

/*
join type: join vs leftJoin (default leftJoin)
for more information see https://github.com/Kyslik/column-sortable/issues/59
*/
'join_type' => 'leftJoin',
];
Loading

0 comments on commit 6f64d47

Please sign in to comment.