Commit 1aaa7127 authored by Matt Westgate's avatar Matt Westgate

* Generate printer friendly pages for all node types. Features include the...

* Generate printer friendly pages for all node types.  Features include the ability to list $node->body URL references at the end of the page, custom print stylesheet and choosing which node elements to display.
parents
Print Module Changelog
October 27, 2004
* Updated to work with Drupal 4.5
October 15, 2004
* Initial Release by Matt Westgate <drupal at asitis dot org>
********************************************************************
D R U P A L M O D U L E
********************************************************************
Name: Print module
Author: Matt Westgate <drupal at asitis dot org>
********************************************************************
INSTALLATION:
1. Place the entire print directory into your Drupal modules/
directory.
2. Enable this module by navigating to:
administer > modules
3. Configure this module by navigating to:
administer > settings > print
PRINT MODULE
M: Matt Westgate <drupal at asitis dot org>
S: fixes/patches
********************************************************************
D R U P A L M O D U L E
********************************************************************
Name: Print module
Author: Matt Westgate <drupal at asitis dot org>
Last update: (See CHANGELOG.txt for details)
Drupal: 4.5
********************************************************************
DESCRIPTION:
This module allows you to generate printer friendly versions of
any node by navigating to example.com/node/print/[node id | node title].
********************************************************************
INSTALLATION:
see the INSTALL.txt file in this directory.
********************************************************************
TODO:
- code cleanup
- add ability to optionally attach comments to nodes for printing
- custom print.css
********************************************************************
UPCOMING FEATURES:
- None planned
<?php
/* $Id$ */
/********************************************************************
* Drupal Hooks :: Overview
********************************************************************/
/**
* Implementation of hook_help().
*/
function print_help($section) {
switch ($section) {
case 'admin/modules#description':
$output = t('Allows users to create printer-friendly pages for nodes.');
break;
}
return $output;
}
/**
* Implementation of hook_menu().
*/
function print_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'node/print', 'title' => t('printer friendly page'),
'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 ($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('Submitted by'), 'print_submitted', 1, variable_get('print_submitted', 0));
$field .= form_checkbox(t('Created'), 'print_created', 1, variable_get('print_created', 0));
$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_textarea(t('Footer message'), 'print_footer', variable_get('print_footer', variable_get('site_footer', '')), 70, 5, t('This text will be displayed at the bottom of each printer friendly page. Useful for adding a copyright and disclaimers.'));
$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/print/nid' where nid is the numeric id of the node."));
return $output;
}
/********************************************************************
* Module Functions :: Controllers
********************************************************************/
function print_page($nid = null) {
if (is_numeric($nid)) {
print print_generate($nid);
}
}
/********************************************************************
* Module Functions
********************************************************************/
/**
* Returns a printer friendly page.
*
* You can also configure how you want the page to look using GET parameters.
*/
function print_generate($title) {
global $base_url;
if ($_GET['urls'] || variable_get('print_urls', 1))
$print_urls = 1;
if ($_GET['submitted'] || variable_get('print_submitted', 1))
$print_submitted = 1;
if ($_GET['created'] || variable_get('print_created', 1))
$print_created = 1;
/* We can take a node id or a node title */
$node = (is_numeric($title)) ? node_load(array('nid' => $title)) : node_load(array('title' => $title));
/* 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 '<!--break-->' also.
$node->body = str_replace('<!--break-->', '', $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 ($node) {
$output = '<h1 class="title">'. $node->title .'</h1>';
if ($print_submitted) {
$output .= '<div class="submitted">'. t('By %author', array('%author' => $node->name)). '</div>';
}
if ($print_created) {
$output .= '<div class="created">'. t('Created %date', array('%date' => format_date($node->created, 'small'))). '</div>';
}
$output .= '<div class="content">';
if ($print_urls) {
/* Collect links and display them at the bottom of the page. Code once taken from Kjartan Mannes' project.module */
$pattern = "@href=([\']?[\"]?)([^\"|^\'|^|^>]*)([^>]*)>(.+?)</a>@ise";
$node->body = preg_replace($pattern, "'<u>'.stripslashes('\\4').'</u> ['. print_friendly_urls(stripslashes('\\2')) .']'", $node->body);
$urls = print_friendly_urls();
if (count($urls)) {
$node->body .= '<h2 class="links">Links</h2>';
$max = count($urls);
for ($i = 0; $i < $max; $i++) {
$node->body .= '['. ($i + 1) .'] '. $urls[$i] ."<br />\n";
}
}
}
$output .= $node->body;
/* Grab and format the src URL */
$url = $base_url. '/'. url("node/view/$node->nid");
$output .= '<p class="source_url"><strong>Source URL:</strong> <a href="'. $url .'">'. $url. "</a></p>\n";
$output .= '<div class="footer">'. variable_get('print_footer', variable_get('site_footer', '')). '</div>';
}
$html = "<html><head><title>$node->title</title>";
$html .= "<base href=\"$base_url/\" />";
$html .= "<style type=\"text/css\">\n@import url(". variable_get('print_css', 'misc/print.css') .");\n</style>";
$html .= "</head><body>". $output ."</body></html>";
return $html;
}
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) {
return l(t('printer friendly page'), "node/print/$node->nid");
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment