diff --git a/app/Http/Controllers/DashController.php b/app/Http/Controllers/DashController.php index d220fcf..f581b6d 100644 --- a/app/Http/Controllers/DashController.php +++ b/app/Http/Controllers/DashController.php @@ -20,71 +20,20 @@ class DashController extends Controller return back(); } - $defaultSort = 'last-updated'; - - $sort = \Input::get('tableSort', $defaultSort); - - $data['sortOptions'] = [ - 'most-visited' => 'Most Visited', - 'last-updated' => 'Last Updated', - 'most-updated' => 'Most Changed', -// 'most-discussed' => 'Most Discussed', -// 'most-liked' => 'Most Liked', - 'newest' => 'Newest', - 'oldest' => 'Oldest', - 'most-rows' => 'Longest', - 'least-rows' => 'Shortest', - ]; - - if (!in_array($sort, array_keys($data['sortOptions']))) { - $sort = $defaultSort; - } - - $data['tableSort'] = $sort; - $data['users'] = User::orderBy('updated_at', 'desc') ->withCount(['tables']) ->paginate(10, ['id', 'title', 'name', 'tables_count'], 'userPage') ->appends(\Input::except('userPage')); - $tableQuery = Table::forList(); - switch ($sort) { - case 'most-visited': - $tableQuery = $tableQuery->orderBy('visits', 'desc'); - break; - case 'last-updated': - $tableQuery = $tableQuery->orderBy('updated_at', 'desc'); - break; - case 'most-updated': - $tableQuery = $tableQuery->orderBy('revisions_count', 'desc'); - break; - case 'most-discussed': - $tableQuery = $tableQuery->orderBy('comments_count', 'desc'); - break; - case 'most-liked': - $tableQuery = $tableQuery->orderBy('favourites_count', 'desc'); - break; - case 'most-rows': - $tableQuery = $tableQuery - ->join('revisions', 'revisions.id', '=', 'tables.revision_id') - ->orderBy('revisions.row_count', 'desc'); - break; - case 'least-rows': - $tableQuery = $tableQuery - ->join('revisions', 'revisions.id', '=', 'tables.revision_id') - ->orderBy('revisions.row_count', 'asc'); - break; - case 'newest': - $tableQuery = $tableQuery->orderBy('created_at', 'desc'); - break; - case 'oldest': - $tableQuery = $tableQuery->orderBy('created_at', 'asc'); - break; - } + $sort = Table::resolveSortType(\Input::get('tableSort')); + $data['sortOptions'] = Table::getSortOptions(); - $data['tables'] = $tableQuery->paginate(10, ['*'], 'tablePage') + $data['tables'] = Table::forList()->tableSort($sort) + ->paginate(10, ['*'], 'tablePage') ->appends(\Input::except('tablePage')); + $data['tableSort'] = $sort; + $data['showGreeter'] = !$dismiss && !$request->cookie('dismiss-greeter', false); return view('welcome', $data); diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 1306d12..0ed0000 100644 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; +use App\Models\Table; use App\Models\User; use Illuminate\Http\Request; @@ -18,14 +19,15 @@ class ProfileController extends Controller */ public function view(User $user) { - $tables = $user->tables()->forList()->orderByDesc('updated_at')->paginate(10); - $tables_count = $user->tables()->count(); - - return view('profile.view')->with(compact( - 'tables', - 'user', - 'tables_count' - )); + $data['user'] = $user; + $data['tables_count'] = $user->tables()->count(); + + $data['tableSort'] = $sort = Table::resolveSortType(\Input::get('tableSort')); + $data['sortOptions'] = Table::getSortOptions(); + + $data['tables'] = $user->tables()->forList()->tableSort($sort)->paginate(10); + + return view('profile.view', $data); } /** diff --git a/app/Models/Table.php b/app/Models/Table.php index 3d0cb83..3c13d5c 100644 --- a/app/Models/Table.php +++ b/app/Models/Table.php @@ -150,6 +150,8 @@ class Table extends BaseModel } } + if ($name == 'handle') return '@' . $this->cachedOwner()->name . '/' . $this->name; + if ($name == 'draftSessionKey') return "proposal.{$this->id}"; return parent::__get($name); @@ -192,4 +194,68 @@ class Table extends BaseModel ->with('owner:id,name,title') ->withCount(['favourites', 'forks', 'revisions', 'proposals', 'comments']); } + + public static function getSortOptions() + { + return [ + 'most-visited' => 'Most Visited', + 'last-updated' => 'Last Updated', + 'most-updated' => 'Most Changed', +// 'most-discussed' => 'Most Discussed', +// 'most-liked' => 'Most Liked', + 'newest' => 'Newest', + 'oldest' => 'Oldest', + 'most-rows' => 'Longest', + 'least-rows' => 'Shortest', + ]; + } + + public static function resolveSortType($sort) + { + $defaultSort = 'last-updated'; + if (!in_array($sort, array_keys(self::getSortOptions()))) { + $sort = $defaultSort; + } + return $sort; + } + + public function scopeTableSort(Builder $query, $sort) + { + + switch ($sort) { + case 'most-visited': + $query = $query->orderBy('visits', 'desc'); + break; + case 'last-updated': + $query = $query->orderBy('updated_at', 'desc'); + break; + case 'most-updated': + $query = $query->orderBy('revisions_count', 'desc'); + break; + case 'most-discussed': + $query = $query->orderBy('comments_count', 'desc'); + break; + case 'most-liked': + $query = $query->orderBy('favourites_count', 'desc'); + break; + case 'most-rows': + $query = $query + ->join('revisions', 'revisions.id', '=', 'tables.revision_id') + ->orderBy('revisions.row_count', 'desc'); + break; + case 'least-rows': + $query = $query + ->join('revisions', 'revisions.id', '=', 'tables.revision_id') + ->orderBy('revisions.row_count', 'asc'); + break; + case 'newest': + $query = $query->orderBy('created_at', 'desc'); + break; + case 'oldest': + $query = $query->orderBy('created_at', 'asc'); + break; + } + + return $query; + } } diff --git a/resources/views/profile/view.blade.php b/resources/views/profile/view.blade.php index 6e930e3..f9cf909 100644 --- a/resources/views/profile/view.blade.php +++ b/resources/views/profile/view.blade.php @@ -77,12 +77,18 @@ @icon(fa-table fa-pr fa-large)

@if(authed() && user()->is($user)) - Your Tables + Your Tables: @else - User's Tables + User's Tables: @endif

+ @include('table._sort-dropdown', [ + 'link' => route('profile.view', + Input::except(['tableSort', 'page']) + + ['tableSort' => '__sort'] + ['user' => $user->name]) + ]) + diff --git a/resources/views/table/_sort-dropdown.blade.php b/resources/views/table/_sort-dropdown.blade.php new file mode 100644 index 0000000..d4164f0 --- /dev/null +++ b/resources/views/table/_sort-dropdown.blade.php @@ -0,0 +1,16 @@ + + + + @foreach($sortOptions as $k => $label) + + {{ $label }} + + {{--@endif--}} + @endforeach + + + +{{-- route('dash', Input::except(['tableSort', 'tablePage']) + ['tableSort' => $k]) --}} diff --git a/resources/views/welcome.blade.php b/resources/views/welcome.blade.php index 53f91ad..f0e97ac 100644 --- a/resources/views/welcome.blade.php +++ b/resources/views/welcome.blade.php @@ -51,21 +51,12 @@
@icon(fa-table fa-pr fa-large)

Tables:

- - - - + + @include('table._sort-dropdown', [ + 'link' => route('dash', + Input::except(['tableSort', 'tablePage']) + + ['tableSort' => '__sort']) + ])