Newer
Older
Matt Westgate
committed
<?php
Matt Westgate
committed
/**
* @file
* Displays Printer-friendly versions of Drupal pages.
*
* This is the core module of the PF package, with the Drupal hooks
* and other administrative functions.
define('PRINT_PATH', 'print');
define('PRINT_LOGO_URL_DEFAULT', '');
define('PRINT_CSS_DEFAULT', '');
define('PRINT_URLS_DEFAULT', 1);
define('PRINT_COMMENTS_DEFAULT', 0);
define('PRINT_NEWWINDOW_DEFAULT', 1);
define('PRINT_HTML_SHOW_LINK_DEFAULT', 1);
define('PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT', 0);
define('PRINT_HTML_NODE_LINK_PAGES_DEFAULT', '');
define('PRINT_HTML_LINK_CLASS_DEFAULT', 'print-page');
define('PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT', 1);
define('PRINT_HTML_SYS_LINK_PAGES_DEFAULT', '<front>');
define('PRINT_HTML_BOOK_LINK_DEFAULT', 1);
define('PRINT_HTML_NEW_WINDOW_DEFAULT', 0);
define('PRINT_HTML_SENDTOPRINTER_DEFAULT', 0);
define('PRINT_SOURCEURL_ENABLED_DEFAULT', 1);
define('PRINT_SOURCEURL_DATE_DEFAULT', 0);
define('PRINT_SOURCEURL_FORCENODE_DEFAULT', 0);
define('PRINT_ROBOTS_NOINDEX_DEFAULT', 1);
define('PRINT_ROBOTS_NOFOLLOW_DEFAULT', 1);
define('PRINT_ROBOTS_NOARCHIVE_DEFAULT', 0);
define('PRINT_TYPE_SHOW_LINK_DEFAULT', 1);
define('PRINT_TYPE_COMMENT_LINK_DEFAULT', 0);
Matt Westgate
committed
/**
João Ventura
committed
* Implementation of hook_perm().
*/
function print_perm() {
return array('access print', 'administer print');
}
João Ventura
committed
* Implementation of hook_theme().
*/
function print_theme() {
return array(
'print_format_link' => array(
'arguments' => array(),
),
'print_text' => array(
'arguments' => array(),
),
);
}
João Ventura
committed
* Implementation of hook_menu().
Matt Westgate
committed
*/
Matt Westgate
committed
$items = array();
$items[PRINT_PATH] = array(
'title' => 'Printer-friendly',
'access arguments' => array('access print'),
'type' => MENU_CALLBACK,
'file' => 'print.pages.inc',
);
$items['admin/settings/print'] = array(
'title' => 'Printer-friendly pages',
João Ventura
committed
'description' => 'Adds a printer-friendly version link to content and administrative pages.',
'page callback' => 'drupal_get_form',
'page arguments' => array('print_html_settings'),
'access arguments' => array('administer print'),
'file' => 'print.admin.inc',
);
$items['admin/settings/print/html'] = array(
'title' => 'Web page',
'weight' => 0,
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/settings/print/common'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('print_main_settings'),
'access arguments' => array('administer print'),
'weight' => 10,
'type' => MENU_LOCAL_TASK,
'file' => 'print.admin.inc',
Matt Westgate
committed
return $items;
}
/**
* Implementation of hook_link().
*/
function print_link($type, $node = NULL, $teaser = FALSE) {
$print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
$print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT);
$print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
static $print_display_comment = FALSE;
if (($print_display_comment == FALSE) && (isset($node->type))) {
$node_type = $node->type;
$print_display_comment = variable_get('print_display_comment_'. $node->type, PRINT_TYPE_COMMENT_LINK_DEFAULT);
$print_display = variable_get('print_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
}
else {
$node_type = '';
$print_display = 1;
}
// No link is shown for several motives...
João Ventura
committed
if ( !($teaser) && ($node_type != 'book') && (!isset($node->book)) &&
($print_html_show_link) && user_access('access print') &&
_print_page_match($print_html_node_link_visibility, $print_html_node_link_pages) &&
(($type == 'comment' && $print_display_comment) ||
($type == 'node' && $print_display))) {
$links = array();
João Ventura
committed
$query_arr = $_GET;
João Ventura
committed
$query_arr['comment'] = $node->cid;
João Ventura
committed
$query = drupal_query_string_encode($query_arr, array('q'));
if (empty($query)) $query = NULL;
Matt Westgate
committed
$links['print'] = array('href' => PRINT_PATH .'/'. $node->nid,
'title' => $format['text'],
'attributes' => $format['attributes'],
João Ventura
committed
'query' => $query,
/**
* Implementation of hook_link_alter().
*/
function print_link_alter(&$links, $node) {
João Ventura
committed
foreach ($links as $module => $link) {
if (strpos($module, 'book_printer') !== FALSE) {
$print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
if ($print_html_book_link) {
$format = theme('print_format_link');
$format['attributes']['title'] = $link['attributes']['title'];
$links[$module]['href'] = PRINT_PATH .'/'. $link['href'];
$links[$module]['attributes'] = $format['attributes'];
}
}
}
}
João Ventura
committed
* Implementation of hook_help().
*/
function print_help($path, $arg) {
switch ($path) {
case 'admin/help#print':
// Return a line-break version of the module README
return filter_filter('process', 1, NULL, file_get_contents(drupal_get_path('module', 'print') .'/README.txt') );
$print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
$print_html_sys_link_visibility = variable_get('print_html_sys_link_visibility', PRINT_HTML_SYS_LINK_VISIBILITY_DEFAULT);
$print_html_sys_link_pages = variable_get('print_html_sys_link_pages', PRINT_HTML_SYS_LINK_PAGES_DEFAULT);
if (($print_html_show_link) && user_access('access print') &&
_print_page_match($print_html_sys_link_visibility, $print_html_sys_link_pages) &&
(preg_match('!^node/!i', $path) == 0)) {
static $output = FALSE;
if ($output === FALSE) {
drupal_add_css(drupal_get_path('module', 'print') .'/printlinks.css');
return '<span class="print-syslink">'. print_insert_link() .'</span>';
Matt Westgate
committed
}
Matt Westgate
committed
João Ventura
committed
/**
* Implementation of hook_nodeapi().
*/
function print_nodeapi(&$node, $op = 'view', $teaser, $page) {
switch ($op) {
case 'view':
$print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
$print_html_node_link_visibility = variable_get('print_html_node_link_visibility', PRINT_HTML_NODE_LINK_VISIBILITY_DEFAULT)
$print_html_node_link_pages = variable_get('print_html_node_link_pages', PRINT_HTML_NODE_LINK_PAGES_DEFAULT);
$print_display = variable_get('print_display_'. $node->type, PRINT_TYPE_SHOW_LINK_DEFAULT);
if ((!$teaser) && (!$node->printing) && ($print_html_show_link) &&
user_access('access print') && ($print_display) &&
_print_page_match($print_html_node_link_visibility, $print_html_node_link_pages)) {
drupal_add_css(drupal_get_path('module', 'print') .'/printlinks.css');
$print_html_book_link = variable_get('print_html_book_link', PRINT_HTML_BOOK_LINK_DEFAULT);
if (($print_html_book_link) && user_access('access printer-friendly version') &&
(($node->type == 'book') || isset($node->book))) {
$path = PRINT_PATH .'/book/export/html/'. $node->nid;
}
$link = print_insert_link($path);
João Ventura
committed
$node->content['print_link'] = array(
'#value' => "<span class='print-link'>$link</span>",
João Ventura
committed
'#weight' => -1,
);
}
}
}
João Ventura
committed
* Implementation of hook_form_alter().
*/
function print_form_alter(&$form, $form_state, $form_id) {
// Add the node-type settings option to activate the printer-friendly version link
João Ventura
committed
if ($form_id == 'node_type_form') {
João Ventura
committed
$form['print'] = array(
'#type' => 'fieldset',
'#title' => t('Printer-friendly pages'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['print']['print_display'] = array(
'#type' => 'checkbox',
'#title' => t('Show printer-friendly version link'),
'#default_value' => variable_get('print_display_'. $form['#node_type']->type, PRINT_TYPE_SHOW_LINK_DEFAULT),
'#description' => t('Displays the link to a printer-friendly version of the content. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
);
João Ventura
committed
$form['print']['print_display_comment'] = array(
'#type' => 'checkbox',
'#title' => t('Show printer-friendly version link in individual comments'),
'#default_value' => variable_get('print_display_comment_'. $form['#node_type']->type, PRINT_TYPE_COMMENT_LINK_DEFAULT),
'#description' => t('Displays the link to a printer-friendly version of the comment. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
);
}
Matt Westgate
committed
}
* Auxiliary function to fill the Printer-friendly link attributes
* @param $title
* text to displayed by the link when hovering over it with the mouse
* @param $class
* class attribute to be used in the link
* @param $new_window
* if TRUE opens the target page in a new window
João Ventura
committed
function print_fill_attributes($title = '', $class = '', $new_window = FALSE) {
$print_newwindow = variable_get('print_newwindow', PRINT_NEWWINDOW_DEFAULT);
$print_robots_noindex = variable_get('print_robots_noindex', PRINT_ROBOTS_NOINDEX_DEFAULT);
$attributes = array();
$attributes['title'] = $title;
if (!empty($class)) {
$attributes['class'] = $class;
}
João Ventura
committed
if ($new_window) {
switch ($print_newwindow) {
João Ventura
committed
case 0:
$attributes['target'] = '_blank';
break;
case 1:
$attributes['onclick'] = 'window.open(this.href); return false';
break;
}
if (!empty($print_robots_noindex)) {
/**
* Auxiliary function to set the link text and html flag
*
* @param $type
* type of link: 0 or 1 for a text-only link, 2 for icon-only and 3 for
* both text and icon
* @param $text
* text to be displayed on the link to the printer-friendly page
* @param $img
* path to the icon file
* @return
* array with the link text and html flag
*/
function _print_format_link_aux($type = 0, $text = '', $img = '') {
$img = theme('image', $img, $text, '', array('class' => 'print-icon'));
$html = TRUE;
switch ($type) {
case 2:
$text = $img;
break;
case 3:
$text = $img .' '. $text;
break;
}
}
else {
$html = FALSE;
}
return array('text' => $text,
'html' => $html,
João Ventura
committed
);
/**
* Format the Printer-friendly link
*
* @return
* array of formatted attributes
* @ingroup themeable
*/
$print_html_link_class = variable_get('print_html_link_class', PRINT_HTML_LINK_CLASS_DEFAULT);
$print_html_new_window = variable_get('print_html_new_window', PRINT_HTML_NEW_WINDOW_DEFAULT);
$print_html_show_link = variable_get('print_html_show_link', PRINT_HTML_SHOW_LINK_DEFAULT);
$img = drupal_get_path('module', 'print') .'/icons/print_icon.gif';
$title = t('Display a printer-friendly version of this page.');
$class = strip_tags($print_html_link_class);
$new_window = $print_html_new_window;
$format = _print_format_link_aux($print_html_show_link, $text, $img);
return array('text' => $format['text'],
'html' => $format['html'],
João Ventura
committed
'attributes' => print_fill_attributes($title, $class, $new_window),
João Ventura
committed
);
/**
* Define the strings displayed by the module in the printer-friendly template
*
* @return
* array with the used text strings
* @ingroup themeable
*/
return array('retrieved' => '',
'sourceURL' => '',
'published' => '',
'by' => '',
'created' => '',
'links' => '',
João Ventura
committed
);
Matt Westgate
committed
}
* Auxiliary function to display a formatted Printer-friendly link
*
* Function made available so that developers may call this function from
* their defined pages/blocks.
*
* @param $path
* path of the original page (optional). If not specified, the current URL
* is used
* @return
* string with the HTML link to the printer-friendly page
function print_insert_link($path = NULL) {
if (user_access('access print')) {
if ($path === NULL) {
$path = PRINT_PATH .'/'. $_GET['q'];
$query = drupal_query_string_encode($_GET, array('q'));
if (empty($query)) {
$query = NULL;
}
$format = theme('print_format_link');
return '<span class="print">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>';
Matt Westgate
committed
}
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/**
* Determine if the current page is enabled according to the visibility settings
*
* @param $visibility
* current visibility settings:
* 0 for show on every page except the listed pages
* 1 for show on only the listed pages
* @param $pages
* list of pages
* @return
* TRUE if it is enabled, FALSE otherwise
*/
function _print_page_match($visibility, $pages) {
if ($pages) {
$path = drupal_get_path_alias($_GET['q']);
$page_match = drupal_match_path($path, $pages);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $pages);
}
return !($visibility xor $page_match);
}
else {
return TRUE;
}
}