Question
This task you need to create an application which helps us keep track of what ingredients we have in the fridge using a session to
This task you need to create an application which helps us keep track of what ingredients we have in the fridge using a session to bake something. I have pasted recipes.json which stores the list of ingredients needed to prepare some of the most famous Hungarian Christmas dishes. (It might be helpful to analyse and understand the data structure before starting to code.)
a. Display the names of the known recipes and make them redirect to a details page.
b. Also display the contents of the fridge that is stored in a session during this task. If there is no session started when the page is loaded, initialize the fridge as empty.
c. On the details page, list all the ingredients with checkboxes which are needed to prepare the chosen recipe. The checkbox should be checked and disabled if the given ingredient is already in the fridge.
d. Make the list of checkboxes on the details page submittable using a form. Add the items that the user marks checked into the fridge. (Warning! A checked field does not automatically mean that the item needs to be added to the fridge since the checkbox may alredy be checked when the page renders.)
e. Mark (e.g. by displaying the text "Can make!") those recipes on the main page where every needed ingredient is available in the fridge.
f. If everything is in the fridge for a recipe, then there should be a way to make the recipe with by clicking on a link or button. Making the recipe means removing all the included ingredients from the fridge.
index.php
Task 4: Recipe tracker
List of recipes
Fridge contents
storage.php
interface IFileIO {
function save($data);
function load();
}
abstract class FileIO implements IFileIO {
protected $filepath;
public function __construct($filename) {
if (!is_readable($filename) || !is_writable($filename)) {
throw new Exception("Data source ${filename} is invalid.");
}
$this->filepath = realpath($filename);
}
}
class JsonIO extends FileIO {
public function load($assoc = true) {
$file_content = file_get_contents($this->filepath);
return json_decode($file_content, $assoc) ?: [];
}
public function save($data) {
$json_content = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents($this->filepath, $json_content);
}
}
class SerializeIO extends FileIO {
public function load() {
$file_content = file_get_contents($this->filepath);
return unserialize($file_content) ?: [];
}
public function save($data) {
$serialized_content = serialize($data);
file_put_contents($this->filepath, $serialized_content);
}
}
interface IStorage {
function add($record): string;
function findById(string $id);
function findAll(array $params = []);
function findOne(array $params = []);
function update(string $id, $record);
function delete(string $id);
function findMany(callable $condition);
function updateMany(callable $condition, callable $updater);
function deleteMany(callable $condition);
}
class Storage implements IStorage {
protected $contents;
protected $io;
public function __construct(IFileIO $io, $assoc = true) {
$this->io = $io;
$this->contents = (array)$this->io->load($assoc);
}
public function __destruct() {
$this->io->save($this->contents);
}
public function add($record): string {
$id = uniqid();
if (is_array($record)) {
$record['id'] = $id;
}
else if (is_object($record)) {
$record->id = $id;
}
$this->contents[$id] = $record;
return $id;
}
public function findById(string $id) {
return $this->contents[$id] ?? NULL;
}
public function findAll(array $params = []) {
return array_filter($this->contents, function ($item) use ($params) {
foreach ($params as $key => $value) {
if (((array)$item)[$key] !== $value) {
return FALSE;
}
}
return TRUE;
});
}
public function findOne(array $params = []) {
$found_items = $this->findAll($params);
$first_index = array_keys($found_items)[0] ?? NULL;
return $found_items[$first_index] ?? NULL;
}
public function update(string $id, $record) {
$this->contents[$id] = $record;
}
public function delete(string $id) {
unset($this->contents[$id]);
}
public function findMany(callable $condition) {
return array_filter($this->contents, $condition);
}
public function updateMany(callable $condition, callable $updater) {
array_walk($this->contents, function (&$item) use ($condition, $updater) {
if ($condition($item)) {
$updater($item);
}
});
}
public function deleteMany(callable $condition) {
$this->contents = array_filter($this->contents, function ($item) use ($condition) {
return !$condition($item);
});
}
}
index.css
@import url('https://cdn.jsdelivr.net/gh/elte-fi/www-assets@19.10.16/styles/mdss.min.css');
details.php
Task 4: Recipe tracker
Back to recipes
Title
recipes.json
{
"Poppy seed roll": [
"poppyseed",
"sugar",
"milk",
"egg",
"yeast",
"flour"
],
"Gingerbread": [
"flour",
"sugar",
"egg",
"honey",
"butter",
"bakingsoda"
],
"Coconut balls": [
"coconut",
"sugar",
"vanilla",
"butter",
"milk",
"biscuit"
],
"Tiramisu": [
"mascarpone",
"coffee",
"sugar",
"egg",
"cocoa",
"ladyfinger"
],
"Zserb\u00f3": [
"peach",
"milk",
"sugar",
"yeast",
"flour",
"butter",
"walnut"
]
}
TASK 4: RECIPE TRACKER LIST OF RECIPES - Poppy seed roll - Gingergread - Coconur balls - Tiramisu - Zserb FRIDGE CONTENTS Fridge is empty. flour TASK 4: RECIPE TRACKER LIST OF RECIPES - Poppy seed roll - Gingerbread - Coconut balls - Tiramisu - Zserb FRIDGE CONTENTS - sugar - sugar - honey TASK 4: RECIPE TRACKER LIST OF RECIPES - Poppy seed roll - Can make! - Gingerbread - Coconut balls - Tiramisu - Zserb FRIDGE CONTENTS - sugar - egg - honey - milk - yeast - poppyseed
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started