'node/'. arg(1) .'/print', 'title' => t('printer friendly'),
'callback' => 'print_page', 'access' => user_access('access content'),
'type' => MENU_CALLBACK);
}
return $items;
}
/********************************************************************
* Drupal Hooks :: Core
********************************************************************/
/**
* Implementation of hook_link().
*/
function print_link($type, $node = 0, $main) {
$links = array();
if ($node->type == 'book' && function_exists('book_link')) {
return;
}
if ($type == 'node' && variable_get('print_show_link', 1) && $main == 0) {
$links[] = theme('print_link', $node);
}
return $links;
}
function print_settings() {
$output = form_textfield(t('Stylesheet URL'), 'print_css', variable_get('print_css', 'misc/print.css'), 60, 64, t('The URL to your print cascading stylesheet.'));
$field .= form_checkbox(t('Printer friendly URLs'), 'print_urls', 1, variable_get('print_urls', 0));
$output .= form_group(t('Print page elements'), $field);
$output .= form_radios(t('Printer friendly page link'), 'print_show_link', variable_get('print_show_link', 1), array(t("Disabled"), t("Enabled")), t("Enable or disable the 'printer friendly page' link for each node. Even if the link is disabled, you can still view the print version of a node by going to 'node/nid/print' where nid is the numeric id of the node."));
return $output;
}
/********************************************************************
* Module Functions :: Controllers
********************************************************************/
function print_page() {
$nid = arg(1);
if (is_numeric($nid)) {
print_generate($nid);
}
}
/********************************************************************
* Module Functions
********************************************************************/
/**
* Outputs a printer friendly page.
*/
function print_generate($title) {
global $base_url;
/* We can take a node id or a node title */
$node = (is_numeric($title)) ? node_load(array('nid' => $title)) : node_load(array('title' => $title));
if (!$node->title) return false;
$teaser = false;
$page = true;
/* This section is ripped from node_view.
This does everything node_view does except theme the node! */
// Remove the delimiter (if any) that separates the teaser from the body.
// TODO: this strips legitimate uses of '' also.
$node->body = str_replace('', '', $node->body);
// The 'view' hook can be implemented to overwrite the default function
// to display nodes.
if (node_hook($node, 'view'))
node_invoke($node, 'view', $teaser, $page);
else
$node = node_prepare($node, $teaser);
// Allow modules to change $node->body before viewing.
node_invoke_nodeapi($node, 'view', $teaser, $page);
/* End of code stealing from node_view() */
if (variable_get('print_urls', 1)) {
/* Collect links and display them at the bottom of the page. Code once taken from Kjartan Mannes' project.module */
$pattern = "@href=([\']?[\"]?)([^\"|^\'|^|^>]*)([^>]*)>(.+?)@ise";
$node->body = preg_replace($pattern, "''.stripslashes('\\4').' ['. print_friendly_urls(stripslashes('\\2')) .']'", $node->body);
$urls = print_friendly_urls();
if (count($urls)) {
$node->pfp_links = '';
$max = count($urls);
for ($i = 0; $i < $max; $i++) {
$node->pfp_links .= '['. ($i + 1) .'] '. $urls[$i] ."
\n";
}
}
}
init_theme();
/* Grab and format the src URL */
$node->source_url = $base_url. '/'. url("node/$node->nid");
$node->language = $GLOBALS['locale'];
$node->printcss = variable_get('print_css', 'misc/print.css');
include_once('print.tpl.php');
}
function print_friendly_urls($url = 0) {
global $base_url;
static $urls = array();
if ($url) {
$urls[] = strpos($url, '://') ? $url : $base_url. '/'. url($url);
return count($urls);
}
return $urls;
}
/********************************************************************
* Module Functions :: Themeable Functions
********************************************************************/
function theme_print_link($node) {
$links = l(t('printer friendly version'), "node/$node->nid/print", array('title' => t('Display a printer friendly version of this page.')));
return $links;
}
?>