You are here

recipe_html.module in Recipe 7

recipe_html.module - Enables a print view for recipes. This supports full 8-1/2" x 11", and 5"x7" and 3"x5" index cards. Some printers may not be able to deal with small page sizes like this. They may have to print on 8-1/2"x11" paper and cut.

File

includes/recipe_html.module
View source
<?php

/**
 * @file
 * recipe_html.module - Enables a print view for recipes.
 * This supports full 8-1/2" x 11", and 5"x7" and 3"x5" index cards.
 * Some printers may not be able to deal with small page sizes like this.
 * They may have to print on 8-1/2"x11" paper and cut.
 */

/**
 * Implementation of hook_recipeio($type).
 */
function recipe_html_recipeio($type) {
  $supported = array(
    'export_single' => array(
      'format_name' => t('Print View'),
      'callback' => 'recipe_html_export_single',
      'format_help' => t('NOTE: You must use your web browser\'s print dialog to change the page size and orientation.'),
      'access arguments' => 'access content',
    ),
  );
  if (isset($supported[$type])) {

    // Key needs to be lower case.
    return array(
      'recipeprint' => $supported[$type],
    );
  }
  else {
    return FALSE;
  }
}
function recipe_html_export_single($nid = NULL, $yield = NULL) {
  if ($nid === NULL) {
    drupal_set_message(t('Recipe not found.'));
    drupal_not_found();
    return;
  }
  $node = node_load($nid);

  // Set the custom yield so we can scale up/down the recipe quantities.
  $node->recipe_custom_yield = $yield;

  // Remove the yield buttons.
  $node->recipe_show_yield_form = FALSE;

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

  // Don't pass to theme handlers.
  unset($build['#theme']);

  // Don't want to show links in print view.
  unset($build['links']);

  // Streamline the summary box.
  $build['recipe_summary_box'] = array(
    '#markup' => theme('recipe_html_summary_box', array(
      'node' => $node,
      'show_yield_form' => isset($node->recipe_show_yield_form) ? $node->recipe_show_yield_form : TRUE,
    )),
  );

  // Pass a fully rendered variable.  This is modeled after the book module in core.
  $node->rendered = drupal_render($build);
  drupal_add_http_header('Content-Type', 'text/html; charset=utf-8', FALSE);
  return theme('recipe_html_page', array(
    'node' => $node,
  ));
}

/**
 * Implementation of hook_theme().
 */
function recipe_html_theme() {
  return array(
    'recipe_html_page' => array(
      'template' => 'recipe_html_node',
      'variables' => array(
        'node' => NULL,
      ),
    ),
    'recipe_html_summary_box' => array(
      'function' => 'theme_recipe_html_summary_box',
      'variables' => array(
        'node' => NULL,
      ),
    ),
  );
}
function template_preprocess_recipe_html_page(&$variables) {

  // Add recipe.css file since we are bypassing the normal page rendering routinee to get rid of sidebars and headers.
  $css_path = drupal_get_path("module", "recipe") . "/includes/recipe_html.css";
  $variables['styles'] = '<style type="text/css" media="all">@import url("' . file_create_url($css_path) . '");</style>';
  $variables['title'] = check_plain($variables['node']->title);

  // 'contents' is already rendered.  Use the contents key so as not to confuse template authors.
  $variables['contents'] = $variables['node']->rendered;
}

/**
 * Default theme implementation for the recipe summary box.
 */
function theme_recipe_html_summary_box($variables) {
  $node = $variables['node'];

  // Construct the summary
  $output = '<div class="recipe-summary">';
  $output .= '<table>';
  $cols = 0;
  if ($node->recipe_preptime) {
    $cols++;
  }
  if ($node->recipe_cooktime) {
    $cols++;
  }
  if (!empty($node->recipe_cooktime) && !empty($node->recipe_preptime)) {
    $cols++;
  }
  if (!empty($node->recipe_source)) {
    $output .= '<tr><td colspan="' . $cols . '">';
    $output .= t('Source: @source', array(
      '@source' => $node->recipe_source,
    ));
    $output .= '<td></tr>';
  }
  if ($cols > 0) {
    $output .= '<tr>';
  }
  if ($node->recipe_preptime) {
    $_minutes = $node->recipe_preptime;
    $_text = '';
    if ($node->recipe_preptime < 60) {
      $_text = format_plural($_minutes, '1 minute', '@count minutes');
    }
    elseif ($node->recipe_preptime % 60 == 0) {
      $_text = format_plural($_minutes / 60, '1 hour', '@count hours');
    }
    else {
      $_text = t('!time hours', array(
        '!time' => recipe_ingredient_quantity_from_decimal($_minutes / 60),
      ));
    }
    $output .= '<td>' . t('Prep time: !time', array(
      '!time' => $_text,
    )) . '</td>';
  }
  if ($node->recipe_cooktime) {
    $_minutes = $node->recipe_cooktime;
    $_text = '';
    if ($node->recipe_cooktime < 60) {
      $_text = format_plural($_minutes, '1 minute', '@count minutes');
    }
    elseif ($node->recipe_cooktime % 60 == 0) {
      $_text = format_plural($_minutes / 60, '1 hour', '@count hours');
    }
    else {
      $_text = t('!time hours', array(
        '!time' => recipe_ingredient_quantity_from_decimal($_minutes / 60),
      ));
    }
    $output .= '<td>' . t('Cooking time: !time', array(
      '!time' => $_text,
    )) . '</td>';
  }
  if (!empty($node->recipe_cooktime) && !empty($node->recipe_preptime)) {
    $_minutes = $node->recipe_cooktime + $node->recipe_preptime;
    $_text = '';
    if ($_minutes < 60) {
      $_text = format_plural($_minutes, '1 minute', '@count minutes');
    }
    elseif ($_minutes % 60 == 0) {
      $_text = format_plural($_minutes / 60, '1 hour', '@count hours');
    }
    else {
      $_text = t('!time hours', array(
        '!time' => recipe_ingredient_quantity_from_decimal($_minutes / 60),
      ));
    }
    $output .= '<td>' . t('Total time: !time', array(
      '!time' => $_text,
    )) . '</td>';
  }
  if ($cols > 0) {
    $output .= '</tr>';
  }
  $output .= '</table>';
  $output .= '</div>';
  return $output;
}

Functions

Namesort descending Description
recipe_html_export_single
recipe_html_recipeio Implementation of hook_recipeio($type).
recipe_html_theme Implementation of hook_theme().
template_preprocess_recipe_html_page
theme_recipe_html_summary_box Default theme implementation for the recipe summary box.