From 4b543ea17992301a5541638f7ada8bec2b00eb7e Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Sun, 19 Apr 2015 13:48:02 -0700 Subject: [PATCH] include the date last eaten as a subtitle --- controllers/controllers.php | 2 +- lib/helpers.php | 38 ++++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/controllers/controllers.php b/controllers/controllers.php index 8301b8b..e5f82e0 100644 --- a/controllers/controllers.php +++ b/controllers/controllers.php @@ -221,7 +221,7 @@ $app->get('/new/options', function() use($app) { if($user=require_login($app)) { $params = $app->request()->params(); - $options = get_entry_options($user->id, $params['latitude'], $params['longitude']); + $options = get_entry_options($user->id, k($params,'latitude'), k($params,'longitude')); $html = partial('partials/entry-buttons', ['options'=>$options]); $app->response()->body($html); diff --git a/lib/helpers.php b/lib/helpers.php index 130e533..d5ba67a 100644 --- a/lib/helpers.php +++ b/lib/helpers.php @@ -246,6 +246,7 @@ function query_user_nearby_options($type, $user_id, $latitude, $longitude) { foreach($optionsQ as $o) { $options[] = [ 'title' => $o->content, + 'subtitle' => query_last_eaten($user_id, $o->type, $o->content), 'type' => $o->type ]; } @@ -256,7 +257,7 @@ function query_user_frequent_options($type, $user_id) { $published = date('Y-m-d H:i:s', strtotime('-4 months')); $options = []; $optionsQ = ORM::for_table('entries')->raw_query(' - SELECT type, content + SELECT type, content, MAX(published) AS published FROM entries WHERE user_id = :user_id AND type = :type @@ -268,12 +269,44 @@ function query_user_frequent_options($type, $user_id) { foreach($optionsQ as $o) { $options[] = [ 'title' => $o->content, + 'subtitle' => query_last_eaten($user_id, $o->type, $o->content), 'type' => $o->type ]; } return $options; } +function query_last_eaten($user_id, $type, $content) { + $lastQ = ORM::for_table('entries')->raw_query(' + SELECT published, timezone + FROM entries + WHERE user_id=:user_id + AND type=:type + AND content=:content + ORDER BY published DESC + LIMIT 1; + ', ['user_id'=>$user_id, 'type'=>$type, 'content'=>$content])->find_one(); + if(!$lastQ) + return ''; + + $timestamp = strtotime($lastQ->published); + // If less than 8 hours ago, use relative time, otherwise show the actual time + if(time() - $timestamp > 60*60*8) { + $date = new DateTime($lastQ->published); + $date->setTimeZone(new DateTimeZone($lastQ->timezone)); + return $date->format('D, M j, g:ia'); + } else { + $config = array( + 'language' => '\RelativeTime\Languages\English', + 'separator' => ', ', + 'suffix' => true, + 'truncate' => 1, + ); + $relativeTime = new \RelativeTime\RelativeTime($config); + return $relativeTime->timeAgo($lastQ->published); + } +} + function get_entry_options($user_id, $latitude=null, $longitude=null) { /* Sections: @@ -303,6 +336,7 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) { foreach($recentQ as $r) { $recent[] = [ 'title' => $r->content, + 'subtitle' => query_last_eaten($user_id, $r->type, $r->content), 'type' => $r->type ]; } @@ -322,6 +356,7 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) { if(!in_array(['title'=>$next,'type'=>'drink'], $drinks)) { $drinks[] = [ 'title' => $next, + 'subtitle' => query_last_eaten($user_id, 'drink', $next), 'type' => 'drink' ]; } @@ -341,6 +376,7 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) { if(!in_array(['title'=>$next,'type'=>'eat'], $food)) { $food[] = [ 'title' => $next, + 'subtitle' => query_last_eaten($user_id, 'eat', $next), 'type' => 'eat' ]; }