diff options
author | Chaoyi Zha <summermontreal@gmail.com> | 2017-03-25 21:26:12 +0300 |
---|---|---|
committer | Chaoyi Zha <summermontreal@gmail.com> | 2017-03-25 21:26:12 +0300 |
commit | c7bb9b00fa24eed9179b78e453a0c6019068927a (patch) | |
tree | e8619ebaa38b317c502f5e29f9e12f7caf58009a /app | |
parent | 29d6c1c4c11ddef668a2ea1d936f55a3f0942a2a (diff) |
Add datetimepicker to aallow modification of date bounds for StatController
Diffstat (limited to 'app')
-rw-r--r-- | app/Http/Controllers/StatsController.php | 33 |
1 files changed, 29 insertions, 4 deletions
diff --git a/app/Http/Controllers/StatsController.php b/app/Http/Controllers/StatsController.php index 7657962..715710d 100644 --- a/app/Http/Controllers/StatsController.php +++ b/app/Http/Controllers/StatsController.php @@ -13,9 +13,22 @@ class StatsController extends Controller { const DAYS_TO_FETCH = 30; public function displayStats(Request $request, $short_url) { + $validator = \Validator::make($request->all(), [ + 'left_bound' => 'date', + 'right_bound' => 'date' + ]); + + if ($validator->fails() && !session('error')) { + // Do not flash error if there is already an error flashed + return redirect()->back()->with('error', 'Invalid date bounds.'); + } + + $user_left_bound = $request->input('left_bound'); + $user_right_bound = $request->input('right_bound'); + // Carbon bounds for StatHelper - $left_bound = Carbon::now()->subDays(self::DAYS_TO_FETCH); - $right_bound = Carbon::now(); + $left_bound = $user_left_bound ?: Carbon::now()->subDays(self::DAYS_TO_FETCH); + $right_bound = $user_right_bound ?: Carbon::now(); if (!$this->isLoggedIn()) { return redirect(route('login'))->with('error', 'Please login to view link stats.'); @@ -38,8 +51,17 @@ class StatsController extends Controller { return redirect(route('admin'))->with('error', 'You do not have permission to view stats for this link.'); } - // Fetch base rows for StatHelper - $stats = new StatsHelper($link_id, $left_bound, $right_bound); + try { + // Initialize StatHelper + $stats = new StatsHelper($link_id, $left_bound, $right_bound); + } + catch (\Exception $e) { + if (!session('error')) { + // Do not flash error if there is already an error flashed + return redirect()->back()->with('error', 'Invalid date bounds. + The right date bound must be more recent than the left bound.'); + } + } $day_stats = $stats->getDayStats(); $country_stats = $stats->getCountryStats(); @@ -51,6 +73,9 @@ class StatsController extends Controller { 'country_stats' => $country_stats, 'referer_stats' => $referer_stats, + 'left_bound' => ($user_left_bound ?: ''), + 'right_bound' => ($user_right_bound ?: ''), + 'no_div_padding' => true ]); } |