@ 2014-05-14T11:44:26Z <?php
use Tonic\Resource,
Tonic\Response,
Tonic\ConditionException;
/**
* Class to add, update and remove Meals
* @uri /eatLogs/logs/{date}/meals
*/
class EatLogMeals extends EatLogs {
/*
{
"id":1,
"servings":2,
"mealoccasion_id":4,
"is_eat_out":true
}
*/
/**
* Add new Meal - return the whole eat log (/eatLogs/logs/{date}/meals)
* @method POST
* @provides application/json
* @secure basic
* @return Response
*/
function addEatLogMeal() {
global $app;
$user = $this->getUser();
//check if date is in correct format yyyyMMdd
$this->date = $this->validateDate($this->date);
//TODO validate other input
//find a plan for current user
$plan = $user->getPlan();
if(!$plan || !$plan->id) {
throw new Tonic\NotFoundException('EatLogMeal couldn\'t be created. Plan for current user not found');
}
$eatLog = R::findOne('eatlog', ' plan_id = ? AND log_date = ?', array($plan->id, $this->date));
//eatlog exists?
if (!$eatLog || !$eatLog->id) {
//create a EatLog entry
$eatLog = $this->createEatLog($plan);
}
//meal exists?
$meal = R::load('meal', $this->request->data['id']);
if (!$meal || !$meal->id) {
throw new Tonic\NotFoundException('EatLogMeal couldn\'t be created. Meal with id ' . $this->request->data['id'] . ' not found.');
}
//meal occasion exists?
$mealOccasion = R::load('mealoccasion', $this->request->data['mealoccasion_id']);
if (!$mealOccasion || !$mealOccasion->id) {
throw new Tonic\NotFoundException('EatLogMeal couldn\'t be created. Meal occasion with id ' . $this->request->data['mealoccasion_id'] . ' not found.');
}
if ($this->request->data['servings'] > 0) {
//insert EatLogMeals - as many as servings argument
$items = array();
/*
if ($this->request->data->servings > 1) {
$items = R::dispense('eatlogmeal', $this->request->data->servings);
} else {
$items[0] = R::dispense('eatlogmeal');
}
*/
for ($i = 0; $i < $this->request->data['servings']; $i++) {
$item = R::dispense('eatlogmeal');
$item->eatlog = $eatLog;
$item->meal = $meal;
$item->mealoccasion = $mealOccasion;
$item->is_eat_out = (bool) $this->request->data['is_eat_out'];
$eatLogMealId = R::store($item);
//add EatLogItems for this Meal
$mealImpactGroup = R::find('mealimpactgroup', ' meal_id = :meal_id', array(':meal_id' => $meal->id));
foreach ($mealImpactGroup as $mIG) {
//insert EatLogItems - as many as servings argument
$items2 = array();
if ($mIG->servings > 1) {
$items2 = R::dispense('eatlogitem', $mIG->servings);
} else {
$items2[0] = R::dispense('eatlogitem');
}
for ($j = 0; $j < $mIG->servings; $j++) {
$items2[$j]->eatlog = $eatLog;
$items2[$j]->eatimpactgroup_id = $mIG->eatimpactgroup_id;
$items2[$j]->mealoccasion = $mealOccasion;
$items2[$j]->is_eat_out = $this->request->data['is_eat_out'];
$items2[$j]->eatlogmeal = $item;
}
R::storeAll($items2);
}
}
//R::storeAll($items);
}
//calculate total impact factor
$this->calculateTotalImpactFactor($eatLog, $plan->program_id);
return $this->listEatLog();
}
/**
* Remove EatLogMeal(s) (/eatLogs/logs/{date}/meals) - return the whole eat log
* @method DELETE
* @provides application/json
* @secure basic
* @return Response
*/
function removeEatLogMeals() {
$user = $this->getUser();
//check if date is in correct format yyyyMMdd
$this->date = $this->validateDate($this->date);
//TODO validate other input
//find a plan for current user
$plan = $user->getPlan();
if(!$plan || !$plan->id) {
throw new Tonic\NotFoundException('EatLog couldn\'t be created. Plan for current user not found');
}
$eatLog = R::findOne('eatlog', ' plan_id = ? AND log_date = ?', array($plan->id, $this->date));
//eatlog exists?
if (!$eatLog || !$eatLog->id) {
//create a EatLog entry
$eatLog = $this->createEatLog($plan);
}
if ($this->request->data['servings'] > 0) {
//TODO check whether there are actually those eatlogmeals?
R::exec("DELETE FROM eatlogmeal WHERE eatlog_id = :eatlog_id AND meal_id = :meal_id AND mealoccasion_id = :mealoccasion_id AND is_eat_out = :is_eat_out LIMIT :servings",
array(
':eatlog_id' => $eatLog->id,
':meal_id' => $this->request->data['id'],
':mealoccasion_id' => $this->request->data['mealoccasion_id'],
':is_eat_out' => $this->request->data['is_eat_out'],
':servings' => $this->request->data['servings']
));
//any EatLogItems associated with this meal are deleted thanks to ON DELETE CASCADE constraint on eatlogmeal_id foreign key in eatlogitme table
}
$this->calculateTotalImpactFactor($eatLog, $plan->program_id);
return $this->listEatLog();
//return new Response(Response::NOCONTENT);
}
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Output for 8.0.0 - 8.0.30 , 8.1.0 - 8.1.28 , 8.2.0 - 8.2.18 , 8.3.0 - 8.3.6 Fatal error: Uncaught Error: Class "EatLogs" not found in /in/m2Shf:11
Stack trace:
#0 {main}
thrown in /in/m2Shf on line 11
Process exited with code 255 . Output for 7.3.0 - 7.3.33 , 7.4.0 - 7.4.33 Fatal error: Uncaught Error: Class 'EatLogs' not found in /in/m2Shf:11
Stack trace:
#0 {main}
thrown in /in/m2Shf on line 11
Process exited with code 255 . Output for 5.3.0 - 5.3.29 , 5.4.0 - 5.4.45 , 5.5.24 - 5.5.35 , 5.6.8 - 5.6.28 , 7.0.0 - 7.0.20 , 7.1.0 - 7.1.33 , 7.2.0 - 7.2.33 Fatal error: Class 'EatLogs' not found in /in/m2Shf on line 11
Process exited with code 255 . Output for 4.4.2 - 4.4.9 , 5.1.0 - 5.1.6 , 5.2.0 - 5.2.17 Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in /in/m2Shf on line 3
Process exited with code 255 . Output for 4.3.0 - 4.3.1 , 4.3.5 - 4.3.11 , 4.4.0 - 4.4.1 , 5.0.0 - 5.0.5 Parse error: parse error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING or '(' in /in/m2Shf on line 3
Process exited with code 255 . Output for 4.3.2 - 4.3.4 Parse error: parse error, expecting `T_CONSTANT_ENCAPSED_STRING' or `'('' in /in/m2Shf on line 3
Process exited with code 255 . preferences:dark mode live preview
241.43 ms | 401 KiB | 357 Q