diff --git a/CHANGELOG.md b/CHANGELOG.md index cea10ec..9025afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 6b31500..4c3034c 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/models/Recipe.php b/src/models/Recipe.php index 14f371c..17026b4 100644 --- a/src/models/Recipe.php +++ b/src/models/Recipe.php @@ -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; @@ -407,6 +410,14 @@ private function convertToFractions($quantity) $fraction = " ¼"; break; + case 0.33: + $fraction = " ⅓"; + break; + + case 0.165: + $fraction = " ⅙"; + break; + case 0.5: $fraction = " ½"; break; @@ -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;