Browse Source

include the date last eaten as a subtitle

pull/10/head
Aaron Parecki 9 years ago
parent
commit
4b543ea179
2 changed files with 38 additions and 2 deletions
  1. +1
    -1
      controllers/controllers.php
  2. +37
    -1
      lib/helpers.php

+ 1
- 1
controllers/controllers.php View File

@ -221,7 +221,7 @@ $app->get('/new/options', function() use($app) {
if($user=require_login($app)) { if($user=require_login($app)) {
$params = $app->request()->params(); $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]); $html = partial('partials/entry-buttons', ['options'=>$options]);
$app->response()->body($html); $app->response()->body($html);

+ 37
- 1
lib/helpers.php View File

@ -246,6 +246,7 @@ function query_user_nearby_options($type, $user_id, $latitude, $longitude) {
foreach($optionsQ as $o) { foreach($optionsQ as $o) {
$options[] = [ $options[] = [
'title' => $o->content, 'title' => $o->content,
'subtitle' => query_last_eaten($user_id, $o->type, $o->content),
'type' => $o->type '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')); $published = date('Y-m-d H:i:s', strtotime('-4 months'));
$options = []; $options = [];
$optionsQ = ORM::for_table('entries')->raw_query(' $optionsQ = ORM::for_table('entries')->raw_query('
SELECT type, content
SELECT type, content, MAX(published) AS published
FROM entries FROM entries
WHERE user_id = :user_id WHERE user_id = :user_id
AND type = :type AND type = :type
@ -268,12 +269,44 @@ function query_user_frequent_options($type, $user_id) {
foreach($optionsQ as $o) { foreach($optionsQ as $o) {
$options[] = [ $options[] = [
'title' => $o->content, 'title' => $o->content,
'subtitle' => query_last_eaten($user_id, $o->type, $o->content),
'type' => $o->type 'type' => $o->type
]; ];
} }
return $options; 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) { function get_entry_options($user_id, $latitude=null, $longitude=null) {
/* /*
Sections: Sections:
@ -303,6 +336,7 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) {
foreach($recentQ as $r) { foreach($recentQ as $r) {
$recent[] = [ $recent[] = [
'title' => $r->content, 'title' => $r->content,
'subtitle' => query_last_eaten($user_id, $r->type, $r->content),
'type' => $r->type '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)) { if(!in_array(['title'=>$next,'type'=>'drink'], $drinks)) {
$drinks[] = [ $drinks[] = [
'title' => $next, 'title' => $next,
'subtitle' => query_last_eaten($user_id, 'drink', $next),
'type' => 'drink' 'type' => 'drink'
]; ];
} }
@ -341,6 +376,7 @@ function get_entry_options($user_id, $latitude=null, $longitude=null) {
if(!in_array(['title'=>$next,'type'=>'eat'], $food)) { if(!in_array(['title'=>$next,'type'=>'eat'], $food)) {
$food[] = [ $food[] = [
'title' => $next, 'title' => $next,
'subtitle' => query_last_eaten($user_id, 'eat', $next),
'type' => 'eat' 'type' => 'eat'
]; ];
} }

Loading…
Cancel
Save