Sync 6.x with HEAD
[project/print.git] / print_pdf.module
CommitLineData
9c35190f
JV
1<?php
2/* $Id$ */
3
4/**
5 * @file
6 * Display printer-friendly versions of Drupal pages
7 */
8
9define("PRINTPDF_PATH", "printpdf");
10
cef86536
JV
11/**
12 * Default values of the print_pdf_settings variable
13 */
14function print_pdf_settings_default() {
15 return array('show_link' => 0, 'show_sys_link' => 1, 'book_link' => 1, 'pdf_tool' => 0);
16}
17
9c35190f
JV
18//*******************************************************************
19// Drupal Hooks
20//*******************************************************************
21
22/**
e8517dd5 23 * Implementation of hook_theme().
9c35190f
JV
24 */
25function print_pdf_theme() {
26 return array(
27 'print_pdf_format_link' => array(
28 'arguments' => array(),
29 ),
30 );
31}
32
33/**
e8517dd5 34 * Implementation of hook_menu().
9c35190f
JV
35 */
36function print_pdf_menu() {
37 $items = array();
38
39 $items[PRINTPDF_PATH] = array(
40 'title' => 'Printer-friendly PDF',
41 'page callback' => 'print_pdf_controller',
42 'access arguments' => array('access print'),
43 'type' => MENU_CALLBACK,
44 'file' => 'print.pdf.inc',
45 );
cef86536
JV
46 $items['admin/settings/print/pdf'] = array(
47 'title' => 'PDF',
48 'page callback' => 'drupal_get_form',
49 'page arguments' => array('print_pdf_settings'),
50 'access arguments' => array('administer print'),
51 'weight' => 2,
52 'type' => MENU_LOCAL_TASK,
53 'file' => 'print_pdf.admin.inc',
54 );
9c35190f
JV
55
56 return $items;
57}
58
59/**
60 * Implementation of hook_link().
61 */
62function print_pdf_link($type, $node = NULL, $teaser = FALSE) {
e8517dd5 63 $print_pdf_settings = variable_get('print_pdf_settings', print_pdf_settings_default());
4ed2260c 64 static $print_pdf_display = FALSE;
9c35190f 65
cef86536 66 if ($print_pdf_display == FALSE) {
e8517dd5
JV
67 if (isset($node->type)) {
68 $node_type = $node->type;
69 $print_pdf_display_comment = variable_get('print_pdf_display_comment_'. $node->type, 0);
70 $print_pdf_display = variable_get('print_pdf_display_'. $node->type, 1);
71 }
72 else {
73 $node_type = '';
74 $print_pdf_display_comment = 0;
75 $print_pdf_display = 1;
76 }
9c35190f
JV
77 }
78
9c35190f 79 // No link is shown for several motives...
4ed2260c 80 if ( !($teaser) &&
cef86536 81 ($print_pdf_settings['show_link']) && user_access('access print') &&
4ed2260c
JV
82 (($type == 'comment' && $print_pdf_display_comment) ||
83 ($type == 'node' && $print_pdf_display))) {
9c35190f
JV
84 $links = array();
85
86 $format = theme('print_pdf_format_link');
87
e8517dd5 88 $query_arr = $_GET;
9c35190f 89 if ($type == 'comment') {
e8517dd5 90 $query_arr['comment'] = $node->cid;
9c35190f 91 }
e8517dd5 92 $query = drupal_query_string_encode($query_arr, array('q'));
9c35190f
JV
93
94 $links['print_pdf'] = array('href' => PRINTPDF_PATH ."/". $node->nid,
e8517dd5
JV
95 'title' => $format['text'],
96 'attributes' => $format['attributes'],
97 'html' => $format['html'],
98 'query' => $query,
99 );
9c35190f
JV
100
101 return $links;
102 }
103 else {
104 return;
105 }
106}
107
108/**
e8517dd5 109 * Implementation of hook_help().
9c35190f
JV
110 */
111function print_pdf_help($path, $arg) {
cef86536 112 $print_pdf_settings = variable_get('print_pdf_settings', print_pdf_settings_default());
9c35190f 113
cef86536 114 if (($print_pdf_settings['show_link']) && ($print_pdf_settings['show_sys_link']) &&
9c35190f
JV
115 user_access('access print') && (preg_match("/^node\//i", $path) == 0)) {
116 static $output = FALSE;
117
118 if ($output === FALSE) {
119 $output = TRUE;
120
121 return print_pdf_insert_link();
122 }
123 }
124}
125
126/**
e8517dd5 127 * Implementation of hook_form_alter().
9c35190f
JV
128 */
129function print_pdf_form_alter(&$form, $form_state, $form_id) {
4ed2260c 130 // Add the node-type settings option to activate the PDF version link
e8517dd5 131 if ($form_id == 'node_type_form') {
4ed2260c 132 $form['workflow']['print_pdf_display'] = array(
9c35190f 133 '#type' => 'checkbox',
4ed2260c 134 '#title' => t('Show PDF version link'),
e8517dd5 135 '#default_value' => variable_get('print_pdf_display_'. $form['#node_type']->type, 1),
4ed2260c 136 '#description' => t('Displays the link to a PDF version of the content. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
9c35190f 137 );
4ed2260c 138 $form['comment']['print_pdf_display_comment'] = array(
9c35190f 139 '#type' => 'checkbox',
4ed2260c 140 '#title' => t('Show PDF version link in individual comments'),
e8517dd5 141 '#default_value' => variable_get('print_pdf_display_comment_'. $form['#node_type']->type, 0),
4ed2260c 142 '#description' => t('Displays the link to a PDF version of the comment. Further configuration is available on the !settings.', array('!settings' => l(t('settings page'), 'admin/settings/print' ))),
9c35190f
JV
143 );
144 }
145}
146
9c35190f 147function theme_print_pdf_format_link() {
1d37b654
JV
148 $print_pdf_settings = variable_get('print_pdf_settings', print_pdf_settings_default());
149 $text = t('PDF version');
e8517dd5 150 $img = base_path() . drupal_get_path('module', 'print') .'/icons/pdf_icon.gif';
67026c64
JV
151 $title = t('Display a PDF version of this page.');
152 $class = 'print-pdf';
153 $format = _print_format_link_aux($print_pdf_settings['show_link'], $text, $img, $title, $class);
154
155 return array('text' => $format['text'],
156 'html' => $format['html'],
e8517dd5
JV
157 'attributes' => $format['attributes'],
158 );
9c35190f
JV
159}
160
161/**
162 * Auxiliary function to display a formatted Printer-friendly link
163 *
164 * @return string
165 */
166function print_pdf_insert_link($path = NULL) {
167 if (user_access('access print')) {
168 if ($path === NULL) {
169 $path = PRINTPDF_PATH ."/". $_GET['q'];
170 $query = drupal_query_string_encode($_GET, array('q'));
171 if (empty($query)) {
172 $query = NULL;
173 }
174 }
175 $format = theme('print_pdf_format_link');
176 return '<span class="print">'. l($format['text'], $path, array('attributes' => $format['attributes'], 'query' => $query, 'absolute' => TRUE, 'html' => $format['html'])) .'</span>';
177 }
178}