@ 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 git.master , git.master_jit , rfc.property-hooks 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 . This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.
Active branches Archived branches Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page
preferences:dark mode live preview
63.86 ms | 401 KiB | 8 Q