function format_plaintext_ingredients in Recipe 7
Same name and namespace in other branches
- 6 plugins/recipe_plaintext.module \format_plaintext_ingredients()
1 call to format_plaintext_ingredients()
- recipe_plaintext_export in includes/
recipe_plaintext.module
File
- includes/
recipe_plaintext.module, line 201
Code
function format_plaintext_ingredients($node = NULL) {
$col_widths = array(
'quantity' => 0,
'unit' => 0,
);
//prepare ingredients factor
$factor = 1;
if (isset($node->recipe_custom_yield)) {
$factor = $node->recipe_custom_yield / $node->recipe_yield;
$node->recipe_yield = $node->recipe_custom_yield;
}
$unit_list = recipe_get_units();
foreach ($node->recipe_ingredients['ing'] as $key => &$i) {
// Translate decimal quantity to text fraction.
if ($i['quantity'] > 0) {
$i['quantity'] *= $factor;
}
else {
$i['quantity'] = ' ';
}
$i['str_quantity'] = recipe_ingredient_quantity_from_decimal($i['quantity'], TRUE);
// Strip html entities.
$i['str_quantity'] = str_replace('⁄', '/', $i['str_quantity']);
// Collect column widths.
if (strlen($i['str_quantity']) > $col_widths['quantity']) {
$col_widths['quantity'] = strlen($i['str_quantity']);
}
$i['str_ingredient'] = strlen($i['note']) > 0 ? $i['name'] . ' (' . $i['note'] . ')' : $i['name'];
if (isset($unit_list[$i['unit_key']])) {
// Print the singular or plural term depending on the quantity.
$title = $i['quantity'] > 1 ? $unit_list[$i['unit_key']]['plural'] : $unit_list[$i['unit_key']]['name'];
}
else {
$title = $i['unit_key'];
}
// Print the abbreviation if recipe_unit_display says to or the abbreviation is blank (ie = Unit, which we don't print).
if (!isset($i['abbreviation']) && isset($unit_list[$i['unit_key']])) {
$i['abbreviation'] = $unit_list[$i['unit_key']]['abbreviation'];
}
if (empty($i['abbreviation'])) {
$i['abbreviation'] = ' ';
}
$i['str_unit'] = '';
if (variable_get('recipe_unit_display', 0) == 0 || $i['abbreviation'] == ' ') {
$i['str_unit'] = $i['abbreviation'];
}
else {
$i['str_unit'] = $title;
}
if (strlen($i['str_unit']) > $col_widths['unit']) {
$col_widths['unit'] = strlen($i['str_unit']);
}
}
// Render output with the correct column padding.
$output = '';
$wrap_pad = str_repeat(" ", $col_widths['unit'] + $col_widths['quantity'] + 2);
foreach ($node->recipe_ingredients['ing'] as $key => $j) {
$output .= wordwrap(sprintf("%" . $col_widths['quantity'] . "s %-" . $col_widths['unit'] . "s %s\n", $j['str_quantity'], $j['str_unit'], $j['str_ingredient']), 75, "\n{$wrap_pad}");
}
return $output;
}