You are here

function recipe_recipeML_export_single in Recipe 7

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

File

includes/recipe_recipeML.module, line 53
recipe_recipeML.module - 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>';
    }
  */
  $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>' . my_xml_escape($node->recipe_source == '' ? url('node/' . $node->nid, array(
    'absolute' => TRUE,
  )) : $node->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>' . $node->recipe_preptime . '</qty><timeunit>minutes</timeunit></time></preptime>' . "\n{$cat_string}" . '</head>' . "\n" . '<description>' . my_xml_escape($node->recipe_description) . '</description>' . "\n" . '<ingredients>';
  $unit_list = recipe_get_units();
  foreach ($node->recipe_ingredients['ing'] as $ingredient) {
    $prep = '';
    if (strlen($ingredient['note']) > 0) {
      $prep = '<prep>' . my_xml_escape($ingredient['note']) . '</prep>';
    }
    if ($ingredient['quantity'] > 0) {
      $ingredient['quantity'] *= $factor;
    }
    if (isset($unit_list[$ingredient['unit_key']])) {

      // Print the singular or plural term depending on the quantity.
      $title = $ingredient['quantity'] > 1 ? $unit_list[$ingredient['unit_key']]['plural'] : $unit_list[$ingredient['unit_key']]['name'];
    }
    else {
      $title = $ingredient['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($ingredient['abbreviation']) && isset($unit_list[$ingredient['unit_key']])) {
      $ingredient['abbreviation'] = $unit_list[$ingredient['unit_key']]['abbreviation'];
    }
    if (empty($ingredient['abbreviation'])) {
      $ingredient['abbreviation'] = ' ';
    }
    $ingredient['str_unit'] = '';
    if (variable_get('recipe_unit_display', 0) == 0 || $ingredient['abbreviation'] == ' ') {
      $ingredient['str_unit'] = $ingredient['abbreviation'];
    }
    else {
      $ingredient['str_unit'] = $title;
    }
    $output .= "\n" . '<ing><amt><qty>' . $ingredient['quantity'] . '</qty><unit>' . $ingredient['str_unit'] . '</unit></amt><item>' . my_xml_escape($ingredient['name']) . "</item>{$prep}</ing>";
  }
  $output .= "\n" . '</ingredients>' . "\n" . '<directions>' . my_xml_escape($node->recipe_instructions) . '</directions>' . "\n" . '<note>' . my_xml_escape($node->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;
}