Issue #1196248: only display PHP filter option when that core module is enabled.
[project/print.git] / print_pdf / print_pdf.admin.inc
1 <?php
2
3 /**
4 * @file
5 * Contains the administrative functions of the PDF version module.
6 *
7 * This file is included by the PDF version module, and includes the
8 * settings form.
9 *
10 * @ingroup print
11 */
12
13 /**
14 * Menu callback for the PDF version module settings form.
15 *
16 * @ingroup forms
17 * @see _print_pdf_tools()
18 */
19 function print_pdf_settings() {
20 $pdf_tools = _print_pdf_tools();
21
22 if ($pdf_tools != -1) {
23 $form['settings'] = array(
24 '#type' => 'fieldset',
25 '#title' => t('PDF options'),
26 );
27
28 $form['settings']['print_pdf_pdf_tool'] = array(
29 '#type' => 'radios',
30 '#title' => t('PDF generation tool'),
31 '#options' => $pdf_tools,
32 '#default_value' => variable_get('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT),
33 '#description' => t('This option selects the PDF generation tool being used by this module to create the PDF version.'),
34 );
35
36 $form['settings']['print_pdf_link_pos'] = array(
37 '#type' => 'checkboxes',
38 '#title' => t('PDF version link'),
39 '#default_value' => variable_get('print_pdf_link_pos', array(PRINT_PDF_LINK_POS_DEFAULT => PRINT_PDF_LINK_POS_DEFAULT)),
40 '#options' => array('link' => t('Links area'), 'corner' => t('Content corner')),
41 '#description' => t('Choose the location of the link(s) to the PDF version. The Links area is usually below the node content, whereas the Content corner is placed in the upper-right corner of the node content. Unselect all options to disable the link. Even if the link is disabled, you can still view the PDF version of a node by going to !path/nid where nid is the numeric id of the node.', array('!path' => PRINTPDF_PATH)),
42 );
43
44 $form['settings']['print_pdf_link_teaser'] = array(
45 '#type' => 'checkbox',
46 '#title' => t('Display link to the PDF version in teaser'),
47 '#default_value' => variable_get('print_pdf_link_teaser', PRINT_PDF_LINK_TEASER_DEFAULT),
48 '#description' => t('Enabling this will display the link in teaser mode.'),
49 );
50
51 $form['settings']['adv_link'] = array(
52 '#type' => 'fieldset',
53 '#title' => t('Advanced link options'),
54 '#collapsible' => TRUE,
55 '#collapsed' => FALSE,
56 );
57
58 $form['settings']['adv_link']['print_pdf_show_link'] = array(
59 '#type' => 'radios',
60 '#title' => t('Link style'),
61 '#default_value' => variable_get('print_pdf_show_link', PRINT_PDF_SHOW_LINK_DEFAULT),
62 '#options' => array(1 => t('Text only'), 2 => t('Icon only'), 3 => t('Icon and Text')),
63 '#description' => t('Select the visual style of the link.'),
64 );
65
66 $form['settings']['adv_link']['print_pdf_link_use_alias'] = array(
67 '#type' => 'checkbox',
68 '#title' => t('Use URL alias instead of node ID'),
69 '#default_value' => variable_get('print_pdf_link_use_alias', PRINT_PDF_LINK_USE_ALIAS_DEFAULT),
70 '#description' => t('Enabling this will create the link using the URL alias instead of the node ID.'),
71 );
72
73 $form['settings']['adv_link']['print_pdf_link_class'] = array(
74 '#type' => 'textfield',
75 '#title' => t('Link class'),
76 '#default_value' => variable_get('print_pdf_link_class', PRINT_PDF_LINK_CLASS_DEFAULT),
77 '#size' => 60,
78 '#maxlength' => 250,
79 '#description' => t('This can be used by themers to change the link style or by jQuery modules to open in a new window (e.g. greybox or thickbox). Multiple classes can be specified, separated by spaces.'),
80 );
81
82 $form['settings']['adv_link']['print_pdf_node_link_visibility'] = array(
83 '#type' => 'radios',
84 '#title' => t('Link visibility'),
85 '#default_value' => variable_get('print_pdf_node_link_visibility', PRINT_PDF_NODE_LINK_VISIBILITY_DEFAULT),
86 '#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')),
87 );
88
89 $form['settings']['adv_link']['print_pdf_node_link_pages'] = array(
90 '#type' => 'textarea',
91 '#default_value' => variable_get('print_pdf_node_link_pages', PRINT_PDF_NODE_LINK_PAGES_DEFAULT),
92 '#rows' => 3,
93 '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
94 );
95
96 if (module_exists('php')) {
97 $access = user_access('use PHP for settings');
98
99 if ($form['settings']['adv_link']['print_pdf_node_link_visibility']['#default_value'] == 2 && !$access) {
100 $form['settings']['adv_link']['print_pdf_node_link_visibility'] = array('#type' => 'value', '#value' => 2);
101 $form['settings']['adv_link']['print_pdf_node_link_pages'] = array('#type' => 'value', '#value' => $form['settings']['adv_link']['print_pdf_node_link_pages']['#default_value']);
102 }
103 elseif ($access) {
104 $form['settings']['adv_link']['print_pdf_node_link_visibility']['#options'][] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
105 $form['settings']['adv_link']['print_pdf_node_link_pages']['#description'] .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
106 }
107 }
108
109 $form['settings']['adv_link']['print_pdf_sys_link_visibility'] = array(
110 '#type' => 'radios',
111 '#title' => t('Show link in system (non-content) pages'),
112 '#default_value' => variable_get('print_pdf_sys_link_visibility', PRINT_PDF_SYS_LINK_VISIBILITY_DEFAULT),
113 '#options' => array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.')),
114 );
115
116 $form['settings']['adv_link']['print_pdf_sys_link_pages'] = array(
117 '#type' => 'textarea',
118 '#default_value' => variable_get('print_pdf_sys_link_pages', PRINT_PDF_SYS_LINK_PAGES_DEFAULT),
119 '#rows' => 3,
120 '#description' => t('Setting this option will add a PDF version page link on pages created by Drupal or the enabled modules.') . '<br />' .
121 t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
122 );
123
124 if (module_exists('php')) {
125 if ($form['settings']['adv_link']['print_pdf_sys_link_visibility']['#default_value'] == 2 && !$access) {
126 $form['settings']['adv_link']['print_pdf_sys_link_visibility'] = array('#type' => 'value', '#value' => 2);
127 $form['settings']['adv_link']['print_pdf_sys_link_pages'] = array('#type' => 'value', '#value' => $form['settings']['adv_link']['print_pdf_sys_link_pages']['#default_value']);
128 }
129 elseif ($access) {
130 $form['settings']['adv_link']['print_pdf_sys_link_visibility']['#options'][] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
131 $form['settings']['adv_link']['print_pdf_sys_link_pages']['#description'] .= ' ' . t('If the PHP-mode is chosen, enter PHP code between %php. Note that executing incorrect PHP-code can break your Drupal site.', array('%php' => '<?php ?>'));
132 }
133 }
134
135 $form['settings']['adv_link']['print_pdf_book_link'] = array(
136 '#type' => 'radios',
137 '#title' => t('Link in book hierarchy nodes'),
138 '#default_value' => variable_get('print_pdf_book_link', PRINT_PDF_BOOK_LINK_DEFAULT),
139 '#options' => array(t('No link'), t('Current page and sub-pages'), t('Current page only')),
140 );
141
142 $form['settings']['print_pdf_content_disposition'] = array(
143 '#type' => 'radios',
144 '#title' => t('Open PDF in'),
145 '#options' => array(t('Same browser window'), t('New browser window'), t('Save dialog')),
146 '#default_value' => variable_get('print_pdf_content_disposition', PRINT_PDF_CONTENT_DISPOSITION_DEFAULT),
147 '#description' => t("Select the desired method for opening the PDF in the user's browser."),
148 );
149
150 $form['settings']['print_pdf_paper_size'] = array(
151 '#type' => 'select',
152 '#title' => t('Paper size'),
153 '#options' => array('4A0' => '4A0', '2A0' => '2A0', 'A0' => 'A0',
154 'A1' => 'A1', 'A2' => 'A2', 'A3' => 'A3', 'A4' => 'A4',
155 'A5' => 'A5', 'A6' => 'A6', 'A7' => 'A7', 'A8' => 'A8',
156 'A9' => 'A9', 'A10' => 'A10', 'B0' => 'B0', 'B1' => 'B1',
157 'B2' => 'B2', 'B3' => 'B3', 'B4' => 'B4', 'B5' => 'B5',
158 'B6' => 'B6', 'B7' => 'B7', 'B8' => 'B8', 'B9' => 'B9',
159 'B10' => 'B10', 'C0' => 'C0', 'C1' => 'C1', 'C2' => 'C2',
160 'C3' => 'C3', 'C4' => 'C4', 'C5' => 'C5', 'C6' => 'C6',
161 'C7' => 'C7', 'C8' => 'C8', 'C9' => 'C9', 'C10' => 'C10',
162 'RA0' => 'RA0', 'RA1' => 'RA1', 'RA2' => 'RA2',
163 'RA3' => 'RA3', 'RA4' => 'RA4', 'SRA0' => 'SRA0',
164 'SRA1' => 'SRA1', 'SRA2' => 'SRA2', 'SRA3' => 'SRA3',
165 'SRA4' => 'SRA4', 'LETTER' => 'Letter', 'LEGAL' => 'Legal',
166 'EXECUTIVE' => 'Executive', 'FOLIO' => 'Folio',
167 ),
168 '#default_value' => variable_get('print_pdf_paper_size', PRINT_PDF_PAPER_SIZE_DEFAULT),
169 '#description' => t('Choose the paper size of the generated PDF.'),
170 );
171
172 $form['settings']['print_pdf_page_orientation'] = array(
173 '#type' => 'select',
174 '#title' => t('Page orientation'),
175 '#options' => array('portrait' => t('Portrait'), 'landscape' => t('Landscape')),
176 '#default_value' => variable_get('print_pdf_page_orientation', PRINT_PDF_PAGE_ORIENTATION_DEFAULT),
177 '#description' => t('Choose the page orientation of the generated PDF.'),
178 );
179 $form['settings']['print_pdf_autoconfig'] = array(
180 '#type' => 'checkbox',
181 '#title' => t('Auto-configure the PDF tool settings'),
182 '#default_value' => variable_get('print_pdf_autoconfig', PRINT_PDF_AUTOCONFIG_DEFAULT),
183 '#description' => t('If you disable this option, the pdf tool settings must be configured manually. For TCDPF, edit the tcpdf/config/tcpdf_config.php file. For dompdf, edit the dompdf/dompdf_config.inc.php file.'),
184 );
185 $form['settings']['print_pdf_font_family'] = array(
186 '#type' => 'textfield',
187 '#title' => t('Font Family'),
188 '#default_value' => variable_get('print_pdf_font_family', PRINT_PDF_FONT_FAMILY_DEFAULT),
189 '#size' => 60,
190 '#maxlength' => 250,
191 '#description' => t('(TCPDF only) Set the font family to be used. Examples: %examples.', array('%examples' => 'helvetica, times, courier, dejavusans, dejavuserif, freesans, freeserif, freemono')) . '<br />' .
192 t("CAUTION: TCPDF embeds the complete font in the generated PDF. If you're not using Unicode, then helvetica or times are safe choices that will keep the PDF small. Unicode fonts can increase the size of the PDF to the 1MB region."),
193 );
194 $form['settings']['print_pdf_font_size'] = array(
195 '#type' => 'textfield',
196 '#title' => t('Font Size'),
197 '#default_value' => variable_get('print_pdf_font_size', PRINT_PDF_FONT_SIZE_DEFAULT),
198 '#size' => 2,
199 '#maxlength' => 3,
200 '#description' => t('(TCPDF only) Set the font size to be used for normal text. This is the base value for the scaling applied to other text styles.'),
201 );
202 $form['settings']['print_pdf_font_subsetting'] = array(
203 '#type' => 'checkbox',
204 '#title' => t('Enable font subsetting'),
205 '#default_value' => variable_get('print_pdf_font_subsetting', PRINT_PDF_FONT_SUBSETTING_DEFAULT),
206 '#description' => t('(TCPDF only) Only embed those font characters that are actually used. This can generates smaller PDF files but may significantly slow down processing.'),
207 );
208 $form['settings']['print_pdf_dompdf_unicode'] = array(
209 '#type' => 'checkbox',
210 '#title' => t("Use dompdf's Unicode Mode"),
211 '#default_value' => variable_get('print_pdf_dompdf_unicode', PRINT_PDF_DOMPDF_UNICODE_DEFAULT),
212 '#description' => t("If enabled, dompdf's Unicode mode is used. If not, the module will attempt to convert some non-ASCII chars to ISO-8859-1."),
213 );
214 $form['settings']['print_pdf_wkhtmltopdf_options'] = array(
215 '#type' => 'textfield',
216 '#title' => t('wkhtmltopdf options'),
217 '#size' => 60,
218 '#maxlength' => 500,
219 '#default_value' => variable_get('print_pdf_wkhtmltopdf_options', PRINT_PDF_WKHTMLTOPDF_OPTIONS),
220 '#description' => t('(wkhtmltopdf only) Set any additional options to be passed to the wkhtmltopdf executable. Tokens may be used in these options (see list below).'),
221 );
222
223 $form['settings']['print_pdf_filename'] = array(
224 '#type' => 'textfield',
225 '#title' => t('PDF File Name'),
226 '#default_value' => variable_get('print_pdf_filename', PRINT_PDF_FILENAME_DEFAULT),
227 '#description' => t("If left empty the generated filename defaults to the node's path. Tokens may be used to build the filename (see following list). The .pdf extension will be appended automatically."),
228 );
229 if (module_exists('token')) {
230 $form['settings']['print_pdf_filename_patterns'] = array(
231 '#type' => 'fieldset',
232 '#title' => t('Replacement Patterns'),
233 '#collapsible' => TRUE,
234 '#collapsed' => TRUE,
235 );
236 $form['settings']['print_pdf_filename_patterns']['descriptions'] = array(
237 '#theme' => 'token_tree',
238 '#token_types' => array('node'),
239 );
240 }
241 $form['#validate'][] = '_print_pdf_settings_validate';
242 }
243 else {
244 $form['settings'] = array(
245 '#type' => 'markup',
246 '#markup' => '<p>' . t("No PDF generation tool found! Please dowload a supported PHP PDF generation tool. Check this module's INSTALL.txt for more details.") . '</p>',
247 );
248 }
249
250 return system_settings_form($form);
251 }
252
253 /**
254 * Validate print_pdf_settings form.
255 */
256 function _print_pdf_settings_validate($form, &$form_state) {
257 if (empty($form_state['values']['print_pdf_pdf_tool'])) {
258 form_set_error('print_pdf_pdf_tool', t("No PDF tool selected"));
259 }
260 if ($form_state['values']['print_pdf_font_size'] < 1) {
261 form_set_error('print_pdf_font_size', t("Font size must be at least 1."));
262 }
263 }
264
265 /**
266 * Auxiliary function to locate suitable PDF generation tools
267 *
268 * @return
269 * array of filenames with the include-able PHP file of the located tools
270 */
271 function _print_pdf_tools() {
272 $pattern = '!^(?:dompdf_config.inc.php|tcpdf.php|wkhtmltopdf.*)$!';
273 $tools = array_keys(file_scan_directory(drupal_get_path('module', 'print'), $pattern));
274 $tools = array_merge($tools, array_keys(file_scan_directory(PRINT_PDF_LIB_PATH, $pattern)));
275 if (module_exists('libraries')) {
276 $tools = array_merge($tools, array_keys(file_scan_directory(libraries_get_path('dompdf'), '!^dompdf_config.inc.php$!')));
277 $tools = array_merge($tools, array_keys(file_scan_directory(libraries_get_path('tcpdf'), '!^tcpdf.php$!')));
278 $tools = array_merge($tools, array_keys(file_scan_directory(libraries_get_path('wkhtmltopdf'), '!^wkhtmltopdf!')));
279 }
280
281 $num_tools = count($tools);
282
283 if ($num_tools == 0) {
284 variable_set('print_pdf_pdf_tool', PRINT_PDF_PDF_TOOL_DEFAULT);
285 return -1;
286 }
287 else {
288 return array_combine($tools, $tools);
289 }
290 }
291
292 /**
293 * Menu callback for the PDF version module text strings settings form.
294 *
295 * @ingroup forms
296 */
297 function print_pdf_strings_settings() {
298 drupal_set_message(t("Saving these strings will disable their translation via Drupal's language system. Use the reset button to return them to the original state."), 'warning');
299
300 $form['print_pdf_text'] = array(
301 '#type' => 'fieldset',
302 '#title' => t('Text strings'),
303 );
304
305 $form['print_pdf_text']['print_pdf_link_text'] = array(
306 '#type' => 'textfield',
307 '#title' => t('Link text'),
308 '#default_value' => variable_get('print_pdf_link_text', t('PDF version')),
309 '#description' => t('Text used in the link to the PDF version.'),
310 );
311
312 return system_settings_form($form);
313 }