Add sort dropdown to profile pages

master
Ondřej Hruška 6 years ago
parent 4392ab9d7b
commit 6b04218f67
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 63
      app/Http/Controllers/DashController.php
  2. 18
      app/Http/Controllers/ProfileController.php
  3. 66
      app/Models/Table.php
  4. 10
      resources/views/profile/view.blade.php
  5. 16
      resources/views/table/_sort-dropdown.blade.php
  6. 21
      resources/views/welcome.blade.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);

@ -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);
}
/**

@ -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;
}
}

@ -77,12 +77,18 @@
@icon(fa-table fa-pr fa-large)
<h2>
@if(authed() && user()->is($user))
Your Tables
Your Tables:
@else
User's Tables
User's Tables:
@endif
</h2>
@include('table._sort-dropdown', [
'link' => route('profile.view',
Input::except(['tableSort', 'page'])
+ ['tableSort' => '__sort'] + ['user' => $user->name])
])
<nav class="ml-auto" aria-label="Pages of the table list">
{{ $tables->links(null, ['ulClass' => 'pagination-sm mb-0 pagination-outline-light']) }}
</nav>

@ -0,0 +1,16 @@
<span>
<a class="nav-link dropdown-toggle link-no-color pl-1 ml-1 pr-2"
data-toggle="dropdown" href="#" role="button" aria-haspopup="true"
aria-expanded="false">{{ $sortOptions[$tableSort] }}</a>
<span class="dropdown-menu">
@foreach($sortOptions as $k => $label)
<a class="dropdown-item"
href="{{ str_replace('__sort', $k, $link) }}">
{{ $label }}
</a>
{{--@endif--}}
@endforeach
</span>
</span>
{{-- route('dash', Input::except(['tableSort', 'tablePage']) + ['tableSort' => $k]) --}}

@ -51,21 +51,12 @@
<div class="card-header card-header-extra">
@icon(fa-table fa-pr fa-large)
<h2>Tables:</h2>
<span>
<a class="nav-link dropdown-toggle link-no-color pl-1 ml-1 pr-2"
data-toggle="dropdown" href="#" role="button" aria-haspopup="true"
aria-expanded="false">{{ $sortOptions[$tableSort] }}</a>
<div class="dropdown-menu">
@foreach($sortOptions as $k => $label)
{{--@if($k != $tableSort)--}}
<a class="dropdown-item"
href="{{ route('dash', Input::except(['tableSort', 'tablePage']) + ['tableSort' => $k]) }}">
{{ $label }}
</a>
{{--@endif--}}
@endforeach
</div>
</span>
@include('table._sort-dropdown', [
'link' => route('dash',
Input::except(['tableSort', 'tablePage'])
+ ['tableSort' => '__sort'])
])
<nav class="ml-auto" aria-label="Pages of the table list">
{{ $tables->links(null, [

Loading…
Cancel
Save