You are here

function merge_template in Recipe 7.2

Same name and namespace in other branches
  1. 6 plugins/recipe_mastercook4.module \merge_template()
  2. 7 includes/recipe_mastercook4.module \merge_template()
1 call to merge_template()
recipe_mastercook4_export_single in modules/recipe_mastercook4.module

File

modules/recipe_mastercook4.module, line 74
Enables importing and exporting of MasterCook4 format recipes.

Code

function merge_template($node) {
  $categories = '';

  //prepare ingredients
  $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();

  // get the template string
  $template = get_template();

  // merge title
  $template = str_replace("<<title>>", filter_xss($node->title, array()), $template);

  // merge recipe by
  $source_items = field_get_items('node', $node, 'recipe_source');
  $recipe_source = '';
  if (!empty($source_items[0]['value'])) {
    $recipe_source = strip_html_and_encode_entities($source_items[0]['value']);
  }
  $template = str_replace("<<recipeby>>", $recipe_source, $template);

  // merge serving size
  $template = str_replace("<<servingsize>>", $node->recipe_yield, $template);

  // merge preptime
  $prep_time_items = field_get_items('node', $node, 'recipe_prep_time');
  $recipe_prep_time = '0:00';
  if (!empty($prep_time_items[0]['value'])) {
    $duration = $prep_time_items[0]['value'];
    $hours = floor($duration / 60);
    $minutes = sprintf("%02d", $duration % 60);
    $recipe_prep_time = $hours . ':' . $minutes;
  }
  $template = str_replace("<<preptime>>", $recipe_prep_time, $template);

  // merge categories
  $template = str_replace("<<categories>>", $categories, $template);

  // merge ingredients
  $ingredient_items = field_get_items('node', $node, 'recipe_ingredient');
  $ingredient_instance = field_read_instance('node', 'recipe_ingredient', 'recipe');
  $quantity_format = $ingredient_instance['display']['default']['settings']['fraction_format'];
  $abbreviation_setting = $ingredient_instance['display']['default']['settings']['unit_abbreviation'];
  $recipe_ingredients = '';
  foreach ($ingredient_items as $item) {

    // Load the ingredient so we can print its name.
    $ingredient = recipe_load_ingredient($item['iid']);
    $item['name'] = $ingredient->name;

    // Sanitize the name and note.
    $name = filter_xss($item['name'], array());
    $note = filter_xss($item['note'], array());

    // Format the ingredient quantity.
    if ($item['quantity'] > 0) {
      $quantity = recipe_ingredient_quantity_from_decimal($item['quantity'] * $factor, $quantity_format, TRUE);
      $quantity = str_replace('&frasl;', '/', $quantity);
    }
    else {
      $quantity = ' ';
    }

    // Print the unit unless it has no abbreviation. Those units do not get
    // printed in any case.
    $unit_display = '';
    $item['unit'] = isset($unit_list[$item['unit_key']]) ? $unit_list[$item['unit_key']] : array();
    if (!empty($item['unit']['abbreviation'])) {

      // Print the abbreviation if recipe_unit_display == 0.
      if ($abbreviation_setting == 0) {
        $unit_display = $item['unit']['abbreviation'];
      }
      else {
        $unit_display = $item['quantity'] > 1 ? $item['unit']['plural'] : $item['unit']['name'];
      }
    }

    // Add the ingredient text to the output.
    $recipe_ingredients .= recipe_mastercook4_format_ingredient($name, $quantity, $unit_display, $note);
  }
  $template = str_replace("<<ingredients>>", $recipe_ingredients, $template);

  // merge instructions
  $instructions_items = field_get_items('node', $node, 'recipe_instructions');
  $recipe_instructions = '';
  if (!empty($instructions_items[0]['value'])) {
    $recipe_instructions = strip_html_and_encode_entities($instructions_items[0]['value']);
  }
  $template = str_replace("<<instructions>>", $recipe_instructions, $template);

  // merge notes
  $notes_items = field_get_items('node', $node, 'recipe_notes');
  $recipe_notes = '';
  if (!empty($notes_items[0]['value'])) {
    $recipe_notes = 'NOTES : ' . strip_html_and_encode_entities($notes_items[0]['value']);
  }
  $template = str_replace("<<notes>>", $recipe_notes, $template);
  $description_items = field_get_items('node', $node, 'recipe_description');
  if (!empty($description_items[0]['value'])) {
    $recipe_description = "DESCRIPTION : " . strip_html_and_encode_entities($description_items[0]['value']) . "\n\n";
    $template = str_replace("<<description>>", $recipe_description, $template);
  }
  else {
    $template = str_replace("<<description>>\n", "", $template);
  }
  return $template;
}