Skip to content

Commit

Permalink
Merge branch 'release/1.0.9' into v1
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Mar 8, 2019
2 parents 8dd014d + d7ee6fd commit c966063
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 85 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Recipe Changelog

## 1.0.9 - 2019.03.08
### Changed
* Add 1/3 and 1/6 fractions
* Prevent error on no directions or ingredients

## 1.0.8 - 2019.03.08
### Changed
* Fixed an issue where tabs didn't work properly in the Field
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-recipe",
"description": "A comprehensive recipe FieldType for Craft CMS that includes metric/imperial conversion, portion calculation, and JSON-LD microdata support",
"type": "craft-plugin",
"version": "1.0.8",
"version": "1.0.9",
"keywords": [
"craft",
"cms",
Expand Down
181 changes: 97 additions & 84 deletions src/models/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,97 +291,100 @@ public function getImageUrl()
public function getIngredients($outputUnits = "imperial", $serving = 0, $raw = true)
{
$result = [];
foreach ($this->ingredients as $row) {
$convertedUnits = "";
$ingredient = "";
if ($row['quantity']) {
// Multiply the quantity by how many servings we want
$multiplier = 1;
if ($serving > 0) {
$multiplier = $serving / $this->serves;
}
$quantity = $row['quantity'] * $multiplier;
$originalQuantity = $quantity;

// Do the units conversion

if ($outputUnits == 'imperial') {
if ($row['units'] == "mls") {
$convertedUnits = "tsps";
$quantity = $quantity * 0.2;

if ($this->ingredients != '') {
foreach ($this->ingredients as $row) {
$convertedUnits = "";
$ingredient = "";
if ($row['quantity']) {
// Multiply the quantity by how many servings we want
$multiplier = 1;
if ($serving > 0) {
$multiplier = $serving / $this->serves;
}

if ($row['units'] == "ls") {
$convertedUnits = "cups";
$quantity = $quantity * 4.2;
$quantity = $row['quantity'] * $multiplier;
$originalQuantity = $quantity;

// Do the units conversion

if ($outputUnits == 'imperial') {
if ($row['units'] == "mls") {
$convertedUnits = "tsps";
$quantity = $quantity * 0.2;
}

if ($row['units'] == "ls") {
$convertedUnits = "cups";
$quantity = $quantity * 4.2;
}

if ($row['units'] == "mgs") {
$convertedUnits = "ozs";
$quantity = $quantity * 0.000035274;
}

if ($row['units'] == "gs") {
$convertedUnits = "ozs";
$quantity = $quantity * 0.035274;
}
}

if ($row['units'] == "mgs") {
$convertedUnits = "ozs";
$quantity = $quantity * 0.000035274;
if ($outputUnits == 'metric') {
if ($row['units'] == "tsps") {
$convertedUnits = "mls";
$quantity = $quantity * 4.929;
}

if ($row['units'] == "tbsps") {
$convertedUnits = "mls";
$quantity = $quantity * 14.787;
}

if ($row['units'] == "flozs") {
$convertedUnits = "mls";
$quantity = $quantity * 29.574;
}

if ($row['units'] == "cups") {
$convertedUnits = "ls";
$quantity = $quantity * 0.236588;
}

if ($row['units'] == "ozs") {
$convertedUnits = "gs";
$quantity = $quantity * 28.3495;
}

$quantity = round($quantity, 1);
}

if ($row['units'] == "gs") {
$convertedUnits = "ozs";
$quantity = $quantity * 0.035274;
}
}
// Convert imperial units to nice fractions

if ($outputUnits == 'metric') {
if ($row['units'] == "tsps") {
$convertedUnits = "mls";
$quantity = $quantity * 4.929;
if ($outputUnits == 'imperial') {
$quantity = $this->convertToFractions($quantity);
}

if ($row['units'] == "tbsps") {
$convertedUnits = "mls";
$quantity = $quantity * 14.787;
}

if ($row['units'] == "flozs") {
$convertedUnits = "mls";
$quantity = $quantity * 29.574;
}

if ($row['units'] == "cups") {
$convertedUnits = "ls";
$quantity = $quantity * 0.236588;
}

if ($row['units'] == "ozs") {
$convertedUnits = "gs";
$quantity = $quantity * 28.3495;
$ingredient .= $quantity;

if ($row['units']) {
$units = $row['units'];
if ($convertedUnits) {
$units = $convertedUnits;
}
if ($originalQuantity <= 1) {
$units = rtrim($units);
$units = rtrim($units, 's');
}
$ingredient .= " ".$units;
}

$quantity = round($quantity, 1);
}

// Convert imperial units to nice fractions

if ($outputUnits == 'imperial') {
$quantity = $this->convertToFractions($quantity);
if ($row['ingredient']) {
$ingredient .= " ".$row['ingredient'];
}
$ingredient .= $quantity;

if ($row['units']) {
$units = $row['units'];
if ($convertedUnits) {
$units = $convertedUnits;
}
if ($originalQuantity <= 1) {
$units = rtrim($units);
$units = rtrim($units, 's');
}
$ingredient .= " ".$units;
if ($raw) {
$ingredient = Template::raw($ingredient);
}
array_push($result, $ingredient);
}
if ($row['ingredient']) {
$ingredient .= " ".$row['ingredient'];
}
if ($raw) {
$ingredient = Template::raw($ingredient);
}
array_push($result, $ingredient);
}

return $result;
Expand All @@ -407,6 +410,14 @@ private function convertToFractions($quantity)
$fraction = " &frac14;";
break;

case 0.33:
$fraction = " &frac13;";
break;

case 0.165:
$fraction = " &frac16;";
break;

case 0.5:
$fraction = " &frac12;";
break;
Expand Down Expand Up @@ -461,12 +472,14 @@ private function convertToFractions($quantity)
public function getDirections($raw = true)
{
$result = [];
foreach ($this->directions as $row) {
$direction = $row['direction'];
if ($raw) {
$direction = Template::raw($direction);
if ($this->directions != '') {
foreach ($this->directions as $row) {
$direction = $row['direction'];
if ($raw) {
$direction = Template::raw($direction);
}
array_push($result, $direction);
}
array_push($result, $direction);
}

return $result;
Expand Down

0 comments on commit c966063

Please sign in to comment.