diff --git a/app/Http/Controllers/Admin/SubmissionController.php b/app/Http/Controllers/Admin/SubmissionController.php index d8131fe2cb..cba3114152 100644 --- a/app/Http/Controllers/Admin/SubmissionController.php +++ b/app/Http/Controllers/Admin/SubmissionController.php @@ -27,12 +27,25 @@ class SubmissionController extends Controller public function getSubmissionIndex(Request $request, $status = null) { $submissions = Submission::with('prompt')->where('status', $status ? ucfirst($status) : 'Pending')->whereNotNull('prompt_id'); - if($request->get('prompt_category_id')) - $submissions->whereHas('prompt', function($query) use ($request) { - $query->where('prompt_category_id', $request->get('prompt_category_id')); + $data = $request->only(['prompt_category_id', 'sort']); + if(isset($data['prompt_category_id']) && $data['prompt_category_id'] != 'none') + $submissions->whereHas('prompt', function($query) use ($data) { + $query->where('prompt_category_id', $data['prompt_category_id']); }); + if(isset($data['sort'])) + { + switch($data['sort']) { + case 'newest': + $submissions->sortNewest(); + break; + case 'oldest': + $submissions->sortOldest(); + break; + } + } + else $submissions->sortOldest(); return view('admin.submissions.index', [ - 'submissions' => $submissions->orderBy('id', 'DESC')->paginate(30)->appends($request->query()), + 'submissions' => $submissions->paginate(30)->appends($request->query()), 'categories' => ['none' => 'Any Category'] + PromptCategory::orderBy('sort', 'DESC')->pluck('name', 'id')->toArray(), 'isClaims' => false ]); @@ -65,10 +78,24 @@ public function getSubmission($id) * @param string $status * @return \Illuminate\Contracts\Support\Renderable */ - public function getClaimIndex($status = null) + public function getClaimIndex(Request $request, $status = null) { + $submissions = Submission::where('status', $status ? ucfirst($status) : 'Pending')->whereNull('prompt_id'); + $data = $request->only(['sort']); + if(isset($data['sort'])) + { + switch($data['sort']) { + case 'newest': + $submissions->sortNewest(); + break; + case 'oldest': + $submissions->sortOldest(); + break; + } + } + else $submissions->sortOldest(); return view('admin.submissions.index', [ - 'submissions' => Submission::where('status', $status ? ucfirst($status) : 'Pending')->whereNull('prompt_id')->orderBy('id', 'DESC')->paginate(30), + 'submissions' => $submissions->paginate(30), 'isClaims' => true ]); } diff --git a/app/Models/Submission/Submission.php b/app/Models/Submission/Submission.php index 97c9a19af3..7026467844 100644 --- a/app/Models/Submission/Submission.php +++ b/app/Models/Submission/Submission.php @@ -133,6 +133,17 @@ public function scopeSortOldest($query) return $query->orderBy('id'); } + /** + * Scope a query to sort submissions by newest first. + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeSortNewest($query) + { + return $query->orderBy('id', 'DESC'); + } + /********************************************************************************************** ACCESSORS diff --git a/resources/views/admin/submissions/index.blade.php b/resources/views/admin/submissions/index.blade.php index 62c79b8793..b36a379518 100644 --- a/resources/views/admin/submissions/index.blade.php +++ b/resources/views/admin/submissions/index.blade.php @@ -25,18 +25,26 @@ -@if(!$isClaims) -
- {!! Form::open(['method' => 'GET', 'class' => 'form-inline justify-content-end']) !!} -
+{!! Form::open(['method' => 'GET', 'class' => 'form-inline justify-content-end']) !!} +
+ @if(!$isClaims) +
{!! Form::select('prompt_category_id', $categories, Request::get('prompt_category_id'), ['class' => 'form-control']) !!}
-
- {!! Form::submit('Search', ['class' => 'btn btn-primary']) !!} -
- {!! Form::close() !!} + @endif
-@endif +
+
+ {!! Form::select('sort', [ + 'newest' => 'Newest First', + 'oldest' => 'Oldest First', + ], Request::get('sort') ? : 'oldest', ['class' => 'form-control']) !!} +
+
+ {!! Form::submit('Search', ['class' => 'btn btn-primary']) !!} +
+
+{!! Form::close() !!} {!! $submissions->render() !!}