From 27395cc408b1af32a254ee0170afc55cc028b5fe Mon Sep 17 00:00:00 2001 From: Chris Smith Date: Sun, 23 Feb 2025 14:14:41 +0100 Subject: [PATCH] Updated success screen --- frontend/controllers/MealController.php | 25 +++++++++++++++++-------- frontend/views/meal/success.php | 12 ++++++------ 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/frontend/controllers/MealController.php b/frontend/controllers/MealController.php index 63e87f5..4fbd1f6 100644 --- a/frontend/controllers/MealController.php +++ b/frontend/controllers/MealController.php @@ -5,6 +5,7 @@ namespace frontend\controllers; use common\models\Meal; use common\models\MealForm; use common\models\search\MealSearch; +use DateTime; use Yii; use yii\filters\AccessControl; use yii\filters\VerbFilter; @@ -63,9 +64,10 @@ class MealController extends Controller $model = new MealForm(); if (Yii::$app->request->isPost) { + $model->load(Yii::$app->request->post()); $model->picture = UploadedFile::getInstance($model, 'picture'); if ($model->upload()) { - $id = \Yii::$app->gemini->mealInquiry($model->filepath); + $id = \Yii::$app->gemini->mealInquiry($model); return Yii::$app->response->redirect(['meal/success', 'id' => $id]); } } @@ -78,9 +80,7 @@ class MealController extends Controller public function actionSuccess($id) { $model = $this->findModel($id); - $startOfDay = strtotime('today midnight'); - $endOfDay = strtotime('tomorrow midnight') - 1; - $today = Meal::find() + $sum = Meal::find() ->select([ 'SUM(calories) AS calories', 'SUM(protein) AS protein', @@ -88,13 +88,22 @@ class MealController extends Controller 'SUM(carbohydrates) AS carbohydrates', 'SUM(fiber) AS fiber' ]) - ->where(['user_id' => Yii::$app->user->id]) - ->andWhere(['between', 'created_at', $startOfDay, $endOfDay]) + ->where(['user_id' => Yii::$app->user->id, 'date' => $model->date]) ->asArray() ->one(); - $today = array_merge(['calories' => 0, 'protein' => 0, 'fat' => 0, 'carbohydrates' => 0, 'fiber' => 0], $today); + $sum = array_merge(['calories' => 0, 'protein' => 0, 'fat' => 0, 'carbohydrates' => 0, 'fiber' => 0], $sum); - return $this->render('success', ['model' => $model, 'today' => $today]); + $today = new DateTime(); + $modelDate = new DateTime($model->date); // @todo should be a date object + $diff = $today->diff($modelDate); + + $sum['title'] = match ($diff->days) { + 0 => 'Today So Far', + 1 => 'Yesterday\'s Sum', + default => $modelDate->format('M jS'), + }; + + return $this->render('success', ['model' => $model, 'sum' => $sum]); } /** diff --git a/frontend/views/meal/success.php b/frontend/views/meal/success.php index 06c8c45..bd42f11 100644 --- a/frontend/views/meal/success.php +++ b/frontend/views/meal/success.php @@ -83,7 +83,7 @@ var isMobile = window.innerWidth < 768; // Adjust as needed for tablets
-

📊 Today So Far

+

📊

🔥 Calories

@@ -93,11 +93,11 @@ var isMobile = window.innerWidth < 768; // Adjust as needed for tablets

🌾 Fiber

-

kcal

-

g

-

g

-

g

-

g

+

kcal

+

g

+

g

+

g

+

g