You are here

function recipe_recipeML_export_single in Recipe 7.2

Same name and namespace in other branches
  1. 6 plugins/recipe_recipeML.module \recipe_recipeML_export_single()
  2. 7 includes/recipe_recipeML.module \recipe_recipeML_export_single()
1 call to recipe_recipeML_export_single()
recipe_recipeML_export_multi in modules/recipe_recipeML.module
1 string reference to 'recipe_recipeML_export_single'
recipe_recipeML_recipeio in modules/recipe_recipeML.module
Implements hook_recipeio().

File

modules/recipe_recipeML.module, line 52
Enables importing and exporting of recipeML format recipes.

Code

function recipe_recipeML_export_single($nid = NULL, $yield = NULL, $add_content_header = TRUE) {
  if ($nid === NULL) {
    drupal_set_message(t('Recipe not found.'));
    drupal_not_found();
    return;
  }
  $node = node_load($nid);

  // you should not be able to export unpublished recipes
  if ($node->status == 0) {
    drupal_access_denied();
    return;
  }

  // Set the custom yield so we can scale up/down the recipe quantities.
  $node->recipe_custom_yield = $yield;
  $factor = 1;
  if (isset($node->recipe_custom_yield)) {
    $factor = $node->recipe_custom_yield / $node->recipe_yield;
    $node->recipe_yield = $node->recipe_custom_yield;
  }
  $cat_string = '';

  /*
    $vocabs = taxonomy_get_vocabularies('recipe');

    foreach ($vocabs as $vocab) {
      $terms = taxonomy_node_get_terms_by_vocabulary($node, $vocab->vid);
      foreach ( $terms as $term ) {
        $term = array_shift($terms);
        $cat_string .= $term->name . ', ';
      }
      $cat_string = substr($cat_string, 0, -2);
    }
    if ( $cat_string != '' ) {
      $cat_string = '<categories>'. $cat_string .'</categories>';
    }
  */
  $description_items = field_get_items('node', $node, 'recipe_description');
  $recipe_description = '';
  if (!empty($description_items[0]['value'])) {
    $recipe_description = my_xml_escape($description_items[0]['value'], TRUE);
  }
  $instructions_items = field_get_items('node', $node, 'recipe_instructions');
  $recipe_instructions = '';
  if (!empty($instructions_items[0]['value'])) {
    $recipe_instructions = my_xml_escape($instructions_items[0]['value'], TRUE);
  }
  $notes_items = field_get_items('node', $node, 'recipe_notes');
  $recipe_notes = '';
  if (!empty($notes_items[0]['value'])) {
    $recipe_notes = my_xml_escape($notes_items[0]['value'], TRUE);
  }
  $source_items = field_get_items('node', $node, 'recipe_source');
  $recipe_source = '';
  if (!empty($source_items[0]['value'])) {
    $recipe_source = my_xml_escape($source_items[0]['value'], TRUE);
  }
  else {
    $recipe_source = url('node/' . $node->nid, array(
      'absolute' => TRUE,
    ));
  }
  $prep_time_items = field_get_items('node', $node, 'recipe_prep_time');
  $recipe_prep_time = 0;
  if (!empty($prep_time_items[0]['value'])) {
    $recipe_prep_time = $prep_time_items[0]['value'];
  }
  $output = '';
  if ($add_content_header == TRUE) {
    $output .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n" . '<!DOCTYPE recipeml PUBLIC "-//FormatData//DTD RecipeML 0.5//EN" "http://www.formatdata.com/recipeml/recipeml.dtd">' . "\n" . '<recipeml version="0.5" generator="http://drupal.org/project/recipe">' . "\n";
  }
  $output .= "<recipe>\n" . "<head>\n" . '<title>' . my_xml_escape($node->title) . "</title>\n" . '      <version>' . date('m-d-Y', $node->changed) . "</version>\n" . '      <source>' . $recipe_source . '</source>' . "\n" . '      <yield><qty>' . floatval($node->recipe_yield) . '</qty><unit>' . my_xml_escape($node->recipe_yield_unit == '' ? t('Servings') : $node->recipe_yield_unit) . "</unit></yield>\n" . '      <preptime type="cooking"><time><qty>' . $recipe_prep_time . '</qty><timeunit>minutes</timeunit></time></preptime>' . "\n{$cat_string}" . "</head>\n" . '<description>' . $recipe_description . "</description>\n" . '<ingredients>';
  $unit_list = recipe_get_units();
  $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'];
  foreach ($ingredient_items as $item) {

    // Load the ingredient so we can print its name.
    $ingredient = recipe_load_ingredient($item['iid']);
    $name = my_xml_escape($ingredient->name);
    $prep = '';
    if (strlen($item['note']) > 0) {
      $prep = '<prep>' . my_xml_escape($item['note']) . '</prep>';
    }
    if ($item['quantity'] > 0) {
      $item['quantity'] *= $factor;
    }

    // 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'];
      }
    }
    $output .= "\n<ing><amt><qty>" . $item['quantity'] . '</qty><unit>' . $unit_display . '</unit></amt><item>' . $name . "</item>{$prep}</ing>";
  }
  $output .= "\n" . "</ingredients>\n" . '<directions>' . $recipe_instructions . "</directions>\n" . '<note>' . $recipe_notes . "</note>\n" . "</recipe>\n";
  if ($add_content_header == TRUE) {
    $output .= '</recipeml>';
    drupal_add_http_header('Content-type', 'text/xml; charset=utf-8');
  }
  return $output;
}